初始化项目
This commit is contained in:
38
src/utils/common.ts
Normal file
38
src/utils/common.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
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>[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate options
|
||||
*
|
||||
* @param options
|
||||
*/
|
||||
export function translateOptions(options: CommonType.Option<string>[]) {
|
||||
return options.map(option => ({
|
||||
...option,
|
||||
label: $t(option.label as App.I18n.I18nKey)
|
||||
}));
|
||||
}
|
||||
74
src/utils/menu.ts
Normal file
74
src/utils/menu.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import type { DataNode } from 'ant-design-vue/es/tree';
|
||||
|
||||
/**
|
||||
* Antd tree component don't add root array key to checkedKeys(only in select all), so we need to add root node id to
|
||||
* checkedKeys
|
||||
*
|
||||
* @param menuList menu list
|
||||
* @param checkedKeys checked keys
|
||||
* @returns
|
||||
*/
|
||||
export function transformMenuChildWithRootIds(menuList: DataNode[], checkedKeys: number[]): number[] {
|
||||
const checkedKeysSet = new Set(checkedKeys);
|
||||
|
||||
function changeCheckedKeys(menus: DataNode[]) {
|
||||
menus.forEach(menu => {
|
||||
if (menu.children) {
|
||||
changeCheckedKeys(menu.children);
|
||||
checkedKeysSet.add(menu.key as number);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
changeCheckedKeys(menuList);
|
||||
|
||||
return Array.from(checkedKeysSet);
|
||||
}
|
||||
|
||||
export function transformListToTree<T extends { parentId: number; children?: any[] }>(
|
||||
list: T[],
|
||||
rowKey: Exclude<keyof T, 'children'>
|
||||
) {
|
||||
const tree = cloneDeep(list);
|
||||
const map = new Map<number | string, T>();
|
||||
tree.forEach(item => {
|
||||
map.set(item[rowKey] as string, item);
|
||||
});
|
||||
|
||||
const treeData: T[] = [];
|
||||
tree.forEach(item => {
|
||||
if (item.parentId === 0) {
|
||||
treeData.push(item);
|
||||
} else {
|
||||
const parent = map.get(item.parentId);
|
||||
if (parent) {
|
||||
if (!parent.children) {
|
||||
parent.children = [];
|
||||
}
|
||||
parent.children.push(item);
|
||||
}
|
||||
}
|
||||
});
|
||||
return removeEmptyChildren(treeData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove empty children
|
||||
*
|
||||
* @param treeDataGet tree data
|
||||
*/
|
||||
export function removeEmptyChildren<T extends { parentId: number; children?: any[] }>(
|
||||
treeDataGet: T[],
|
||||
isClone = false
|
||||
) {
|
||||
const tree = isClone ? cloneDeep(treeDataGet) : treeDataGet;
|
||||
tree.forEach(item => {
|
||||
if (item.children && item.children.length) {
|
||||
item.children = item.children.sort((a, b) => a.orderNum - b.orderNum);
|
||||
removeEmptyChildren(item.children);
|
||||
} else {
|
||||
delete item.children;
|
||||
}
|
||||
});
|
||||
return tree;
|
||||
}
|
||||
7
src/utils/permission.ts
Normal file
7
src/utils/permission.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { useAuthStore } from '@/store/modules/auth';
|
||||
|
||||
export function isShowBtn(permission: string) {
|
||||
const { permissions, userInfo } = storeToRefs(useAuthStore());
|
||||
|
||||
return userInfo.value.roles.includes('admin') || permissions.value.includes(permission);
|
||||
}
|
||||
73
src/utils/service.ts
Normal file
73
src/utils/service.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Create service config by current env
|
||||
*
|
||||
* @param env The current env
|
||||
*/
|
||||
export function createServiceConfig(env: Env.ImportMeta) {
|
||||
const { VITE_SERVICE_BASE_URL, VITE_OTHER_SERVICE_BASE_URL } = env;
|
||||
|
||||
let other = {} as Record<App.Service.OtherBaseURLKey, string>;
|
||||
try {
|
||||
other = JSON.parse(VITE_OTHER_SERVICE_BASE_URL);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('VITE_OTHER_SERVICE_BASE_URL is not a valid JSON string');
|
||||
}
|
||||
|
||||
const httpConfig: App.Service.SimpleServiceConfig = {
|
||||
baseURL: VITE_SERVICE_BASE_URL,
|
||||
other
|
||||
};
|
||||
|
||||
const otherHttpKeys = Object.keys(httpConfig.other) as App.Service.OtherBaseURLKey[];
|
||||
|
||||
const otherConfig: App.Service.OtherServiceConfigItem[] = otherHttpKeys.map(key => {
|
||||
return {
|
||||
key,
|
||||
baseURL: httpConfig.other[key],
|
||||
proxyPattern: createProxyPattern(key)
|
||||
};
|
||||
});
|
||||
|
||||
const config: App.Service.ServiceConfig = {
|
||||
baseURL: httpConfig.baseURL,
|
||||
proxyPattern: createProxyPattern(),
|
||||
other: otherConfig
|
||||
};
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* get backend service base url
|
||||
*
|
||||
* @param env - the current env
|
||||
* @param isProxy - if use proxy
|
||||
*/
|
||||
export function getServiceBaseURL(env: Env.ImportMeta, isProxy: boolean) {
|
||||
const { baseURL, other } = createServiceConfig(env);
|
||||
|
||||
const otherBaseURL = {} as Record<App.Service.OtherBaseURLKey, string>;
|
||||
|
||||
other.forEach(item => {
|
||||
otherBaseURL[item.key] = isProxy ? item.proxyPattern : item.baseURL;
|
||||
});
|
||||
|
||||
return {
|
||||
baseURL: isProxy ? createProxyPattern() : baseURL,
|
||||
otherBaseURL
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get proxy pattern of backend service base url
|
||||
*
|
||||
* @param key If not set, will use the default key
|
||||
*/
|
||||
function createProxyPattern(key?: App.Service.OtherBaseURLKey) {
|
||||
if (!key) {
|
||||
return '/proxy-default';
|
||||
}
|
||||
|
||||
return `/proxy-${key}`;
|
||||
}
|
||||
7
src/utils/storage.ts
Normal file
7
src/utils/storage.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { createLocalforage, createStorage } from '@sa/utils';
|
||||
|
||||
export const localStg = createStorage<StorageType.Local>('local');
|
||||
|
||||
export const sessionStg = createStorage<StorageType.Session>('session');
|
||||
|
||||
export const localforage = createLocalforage<StorageType.Local>('local');
|
||||
Reference in New Issue
Block a user