chore: utils

This commit is contained in:
2025-08-04 14:10:35 +08:00
parent ff657f29d4
commit 9a45304dd9
7 changed files with 622 additions and 12 deletions

58
src/utils/cipher.ts Normal file
View File

@@ -0,0 +1,58 @@
import { decrypt, encrypt } from 'crypto-js/aes';
import Base64 from 'crypto-js/enc-base64';
import UTF8, { parse } from 'crypto-js/enc-utf8';
import md5 from 'crypto-js/md5';
import ECB from 'crypto-js/mode-ecb';
import pkcs7 from 'crypto-js/pad-pkcs7';
export interface EncryptionParams {
key: string;
iv: string;
}
export class AesEncryption {
private key;
private iv;
constructor(opt?: Partial<EncryptionParams>) {
const { key, iv } = opt ?? {};
if (key) {
this.key = parse(key);
} else {
this.key = parse('_11111000001111@');
}
if (iv) {
this.iv = parse(iv);
} else {
this.iv = parse('@11111000001111_');
}
}
get getOptions() {
return {
mode: ECB,
padding: pkcs7,
iv: this.iv,
};
}
encryptByAES(cipherText: string) {
return encrypt(cipherText, this.key, this.getOptions).toString();
}
decryptByAES(cipherText: string) {
return decrypt(cipherText, this.key, this.getOptions).toString(UTF8);
}
}
export function encryptByBase64(cipherText: string) {
return UTF8.parse(cipherText).toString(Base64);
}
export function decodeByBase64(cipherText: string) {
return Base64.parse(cipherText).toString(UTF8);
}
export function encryptByMd5(password: string) {
return md5(password).toString();
}

21
src/utils/env.ts Normal file
View File

@@ -0,0 +1,21 @@
export const getAppEnvConfig = () => {
const env = import.meta.env;
const {
VITE_REQUEST_INTERVAL,
VITE_NDM_APP_KEY,
VITE_LAMP_CLIENT_ID,
VITE_LAMP_CLIENT_SECRET,
VITE_LAMP_USERNAME,
VITE_LAMP_PASSWORD,
VITE_LAMP_AUTHORIZATION,
} = env;
return {
requestInterval: Number.parseInt(VITE_REQUEST_INTERVAL as string),
ndmAppKey: VITE_NDM_APP_KEY as string,
lampClientId: VITE_LAMP_CLIENT_ID as string,
lampClientSecret: VITE_LAMP_CLIENT_SECRET as string,
lampUsername: VITE_LAMP_USERNAME as string,
lampPassword: VITE_LAMP_PASSWORD as string,
lampAuthorization: VITE_LAMP_AUTHORIZATION as string,
};
};