feat: 更新版本至 0.0.61,重构登录缓存逻辑,添加浏览器缓存支持

This commit is contained in:
2026-03-05 21:53:42 +08:00
parent b32e622707
commit 27503bf4a7
8 changed files with 217 additions and 23 deletions

View File

@@ -4,6 +4,7 @@ import { filter } from '@kevisual/js-filter'
import { EventEmitter } from 'eventemitter3';
import { initApi } from './router-api-proxy.ts';
import Fuse from 'fuse.js';
import { cloneDeep } from 'es-toolkit';
export const RouteTypeList = ['api', 'context', 'worker', 'page'] as const;
export type RouterViewItemInfo = RouterViewApi | RouterViewContext | RouterViewWorker | RouteViewPage;
@@ -26,6 +27,10 @@ type RouteViewBase = {
* 默认动作配置
*/
action?: { path?: string; key?: string; id?: string; payload?: any;[key: string]: any };
/**
* 本地状态loading、active、error等
*/
routerStatus?: 'loading' | 'active' | 'inactive' | 'error';
}
export type RouterViewApi = {
type: 'api',
@@ -67,7 +72,7 @@ export type RouterViewWorker = {
* @returns
*/
export const pickRouterViewData = (item: RouterViewItem) => {
const { action, response, _id, ...rest } = item;
const { action, response, _id, ...rest } = cloneDeep(item);
if (rest.type === 'api') {
if (rest.api) {
delete rest.api.query;
@@ -83,6 +88,7 @@ export const pickRouterViewData = (item: RouterViewItem) => {
delete rest.context.router;
}
}
delete rest.routerStatus;
return rest
}
/**
@@ -98,7 +104,7 @@ export type RouteViewPage = {
export type RouterViewQuery = {
id: string,
query: string,
title: string
title: string,
}
/**
* 后端存储结构
@@ -143,6 +149,7 @@ export class QueryProxy {
}
private initRouterView(item: RouterViewItem) {
item.routerStatus = 'loading';
if (item.type === 'api' && item.api?.url) {
const url = item.api.url;
if (item?.api?.query) return item;
@@ -245,10 +252,14 @@ export class QueryProxy {
// @ts-ignore
const context = globalThis['context'] || {}
const router = item?.context?.router || context[item?.context?.key] as QueryRouterServer;
if (item) {
item.routerStatus = router ? 'active' : 'error';
}
if (!router) {
console.warn(`未发现Context router ${item?.context?.key}`);
return
}
const routes = router.getList();
// TODO: args
// const args = fromJSONSchema(r);
@@ -308,6 +319,9 @@ export class QueryProxy {
}
const viewItem = item.worker;
const worker = viewItem?.worker;
if (item) {
item.routerStatus = worker ? 'active' : 'error';
}
if (!worker) {
console.warn('Worker not initialized');
return;
@@ -377,11 +391,15 @@ export class QueryProxy {
const url = item.page.url;
try {
if (typeof window !== 'undefined') {
await import(url).then((module) => { }).catch((err) => {
console.error('引入Page脚本失败:', url, err);
});
await import(url)
if (item) {
item.routerStatus = 'active';
}
}
} catch (e) {
if (item) {
item.routerStatus = 'error';
}
console.warn('引入Page脚本失败:', url, e);
return;
}

View File

@@ -17,12 +17,16 @@ export const initApi = async (opts: {
const token = opts?.token;
const query = item?.api?.query || new Query({ url: item?.api?.url || '/api/router' })
const res = await query.post<{ list: RouterItem[] }>({ path: "router", key: 'list', token: token });
if (item) {
item.routerStatus = res?.code === 200 ? 'active' : 'error';
}
if (res.code !== 200) {
return {
code: res.code,
message: `初始化路由失败: ${res.message}, url: ${query.url}`
}
}
let _list = res.data?.list || []
if (opts?.exclude) {
if (opts?.exclude) {