95 lines
2.8 KiB
TypeScript
95 lines
2.8 KiB
TypeScript
import { SettingKey } from "~/types/Setting";
|
|
import { readonly, Ref, ref } from "vue";
|
|
import CommonAPI from "~/api/CommonAPI";
|
|
import { RunEnv, Toolkit } from "timi-web";
|
|
|
|
export default class SettingMapper {
|
|
|
|
private static instance: SettingMapper;
|
|
|
|
private map = new Map<string, Ref<string>>();
|
|
|
|
public static async loadSetting(...settings: { 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);
|
|
|
|
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++) {
|
|
map.set(settings[i].key, settings[i].args);
|
|
}
|
|
}
|
|
const instance = this.getInstance();
|
|
const result = await CommonAPI.listSetting(map);
|
|
for (const [key, value] of result) {
|
|
instance.map.set(key, ref(value));
|
|
}
|
|
}
|
|
|
|
public static is(key: SettingKey | string, args?: { [key: string]: any }): boolean {
|
|
const value = this.getValueRef(key, args).value;
|
|
return !value && value === 'true';
|
|
}
|
|
|
|
public static getValue(key: SettingKey | string, args?: { [key: string]: any }): string | undefined {
|
|
return this.getValueRef(key, args).value;
|
|
}
|
|
|
|
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;
|
|
}
|
|
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);
|
|
}
|
|
|
|
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;
|
|
}
|
|
return `${protocol}://${this.getValue(domainKey)}`;
|
|
}
|
|
|
|
public static getInstance(): SettingMapper {
|
|
if (SettingMapper.instance) {
|
|
return SettingMapper.instance;
|
|
}
|
|
return SettingMapper.instance = new SettingMapper();
|
|
}
|
|
}
|