|
@@ -0,0 +1,186 @@
|
|
|
|
|
+/**
|
|
|
|
|
+ * 配置管理器
|
|
|
|
|
+ * 负责从远程API获取配置参数并保存到AppStorage中
|
|
|
|
|
+ */
|
|
|
|
|
+import { http } from '@kit.NetworkKit';
|
|
|
|
|
+import Logger from './Logger';
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 配置项接口定义
|
|
|
|
|
+ */
|
|
|
|
|
+export interface ConfigItem {
|
|
|
|
|
+ name: string;
|
|
|
|
|
+ description: string;
|
|
|
|
|
+ value: string | number | boolean;
|
|
|
|
|
+ type: 'json' | 'boolean' | 'number' | 'string';
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * JSON配置值类型 - 使用ESObject作为通用对象类型
|
|
|
|
|
+ */
|
|
|
|
|
+export type JsonConfigValue = ESObject;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 配置值联合类型
|
|
|
|
|
+ */
|
|
|
|
|
+export type ConfigValue = string | number | boolean | ESObject;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * API响应接口定义
|
|
|
|
|
+ */
|
|
|
|
|
+export interface ConfigResponse {
|
|
|
|
|
+ code: number;
|
|
|
|
|
+ msg: string;
|
|
|
|
|
+ data: ConfigItem[];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 配置管理器类
|
|
|
|
|
+ */
|
|
|
|
|
+export class ConfigManager {
|
|
|
|
|
+ private static readonly TAG = 'ConfigManager';
|
|
|
|
|
+ private static readonly CONFIG_API_URL = 'https://pay.ss5.xyz/switches/lists';
|
|
|
|
|
+ private static readonly REQUEST_TIMEOUT = 5000; // 5秒超时
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 初始化配置 - 从API获取配置并保存到AppStorage
|
|
|
|
|
+ * @returns Promise<boolean> 是否初始化成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public static async initConfig(): Promise<boolean> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Logger.info(ConfigManager.TAG, '开始初始化配置...');
|
|
|
|
|
+
|
|
|
|
|
+ const configData = await ConfigManager.fetchConfigFromAPI();
|
|
|
|
|
+ if (!configData) {
|
|
|
|
|
+ Logger.error(ConfigManager.TAG, '获取配置数据失败');
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ConfigManager.saveConfigToAppStorage(configData);
|
|
|
|
|
+ Logger.info(ConfigManager.TAG, '配置初始化完成');
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ Logger.error(ConfigManager.TAG, '配置初始化失败:' + error);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从API获取配置数据
|
|
|
|
|
+ * @returns Promise<ConfigItem[] | null> 配置数据数组或null
|
|
|
|
|
+ */
|
|
|
|
|
+ private static async fetchConfigFromAPI(): Promise<ConfigItem[] | null> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const httpRequest = http.createHttp();
|
|
|
|
|
+ const options: http.HttpRequestOptions = {
|
|
|
|
|
+ method: http.RequestMethod.GET,
|
|
|
|
|
+ readTimeout: ConfigManager.REQUEST_TIMEOUT,
|
|
|
|
|
+ connectTimeout: ConfigManager.REQUEST_TIMEOUT,
|
|
|
|
|
+ header: {
|
|
|
|
|
+ 'Content-Type': 'application/json'
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ Logger.info(ConfigManager.TAG, '请求配置API: ' + ConfigManager.CONFIG_API_URL);
|
|
|
|
|
+ const response: http.HttpResponse = await httpRequest.request(ConfigManager.CONFIG_API_URL, options);
|
|
|
|
|
+
|
|
|
|
|
+ if (response.responseCode === 200) {
|
|
|
|
|
+ const responseData = response.result as string;
|
|
|
|
|
+ const configResponse: ConfigResponse = JSON.parse(responseData);
|
|
|
|
|
+
|
|
|
|
|
+ if (configResponse.code === 0) {
|
|
|
|
|
+ Logger.info(ConfigManager.TAG, `成功获取${configResponse.data.length}个配置项`);
|
|
|
|
|
+ return configResponse.data;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Logger.error(ConfigManager.TAG, 'API返回错误:' + configResponse.msg);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ Logger.error(ConfigManager.TAG, '请求失败,状态码:' + response.responseCode);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ Logger.error(ConfigManager.TAG, '请求配置API异常:' + error);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 将配置数据保存到AppStorage
|
|
|
|
|
+ * @param configData 配置数据数组
|
|
|
|
|
+ */
|
|
|
|
|
+ private static saveConfigToAppStorage(configData: ConfigItem[]): void {
|
|
|
|
|
+ try {
|
|
|
|
|
+ configData.forEach(config => {
|
|
|
|
|
+ let processedValue: ConfigValue = config.value;
|
|
|
|
|
+
|
|
|
|
|
+ // 根据类型处理值
|
|
|
|
|
+ switch (config.type) {
|
|
|
|
|
+ case 'json':
|
|
|
|
|
+ try {
|
|
|
|
|
+ processedValue = JSON.parse(config.value as string) as ESObject;
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ Logger.error(ConfigManager.TAG, `解析JSON配置失败 ${config.name}: ${e}`);
|
|
|
|
|
+ processedValue = config.value;
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'boolean':
|
|
|
|
|
+ processedValue = Boolean(config.value);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'number':
|
|
|
|
|
+ processedValue = Number(config.value);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'string':
|
|
|
|
|
+ default:
|
|
|
|
|
+ processedValue = String(config.value);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 保存到AppStorage
|
|
|
|
|
+ AppStorage.setOrCreate(config.name, processedValue);
|
|
|
|
|
+ Logger.info(ConfigManager.TAG, `保存配置 ${config.name}: ${processedValue} (${config.type})`);
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ Logger.error(ConfigManager.TAG, '保存配置到AppStorage失败:' + error);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取配置值
|
|
|
|
|
+ * @param key 配置键名
|
|
|
|
|
+ * @param defaultValue 默认值
|
|
|
|
|
+ * @returns 配置值
|
|
|
|
|
+ */
|
|
|
|
|
+ public static getConfig<T extends ConfigValue>(key: string, defaultValue: T): T {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const value: ConfigValue | undefined = AppStorage.get(key) as ConfigValue | undefined;
|
|
|
|
|
+ return value !== undefined ? value as T : defaultValue;
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ Logger.error(ConfigManager.TAG, `获取配置失败 ${key}: ${error}`);
|
|
|
|
|
+ return defaultValue;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 设置配置值
|
|
|
|
|
+ * @param key 配置键名
|
|
|
|
|
+ * @param value 配置值
|
|
|
|
|
+ */
|
|
|
|
|
+ public static setConfig(key: string, value: ConfigValue): void {
|
|
|
|
|
+ try {
|
|
|
|
|
+ AppStorage.setOrCreate(key, value);
|
|
|
|
|
+ Logger.info(ConfigManager.TAG, `更新配置 ${key}: ${value}`);
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ Logger.error(ConfigManager.TAG, `设置配置失败 ${key}: ${error}`);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 刷新配置 - 重新从API获取配置
|
|
|
|
|
+ * @returns Promise<boolean> 是否刷新成功
|
|
|
|
|
+ */
|
|
|
|
|
+ public static async refreshConfig(): Promise<boolean> {
|
|
|
|
|
+ Logger.info(ConfigManager.TAG, '刷新配置...');
|
|
|
|
|
+ return await ConfigManager.initConfig();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|