initial commit
This commit is contained in:
58
src/utils/cipher.ts
Normal file
58
src/utils/cipher.ts
Normal 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();
|
||||
}
|
||||
17
src/utils/download.ts
Normal file
17
src/utils/download.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export function downloadByData(data: BlobPart, filename: string, mime?: string, bom?: BlobPart) {
|
||||
const blobData = typeof bom !== 'undefined' ? [bom, data] : [data];
|
||||
const blob = new Blob(blobData, { type: mime || 'application/octet-stream' });
|
||||
|
||||
const blobURL = window.URL.createObjectURL(blob);
|
||||
const tempLink = document.createElement('a');
|
||||
tempLink.style.display = 'none';
|
||||
tempLink.href = blobURL;
|
||||
tempLink.setAttribute('download', filename);
|
||||
if (typeof tempLink.download === 'undefined') {
|
||||
tempLink.setAttribute('target', '_blank');
|
||||
}
|
||||
document.body.appendChild(tempLink);
|
||||
tempLink.click();
|
||||
document.body.removeChild(tempLink);
|
||||
window.URL.revokeObjectURL(blobURL);
|
||||
}
|
||||
21
src/utils/env.ts
Normal file
21
src/utils/env.ts
Normal 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,
|
||||
};
|
||||
};
|
||||
15
src/utils/post-build.ts
Normal file
15
src/utils/post-build.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { tgz } from 'compressing';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import packageJson from '../../package.json';
|
||||
|
||||
const now = dayjs();
|
||||
|
||||
const fileName = `${packageJson.name}-${now.format('YYMMDD-HHmm')}`;
|
||||
|
||||
try {
|
||||
await tgz.compressDir('./dist', `${fileName}.tar`);
|
||||
await tgz.compressDir('./dist', `${fileName}.tar.gz`);
|
||||
} catch (error) {
|
||||
console.error('压缩失败:', error);
|
||||
}
|
||||
155
src/utils/request.ts
Normal file
155
src/utils/request.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import type {
|
||||
AxiosError,
|
||||
AxiosInstance,
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse,
|
||||
CreateAxiosDefaults,
|
||||
InternalAxiosRequestConfig,
|
||||
} from 'axios';
|
||||
|
||||
import axios from 'axios';
|
||||
|
||||
import type { Result } from '@/axios';
|
||||
|
||||
import { getAppEnvConfig } from './env';
|
||||
|
||||
export type Response<T> = [err: AxiosError | null, data: T | null, resp: Result<T> | null];
|
||||
|
||||
export interface RequestOptions extends CreateAxiosDefaults {
|
||||
requestInterceptor?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
|
||||
responseInterceptor?: (resp: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>;
|
||||
responseErrorInterceptor?: (error: any) => any;
|
||||
}
|
||||
|
||||
export class Request {
|
||||
private instance: AxiosInstance;
|
||||
|
||||
private abortController: AbortController;
|
||||
private lastAbortController: AbortController | null;
|
||||
private uniq: boolean;
|
||||
|
||||
constructor(config?: RequestOptions) {
|
||||
this.instance = axios.create(config);
|
||||
this.abortController = new AbortController();
|
||||
this.lastAbortController = null;
|
||||
this.uniq = false;
|
||||
|
||||
this.instance.interceptors.request.use((config) => {
|
||||
// 取消上一次请求
|
||||
if (this.uniq && this.lastAbortController?.signal) {
|
||||
this.lastAbortController.abort();
|
||||
}
|
||||
this.lastAbortController = this.abortController;
|
||||
this.abortController = new AbortController();
|
||||
return config;
|
||||
|
||||
// 业务登录所需headers
|
||||
// const { lampAuthorization, lampClientId, lampClientSecret } = getAppEnvConfig();
|
||||
// const newAuthorization = window.btoa(`${lampClientId}:${lampClientSecret}`);
|
||||
// const authorization = lampAuthorization.trim() !== '' ? lampAuthorization : newAuthorization;
|
||||
// config.headers.set('accept-language', 'zh-CN,zh;q=0.9');
|
||||
// config.headers.set('accept', 'application/json, text/plain, */*');
|
||||
// config.headers.set('Applicationid', '')
|
||||
// config.headers.set('Tenantid', '1');
|
||||
// config.headers.set('Authorization', authorization);
|
||||
// config.headers.set('token', this.extraInfo?.token ?? '')
|
||||
// return config;
|
||||
});
|
||||
|
||||
const requestInterceptor = config?.requestInterceptor ?? Request.defaultRequestInterceptor;
|
||||
const responseInterceptor = config?.responseInterceptor ?? Request.defaultResponseInterceptor;
|
||||
const responseErrorInterceptor = config?.responseErrorInterceptor ?? Request.defaultResponseErrorInterceptor;
|
||||
|
||||
this.instance.interceptors.request.use(requestInterceptor);
|
||||
|
||||
this.instance.interceptors.response.use(responseInterceptor, responseErrorInterceptor);
|
||||
}
|
||||
|
||||
private static defaultRequestInterceptor(config: InternalAxiosRequestConfig) {
|
||||
return config;
|
||||
}
|
||||
|
||||
private static defaultResponseInterceptor(response: AxiosResponse) {
|
||||
return response;
|
||||
}
|
||||
|
||||
private static defaultResponseErrorInterceptor(error: any) {
|
||||
const err = error as AxiosError;
|
||||
if (err.status === 401) {
|
||||
//
|
||||
}
|
||||
if (err.status === 404) {
|
||||
//
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
get<T>(url: string, option?: AxiosRequestConfig & { uniq?: boolean }): Promise<Response<T>> {
|
||||
const { uniq, ...reqConfig } = option ?? {};
|
||||
this.uniq = !!uniq;
|
||||
return new Promise((resolve) => {
|
||||
this.instance
|
||||
.get<Result<T>>(url, {
|
||||
...reqConfig,
|
||||
signal: this.abortController.signal,
|
||||
})
|
||||
.then((res) => {
|
||||
resolve([null, res.data.data, res.data]);
|
||||
})
|
||||
.catch((err) => {
|
||||
resolve([err as AxiosError, null, null]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
post<T>(url: string, data: AxiosRequestConfig['data'], option?: Partial<Omit<AxiosRequestConfig, 'data'>> & { retRaw?: boolean; uniq?: boolean; upload?: boolean }): Promise<Response<T>> {
|
||||
const { retRaw, uniq, upload, ...reqConfig } = option ?? {};
|
||||
this.uniq = !!uniq;
|
||||
return new Promise((resolve) => {
|
||||
this.instance
|
||||
.post(url, data, { ...reqConfig, headers: { 'content-type': upload ? 'multipart/form-data' : 'application/json' }, signal: this.abortController.signal })
|
||||
.then((res) => {
|
||||
if (retRaw) {
|
||||
const data = res as T;
|
||||
resolve([null, data, null]);
|
||||
} else {
|
||||
const resp = res as AxiosResponse<Result<T>>;
|
||||
resolve([null, resp.data.data, resp.data]);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
resolve([err as AxiosError, null, null]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
put<T>(url: string, data: AxiosRequestConfig['data'], option?: Partial<Omit<AxiosRequestConfig, 'data'>> & { uniq?: boolean }): Promise<Response<T>> {
|
||||
const { uniq, ...reqConfig } = option ?? {};
|
||||
this.uniq = !!uniq;
|
||||
return new Promise((resolve) => {
|
||||
this.instance
|
||||
.put<Result<T>>(url, data, { ...reqConfig, signal: this.abortController.signal })
|
||||
.then((res) => {
|
||||
resolve([null, res.data.data, res.data]);
|
||||
})
|
||||
.catch((err) => {
|
||||
resolve([err as AxiosError, null, null]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
delete<T>(url: string, idList: string[], option?: Partial<Omit<AxiosRequestConfig, 'data'>> & { uniq?: boolean }): Promise<Response<T>> {
|
||||
const { uniq, ...reqConfig } = option ?? {};
|
||||
this.uniq = !!uniq;
|
||||
return new Promise((resolve) => {
|
||||
this.instance
|
||||
.delete<Result<T>>(url, { ...reqConfig, data: { ids: idList }, signal: this.abortController.signal })
|
||||
.then((res) => {
|
||||
resolve([null, res.data.data, res.data]);
|
||||
})
|
||||
.catch((err) => {
|
||||
resolve([err as AxiosError, null, null]);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user