diff --git a/src/utils/request.js b/src/utils/request.js index 7ef5f76..d495a29 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -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) => {