Files
WeChatDataAnalysis/frontend/composables/useApiBase.js
T
2977094657 b8d0912907 fix(dev-runtime): 统一开发端口并优化页面初始化
- 新增 dev 启动脚本,自动分配前后端端口并等待 Nuxt 就绪后再启动 Electron
- 开发模式忽略持久化 API 覆盖,统一 Nuxt 与桌面端端口配置
- API 健康检查改为挂载后执行,聊天页预取改为 lazy,并隐藏搜索区账号切换
2026-03-15 19:35:36 +08:00

46 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 = ''
}