70 lines
1.5 KiB
JavaScript
70 lines
1.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') {
|
|
// 开发环境
|
|
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,
|
|
}) {
|
|
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);
|
|
}
|
|
|
|
// 模拟拦截器功能
|
|
const res = response.data
|
|
if (!res) {
|
|
common.msg("系统异常.")
|
|
return reject()
|
|
}
|
|
if (res.rtnCode === '0000'){
|
|
return resolve(res)
|
|
}
|
|
if (res.rtnCode === '0005') {
|
|
common.msg(res.message || 'Error')
|
|
} else if (res.rtnCode !== '0000') {
|
|
if (res.rtnCode !== '0002') { // 0002 报错 这里不拦截,和后端约定好的
|
|
common.msg(res.rtnCode == "9999" ? "系统异常" : res.message || 'Error')
|
|
return reject(res)
|
|
}
|
|
return resolve(res)
|
|
} else {
|
|
return resolve(res)
|
|
}
|
|
}
|
|
});
|
|
})
|
|
}; |