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.
114 lines
2.3 KiB
114 lines
2.3 KiB
1 year ago
|
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
|
||
|
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) => {
|
||
|
reject(error)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
})
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 请求拦截
|
||
|
*/
|
||
|
instance.interceptors.request.use((config) => {
|
||
|
const { method, params, url } = config
|
||
|
// 附带鉴权的token
|
||
|
const tenantId = 1
|
||
|
const headers : any = {
|
||
|
token: getAccessToken(),
|
||
|
"tenant-id":tenantId,
|
||
|
'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) => {
|
||
|
const code = v.data?.code || 200
|
||
|
if (code === 401) {
|
||
|
// alert('即将跳转登录页。。。', '登录过期')
|
||
|
// setTimeout(redirectHome, 1500)
|
||
|
removeToken()
|
||
|
uni.showModal({
|
||
|
title: '系统提示',
|
||
|
content: '登录状态已过期,您可以继续留在该页面,或者重新登录',
|
||
|
cancelText: '关闭',
|
||
|
confirmText: '重新登录',
|
||
|
success: function (res) {
|
||
|
if (res.confirm) {
|
||
|
uni.reLaunch({ url: '/pages/login' })
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
return v.data
|
||
|
} else if (code === 500) {
|
||
|
uni.showToast({
|
||
|
title: v.data.msg,
|
||
|
icon: 'none'
|
||
|
})
|
||
|
return v.data
|
||
|
} else if (code !== 200) {
|
||
|
uni.showToast({
|
||
|
title: v.data.msg,
|
||
|
icon: 'none'
|
||
|
})
|
||
|
return v.data
|
||
|
}
|
||
|
|
||
|
// @ts-ignore
|
||
|
if ((v.status || v.statusCode) === 200) {
|
||
|
return v.data
|
||
|
}else{
|
||
|
|
||
|
}
|
||
|
return Promise.reject(v)
|
||
|
},error=>{
|
||
|
console.log(error)
|
||
|
uni.showToast({
|
||
|
title: '网络错误',
|
||
|
icon: 'none'
|
||
|
})
|
||
|
})
|
||
|
export default instance
|