refactor: 重构前端骨架,及admin模块子路由和懒加载
refactor: 优化多次请求,并使用pinia状态管理
This commit is contained in:
1 parent
f877f61efd
commit
61693d014d
74 files changed
+9493
-6418
No files matched your search
+17
-22
@@ -1,29 +1,25 @@
|
||||
import { createApp } from 'vue'
|
||||
import './style.css'
|
||||
import App from './App.vue'
|
||||
import router from './router'
|
||||
import i18n from './i18n'
|
||||
import { config } from './config'
|
||||
import { refreshAccessToken, getAuthToken, initAuthSync } from './utils/auth'
|
||||
import { initAgentTabSync } from './utils/agentTabs'
|
||||
import { createApp } from 'vue';
|
||||
import './style.css';
|
||||
import App from './App.vue';
|
||||
import router from './app/router';
|
||||
import i18n from './i18n';
|
||||
import { config } from './config';
|
||||
import { pinia } from './app/plugins/pinia';
|
||||
import { bindAuthStoreSync } from './features/auth/stores/auth.store';
|
||||
import { refreshAccessToken, getAuthToken, initAuthSync } from './utils/auth';
|
||||
import { initAgentTabSync } from './utils/agentTabs';
|
||||
|
||||
initAuthSync();
|
||||
initAgentTabSync();
|
||||
|
||||
// Set document title from config
|
||||
document.title = config.appTitle || 'AI工作台';
|
||||
|
||||
// Global 401 interceptor for API calls.
|
||||
const _rawFetch = window.fetch.bind(window);
|
||||
window.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||
// Keep original request closure so we can retry with a refreshed token.
|
||||
const runFetch = (nextInit?: RequestInit) => _rawFetch(input, nextInit);
|
||||
let reqInit = init;
|
||||
let res = await runFetch(reqInit);
|
||||
try {
|
||||
const url = typeof input === 'string'
|
||||
? input
|
||||
: (input instanceof URL ? input.toString() : input.url);
|
||||
const url = typeof input === 'string' ? input : (input instanceof URL ? input.toString() : input.url);
|
||||
const isApi = url.includes('/api/');
|
||||
const isRefreshApi = url.includes('/api/user/casdoor/refresh');
|
||||
const isLoginApi =
|
||||
@@ -31,18 +27,15 @@ window.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Res
|
||||
url.includes('/api/user/casdoor/password-login') ||
|
||||
url.includes('/api/user/casdoor/login');
|
||||
if (res.status === 401 && isApi && !isRefreshApi) {
|
||||
// First unauthorized response: try silent refresh once.
|
||||
const refreshed = await refreshAccessToken();
|
||||
if (refreshed) {
|
||||
const mergedHeaders = new Headers((reqInit && reqInit.headers) || {});
|
||||
mergedHeaders.set('Authorization', `Bearer ${getAuthToken()}`);
|
||||
reqInit = { ...(reqInit || {}), headers: mergedHeaders };
|
||||
// Retry original request exactly once with the renewed access token.
|
||||
res = await runFetch(reqInit);
|
||||
}
|
||||
}
|
||||
if (res.status === 401 && isApi && !isLoginApi) {
|
||||
// Still unauthorized after retry (or no refresh token): force global logout UX.
|
||||
window.dispatchEvent(new CustomEvent('hxops:unauthorized', { detail: { url, status: res.status } }));
|
||||
}
|
||||
} catch {
|
||||
@@ -51,10 +44,12 @@ window.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Res
|
||||
return res;
|
||||
};
|
||||
|
||||
createApp(App)
|
||||
.use(router)
|
||||
.use(i18n)
|
||||
.mount('#app')
|
||||
const app = createApp(App);
|
||||
app.use(pinia);
|
||||
bindAuthStoreSync();
|
||||
app.use(router);
|
||||
app.use(i18n);
|
||||
app.mount('#app');
|
||||
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', async () => {
|
||||
|
||||
Reference in new issue
Block a user