moc变更管理系统框架
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import { $t } from '@/locales';
|
||||
|
||||
/**
|
||||
* 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
|
||||
})) as CommonType.Option<keyof T, T[keyof T]>[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate options
|
||||
*
|
||||
* @param options
|
||||
*/
|
||||
export function translateOptions(options: CommonType.Option<string, App.I18n.I18nKey>[]) {
|
||||
return options.map(option => ({
|
||||
...option,
|
||||
label: $t(option.label)
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
};
|
||||
}
|
||||
|
||||
// 辅助函数:将 hex 转为 rgba
|
||||
export function hexToRgba(hex: string, alpha: number) {
|
||||
// 去掉 # 号
|
||||
hex = hex.replace('#', '');
|
||||
// 处理缩写形式 #RGB
|
||||
if (hex.length === 3) {
|
||||
hex = hex.split('').map(c => c + c).join('');
|
||||
}
|
||||
const r = parseInt(hex.substring(0, 2), 16);
|
||||
const g = parseInt(hex.substring(2, 4), 16);
|
||||
const b = parseInt(hex.substring(4, 6), 16);
|
||||
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
||||
}
|
||||
export const getColorWithOpacity = (color: string, opacity: number) => {
|
||||
// 如果 color 本身已经是 rgba 或 hsl 等,可自行扩展判断
|
||||
// 这里仅处理 hex
|
||||
return hexToRgba(color, opacity);
|
||||
};
|
||||
|
||||
// 时间戳转换成年月日时分秒
|
||||
export function formatTimestamp(timestamp: number | string,format: string = 'yyyy-MM-dd HH:mm:ss') {
|
||||
if(!timestamp) {
|
||||
return '';
|
||||
}
|
||||
const date = new Date(Number(timestamp));
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
const hours = String(date.getHours()).padStart(2, '0');
|
||||
const minutes = String(date.getMinutes()).padStart(2, '0');
|
||||
const seconds = String(date.getSeconds()).padStart(2, '0');
|
||||
if(format === 'yyyy-MM-dd') {
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
if(format === 'yyyy-MM-dd HH:mm') {
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
||||
}
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||||
}
|
||||
Reference in New Issue
Block a user