init
This commit is contained in:
7
src/utils/agent.ts
Normal file
7
src/utils/agent.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export function isPC() {
|
||||
const agents = ['Android', 'iPhone', 'webOS', 'BlackBerry', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
|
||||
|
||||
const isMobile = agents.some(agent => window.navigator.userAgent.includes(agent));
|
||||
|
||||
return !isMobile;
|
||||
}
|
||||
58
src/utils/common.ts
Normal file
58
src/utils/common.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
9
src/utils/icon.ts
Normal file
9
src/utils/icon.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export function getLocalIcons() {
|
||||
const svgIcons = import.meta.glob('/src/assets/svg-icon/*.svg');
|
||||
|
||||
const keys = Object.keys(svgIcons)
|
||||
.map(item => item.split('/').at(-1)?.replace('.svg', '') || '')
|
||||
.filter(Boolean);
|
||||
|
||||
return keys;
|
||||
}
|
||||
75
src/utils/service.ts
Normal file
75
src/utils/service.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import json5 from 'json5';
|
||||
|
||||
/**
|
||||
* 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 = json5.parse(VITE_OTHER_SERVICE_BASE_URL);
|
||||
} catch {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('VITE_OTHER_SERVICE_BASE_URL is not a valid json5 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 '/api';
|
||||
}
|
||||
|
||||
return `/proxy-${key}`;
|
||||
}
|
||||
9
src/utils/storage.ts
Normal file
9
src/utils/storage.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { createLocalforage, createStorage } from '@sa/utils';
|
||||
|
||||
const storagePrefix = import.meta.env.VITE_STORAGE_PREFIX || '';
|
||||
|
||||
export const localStg = createStorage<StorageType.Local>('local', storagePrefix);
|
||||
|
||||
export const sessionStg = createStorage<StorageType.Session>('session', storagePrefix);
|
||||
|
||||
export const localforage = createLocalforage<StorageType.Local>('local');
|
||||
Reference in New Issue
Block a user