refactor Setting
This commit is contained in:
+73
-89
@@ -1,97 +1,99 @@
|
||||
import { readonly, Ref, ref } from "vue";
|
||||
import { RunEnv, SettingKey } from "../types";
|
||||
import Toolkit from "./Toolkit";
|
||||
import CommonAPI from "../api/CommonAPI";
|
||||
import {ref, Ref} from "vue";
|
||||
import {Setting} from "../types";
|
||||
import {CommonAPI} from "../api";
|
||||
|
||||
export default class SettingMapper {
|
||||
|
||||
private static instance: SettingMapper;
|
||||
|
||||
private map = new Map<string, Ref<string>>();
|
||||
private moduleMap = new Map<string, Map<string, Ref<Setting>>>();
|
||||
|
||||
public static async loadSetting(...settings: (string | { key: string, args?: { [key: string]: any }})[]): Promise<void> {
|
||||
const map = new Map<string, object | undefined>();
|
||||
{
|
||||
// 默认配置
|
||||
map.set(SettingKey.RUN_ENV, undefined);
|
||||
map.set(SettingKey.PUBLIC_RESOURCES, {
|
||||
as: "json"
|
||||
});
|
||||
map.set(SettingKey.DOMAIN_ROOT, undefined);
|
||||
map.set(SettingKey.DOMAIN_API, undefined);
|
||||
map.set(SettingKey.DOMAIN_GIT, undefined);
|
||||
map.set(SettingKey.DOMAIN_BLOG, undefined);
|
||||
map.set(SettingKey.DOMAIN_SPACE, undefined);
|
||||
map.set(SettingKey.DOMAIN_RESOURCE, undefined);
|
||||
map.set(SettingKey.DOMAIN_DOWNLOAD, undefined);
|
||||
public static async loadSetting(map: Record<string, string[]>): Promise<void> {
|
||||
const result = await CommonAPI.settingMap(map);
|
||||
this.appendSettingMap(result);
|
||||
}
|
||||
|
||||
map.set(SettingKey.ENABLE_COMMENT, undefined);
|
||||
map.set(SettingKey.ENABLE_DEBUG, undefined);
|
||||
map.set(SettingKey.ENABLE_LOGIN, undefined);
|
||||
map.set(SettingKey.ENABLE_REGISTER, undefined);
|
||||
map.set(SettingKey.ENABLE_USER_UPDATE, undefined);
|
||||
}
|
||||
{
|
||||
// 附加配置
|
||||
for (let i = 0; i < settings.length; i++) {
|
||||
const setting = settings[i];
|
||||
if (typeof setting === 'string') {
|
||||
map.set(setting, undefined);
|
||||
} else {
|
||||
map.set(setting.key, setting.args);
|
||||
}
|
||||
public static appendSettingMap(result: Map<string, Map<string, Setting>>): void {
|
||||
for (const [module, settingMap] of result) {
|
||||
for (const [key, setting] of settingMap) {
|
||||
this.setValue(module, key, setting);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static createModule(module: string): Map<string, Ref<Setting>> {
|
||||
const instance = this.getInstance();
|
||||
const result = await CommonAPI.listSetting(map);
|
||||
for (const [key, value] of result) {
|
||||
instance.map.set(key, ref(value));
|
||||
const moduleVal = instance.moduleMap.get(module);
|
||||
if (moduleVal) {
|
||||
return moduleVal;
|
||||
}
|
||||
const created = new Map<string, Ref<Setting>>();
|
||||
instance.moduleMap.set(module, created);
|
||||
return created;
|
||||
}
|
||||
|
||||
public static is(key: SettingKey | string, args?: { [key: string]: any }): boolean {
|
||||
const value = this.getValueRef(key, args).value;
|
||||
return !!value && value === 'true';
|
||||
public static getModule(module: string): Map<string, Ref<Setting>> | undefined {
|
||||
return this.getInstance().moduleMap.get(module);
|
||||
}
|
||||
|
||||
public static not(key: SettingKey | string, args?: { [key: string]: any }): boolean {
|
||||
return !this.is(key, args);
|
||||
public static hasModule(module: string): boolean {
|
||||
return this.getInstance().moduleMap.has(module);
|
||||
}
|
||||
|
||||
public static getValue(key: SettingKey | string, args?: { [key: string]: any }): string | undefined {
|
||||
return this.getValueRef(key, args).value;
|
||||
public static deleteModule(module: string): boolean {
|
||||
return this.getInstance().moduleMap.delete(module);
|
||||
}
|
||||
|
||||
public static getValueRef(key: SettingKey| string, args?: { [key: string]: any }): Ref<string | undefined> {
|
||||
const instance = this.getInstance();
|
||||
let result = instance.map.get(key);
|
||||
if (result) {
|
||||
return result;
|
||||
public static setModule(module: string, moduleVal: Map<string, Ref<Setting>>): void {
|
||||
this.getInstance().moduleMap.set(module, moduleVal);
|
||||
}
|
||||
|
||||
public static is(module: string, key: string): boolean {
|
||||
const setting = this.getValueRef(module, key).value;
|
||||
return !!setting.value && setting.value === "true";
|
||||
}
|
||||
|
||||
public static not(module: string, key: string): boolean {
|
||||
return !this.is(module, key);
|
||||
}
|
||||
|
||||
public static getValue(module: string, key: string): Setting {
|
||||
return this.getValueRef(module, key).value;
|
||||
}
|
||||
|
||||
public static setValue(module: string, key: string, setting: Setting): void {
|
||||
const moduleVal = this.createModule(module);
|
||||
const settingRef = moduleVal.get(key);
|
||||
if (settingRef) {
|
||||
settingRef.value = setting;
|
||||
return;
|
||||
}
|
||||
instance.map.set(key, result = ref<any>());
|
||||
Toolkit.async(async () => {
|
||||
const value = instance.map.get(key);
|
||||
if (value) {
|
||||
return value.value;
|
||||
}
|
||||
result.value = await CommonAPI.getSetting(key, args);
|
||||
});
|
||||
return readonly(result);
|
||||
moduleVal.set(key, ref(setting));
|
||||
}
|
||||
|
||||
public static getDomainLink(domainKey: SettingKey): string | undefined {
|
||||
const runEnv = <RunEnv>(this.getValue(SettingKey.RUN_ENV));
|
||||
let protocol = "https";
|
||||
switch (runEnv) {
|
||||
case RunEnv.DEV:
|
||||
protocol = "http";
|
||||
break;
|
||||
case RunEnv.DEV_SSL:
|
||||
case RunEnv.PROD:
|
||||
protocol = "https";
|
||||
break;
|
||||
public static setValueRef(module: string, key: string, settingRef: Ref<Setting>): void {
|
||||
const moduleVal = this.createModule(module);
|
||||
moduleVal.set(key, settingRef);
|
||||
}
|
||||
|
||||
public static hasValue(module: string, key: string): boolean {
|
||||
return !!this.getModule(module)?.has(key);
|
||||
}
|
||||
|
||||
public static deleteValue(module: string, key: string): boolean {
|
||||
return !!this.getModule(module)?.delete(key);
|
||||
}
|
||||
|
||||
public static getValueRef(module: string, key: string): Ref<Setting> {
|
||||
const moduleVal = this.getModule(module);
|
||||
if (!moduleVal) {
|
||||
throw new Error(`Setting module not found: ${module}`);
|
||||
}
|
||||
return `${protocol}://${this.getValue(domainKey)}`;
|
||||
const valueRef = moduleVal.get(key);
|
||||
if (!valueRef) {
|
||||
throw new Error(`Setting key not found: ${module}.${key}`);
|
||||
}
|
||||
return valueRef;
|
||||
}
|
||||
|
||||
public static getInstance(): SettingMapper {
|
||||
@@ -100,22 +102,4 @@ export default class SettingMapper {
|
||||
}
|
||||
return SettingMapper.instance = new SettingMapper();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析资源 URL 协议前缀
|
||||
*
|
||||
* @param val 原始值,如 `res@/path/to/file`
|
||||
* @returns 完整 URL
|
||||
*/
|
||||
public static toResURL(val: string): string {
|
||||
const at = val.indexOf("@");
|
||||
const start = val.substring(0, at);
|
||||
const path = val.substring(at + 1);
|
||||
switch (start) {
|
||||
case "res": return SettingMapper.getDomainLink(SettingKey.DOMAIN_RESOURCE) + path;
|
||||
case "dl": return SettingMapper.getDomainLink(SettingKey.DOMAIN_DOWNLOAD) + path;
|
||||
case "attach": return CommonAPI.getAttachmentReadAPI(path);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user