自动加解密

This commit is contained in:
haozq 2024-07-01 17:56:26 +08:00
parent 9f52e6a125
commit 1b9367af68
1 changed files with 27 additions and 7 deletions

View File

@ -11,7 +11,6 @@ import { encryptCBC, decryptCBC } from '@/utils/aescbc'
let downloadLoadingInstance;
// 是否显示重新登录
export let isRelogin = { show: false };
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例
const service = axios.create({
@ -30,7 +29,7 @@ service.interceptors.request.use(config => {
if (getToken() && !isToken) {
config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
}
console.log(typeof (config.data));
// return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
// get请求映射params参数
if (config.method === 'get' && config.params) {
let param=tansParams(config.params);
@ -72,16 +71,36 @@ service.interceptors.request.use(config => {
}
}
}
console.log(config);
console.log(config)
if( config.headers['Content-Type']=='application/json;charset=utf-8'){
if(typeof (config.data)=='object'){
config.data = encryptCBC(JSON.stringify(config.data))
config.headers['Content-Type']='application/json';
}
}
console.log(config);
//对下载请求进行数据参数拦截加密
if(config.headers['Content-Type']=='application/x-www-form-urlencoded'){
console.log(config)
console.log(config.data)
if(typeof (config.data)=='object'){
console.log(config.data)
let formData=tansParams(config.data);
if(formData){
formData = formData.slice(0, -1);
let formdata={};
formdata.formData=encryptCBC(formData)
config.data=formdata;
}
}else{
config.data ="formData="+ encryptCBC(JSON.stringify(config.data))
}
}
if(config.headers['Content-Type']==null || config.headers['Content-Type']==''){
config.headers['Content-Type']='application/json';
console.warn("请求类型为空");
}
return config
}, error => {
console.log(error)
@ -91,7 +110,6 @@ service.interceptors.request.use(config => {
// 响应拦截器
service.interceptors.response.use(res => {
//自动解密
console.log("返回数据")
console.log("res.data.decrypt=="+res.data.decrypt)
if(typeof res.data.decrypt!='undefined' && res.data.decrypt){
const resultData=decryptCBC(res.data.data);
@ -151,7 +169,9 @@ export function download(url, params, filename, config) {
downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })
return service.post(url, params, {
transformRequest: [(params) => { return tansParams(params) }],
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
headers: { 'Content-Type': 'application/x-www-form-urlencoded',
"encryption":"encryption"
},
responseType: 'blob',
...config
}).then(async (data) => {