2024-05-06 01:01:58 +08:00
|
|
|
import { Md5 } from 'ts-md5';
|
2024-03-21 10:57:53 +08:00
|
|
|
import { $t } from '@/locales';
|
|
|
|
|
2024-03-08 17:59:45 +08:00
|
|
|
/**
|
|
|
|
* Transform record to option
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* const record = {
|
|
|
|
* key1: 'label1',
|
|
|
|
* key2: 'label2'
|
|
|
|
* };
|
|
|
|
* const options = transformRecordToOption(record);
|
|
|
|
* // [
|
|
|
|
* // { value: 'key1', label: 'label1' },
|
|
|
|
* // { value: 'key2', label: 'label2' }
|
|
|
|
* // ]
|
|
|
|
* ```;
|
|
|
|
*
|
|
|
|
* @param record
|
|
|
|
*/
|
|
|
|
export function transformRecordToOption<T extends Record<string, string>>(record: T) {
|
|
|
|
return Object.entries(record).map(([value, label]) => ({
|
|
|
|
value,
|
|
|
|
label
|
2024-03-21 10:57:53 +08:00
|
|
|
})) as CommonType.Option<keyof T>[];
|
|
|
|
}
|
|
|
|
|
2024-04-20 16:33:38 +08:00
|
|
|
/**
|
|
|
|
* Transform record to option with keys of number
|
|
|
|
*
|
|
|
|
* @param record
|
2024-04-20 22:36:09 +08:00
|
|
|
* @param reverse
|
2024-04-20 16:33:38 +08:00
|
|
|
*/
|
2024-04-20 17:14:47 +08:00
|
|
|
export function transformRecordToNumberOption<T extends Record<number, string>>(record: T, reverse: boolean = false) {
|
|
|
|
const options = Object.entries(record).map(([value, label]) => ({
|
2024-04-20 16:33:38 +08:00
|
|
|
value: Number(value),
|
|
|
|
label
|
|
|
|
})) as CommonType.Option<keyof T>[];
|
2024-04-20 17:14:47 +08:00
|
|
|
|
|
|
|
return reverse ? options.sort((a: any, b: any) => b.value - a.value) : options;
|
2024-04-20 16:33:38 +08:00
|
|
|
}
|
|
|
|
|
2024-03-21 10:57:53 +08:00
|
|
|
/**
|
|
|
|
* Translate options
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
*/
|
2024-04-19 17:42:11 +08:00
|
|
|
export function translateOptions(options: CommonType.Option<string | number>[]) {
|
2024-03-21 10:57:53 +08:00
|
|
|
return options.map(option => ({
|
|
|
|
...option,
|
|
|
|
label: $t(option.label as App.I18n.I18nKey)
|
|
|
|
}));
|
2024-03-08 17:59:45 +08:00
|
|
|
}
|
2024-04-20 00:03:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translate options
|
|
|
|
*
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
export function translateOptions2(options: string[]) {
|
|
|
|
return options.map(option => ({
|
|
|
|
value: option,
|
|
|
|
label: option
|
|
|
|
}));
|
|
|
|
}
|
2024-04-28 22:58:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* tag Color
|
|
|
|
*
|
|
|
|
* @param index
|
|
|
|
*/
|
2024-05-04 22:34:46 +08:00
|
|
|
export function tagColor(index: number) {
|
2024-04-28 22:58:02 +08:00
|
|
|
const tagMap: Record<number, NaiveUI.ThemeColor> = {
|
|
|
|
0: 'error',
|
|
|
|
1: 'info',
|
|
|
|
2: 'success',
|
|
|
|
3: 'warning',
|
2024-04-30 09:22:37 +08:00
|
|
|
4: 'primary'
|
2024-04-28 22:58:02 +08:00
|
|
|
};
|
|
|
|
|
2024-05-04 22:34:46 +08:00
|
|
|
if (index === null || index < 0) {
|
2024-04-29 17:46:07 +08:00
|
|
|
return tagMap[1];
|
|
|
|
}
|
|
|
|
|
2024-05-04 22:34:46 +08:00
|
|
|
return tagMap[index % 5];
|
2024-04-28 22:58:02 +08:00
|
|
|
}
|
2024-05-06 01:01:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* MD-5 哈希
|
|
|
|
*
|
|
|
|
* @param text 明文
|
|
|
|
* @returns md5哈希
|
|
|
|
*/
|
|
|
|
export function md5(text: string): string {
|
|
|
|
const md5Digest = new Md5();
|
|
|
|
md5Digest.appendAsciiStr(text);
|
|
|
|
return md5Digest.end() as string;
|
|
|
|
}
|
2024-05-08 11:57:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle html class
|
|
|
|
*
|
|
|
|
* @param className
|
|
|
|
*/
|
|
|
|
export function toggleHtmlClass(className: string) {
|
|
|
|
function add() {
|
|
|
|
document.documentElement.classList.add(className);
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove() {
|
|
|
|
document.documentElement.classList.remove(className);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
add,
|
|
|
|
remove
|
|
|
|
};
|
|
|
|
}
|