- composable/autosave.ts: 通用 DOM 快照(.tool-content 内 input/textarea/select/contenteditable) 字段定位 name > tag:type:组内序号(不用动态 id, 实测 Naive UI id 每次刷新都变) 60s 定时自动保存 + markDirty 变更检测 + 静默恢复(仅当页面基本为空) - components/AutosaveBar.vue: 右下角状态条 未保存/立即保存/已保存 HH:MM/已恢复 - tool.layout.vue: 按路由挂载(仅 isTool 页), 切页自动 detach - router.ts: confirmLeave 拦截站内导航 + beforeunload 拦截刷新关闭 文案「当前内容还没保存,离开会丢失」确定=先保存再离开 取消=留在本页 - 用户开始编辑时「已恢复」提示立即让位给「未保存」
79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
import { createRouter, createWebHistory } from 'vue-router';
|
||
import { layouts } from './layouts/index';
|
||
import HomePage from './pages/Home.page.vue';
|
||
import NotFound from './pages/404.page.vue';
|
||
import { tools } from './tools';
|
||
import { config } from './config';
|
||
import { reportToolUsage, startOnlineHeartbeat } from './tools/license/license';
|
||
import { confirmLeave } from './composable/autosave';
|
||
import { routes as demoRoutes } from './ui/demo/demo.routes';
|
||
|
||
const toolsRoutes = tools.map(({ path, name, component, ...config }) => ({
|
||
path,
|
||
name,
|
||
component,
|
||
meta: { isTool: true, layout: layouts.toolLayout, name, ...config },
|
||
}));
|
||
const toolsRedirectRoutes = tools
|
||
.filter(({ redirectFrom }) => redirectFrom && redirectFrom.length > 0)
|
||
.flatMap(
|
||
({ path, redirectFrom }) => redirectFrom?.map(redirectSource => ({ path: redirectSource, redirect: path })) ?? [],
|
||
);
|
||
|
||
const router = createRouter({
|
||
history: createWebHistory(config.app.baseUrl),
|
||
routes: [
|
||
{
|
||
path: '/',
|
||
name: 'home',
|
||
component: HomePage,
|
||
},
|
||
{
|
||
path: '/about',
|
||
name: 'about',
|
||
component: () => import('./pages/About.vue'),
|
||
},
|
||
{
|
||
path: '/admin',
|
||
name: 'admin',
|
||
component: () => import('./pages/Admin.page.vue'),
|
||
},
|
||
{
|
||
path: '/user-center',
|
||
name: 'user-center',
|
||
component: () => import('./pages/UserCenter.page.vue'),
|
||
},
|
||
{
|
||
path: '/pricing',
|
||
name: 'pricing',
|
||
component: () => import('./pages/Pricing.page.vue'),
|
||
},
|
||
{
|
||
path: '/buy',
|
||
name: 'buy',
|
||
component: () => import('./pages/Buy.page.vue'),
|
||
},
|
||
...toolsRoutes,
|
||
...toolsRedirectRoutes,
|
||
...(config.app.env === 'development' ? demoRoutes : []),
|
||
{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
|
||
],
|
||
});
|
||
|
||
// 打开任意工具页即上报一次使用(未登录访客同样统计,见 license.ts reportToolUsage)
|
||
router.afterEach((to) => {
|
||
if (to.meta?.isTool) {
|
||
reportToolUsage(String(to.path));
|
||
}
|
||
// 全站在线心跳:停留满 1 分钟的未登录访客才计在线(内部 once 保护)
|
||
startOnlineHeartbeat();
|
||
});
|
||
|
||
// ===== 全站草稿保护:有未保存修改时拦截站内前进/后退/跳转 =====
|
||
// 「确定」= 先保存再离开;「取消」= 留在本页(返回 false 中断导航)
|
||
router.beforeEach(async () => {
|
||
return await confirmLeave();
|
||
});
|
||
|
||
export default router;
|