update 优化 代码写法 删除无用依赖

This commit is contained in:
疯狂的狮子Li
2026-03-16 19:20:22 +08:00
parent e3227f5cc5
commit b83755e626
16 changed files with 51 additions and 61 deletions

View File

@@ -1,4 +1,6 @@
import CryptoJS from 'crypto-js';
import * as CryptoJSModule from 'crypto-js';
const CryptoJS = ('default' in CryptoJSModule ? CryptoJSModule.default : CryptoJSModule) as typeof CryptoJSModule;
/**
* 随机生成32位的字符串
@@ -26,7 +28,7 @@ export const generateAesKey = () => {
* 加密base64
* @returns {string}
*/
export const encryptBase64 = (str: CryptoJS.lib.WordArray) => {
export const encryptBase64 = (str: CryptoJSModule.lib.WordArray) => {
return CryptoJS.enc.Base64.stringify(str);
};
@@ -43,7 +45,7 @@ export const decryptBase64 = (str: string) => {
* @param aesKey
* @returns {string}
*/
export const encryptWithAes = (message: string, aesKey: CryptoJS.lib.WordArray) => {
export const encryptWithAes = (message: string, aesKey: CryptoJSModule.lib.WordArray) => {
const encrypted = CryptoJS.AES.encrypt(message, aesKey, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
@@ -57,7 +59,7 @@ export const encryptWithAes = (message: string, aesKey: CryptoJS.lib.WordArray)
* @param aesKey
* @returns {string}
*/
export const decryptWithAes = (message: string, aesKey: CryptoJS.lib.WordArray) => {
export const decryptWithAes = (message: string, aesKey: CryptoJSModule.lib.WordArray) => {
const decrypted = CryptoJS.AES.decrypt(message, aesKey, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7

View File

@@ -1,4 +1,4 @@
import JSEncrypt from 'jsencrypt';
import { JSEncrypt } from 'jsencrypt';
// 密钥对生成 http://web.chacuo.net/netrsakeypair
const publicKey = import.meta.env.VITE_APP_RSA_PUBLIC_KEY;

View File

@@ -5,15 +5,15 @@ import { tansParams, blobValidate } from '@/utils/ruoyi';
import cache from '@/plugins/cache';
import { HttpStatus } from '@/enums/RespEnum';
import { errorCode } from '@/utils/errorCode';
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading';
import FileSaver from 'file-saver';
import { getLanguage } from '@/lang';
import { encryptBase64, encryptWithAes, generateAesKey, decryptWithAes, decryptBase64 } from '@/utils/crypto';
import { encrypt, decrypt } from '@/utils/jsencrypt';
import router from '@/router';
import { saveBlob } from '@/utils/save';
import type { LoadingInstance } from 'element-plus';
const encryptHeader = 'encrypt-key';
let downloadLoadingInstance: LoadingInstance;
let downloadLoadingInstance: LoadingInstance | undefined;
// 是否显示重新登录
export const isRelogin = { show: false };
export const globalHeaders = () => {
@@ -193,7 +193,7 @@ export function download(url: string, params: any, fileName: string) {
const isLogin = blobValidate(resp);
if (isLogin) {
const blob = new Blob([resp]);
FileSaver.saveAs(blob, fileName);
saveBlob(blob, fileName);
} else {
const blob = new Blob([resp]);
const resText = await blob.text();
@@ -201,11 +201,11 @@ export function download(url: string, params: any, fileName: string) {
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'];
ElMessage.error(errMsg);
}
downloadLoadingInstance.close();
downloadLoadingInstance?.close();
}).catch((r: any) => {
console.error(r);
ElMessage.error('下载文件出现错误,请联系管理员!');
downloadLoadingInstance.close();
downloadLoadingInstance?.close();
});
}
// 导出 axios 实例

13
src/utils/save.ts Normal file
View File

@@ -0,0 +1,13 @@
export function saveBlob(blob: Blob, fileName: string) {
const downloadLink = document.createElement('a');
const objectUrl = URL.createObjectURL(blob);
downloadLink.href = objectUrl;
downloadLink.download = fileName;
downloadLink.rel = 'noopener';
downloadLink.click();
setTimeout(() => {
URL.revokeObjectURL(objectUrl);
}, 40_000);
}