mirror of
https://github.com/LifeArchiveProject/WeChatDataAnalysis.git
synced 2026-06-18 15:54:08 +08:00
b8d0912907
- 新增 dev 启动脚本,自动分配前后端端口并等待 Nuxt 就绪后再启动 Electron - 开发模式忽略持久化 API 覆盖,统一 Nuxt 与桌面端端口配置 - API 健康检查改为挂载后执行,聊天页预取改为 lazy,并隐藏搜索区账号切换
46 lines
1.5 KiB
JavaScript
46 lines
1.5 KiB
JavaScript
import { normalizeApiBase, readApiBaseOverride } from '~/lib/api-settings'
|
||
|
||
// Client-side cache so that useApiBase() can be called safely outside
|
||
// the Nuxt composable context (e.g. inside async callbacks / onMounted chains).
|
||
let _clientCache = ''
|
||
|
||
const shouldIgnoreStoredOverride = () => {
|
||
if (!process.client || !import.meta.dev) return false
|
||
return typeof window !== 'undefined' && !!window.wechatDesktop?.__brand
|
||
}
|
||
|
||
export const useApiBase = () => {
|
||
if (process.client && _clientCache) return _clientCache
|
||
|
||
// useRuntimeConfig() requires the Nuxt app context, which is only
|
||
// guaranteed during synchronous setup. On the client we cache the
|
||
// result so later (context-less) calls still work.
|
||
let config
|
||
try {
|
||
config = useRuntimeConfig()
|
||
} catch {
|
||
// Context unavailable – fall back to cached value or default.
|
||
return _clientCache || '/api'
|
||
}
|
||
|
||
// Default to same-origin `/api` so Nuxt devProxy / backend-mounted UI both work.
|
||
// Override priority:
|
||
// 1) Local UI setting (web + desktop)
|
||
// 2) NUXT_PUBLIC_API_BASE env/runtime config
|
||
// 3) `/api`
|
||
const override = process.client && !shouldIgnoreStoredOverride() ? readApiBaseOverride() : ''
|
||
const runtime = String(config?.public?.apiBase || '').trim()
|
||
const result = normalizeApiBase(override || runtime || '/api')
|
||
|
||
if (process.client) _clientCache = result
|
||
return result
|
||
}
|
||
|
||
/**
|
||
* Call this when the user changes the API base override in settings
|
||
* so the cached value is refreshed.
|
||
*/
|
||
export const invalidateApiBaseCache = () => {
|
||
_clientCache = ''
|
||
}
|