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.
130 lines
4.2 KiB
130 lines
4.2 KiB
import axios, { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
|
|
import { useUserStoreHook } from '@/store/modules/user';
|
|
import { ElMessage, ElMessageBox } from 'element-plus';
|
|
import router from '@/router';
|
|
import { useRouter } from 'vue-router'
|
|
import { generateRandomString } from '@/utils/encryptionAndDecryption/randNumber'
|
|
|
|
|
|
const routerPinia = useRouter()
|
|
// 创建 axios 实例
|
|
const service = axios.create({
|
|
baseURL: import.meta.env.VITE_APP_BASE_API,
|
|
timeout: 50000,
|
|
headers: { 'Content-Type': 'application/json;charset=utf-8' }
|
|
});
|
|
|
|
// 请求拦截器
|
|
service.interceptors.request.use(
|
|
(config: InternalAxiosRequestConfig) => {
|
|
const userStore = useUserStoreHook();
|
|
//config.headers["Content-Type"] = "application/json;charset=utf-8";
|
|
if (userStore.tokenIng) {
|
|
config.headers.Authorization = userStore.tokenIng;
|
|
}
|
|
if (userStore.userKey) {
|
|
config.headers["user-key"] = userStore.userKey;
|
|
}
|
|
if (userStore.userToken) {
|
|
config.headers["user-token"] = userStore.userToken;
|
|
}
|
|
|
|
let { data, headers } = config
|
|
console.log('请求拦截----data---1--->', data)
|
|
|
|
//获取16位随机数
|
|
let randomString = generateRandomString(16)
|
|
config.headers['Auth-key'] = randomString
|
|
if (data) {
|
|
console.log('请求拦截----data---2--->', data)
|
|
// 加密请求数据
|
|
config.data = {
|
|
data: sm4EncryptMethod(JSON.stringify(data), randomString)
|
|
}
|
|
}
|
|
|
|
console.log('请求拦截---------->', randomString)
|
|
console.log('请求拦截----headers------>', headers)
|
|
console.log('请求拦截----data---3--->', data)
|
|
console.log('请求拦截----data------>', config.data)
|
|
console.log('请求拦截----config------>', config)
|
|
|
|
|
|
return config;
|
|
},
|
|
(error: any) => {
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
// 响应拦截器
|
|
service.interceptors.response.use(
|
|
(response: AxiosResponse) => {
|
|
|
|
let { data, headers } = response
|
|
let authKey = headers['auth-key']
|
|
console.log('行营结果----authKey------>', authKey)
|
|
if (authKey) {
|
|
let jsonData = sm4DecryptMethod(data.data, authKey)
|
|
response.data.data = JSON.parse(jsonData)
|
|
}
|
|
const { code, msg } = response.data;
|
|
console.log('行营结果----解密结构------>', headers['auth-key'], response)
|
|
// console.error("--------code--------->",code)
|
|
// console.error("--------msg--------->",msg)
|
|
if (code === 0 || code === 200 || code === 10001) {
|
|
return response.data;
|
|
}
|
|
if (code === 7 || code === 300 || code === 301 || code === 302){
|
|
ElMessageBox.confirm("身份令牌已失效!请重新登录!", "提示5", {
|
|
confirmButtonText: "确定",
|
|
type: "warning",
|
|
}).then(() => {
|
|
localStorage.clear();
|
|
window.location.href = "/login";
|
|
// routerPinia.push({path:"/login"})
|
|
});
|
|
return response.data;
|
|
}
|
|
// 响应数据为二进制流处理(Excel导出)
|
|
if (response.data instanceof ArrayBuffer) {
|
|
return response;
|
|
}
|
|
|
|
// ElMessage.error(msg || '系统出错');
|
|
return Promise.reject(new Error(msg || 'Error'));
|
|
},
|
|
(error: any) => {
|
|
if (error.response.data) {
|
|
const { code, msg } = error.response.data;
|
|
// token 过期,重新登录
|
|
if (code === 'A0230') {
|
|
ElMessageBox.confirm('当前页面已失效,请重新登录', '提示6', {
|
|
confirmButtonText: '确定',
|
|
type: 'warning'
|
|
}).then(() => {
|
|
localStorage.clear();
|
|
window.location.href = '/login';
|
|
// router.push({path:"/login"})
|
|
});
|
|
}else if(code === 7 || code === 300 || code === 301 || code === 302){
|
|
ElMessageBox.confirm("身份令牌已失效!请重新登录!", "提示7", {
|
|
confirmButtonText: "确定",
|
|
type: "warning",
|
|
}).then(() => {
|
|
localStorage.clear();
|
|
window.location.href = "/login";
|
|
// routerPinia.push({path:"/login"})
|
|
});
|
|
}else if (code === 10001 || code === 10002 || code === 10003) {
|
|
return Promise.reject(error.message);
|
|
} else {
|
|
// ElMessage.error(msg || '系统出错');
|
|
}
|
|
}
|
|
return Promise.reject(error.message);
|
|
}
|
|
);
|
|
|
|
// 导出 axios 实例
|
|
export default service;
|
|
|