From 1af59005934cb4cbc7a9f1ae0eae43e19569d81f Mon Sep 17 00:00:00 2001 From: xRain Date: Mon, 10 Aug 2026 20:37:23 +0800 Subject: [PATCH] fix config --- src/simapi.core.ts | 46 +++++++++++++++++---------------------------- src/simapi.pinia.ts | 6 +++--- 2 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/simapi.core.ts b/src/simapi.core.ts index 7351e04..0fe6e5d 100644 --- a/src/simapi.core.ts +++ b/src/simapi.core.ts @@ -84,6 +84,7 @@ export class SimApiCore { logout_url: '/auth/logout', login_url: '/auth/login', } + webConfig: Map> = new Map(); private api: SimApiApiConfig = { endpoints: {default: ''}, @@ -212,32 +213,19 @@ export class SimApiCore { console.log('[DEBUG]', ...args) } - /** - * 获取版本信息 - * - * @param endpointName - 指定从哪个 endpoint 获取版本,默认使用 default endpoint - * @returns 版本信息对象 - * - * @example - * // 从默认 endpoint 获取 - * const versions = await api.getVersion() - * - * // 从指定 endpoint 获取 - * const versions = await api.getVersion('backup') - */ - async getVersion(endpointName?: string): Promise { - const versions: SimApiVersions = { - uiApp: typeof AppVersion === 'undefined' ? "0.0.0-develop" : AppVersion, - uiSimApi: typeof SimApiVersion === 'undefined' ? "0.0.0-develop" : SimApiVersion, - apiApp: '0.0.0', - apiSimApi: '0.0.0', - apiAppFull: '0.0.0', - apiSimApiFull: '0.0.0', - }; - try { - const resp = await this.query('/versions', {}, endpointName) + async getConfig(reload: boolean = false, endpointName: string = 'default'): Promise> { + if (!this.webConfig.has(endpointName) || reload) { + const versions: SimApiVersions = { + uiApp: typeof AppVersion === 'undefined' ? "0.0.0-develop" : AppVersion, + uiSimApi: typeof SimApiVersion === 'undefined' ? "0.0.0-develop" : SimApiVersion, + apiApp: '0.0.0', + apiSimApi: '0.0.0', + apiAppFull: '0.0.0', + apiSimApiFull: '0.0.0', + }; + const resp = await this.query('/config', {}, endpointName) if (resp?.data) { - const d = resp.data + const d = resp.data.versions; versions.apiApp = d.App?.split('+')[0] ?? '0.0.0'; versions.apiSimApi = d.SimApi?.split('+')[0] ?? '0.0.0'; versions.apiAppFull = d.App ?? '0.0.0'; @@ -245,12 +233,12 @@ export class SimApiCore { if (this.debug) { console.log(`UI主应用版本: ${versions.uiApp}\nUISimApi版本: ${versions.uiSimApi}\nAPI主应用版本: ${versions.apiApp}\nAPISimApi版本: ${versions.apiSimApi}`) } - return versions + resp.data.versions = versions; + this.webConfig.set(endpointName, resp.data); + } - } catch { - // 版本获取失败返回默认值 } - return versions; + return this.webConfig.get(endpointName)!; } async query( diff --git a/src/simapi.pinia.ts b/src/simapi.pinia.ts index d179257..88e2c91 100644 --- a/src/simapi.pinia.ts +++ b/src/simapi.pinia.ts @@ -15,7 +15,7 @@ export const useSimApi = defineStore('simapi', { }, actions: { // 所有方法直接代理到 core - async loadFromFile(file: string = 'config.json'): Promise { + async loadFromFile(file: string = '/config.json'): Promise { return this._core.loadFromFile(file) }, @@ -76,8 +76,8 @@ export const useSimApi = defineStore('simapi', { return this._core.getEndpoint(name) }, - async getVersion(endpointName?: string): Promise { - return this._core.getVersion(endpointName) + async getConfig(reload = false, endpointName?: string): Promise> { + return this._core.getConfig(reload, endpointName) }, }, })