| | |
| | | import axios from 'axios' |
| | | import apiConfig from './baseurl' |
| | | |
| | | import { |
| | | Message |
| | | } from 'element-ui' |
| | | |
| | | import { Message } from 'element-ui' |
| | | import { encryptBySM4, decryptBySM4 } from './sm4' // 添加decryptBySM4 |
| | | |
| | | const service = axios.create({ |
| | | baseURL: apiConfig.baseURL, |
| | | // baseURL: apiConfig.baseURL, |
| | | withCredentials: false, // 当跨域请求时发送cookie |
| | | timeout: 30000, // request timeout |
| | | }) |
| | |
| | | service.interceptors.request.use( |
| | | config => { |
| | | config['headers']['Authorization'] = `${sessionStorage.getItem('token')}` |
| | | |
| | | // 判断是否需要加密(只对/api开头的请求进行加密) |
| | | const needEncrypt = config.url.startsWith('/api'); |
| | | |
| | | if (config.method == 'get') { |
| | | if (!config.params) config.params = {}; |
| | | config.params = { |
| | |
| | | } |
| | | if (config.method == 'post') { |
| | | if (!config.data) config.data = {}; |
| | | config.data = { |
| | | ...config.data, |
| | | if (needEncrypt) { |
| | | config.data = { param: encryptBySM4(config.data) }; |
| | | } |
| | | } |
| | | return config |
| | |
| | | return |
| | | } |
| | | const res = response; |
| | | |
| | | // 新增解密处理:仅处理/api路径的POST响应 |
| | | if (res.config.method === 'post' && res.config.url.startsWith('/api')) { |
| | | try { |
| | | if (res.data && res.data.data) { |
| | | // 这里假设使用decryptBySM4进行解密 |
| | | res.data.data = decryptBySM4(res.data.data); |
| | | } |
| | | } catch (e) { |
| | | console.error('数据解密失败:', e); |
| | | } |
| | | } |
| | | |
| | | if (res.data.code == 200) { |
| | | if (!res.data.data) { |
| | | if (!res.data) { |
| | | return Promise.resolve({}) |
| | | } |
| | | return Promise.resolve(res.data.data) |
| | | return Promise.resolve(res.data) |
| | | } else { |
| | | if (res.data.code == 103 || res.data.code == 401) { |
| | | Message({ |