77 lines
1.7 KiB
JavaScript
77 lines
1.7 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') {
|
|
// 开发环境
|
|
return ENV.VITE_APP_BASE_API_Url + "/traapp"
|
|
} else if (process.env.NODE_ENV === 'production') {
|
|
// 生产环境
|
|
return ENV.VITE_APP_BASE_API_Url + "/traapp"
|
|
}
|
|
}
|
|
|
|
export default function request({
|
|
url,
|
|
method,
|
|
data,
|
|
meta,
|
|
isStream,
|
|
suppressErrors
|
|
}) {
|
|
const base_url = get_base_url(url);
|
|
|
|
const header = {
|
|
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
}
|
|
|
|
let token = getToken()
|
|
if (token) {
|
|
header['summary'] = token // 让每个请求携带令牌
|
|
}
|
|
let that = this
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: `${base_url}${url}`,
|
|
data: data,
|
|
method: method,
|
|
header: header,
|
|
success: (response) => {
|
|
if (isStream) { // 流式传输,直接返回
|
|
return resolve(response);
|
|
}
|
|
|
|
// 0005 QQ0005 登录已过期
|
|
|
|
// suppressErrors
|
|
// 模拟拦截器功能
|
|
const res = response.data
|
|
if (res?.rtnCode === '0000'){
|
|
return resolve(res)
|
|
}
|
|
//到这里全是异常情况
|
|
if(suppressErrors){ // 静默的接口,报错也不处理,因为下面有公共处理
|
|
return reject()
|
|
}
|
|
if (!res) {
|
|
common.msg("系统异常.")
|
|
return reject()
|
|
}
|
|
if (res.rtnCode === '0005' || res.rtnCode === 'QQ0005'){
|
|
common.hideLoading()
|
|
common.show('未登录','现在去登录').then(() => {
|
|
common.navigateTo('/pages/login/login')
|
|
})
|
|
}
|
|
return reject(res)
|
|
},
|
|
fail() {
|
|
console.log('131231');
|
|
}
|
|
});
|
|
})
|
|
}; |