mirror of
https://github.com/certd/certd.git
synced 2026-04-23 19:57:27 +08:00
build: trident-sync prepare
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import * as envs from "./util.env";
|
||||
import * as sites from "./util.site";
|
||||
import * as storages from "./util.storage";
|
||||
import * as commons from "./util.common";
|
||||
import * as mitt from "./util.mitt";
|
||||
export const util = {
|
||||
...envs,
|
||||
...sites,
|
||||
...storages,
|
||||
...commons,
|
||||
...mitt
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
import _ from "lodash-es";
|
||||
export default {
|
||||
arrayToMap(array) {
|
||||
if (!array) {
|
||||
return {};
|
||||
}
|
||||
if (!_.isArray(array)) {
|
||||
return array;
|
||||
}
|
||||
const map = {};
|
||||
for (const item of array) {
|
||||
if (item.key) {
|
||||
map[item.key] = item;
|
||||
}
|
||||
}
|
||||
return map;
|
||||
},
|
||||
mapToArray(map) {
|
||||
if (!map) {
|
||||
return [];
|
||||
}
|
||||
if (_.isArray(map)) {
|
||||
return map;
|
||||
}
|
||||
const array: any = [];
|
||||
for (const key in map) {
|
||||
const item = map[key];
|
||||
item.key = key;
|
||||
array.push(item);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
import _ from "lodash-es";
|
||||
export function getEnvValue(key) {
|
||||
// @ts-ignore
|
||||
return import.meta.env["VITE_APP_" + key];
|
||||
}
|
||||
|
||||
export class EnvConfig {
|
||||
API;
|
||||
MODE;
|
||||
STORAGE;
|
||||
TITLE;
|
||||
PM_ENABLED;
|
||||
constructor() {
|
||||
this.init();
|
||||
}
|
||||
|
||||
init() {
|
||||
// @ts-ignore
|
||||
_.forEach(import.meta.env, (value, key) => {
|
||||
if (key.startsWith("VITE_APP")) {
|
||||
key = key.replace("VITE_APP_", "");
|
||||
this[key] = value;
|
||||
}
|
||||
});
|
||||
// @ts-ignore
|
||||
this.MODE = import.meta.env.MODE;
|
||||
}
|
||||
|
||||
get(key, defaultValue) {
|
||||
return this[key] ?? defaultValue;
|
||||
}
|
||||
isDev() {
|
||||
return this.MODE === "development" || this.MODE === "debug";
|
||||
}
|
||||
isProd() {
|
||||
return this.MODE === "production";
|
||||
}
|
||||
}
|
||||
|
||||
export const env = new EnvConfig();
|
||||
@@ -0,0 +1,2 @@
|
||||
import mitt from "mitt";
|
||||
export const mitter = mitt();
|
||||
@@ -0,0 +1,11 @@
|
||||
import { env } from "./util.env";
|
||||
export const site = {
|
||||
/**
|
||||
* @description 更新标题
|
||||
* @param {String} title 标题
|
||||
*/
|
||||
title: function (titleText) {
|
||||
const processTitle = env.TITLE || "FsAdmin";
|
||||
window.document.title = `${processTitle}${titleText ? ` | ${titleText}` : ""}`;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,113 @@
|
||||
import { env } from "./util.env";
|
||||
function isNullOrUnDef(value) {
|
||||
return value == null;
|
||||
}
|
||||
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;
|
||||
export interface CreateStorageParams {
|
||||
prefixKey: string;
|
||||
storage: Storage;
|
||||
timeout?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
*Cache class
|
||||
*Construction parameters can be passed into sessionStorage, localStorage,
|
||||
* @class Cache
|
||||
* @example
|
||||
*/
|
||||
export class WebStorage {
|
||||
private storage: Storage;
|
||||
private prefixKey?: string;
|
||||
private timeout?: number;
|
||||
/**
|
||||
*
|
||||
* @param option
|
||||
*/
|
||||
constructor(option: Partial<CreateStorageParams>) {
|
||||
this.storage = option.storage ?? localStorage;
|
||||
this.prefixKey = option.prefixKey ?? getStorageShortName();
|
||||
this.timeout = option.timeout ?? DEFAULT_CACHE_TIME;
|
||||
}
|
||||
|
||||
private getKey(key: string) {
|
||||
return `${this.prefixKey}${key}`.toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Set cache
|
||||
* @param {string} key
|
||||
* @param {*} value
|
||||
* @param expire
|
||||
* @expire Expiration time in seconds
|
||||
* @memberof Cache
|
||||
*/
|
||||
set(key: string, value: any, expire: number | undefined = this.timeout) {
|
||||
const stringData = JSON.stringify({
|
||||
value,
|
||||
time: Date.now(),
|
||||
expire: expire != null ? new Date().getTime() + expire * 1000 : null
|
||||
});
|
||||
this.storage.setItem(this.getKey(key), stringData);
|
||||
}
|
||||
|
||||
/**
|
||||
*Read cache
|
||||
* @param {string} key
|
||||
* @param def
|
||||
* @memberof Cache
|
||||
*/
|
||||
get(key: string, def: any = null): any {
|
||||
const val = this.storage.getItem(this.getKey(key));
|
||||
if (!val) return def;
|
||||
|
||||
try {
|
||||
const data = JSON.parse(val);
|
||||
const { value, expire } = data;
|
||||
if (isNullOrUnDef(expire) || expire >= new Date().getTime()) {
|
||||
return value;
|
||||
}
|
||||
this.remove(key);
|
||||
} catch (e) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete cache based on key
|
||||
* @param {string} key
|
||||
* @memberof Cache
|
||||
*/
|
||||
remove(key: string) {
|
||||
this.storage.removeItem(this.getKey(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all caches of this instance
|
||||
*/
|
||||
clear(): void {
|
||||
this.storage.clear();
|
||||
}
|
||||
}
|
||||
export const createStorage = (option: Partial<CreateStorageParams> = {}): WebStorage => {
|
||||
return new WebStorage(option);
|
||||
};
|
||||
|
||||
export type Options = Partial<CreateStorageParams>;
|
||||
|
||||
function getStorageShortName() {
|
||||
return env.MODE + "_" + env.get("STORAGE", "certd") + "_";
|
||||
}
|
||||
|
||||
export const createSessionStorage = (options: Options = {}): WebStorage => {
|
||||
return createStorage({ storage: sessionStorage, ...options });
|
||||
};
|
||||
|
||||
export const createLocalStorage = (options: Options = {}): WebStorage => {
|
||||
return createStorage({ storage: localStorage, timeout: DEFAULT_CACHE_TIME, ...options });
|
||||
};
|
||||
|
||||
export const SessionStorage = createSessionStorage();
|
||||
export const LocalStorage = createLocalStorage();
|
||||
|
||||
export default WebStorage;
|
||||
Reference in New Issue
Block a user