|
|
|
|
/**
|
|
|
|
|
@ 作者: 秦东
|
|
|
|
|
@ 时间: 2025-12-09 10:57:37
|
|
|
|
|
@ 功能: 封装axios请求
|
|
|
|
|
*/
|
|
|
|
|
import axios, { AxiosError, type InternalAxiosRequestConfig } from 'axios'
|
|
|
|
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
|
|
|
|
import { generateRandomString } from '@/utils/encryptionAndDecryption/randNumber'
|
|
|
|
|
import { sm4DecryptMethod, sm4EncryptMethod } from '@/utils/encryptionAndDecryption/sm4Utils'
|
|
|
|
|
import { useUserStoreHook } from '@/store/modules/user';
|
|
|
|
|
import { useRouter } from 'vue-router'
|
|
|
|
|
|
|
|
|
|
const routerPinia = useRouter()
|
|
|
|
|
/**
|
|
|
|
|
@ 作者: 秦东
|
|
|
|
|
@ 时间: 2025-12-09 10:58:34
|
|
|
|
|
@ 功能: 创建axios实例
|
|
|
|
|
*/
|
|
|
|
|
const service = axios.create({
|
|
|
|
|
baseURL: import.meta.env.VITE_APP_BASE_API,
|
|
|
|
|
timeout: 50000,
|
|
|
|
|
headers: { 'Content-Type': 'application/json;charset=utf-8' }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ 作者: 秦东
|
|
|
|
|
@ 时间: 2025-12-09 11:00:05
|
|
|
|
|
@ 功能: 请求拦截
|
|
|
|
|
*/
|
|
|
|
|
service.interceptors.request.use(
|
|
|
|
|
(config: InternalAxiosRequestConfig) => {
|
|
|
|
|
const userStore = useUserStoreHook();
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
console.error('<---------------请求拦截---------->')
|
|
|
|
|
console.error('请求拦截----config------>', config.url)
|
|
|
|
|
console.error('请求拦截----data------>', config.data)
|
|
|
|
|
console.error('<---------------请求拦截---------->')
|
|
|
|
|
// if (config.headers['content-type'] === 'application/json') {
|
|
|
|
|
let { data, headers } = config
|
|
|
|
|
//获取16位随机数
|
|
|
|
|
let randomString = generateRandomString(16)
|
|
|
|
|
config.headers['Auth-key'] = randomString
|
|
|
|
|
if (data) {
|
|
|
|
|
// 加密请求数据
|
|
|
|
|
config.data = {
|
|
|
|
|
data: sm4EncryptMethod(JSON.stringify(data), randomString)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// console.log('请求拦截---------->', randomString)
|
|
|
|
|
// console.log('请求拦截----headers------>', headers)
|
|
|
|
|
// console.log('请求拦截----data------>', config.data)
|
|
|
|
|
// console.log('请求拦截----config------>', config)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// console.log('请求拦截----content-type------>', config.headers['Content-Type'])
|
|
|
|
|
// console.log('请求拦截----config------>', config.headers)
|
|
|
|
|
return config
|
|
|
|
|
},
|
|
|
|
|
(error: AxiosError) => {
|
|
|
|
|
return Promise.reject(error)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
/**
|
|
|
|
|
@ 作者: 秦东
|
|
|
|
|
@ 时间: 2025-12-09 21:00:20
|
|
|
|
|
@ 备注: 响应拦截
|
|
|
|
|
*/
|
|
|
|
|
service.interceptors.response.use(
|
|
|
|
|
response => {
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
console.error('行营结果----解密结构------>', response.config.url)
|
|
|
|
|
console.error('行营结果----解密结构------>', response.data)
|
|
|
|
|
// console.log('行营结果----解密结构------>', headers['auth-key'], response)
|
|
|
|
|
const { code, msg } = response.data;
|
|
|
|
|
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) {
|
|
|
|
|
const status = error.response.status
|
|
|
|
|
switch (status) {
|
|
|
|
|
case 401:
|
|
|
|
|
// 处理401错误,例如跳转到登录页
|
|
|
|
|
break
|
|
|
|
|
case 403:
|
|
|
|
|
// 处理403错误,例如提示无权限
|
|
|
|
|
break
|
|
|
|
|
case 404:
|
|
|
|
|
// 处理404错误,例如提示资源不存在
|
|
|
|
|
break
|
|
|
|
|
case 500:
|
|
|
|
|
// 处理500错误,例如提示服务器错误
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
// 处理其他错误
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
export default service
|