107 lines
2.5 KiB
JavaScript
107 lines
2.5 KiB
JavaScript
import {
|
|
getToken
|
|
} from '@/common/common.js'
|
|
import common from '@/common/common.js'
|
|
|
|
const ENV = import.meta.env
|
|
|
|
export const get_base_url = url => {
|
|
if (process.env.NODE_ENV === 'development') { // 开发环境
|
|
// H5必须用非https,手机端必须用https
|
|
// #ifdef H5
|
|
const baseUrl = ENV.VITE_APP_BASE_API_Url
|
|
if (baseUrl.startsWith('https')) {
|
|
return baseUrl.replace('https', 'http')
|
|
}
|
|
return baseUrl
|
|
// #endif
|
|
// #ifdef APP-PLUS
|
|
return ENV.VITE_APP_BASE_API_Url
|
|
// #endif
|
|
} else if (process.env.NODE_ENV === 'production') { // 生产环境
|
|
return ENV.VITE_APP_BASE_API_Url
|
|
}
|
|
}
|
|
|
|
// 请求错误码
|
|
export const REQUES_ERROR_CODES = {
|
|
NO_LOGIN: ['QQ0005', '0005'] //登录
|
|
};
|
|
|
|
// 去登录弹窗状态
|
|
let no_login_show_modal_state = false
|
|
// 去登录方法
|
|
export const goto_login_fun = () => {
|
|
if (no_login_show_modal_state) return;
|
|
no_login_show_modal_state = true
|
|
common.hideLoading()
|
|
common.show('未登录', '现在去登录').then(() => {
|
|
common.navigateTo('/pages/login/login')
|
|
}).finally(() => {
|
|
no_login_show_modal_state = false
|
|
})
|
|
}
|
|
|
|
export default function request({
|
|
url,
|
|
method,
|
|
data,
|
|
meta,
|
|
isStream,
|
|
suppressErrors,
|
|
header = {},
|
|
timeout = 6000
|
|
}) {
|
|
const base_url = get_base_url(url);
|
|
|
|
const headers_base = {
|
|
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
...(header || {})
|
|
}
|
|
|
|
let token = getToken()
|
|
if (token) {
|
|
headers_base['summary'] = token // 让每个请求携带令牌
|
|
}
|
|
let that = this
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: `${base_url}${url}`,
|
|
data: data,
|
|
method: method,
|
|
header: headers_base,
|
|
timeout:timeout,
|
|
success: (response) => {
|
|
if (isStream) { // 流式传输,直接返回
|
|
return resolve(response);
|
|
}
|
|
// suppressErrors
|
|
// 模拟拦截器功能
|
|
const res = response.data
|
|
if (res?.rtnCode === '0000') {
|
|
return resolve(res)
|
|
}
|
|
//到这里下面全是异常的处理
|
|
if (suppressErrors) { // 静默的接口,报错也不处理,因为下面有公共处理
|
|
return reject()
|
|
}
|
|
if (!res) {
|
|
common.msg("系统异常.")
|
|
return reject()
|
|
}
|
|
// 0005 QQ0005 登录已过期
|
|
if (REQUES_ERROR_CODES['NO_LOGIN'].includes(res.rtnCode)) {
|
|
goto_login_fun()
|
|
}
|
|
if (res?.rtnCode === '9999') {
|
|
common.msg("系统异常.")
|
|
}
|
|
return reject(res)
|
|
},
|
|
fail(res) {
|
|
console.log('请求错误', res, url, base_url);
|
|
return reject(res)
|
|
}
|
|
});
|
|
})
|
|
}; |