update
This commit is contained in:
@@ -1,29 +1,58 @@
|
||||
import AES from 'crypto-js/aes.js';
|
||||
import utf8 from 'crypto-js/enc-utf8.js';
|
||||
import Crypto from 'crypto-js';
|
||||
|
||||
class AESCrypto {
|
||||
/**
|
||||
* 密钥
|
||||
* @type {string}
|
||||
*/
|
||||
static #AES_KEY = process.env.VITE_AES_KEY;
|
||||
static _AES_KEY = import.meta.env.VITE_AES_KEY;
|
||||
|
||||
/**
|
||||
* AES加密
|
||||
* @param context {string} 加密内容
|
||||
*/
|
||||
static encrypt = (context) => {
|
||||
return AES.encrypt(context, this.#AES_KEY).toString();
|
||||
const IV = this.createIV();
|
||||
return {
|
||||
context: Crypto.AES.encrypt(
|
||||
context,
|
||||
Crypto.enc.Utf8.parse(this._AES_KEY),
|
||||
{
|
||||
iv: Crypto.enc.Utf8.parse(IV),
|
||||
mode: Crypto.mode.CBC,
|
||||
padding: Crypto.pad.Pkcs7
|
||||
}
|
||||
).toString(),
|
||||
iv: IV,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* AES解密
|
||||
* @param context {string}
|
||||
* @param iv {string}
|
||||
* @return {string}
|
||||
*/
|
||||
static decrypt = (context) => {
|
||||
const bytes = AES.decrypt(context, this.#AES_KEY);
|
||||
return bytes.toString(utf8);
|
||||
static decrypt = (context, iv) => {
|
||||
return Crypto.AES.decrypt(
|
||||
context,
|
||||
Crypto.enc.Utf8.parse(this._AES_KEY),
|
||||
{
|
||||
iv: Crypto.enc.Utf8.parse(iv),
|
||||
mode: Crypto.mode.CBC,
|
||||
padding: Crypto.pad.Pkcs7
|
||||
}
|
||||
).toString(Crypto.enc.Utf8);
|
||||
};
|
||||
|
||||
static createIV = (length = 16) => {
|
||||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
let result = '';
|
||||
for (let i = 0; i < length; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * characters.length);
|
||||
result += characters[randomIndex];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,41 +1,56 @@
|
||||
import axios from 'axios';
|
||||
// import {useUserStore} from "../pinia/UserStore";
|
||||
import AESCrypto from "./AESCrypto.js";
|
||||
import {Message} from '@arco-design/web-vue';
|
||||
import {useUserStore} from "../pinia/UserStore/index.js";
|
||||
|
||||
// 创建 Axios 实例
|
||||
const request = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_URL, // 替换为你的基础 URL
|
||||
baseURL: import.meta.env.MODE === 'development' ? '/api' : import.meta.env.VITE_API_URL, // 替换为你的基础 URL
|
||||
timeout: 10000, // 请求超时设置
|
||||
});
|
||||
|
||||
// 请求拦截器
|
||||
request.interceptors.request.use(
|
||||
(config) => {
|
||||
// const {userInfo} = useUserStore();
|
||||
request.interceptors.request.use((config) => {
|
||||
const {token} = useUserStore();
|
||||
|
||||
// 如果 token 存在,则将其添加到请求头中
|
||||
// if (userInfo?.token) {
|
||||
// config.headers['token'] = `${userInfo?.token}`;
|
||||
// }
|
||||
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
// 如果 token 存在,则将其添加到请求头中
|
||||
if (token) {
|
||||
config.headers['Access-Token'] = token;
|
||||
}
|
||||
);
|
||||
|
||||
console.log('请求拦截器', config.data);
|
||||
|
||||
const {context, iv} = AESCrypto.encrypt(JSON.stringify(config.data));
|
||||
|
||||
config.data = {
|
||||
requestData: context, iv: iv,
|
||||
};
|
||||
|
||||
return config;
|
||||
}, (error) => {
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
||||
// 响应拦截器
|
||||
request.interceptors.response.use(
|
||||
(response) => {
|
||||
return response.data;
|
||||
},
|
||||
(error) => {
|
||||
if (error.response) {
|
||||
return Promise.reject(error.response.data); // 返回错误信息
|
||||
} else { // 网络错误
|
||||
return Promise.reject(error.message);
|
||||
}
|
||||
request.interceptors.response.use((response) => {
|
||||
const {data: {msg, code, data}, config: {url}} = response;
|
||||
console.log(response)
|
||||
if (code !== 1) {
|
||||
Message.error(msg);
|
||||
}
|
||||
);
|
||||
if (!data.data) {
|
||||
return {msg, code, data}
|
||||
} else {
|
||||
const resp = JSON.parse(AESCrypto.decrypt(data.data, data.iv));
|
||||
console.log(`接口${url}返回`, resp);
|
||||
return {data: resp};
|
||||
}
|
||||
}, (error) => {
|
||||
if (error.response) {
|
||||
return Promise.reject(error.response.data); // 返回错误信息
|
||||
} else { // 网络错误
|
||||
return Promise.reject(error.message);
|
||||
}
|
||||
});
|
||||
|
||||
export default request; // 导出 Axios 实例
|
||||
|
||||
Reference in New Issue
Block a user