You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.2 KiB
132 lines
3.2 KiB
import axios from 'axios'
|
|
|
|
import { getFullURL } from '@/utils/http'
|
|
import { getAccessToken, removeToken } from '@/utils/auth'
|
|
|
|
const instance = axios.create({
|
|
baseURL: import.meta.env.VITE_BASE_URL,
|
|
adapter(config) {
|
|
const { url, method, data, params, headers, baseURL, paramsSerializer } =
|
|
config
|
|
config.timeout = 300000
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
method: method!.toUpperCase() as any,
|
|
url: getFullURL(baseURL || '', url!, params, paramsSerializer),
|
|
header: headers,
|
|
data,
|
|
dataType: 'json',
|
|
responseType: config.responseType,
|
|
success: (res : any) => {
|
|
resolve(res)
|
|
},
|
|
fail: (error : any) => {
|
|
// var message = error.errMsg
|
|
// if (message === 'Network Error') {
|
|
// message = '接口连接异常'
|
|
// } else if (message.includes('timeout')) {
|
|
// message = '接口请求超时'
|
|
// } else if (message.includes('Request failed with status code')) {
|
|
// message = '接口' + message.substr(message.length - 3) + '异常'
|
|
// }
|
|
// reject("系统异常:"+message);
|
|
// console.log("系统异常",message)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
})
|
|
|
|
|
|
/**
|
|
* 请求拦截
|
|
*/
|
|
instance.interceptors.request.use((config) => {
|
|
const { method, params, url } = config
|
|
// 附带鉴权的token
|
|
// const tenantId = 1
|
|
var tenantId = uni.getStorageSync('tenantId')
|
|
const headers : any = {
|
|
token: getAccessToken(),
|
|
"tenant-id": tenantId,
|
|
"dataSource":"PDA",
|
|
'Authorization': 'Bearer ' + getAccessToken()
|
|
}
|
|
if (uni.getStorageSync('openId')) {
|
|
headers['openId'] = uni.getStorageSync('openId')
|
|
}
|
|
// 不缓存get请求
|
|
if (method === 'get') {
|
|
headers['Cache-Control'] = 'no-cache'
|
|
}
|
|
// delete请求参数放入body中
|
|
if (method === 'delete') {
|
|
headers['Content-type'] = 'application/json;'
|
|
Object.assign(config, {
|
|
data: params,
|
|
params: {}
|
|
})
|
|
}
|
|
|
|
return {
|
|
...config,
|
|
headers
|
|
}
|
|
})
|
|
|
|
/**
|
|
* 响应拦截
|
|
*/
|
|
instance.interceptors.response.use((v) => {
|
|
if (v) {
|
|
if (v.statusCode == 200) {
|
|
if (v.data.code == 0) {
|
|
// return v.data
|
|
return Promise.resolve(v.data)
|
|
}else if(v.data.code == 404){
|
|
uni.showModal({
|
|
title: '系统提示',
|
|
content: '登录状态已过期,您可以继续留在该页面,或者重新登录',
|
|
cancelText: '关闭',
|
|
confirmText: '重新登录',
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
uni.reLaunch({ url: '/pages/login/index' })
|
|
}
|
|
}
|
|
})
|
|
} else if(v.data.code == 401){
|
|
uni.clearStorageSync()
|
|
uni.removeStorageSync('overPackageRecord')
|
|
uni.showModal({
|
|
title: '系统提示',
|
|
content: '账号未登录,请重新登录',
|
|
cancelText: '关闭',
|
|
confirmText: '重新登录',
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
uni.reLaunch({ url: '/pages/login/index' })
|
|
}
|
|
}
|
|
})
|
|
} else {
|
|
// return v.data
|
|
return Promise.reject("系统异常:" + v.data.msg)
|
|
}
|
|
|
|
} else {
|
|
return Promise.reject("系统异常:" + v.data.msg)
|
|
}
|
|
|
|
} else {
|
|
return Promise.reject("系统异常:" + v.data.msg)
|
|
}
|
|
|
|
}, error => {
|
|
console.log(error)
|
|
uni.showToast({
|
|
title: '网络错误',
|
|
icon: 'none'
|
|
})
|
|
})
|
|
export default instance
|