Merge pull request #30 from ll-huihui/main
[router]: 添加路由拦截beforeEach参数from ,to.meta,使用to.meta实现登录拦截
This commit is contained in:
@@ -3,6 +3,7 @@ import { isArray, parse } from "../utils";
|
|||||||
type Page = {
|
type Page = {
|
||||||
path: string;
|
path: string;
|
||||||
style?: UTSJSONObject;
|
style?: UTSJSONObject;
|
||||||
|
meta?: UTSJSONObject;
|
||||||
};
|
};
|
||||||
|
|
||||||
type SubPackage = {
|
type SubPackage = {
|
||||||
@@ -62,7 +63,8 @@ if (isArray(ctx.subPackages)) {
|
|||||||
a.pages.forEach((b) => {
|
a.pages.forEach((b) => {
|
||||||
PAGES.push({
|
PAGES.push({
|
||||||
path: a.root + "/" + b.path, // 拼接子包根路径和页面路径
|
path: a.root + "/" + b.path, // 拼接子包根路径和页面路径
|
||||||
style: b.style
|
style: b.style,
|
||||||
|
meta: b.meta ?? {},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,10 +5,11 @@ import { storage, last, isNull, isEmpty, get, isFunction, toArray, map, debounce
|
|||||||
// 路由信息类型
|
// 路由信息类型
|
||||||
type RouteInfo = {
|
type RouteInfo = {
|
||||||
path: string;
|
path: string;
|
||||||
|
meta?: UTSJSONObject;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 跳转前钩子类型
|
// 跳转前钩子类型
|
||||||
type BeforeEach = (to: RouteInfo, next: () => void) => void;
|
type BeforeEach = (to: RouteInfo, from: PageInstance, next: () => void) => void;
|
||||||
// 登录后回调类型
|
// 登录后回调类型
|
||||||
type AfterLogin = () => void;
|
type AfterLogin = () => void;
|
||||||
|
|
||||||
@@ -53,7 +54,9 @@ export class Router {
|
|||||||
path = "/" + path;
|
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
|
// @ts-ignore
|
||||||
let exposed = e.vm as any;
|
let exposed = e.vm as any;
|
||||||
@@ -69,6 +72,7 @@ export class Router {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
exposed,
|
exposed,
|
||||||
style,
|
style,
|
||||||
|
meta,
|
||||||
query,
|
query,
|
||||||
isCustomNavbar: style?.navigationStyle == "custom"
|
isCustomNavbar: style?.navigationStyle == "custom"
|
||||||
} as PageInstance;
|
} as PageInstance;
|
||||||
@@ -173,10 +177,12 @@ export class Router {
|
|||||||
|
|
||||||
// 跳转前钩子处理
|
// 跳转前钩子处理
|
||||||
if (isFunction(this._events["beforeEach"])) {
|
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)(
|
(this._events["beforeEach"] as BeforeEach)(
|
||||||
{
|
to,
|
||||||
path
|
from,
|
||||||
},
|
|
||||||
next
|
next
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
@@ -193,10 +199,28 @@ export class Router {
|
|||||||
|
|
||||||
// 返回上一页,若为首页则回首页
|
// 返回上一页,若为首页则回首页
|
||||||
back(options: BackOptions | null = null) {
|
back(options: BackOptions | null = null) {
|
||||||
if (this.isFirstPage()) {
|
let path = ''
|
||||||
this.home();
|
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 {
|
} else {
|
||||||
uni.navigateBack({ ...(options ?? {}) });
|
next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,4 +53,5 @@ export type PageInstance = {
|
|||||||
query: UTSJSONObject;
|
query: UTSJSONObject;
|
||||||
exposed: any;
|
exposed: any;
|
||||||
isCustomNavbar: boolean;
|
isCustomNavbar: boolean;
|
||||||
|
meta?: UTSJSONObject;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "cool-unix",
|
"name": "cool-unix",
|
||||||
"appid": "__UNI__651711F",
|
"appid": "__UNI__EC807C1",
|
||||||
"description": "完全开源、永久免费、上手容易、效率极高的开发脚手架",
|
"description": "完全开源、永久免费、上手容易、效率极高的开发脚手架",
|
||||||
"versionName": "1.0.0",
|
"versionName": "1.0.0",
|
||||||
"versionCode": "100",
|
"versionCode": "100",
|
||||||
|
|||||||
24
pages.json
24
pages.json
@@ -27,30 +27,45 @@
|
|||||||
"path": "index",
|
"path": "index",
|
||||||
"style": {
|
"style": {
|
||||||
"navigationBarTitleText": "设置"
|
"navigationBarTitleText": "设置"
|
||||||
|
},
|
||||||
|
"meta":{
|
||||||
|
"isAuth": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "general",
|
"path": "general",
|
||||||
"style": {
|
"style": {
|
||||||
"navigationBarTitleText": "通用设置"
|
"navigationBarTitleText": "通用设置"
|
||||||
|
},
|
||||||
|
"meta":{
|
||||||
|
"isAuth": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "notice",
|
"path": "notice",
|
||||||
"style": {
|
"style": {
|
||||||
"navigationBarTitleText": "通知设置"
|
"navigationBarTitleText": "通知设置"
|
||||||
|
},
|
||||||
|
"meta":{
|
||||||
|
"isAuth": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "about",
|
"path": "about",
|
||||||
"style": {
|
"style": {
|
||||||
"navigationBarTitleText": ""
|
"navigationBarTitleText": ""
|
||||||
|
},
|
||||||
|
"meta":{
|
||||||
|
"isAuth": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "cs",
|
"path": "cs",
|
||||||
"style": {
|
"style": {
|
||||||
"navigationBarTitleText": "联系客服"
|
"navigationBarTitleText": "联系客服"
|
||||||
|
},
|
||||||
|
"meta":{
|
||||||
|
"isAuth": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -62,18 +77,27 @@
|
|||||||
"path": "edit",
|
"path": "edit",
|
||||||
"style": {
|
"style": {
|
||||||
"navigationBarTitleText": "编辑资料"
|
"navigationBarTitleText": "编辑资料"
|
||||||
|
},
|
||||||
|
"meta":{
|
||||||
|
"isAuth": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "edit-name",
|
"path": "edit-name",
|
||||||
"style": {
|
"style": {
|
||||||
"navigationStyle": "custom"
|
"navigationStyle": "custom"
|
||||||
|
},
|
||||||
|
"meta":{
|
||||||
|
"isAuth": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"path": "edit-description",
|
"path": "edit-description",
|
||||||
"style": {
|
"style": {
|
||||||
"navigationStyle": "custom"
|
"navigationStyle": "custom"
|
||||||
|
},
|
||||||
|
"meta":{
|
||||||
|
"isAuth": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,27 +1,14 @@
|
|||||||
import { router, useStore } from "@/cool";
|
import { router, useStore } from "@/cool";
|
||||||
|
|
||||||
const ignoreToken = [
|
router.beforeEach((to, _, next) => {
|
||||||
"/pages/index/home",
|
|
||||||
"/pages/index/my",
|
|
||||||
"/pages/index/template",
|
|
||||||
"/pages/user/login",
|
|
||||||
"/pages/user/doc"
|
|
||||||
];
|
|
||||||
|
|
||||||
router.beforeEach((to, next) => {
|
|
||||||
const { user } = useStore();
|
const { user } = useStore();
|
||||||
|
if (to.meta?.isAuth == true) {
|
||||||
if (
|
|
||||||
ignoreToken.some((e) => to.path.includes(e)) ||
|
|
||||||
to.path.startsWith("/pages/demo") ||
|
|
||||||
to.path.startsWith("/pages/template")
|
|
||||||
) {
|
|
||||||
next();
|
|
||||||
} else {
|
|
||||||
if (!user.isNull()) {
|
if (!user.isNull()) {
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
router.login();
|
router.login();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user