import axios from 'axios'; import { getDvaApp } from 'umi'; import { notification } from 'antd'; import type { AxiosRequestHeaders } from 'axios/index'; import CryptoJS from "crypto-js"; const { UMI_APP_BASEURL } = process.env; // const instance = axios.create({ baseURL: UMI_APP_BASEURL }); // const instance = axios.create({ baseURL: 'https://api.eshangtech.com/EShangApiMain' }); const instance = axios.create({ baseURL: 'https://samember.yciccloud.com:8999/EShangApiMain' }); // const instance = axios.create({ baseURL: '/auth' }); instance.interceptors.request.use( (config) => { // 对data数据进行加密 // if (config.data) { // config.data = preprocessData(JSON.stringify(config.data)); // 调用预处理函数 // } config.headers = { ...config.headers, Authorization: `Bearer ${localStorage.getItem('Authorization') || ''}`, "Content-Type": "application/json;charset=utf-8" } as AxiosRequestHeaders; return config; }, (error) => Promise.reject(error), ); instance.interceptors.response.use( //状态码为2xx的时候执行 (response) => { const { data } = response; if (data.code !== 200 && data.Result_Code !== 100) { notification.error({ message: data.message, }); } const timestamp = getFormattedDate() return data }, //状态码不是2xx的时候执行 (error) => { const { response } = error; if (response && response.status === 401) { // // 清除本地存储的token // localStorage.removeItem('Authorization'); // // 重定向到登录页 // window.location.href = '/user/login'; // notification.error({ // message: response?.data?.message || '请求失败', // description: error.message // }); } else { notification.error({ message: response?.data?.message || '请求失败', description: error.message }); } return Promise.reject({ code: response?.status || 500, message: response?.data?.message || '请求失败' }); }, ); // 加密 const encryptAESECB = (data: string, key: string) => { // const cipher = CryptoJS.createCipheriv('aes-128-ecb', key, null); // ECB 模式不需要 IV const newKey = CryptoJS.enc.Utf8.parse(key); // 密钥必须是 16 字节 const cipher = CryptoJS.AES.encrypt(data, newKey, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); let encrypted = cipher.ciphertext.toString(CryptoJS.enc.Hex); // let encrypted = cipher.update(data, 'utf8', 'hex'); // encrypted += cipher.final('hex'); return encrypted; } // 解密 const decryptAESECB = (data: string, key: string) => { // const decipher = CryptoJS.createDecipheriv('aes-128-ecb', key, null); // let decrypted = decipher.update(data, 'hex', 'utf8'); // decrypted += decipher.final('utf8'); const newKey = CryptoJS.enc.Utf8.parse(key); const encryptedData = CryptoJS.enc.Hex.parse(data); // 解密操作 const decrypted = CryptoJS.AES.decrypt({ ciphertext: encryptedData }, newKey, { mode: CryptoJS.mode.ECB, // ECB 模式 padding: CryptoJS.pad.Pkcs7 // PKCS7 填充方式 }); // 将解密后的结果转为 UTF-8 字符串 const decryptedText = decrypted.toString(CryptoJS.enc.Utf8); return decryptedText; } // md5 签名 const md5 = (key: string, data: string, timestamp: string) => { const text = "s" + key + data + timestamp; return CryptoJS.MD5(text).toString(CryptoJS.enc.Hex); } // 生成签名戳 const getFormattedDate = () => { const date = new Date(); const year = date.getFullYear(); // 获取年份 (yyyy) const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份 (MM) const day = String(date.getDate()).padStart(2, '0'); // 获取日期 (dd) const hours = String(date.getHours()).padStart(2, '0'); // 获取小时 (HH) return `es0${year}${month}${day}${hours}0es`; // 拼接成 yyyyMMddHH 格式 } // 加密方法 const preprocessData = (data: string) => { console.log('data', data); // YYYYMMDD let timestamp = getFormattedDate() console.log('timestamp', timestamp); // 秒为单位的时间戳 let timeSecond = parseInt((new Date().getTime() / 1000).toString()) console.log('timeSecond', timeSecond); // 数据的加密 let encryptionData = encryptAESECB(data, timestamp) console.log('encryptionData', encryptionData); // md5签名方法 let md5Data = md5(timestamp, encryptionData, timestamp) console.log('md5Data', md5Data); let res = { data: encryptionData, timestamp: timeSecond, sign: md5Data } console.log('res', res); return res } export default instance;