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.
82 lines
2.2 KiB
82 lines
2.2 KiB
import storage from "./storage";
|
|
import errorCode from "./errorCode";
|
|
import store from "../store";
|
|
|
|
/**
|
|
* 处理凭证信息
|
|
* @param axiosInstance {AxiosInstance}
|
|
*/
|
|
export const withAuthentication = (axiosInstance) => {
|
|
axiosInstance.interceptors.request.use((config) => {
|
|
const $withCredentials = config.$withCredentials !== false;
|
|
|
|
if ($withCredentials) {
|
|
const token = storage.get('credential')
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
}
|
|
}
|
|
return config
|
|
})
|
|
|
|
axiosInstance.interceptors.response.use(res => {
|
|
// 未设置状态码则默认成功状态
|
|
const code = res.data.code || 200;
|
|
// 获取错误信息
|
|
const msg = errorCode[code] || res.data.msg || errorCode['default']
|
|
// console.log(res)
|
|
if (code === 401) {
|
|
// if (!isRelogin.show) {
|
|
// isRelogin.show = true;
|
|
// MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => {
|
|
// isRelogin.show = false;
|
|
|
|
// }).catch(() => {
|
|
// isRelogin.show = false;
|
|
// });
|
|
// }
|
|
store.dispatch('LogOut')
|
|
return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
|
|
} else if (code === 500) {
|
|
uni.showToast({
|
|
title: msg,
|
|
icon: 'none'
|
|
})
|
|
return Promise.reject(new Error(msg))
|
|
} else if (code === 601) {
|
|
uni.showToast({
|
|
title: msg,
|
|
icon: 'none'
|
|
})
|
|
return Promise.reject('error')
|
|
} else if (code !== 200) {
|
|
uni.showToast({
|
|
title: msg,
|
|
icon: 'none'
|
|
})
|
|
return Promise.reject('error')
|
|
} else {
|
|
return res.data
|
|
}
|
|
},
|
|
error => {
|
|
console.log('err' + error)
|
|
let {
|
|
message
|
|
} = error;
|
|
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) + "异常";
|
|
}
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none',
|
|
duration: 5 * 1000
|
|
})
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
}
|
|
|