diff --git a/frontend-vue/src/shared/utils/api-secret-inheritance.ts b/frontend-vue/src/shared/utils/api-secret-inheritance.ts index 0fe3cb069..6c34dd291 100644 --- a/frontend-vue/src/shared/utils/api-secret-inheritance.ts +++ b/frontend-vue/src/shared/utils/api-secret-inheritance.ts @@ -132,6 +132,8 @@ export async function inheritSecretsFromDevice( console.warn('[api-secret] 读取本机继承快照失败(下次加载重试):', error) return null }) + // 等配置期间可能已经登出/换号:旧账号的快照不能写进新账号的服务端 + if (host.currentUid() !== uid) return { count: 0, notice: null } const stash = readInheritedSecretStash(config, host.moduleKeys()) if (!stash || stash.fromUid === uid) return { count: 0, notice: null } const serverHas = new Set(serverItems.filter((item) => item?.exists).map((item) => item.moduleKey)) diff --git a/frontend-vue/src/shared/utils/api-secret-store.ts b/frontend-vue/src/shared/utils/api-secret-store.ts index 4f7272a68..cd9111076 100644 --- a/frontend-vue/src/shared/utils/api-secret-store.ts +++ b/frontend-vue/src/shared/utils/api-secret-store.ts @@ -410,7 +410,10 @@ export function ensureApiSecretsLoaded(): Promise { return loadApiSecrets() } -async function applyServerBundle(bundle: UserApiSecretBundle | null | undefined): Promise { +async function applyServerBundle( + bundle: UserApiSecretBundle | null | undefined, + epoch: number, +): Promise { if (Array.isArray(bundle?.requiredModules) && bundle.requiredModules.length) { requiredModules = bundle.requiredModules } @@ -420,6 +423,8 @@ async function applyServerBundle(bundle: UserApiSecretBundle | null | undefined) } } serverProxy = toServerProxy(bundle?.proxy) + // 登出/换号可能发生在读配置的等待里:旧账号的锁定代理绝不能写进新账号的本地配置 + if (epoch !== cacheEpoch) return if (serverProxy?.locked && serverProxy.full) { // 后台锁定代理:把后台值对齐到本地配置(客户端任务链读本地),先比对、幂等、失败静默 await syncLockedProxyToLocal(serverProxy.full) @@ -434,7 +439,7 @@ async function doLoad(): Promise { try { let bundle = await fetchMyApiSecrets() if (epoch !== cacheEpoch) return 'unknown' - await applyServerBundle(bundle) + await applyServerBundle(bundle, epoch) if (epoch !== cacheEpoch) return 'unknown' bundleLoaded = true notify() @@ -443,7 +448,7 @@ async function doLoad(): Promise { if (migrated > 0) { bundle = await fetchMyApiSecrets() if (epoch !== cacheEpoch) return 'unknown' - await applyServerBundle(bundle) + await applyServerBundle(bundle, epoch) if (epoch !== cacheEpoch) return 'unknown' notify() } @@ -455,7 +460,7 @@ async function doLoad(): Promise { if (inherited > 0) { bundle = await fetchMyApiSecrets() if (epoch !== cacheEpoch) return 'unknown' - await applyServerBundle(bundle) + await applyServerBundle(bundle, epoch) if (epoch !== cacheEpoch) return 'unknown' notify() } @@ -524,9 +529,12 @@ async function tryMigrateLocalSecrets(serverItems: UserApiSecretItem[]): Promise */ async function tryBackfillProxyToServer(): Promise { try { - if (currentUid() === '0') return false + const uid = currentUid() + if (uid === '0') return false const config = await readConfigForBackfill() if (!config) return false + // 等配置期间可能已经登出/换号:补报请求会带新账号令牌,必须放弃旧账号的本地代理 + if (currentUid() !== uid) return false const url = readLocalProxyUrl(config) if (!url) return false const result = await migrateMyApiSecrets([{ moduleKey: 'proxy', value: url }]) @@ -612,9 +620,11 @@ async function syncLockedProxyToLocal(full: string): Promise { if (typeof window === 'undefined') return const api = window.pywebview?.api if (!api?.read_config || !api?.save_config) return + const uidAtStart = currentUid() const config = (await api.read_config()) as Record | null + if (currentUid() !== uidAtStart) return if (readLocalProxyUrl(config) === full) return - const uid = currentUid() + const uid = uidAtStart if (uid === '0') { const mode = Number(config?.proxy_mode) === 2 ? 2 : 1 await api.save_config({ proxy_url: full, proxy_mode: mode })