router guard: wrap in try/catch, add single-department auto-selection
- Wrap entire beforeEach guard in try/catch to prevent crashes from breaking navigation and leaving a blank page - Add missing next() at end of try block so all code paths resolve - Integrate switchDepartment() after loadMyDepartments() when exactly 1 department is available, enabling transparent single-dept flow Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+12
-4
@@ -70,9 +70,10 @@ const parseCurrentOrgIdFromToken = (token: string | null): string | undefined =>
|
|||||||
};
|
};
|
||||||
|
|
||||||
router.beforeEach(async (to, from , next) => {
|
router.beforeEach(async (to, from , next) => {
|
||||||
|
try {
|
||||||
document.title = to.meta.title as string;
|
document.title = to.meta.title as string;
|
||||||
let { store, read, login, logout, chooseRole, getUnreadTongzhis, set_editing } = useUser();
|
let { store, read, login, logout, chooseRole, getUnreadTongzhis, set_editing } = useUser();
|
||||||
let { store: orgStore, loadMyOrgs, switchOrg, loadMyDepartments } = useOrg();
|
let { store: orgStore, loadMyOrgs, switchOrg, loadMyDepartments, switchDepartment } = useOrg();
|
||||||
set_editing(false)
|
set_editing(false)
|
||||||
store.unionid = (store.unionid || cookie.get(HtyUnionIDToken) || '').toString();
|
store.unionid = (store.unionid || cookie.get(HtyUnionIDToken) || '').toString();
|
||||||
if (!store.current.hty_id) {
|
if (!store.current.hty_id) {
|
||||||
@@ -173,8 +174,10 @@ router.beforeEach(async (to, from , next) => {
|
|||||||
if (currentAuthToken) {
|
if (currentAuthToken) {
|
||||||
window.localStorage.setItem(HtySudoToken, currentAuthToken);
|
window.localStorage.setItem(HtySudoToken, currentAuthToken);
|
||||||
}
|
}
|
||||||
// load departments after org switch (single-department transparent)
|
|
||||||
await loadMyDepartments();
|
await loadMyDepartments();
|
||||||
|
if (orgStore.departments.length === 1 && orgStore.currentDepartmentId) {
|
||||||
|
await switchDepartment(orgStore.currentDepartmentId);
|
||||||
|
}
|
||||||
} else if (organizations.length > 1) {
|
} else if (organizations.length > 1) {
|
||||||
await router.replace('/org/select');
|
await router.replace('/org/select');
|
||||||
next();
|
next();
|
||||||
@@ -184,9 +187,11 @@ router.beforeEach(async (to, from , next) => {
|
|||||||
window.localStorage.setItem(CurrentOrgId, tokenOrgId);
|
window.localStorage.setItem(CurrentOrgId, tokenOrgId);
|
||||||
orgStore.currentOrgId = tokenOrgId;
|
orgStore.currentOrgId = tokenOrgId;
|
||||||
}
|
}
|
||||||
// load departments if not already loaded (after org context is established)
|
|
||||||
if (orgStore.currentOrgId && orgStore.departments.length === 0) {
|
if (orgStore.currentOrgId && orgStore.departments.length === 0) {
|
||||||
await loadMyDepartments();
|
await loadMyDepartments();
|
||||||
|
if (orgStore.departments.length === 1 && orgStore.currentDepartmentId) {
|
||||||
|
await switchDepartment(orgStore.currentDepartmentId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,7 +202,10 @@ router.beforeEach(async (to, from , next) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
|
} catch (e: any) {
|
||||||
|
window.localStorage.setItem('__guardError', e?.message || String(e));
|
||||||
|
next();
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
createApp(App).use(router).use(weixin).mount('#app')
|
createApp(App).use(router).use(weixin).mount('#app')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user