Skip to content

Commit f503bdc

Browse files
committed
feat: 优化首页重复初始化
- 将 `handleUrlQuery` 的失败回调限定在有后端 URL 参数时才执行,避免普通场景误触发连接失败弹窗 - `useHostAPI.setCurrent` 新增 `skipInit` 选项,支持在特定场景下切换当前 API 时不立刻重建 store - `useHostAPI` 增加 `skipNextCurrentInit` 内部开关,确保从 URL 添加/切换后的 `setCurrent` 不再触发重复 `initStores` - `globalStore.setHostAPI` 增加可选 `skipInit` 参数,由调用方决定是否执行 `initStores` - URL 配置流程(已存在/新增接口、magic path)调用 `setCurrent(..., { skipInit: true })`,与页面初始化逻辑保持一致
1 parent 184fadd commit f503bdc

4 files changed

Lines changed: 45 additions & 35 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store-front-end",
3-
"version": "2.17.23",
3+
"version": "2.17.24",
44
"private": true,
55
"scripts": {
66
"dev": "vite --host",

src/App.vue

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -119,40 +119,42 @@ const processUrlApiConfig = async () => {
119119
120120
// 处理URL参数中的后端配置
121121
const result = await handleUrlQuery({
122-
errorCb: async () => {
123-
try {
124-
// 检查用户是否跳过了当前连接检测周期
125-
const skippedCycle = parseInt(sessionStorage.getItem('skippedConnectionCycle') || '0');
126-
127-
// 尝试初始化stores,获取后端环境信息
128-
await initStores(true, true, false);
129-
130-
// 检查是否有环境信息
131-
const hasBackendEnv = Object.keys(globalStore.env).length > 0 && globalStore.env.backend;
132-
133-
if (hasBackendEnv) {
134-
showMagicPathDialog.value = false;
135-
localStorage.setItem('backendConfigured', 'true');
136-
globalStore.setFetchResult(true);
137-
} else {
122+
errorCb: hasUrlParams
123+
? async () => {
124+
try {
125+
// 检查用户是否跳过了当前连接检测周期
126+
const skippedCycle = parseInt(sessionStorage.getItem('skippedConnectionCycle') || '0');
127+
128+
// 尝试初始化stores,获取后端环境信息
129+
await initStores(true, true, false);
130+
131+
// 检查是否有环境信息
132+
const hasBackendEnv = Object.keys(globalStore.env).length > 0 && globalStore.env.backend;
133+
134+
if (hasBackendEnv) {
135+
showMagicPathDialog.value = false;
136+
localStorage.setItem('backendConfigured', 'true');
137+
globalStore.setFetchResult(true);
138+
} else {
139+
globalStore.setFetchResult(false);
140+
141+
if (route.path === '/subs' && skippedCycle !== connectionCheckCycle.value) {
142+
showMagicPathDialog.value = true;
143+
}
144+
}
145+
} catch (e) {
146+
console.error('Error initializing stores:', e);
138147
globalStore.setFetchResult(false);
139148
149+
// 检查用户是否跳过了当前连接检测周期
150+
const skippedCycle = parseInt(sessionStorage.getItem('skippedConnectionCycle') || '0');
151+
140152
if (route.path === '/subs' && skippedCycle !== connectionCheckCycle.value) {
141153
showMagicPathDialog.value = true;
142154
}
143155
}
144-
} catch (e) {
145-
console.error('Error initializing stores:', e);
146-
globalStore.setFetchResult(false);
147-
148-
// 检查用户是否跳过了当前连接检测周期
149-
const skippedCycle = parseInt(sessionStorage.getItem('skippedConnectionCycle') || '0');
150-
151-
if (route.path === '/subs' && skippedCycle !== connectionCheckCycle.value) {
152-
showMagicPathDialog.value = true;
153-
}
154156
}
155-
},
157+
: undefined,
156158
});
157159
158160
if (result) {

src/hooks/useHostAPI.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ export const useHostAPI = () => {
3737
const { showNotify } = useAppNotifyStore();
3838
const apis = ref(getHostAPI().apis);
3939
const currentName = ref(getHostAPI().current);
40+
let skipNextCurrentInit = false;
4041
const currentUrl = computed(() => {
4142
const url = apis.value.find(api => api.name === currentName.value)?.url ?? defaultAPI
4243
return url.startsWith('/') ? `${window.location.origin}${url}` : url;
4344
});
4445

4546
const stopWatchCurrent = watch(currentName, async (newName, oldName) => {
4647
if (newName !== oldName) {
48+
const skipInit = skipNextCurrentInit;
49+
skipNextCurrentInit = false;
50+
4751
// 保存新的API配置
4852
setHostAPI({
4953
...getHostAPI(),
@@ -59,7 +63,7 @@ export const useHostAPI = () => {
5963
globalStore.setFetchResult(false);
6064

6165
// 设置新的API URL并初始化stores
62-
await globalStore.setHostAPI(url);
66+
await globalStore.setHostAPI(url, { skipInit });
6367
}
6468
});
6569
const stopWatchApis = watch(
@@ -77,10 +81,13 @@ export const useHostAPI = () => {
7781
stopWatchApis();
7882
});
7983

80-
const setCurrent = (name: string) => {
84+
const setCurrent = (name: string, options?: { skipInit?: boolean }) => {
8185
if (name !== '' && !apis.value.find(api => api.name === name)) {
8286
return;
8387
}
88+
if (options?.skipInit && currentName.value !== name) {
89+
skipNextCurrentInit = true;
90+
}
8491
currentName.value = name;
8592
};
8693

@@ -269,7 +276,7 @@ export const useHostAPI = () => {
269276
globalStore.setFetchResult(false);
270277

271278
// 切换到新API
272-
setCurrent(isExist.name);
279+
setCurrent(isExist.name, { skipInit: true });
273280

274281
// 设置已配置标志,表示用户已通过URL参数成功配置了后端
275282
localStorage.setItem('backendConfigured', 'true');
@@ -285,7 +292,7 @@ export const useHostAPI = () => {
285292
const addResult = await addApi({ name, url }, true);
286293
if (addResult) {
287294
// 设置为当前API
288-
setCurrent(name);
295+
setCurrent(name, { skipInit: true });
289296

290297
// 清除旧的连接状态
291298
globalStore.setFetchResult(false);
@@ -386,7 +393,7 @@ export const useHostAPI = () => {
386393
globalStore.setFetchResult(false);
387394

388395
// 切换到新API
389-
setCurrent(isExist.name);
396+
setCurrent(isExist.name, { skipInit: true });
390397

391398
// 设置已配置标志
392399
localStorage.setItem('magicPathConfigured', 'true');
@@ -403,7 +410,7 @@ export const useHostAPI = () => {
403410
const addResult = await addApi({ name, url: apiUrl }, true);
404411
if (addResult) {
405412
// 设置为当前API
406-
setCurrent(name);
413+
setCurrent(name, { skipInit: true });
407414

408415
// 清除旧的连接状态
409416
globalStore.setFetchResult(false);

src/store/global.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,10 @@ export const useGlobalStore = defineStore('globalStore', {
184184
}
185185
this.istabBar2 = istabBar2;
186186
},
187-
async setHostAPI(hostApi: string) {
187+
async setHostAPI(hostApi: string, options?: { skipInit?: boolean }) {
188188
this.ishostApi = hostApi;
189189
service.defaults.baseURL = hostApi;
190+
if (options?.skipInit) return;
190191
await initStores(true, true, true);
191192
},
192193
async setEnv(options?: { bypassCache?: boolean; strict?: boolean }) {

0 commit comments

Comments
 (0)