import type { Application } from '#/api/core/application'; import { computed, ref } from 'vue'; import { defineStore } from 'pinia'; import { getApplicationByCodeApi } from '#/api/core/application'; /** * 应用上下文 Store * 用于管理当前应用的状态,支持多应用架构 */ export const useAppContextStore = defineStore('app-context', () => { // 当前应用编码(从 URL 参数中获取) const appCode = ref(null); // 当前应用详情 const currentApp = ref(null); // 是否为子应用模式 const isSubApp = computed(() => !!appCode.value); // 是否为主应用模式 const isMainApp = computed(() => !appCode.value); /** * 从 URL 路径中初始化应用上下文 * URL 格式: * - /app/:appCode/... (正常模式) */ async function initFromUrl() { const path = window.location.pathname; const appMatch = path.match(/^\/app\/([^/]+)/); if (appMatch && appMatch[1]) { appCode.value = appMatch[1]; await loadCurrentApp(); return; } // 主应用模式 appCode.value = null; currentApp.value = null; } /** * 加载当前应用详情 */ async function loadCurrentApp() { if (!appCode.value) { currentApp.value = null; return; } try { const app = await getApplicationByCodeApi(appCode.value); currentApp.value = app; } catch (error) { console.error('Failed to load application:', error); currentApp.value = null; } } /** * 设置当前应用 * @param code 应用编码 */ async function setAppCode(code: null | string) { appCode.value = code; if (code) { await loadCurrentApp(); } else { currentApp.value = null; } } /** * 清除应用上下文 */ function clear() { appCode.value = null; currentApp.value = null; } /** * 获取子应用 URL * @param code 应用编码 */ function getSubAppUrl(code: string): string { const baseUrl = window.location.origin; return `${baseUrl}/app/${code}`; } /** * 获取当前上下文的路由路径 * 在子应用模式下会自动添加 /app/{code} 前缀 * @param path 原始路由路径(如 /ai-platform/workflow/editor/123) */ function getContextPath(path: string): string { if (!appCode.value) { return path; } return `/app/${appCode.value}${path}`; } /** * 重置 store 状态 */ function $reset() { appCode.value = null; currentApp.value = null; } return { appCode, currentApp, isSubApp, isMainApp, initFromUrl, loadCurrentApp, setAppCode, clear, getSubAppUrl, getContextPath, $reset, }; });