From dfad4ff0405ccb5a8f3f5673dd518062214e180b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=BE=89?= <15909216375@163.com> Date: Mon, 22 Sep 2025 18:08:46 +0800 Subject: [PATCH] =?UTF-8?q?update=20[router]:=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E6=8B=A6=E6=88=AA=E5=8F=82=E6=95=B0from=20,t?= =?UTF-8?q?o.meta,=E4=BD=BF=E7=94=A8to.meta=E5=AE=9E=E7=8E=B0=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=8B=A6=E6=88=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cool/ctx/index.ts | 4 +++- cool/router/index.ts | 40 ++++++++++++++++++++++++++++++++-------- pages.json | 24 ++++++++++++++++++++++++ router/index.ts | 15 ++------------- 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/cool/ctx/index.ts b/cool/ctx/index.ts index 08ebe14..d5b57c9 100644 --- a/cool/ctx/index.ts +++ b/cool/ctx/index.ts @@ -3,6 +3,7 @@ import { isArray, parse } from "../utils"; type Page = { path: string; style?: UTSJSONObject; + meta?: UTSJSONObject; }; type SubPackage = { @@ -62,7 +63,8 @@ if (isArray(ctx.subPackages)) { a.pages.forEach((b) => { PAGES.push({ path: a.root + "/" + b.path, // 拼接子包根路径和页面路径 - style: b.style + style: b.style, + meta: b.meta ?? {}, }); }); }); diff --git a/cool/router/index.ts b/cool/router/index.ts index c8ca087..d57b54f 100644 --- a/cool/router/index.ts +++ b/cool/router/index.ts @@ -5,10 +5,11 @@ import { storage, last, isNull, isEmpty, get, isFunction, toArray, map, debounce // 路由信息类型 type RouteInfo = { path: string; + meta: UTSJSONObject; }; // 跳转前钩子类型 -type BeforeEach = (to: RouteInfo, next: () => void) => void; +type BeforeEach = (to: RouteInfo, from: PageInstance, next: () => void) => void; // 登录后回调类型 type AfterLogin = () => void; @@ -53,7 +54,9 @@ export class Router { path = "/" + path; } // 获取页面样式 - const style = PAGES.find((e) => e.path == path)?.style; + const page = PAGES.find((e) => e.path == path); + const style = page?.style ?? {}; + const meta = page?.meta ?? {}; // 获取页面暴露的方法 // @ts-ignore let exposed = e.vm as any; @@ -69,6 +72,7 @@ export class Router { // @ts-ignore exposed, style, + meta, query, isCustomNavbar: style?.navigationStyle == "custom" } as PageInstance; @@ -173,10 +177,12 @@ export class Router { // 跳转前钩子处理 if (isFunction(this._events["beforeEach"])) { + const meta = PAGES.find((e) => e.path == path)?.meta; + const from = this.getPages().slice(-1)[0]; + const to: RouteInfo = { path, meta: meta ?? {} }; (this._events["beforeEach"] as BeforeEach)( - { - path - }, + to, + from, next ); } else { @@ -193,10 +199,28 @@ export class Router { // 返回上一页,若为首页则回首页 back(options: BackOptions | null = null) { - if (this.isFirstPage()) { - this.home(); + let path = '' + const next = ()=>{ + if (this.isFirstPage()) { + this.home(); + path = this.defaultPath("home") + } else { + path = this.getPages().slice(-2)[0].path; + uni.navigateBack({ ...(options ?? {}) }); + } + } + // 跳转前钩子处理 + if (isFunction(this._events["beforeEach"])) { + const meta = PAGES.find((e) => e.path == path)?.meta; + const from = this.getPages().slice(-1)[0]; + const to: RouteInfo = { path, meta: meta ?? {} }; + (this._events["beforeEach"] as BeforeEach)( + to, + from, + next + ); } else { - uni.navigateBack({ ...(options ?? {}) }); + next(); } } diff --git a/pages.json b/pages.json index 2a22c53..294c696 100644 --- a/pages.json +++ b/pages.json @@ -27,30 +27,45 @@ "path": "index", "style": { "navigationBarTitleText": "设置" + }, + "meta":{ + "isAuth": true } }, { "path": "general", "style": { "navigationBarTitleText": "通用设置" + }, + "meta":{ + "isAuth": true } }, { "path": "notice", "style": { "navigationBarTitleText": "通知设置" + }, + "meta":{ + "isAuth": true } }, { "path": "about", "style": { "navigationBarTitleText": "" + }, + "meta":{ + "isAuth": true } }, { "path": "cs", "style": { "navigationBarTitleText": "联系客服" + }, + "meta":{ + "isAuth": true } } ] @@ -62,18 +77,27 @@ "path": "edit", "style": { "navigationBarTitleText": "编辑资料" + }, + "meta":{ + "isAuth": true } }, { "path": "edit-name", "style": { "navigationStyle": "custom" + }, + "meta":{ + "isAuth": true } }, { "path": "edit-description", "style": { "navigationStyle": "custom" + }, + "meta":{ + "isAuth": true } }, { diff --git a/router/index.ts b/router/index.ts index 7d50883..beb40be 100644 --- a/router/index.ts +++ b/router/index.ts @@ -1,20 +1,9 @@ import { router, useStore } from "@/cool"; -const ignoreToken = [ - "/pages/index/home", - "/pages/index/my", - "/pages/index/template", - "/pages/user/login", - "/pages/user/doc" -]; - -router.beforeEach((to, next) => { +router.beforeEach((to, _, next) => { const { user } = useStore(); - if ( - ignoreToken.some((e) => to.path.includes(e)) || - to.path.startsWith("/pages/demo") || - to.path.startsWith("/pages/template") + !to.meta.isAuth ) { next(); } else {