Skip to content

Commit

Permalink
fix: 🔧 修复打包后由于方法名混淆导致路由中间件无法匹配触发的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Sep 9, 2023
1 parent 25bc3bd commit 37515aa
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"lodash-es": "^4.17.21",
"pinia": "2.0.36",
"qs": "^6.11.2",
"uniapp-router-next": "^1.1.6",
"uniapp-router-next": "^1.1.9",
"uview-plus": "^3.1.35",
"vue": "3.2.47",
"vue-i18n": "^9.1.9",
Expand All @@ -81,9 +81,9 @@
"@dcloudio/uni-cli-shared": "3.0.0-3081220230817001",
"@dcloudio/uni-stacktracey": "3.0.0-3081220230817001",
"@dcloudio/vite-plugin-uni": "3.0.0-3081220230817001",
"@uni-helper/vite-plugin-uni-pages": "^0.2.6",
"@uni-helper/vite-plugin-uni-pages": "^0.2.9",
"@unocss/transformer-directives": "0.55.3",
"@viarotel-org/eslint-config": "^0.5.10",
"@viarotel-org/eslint-config": "^0.7.0",
"@vue/runtime-core": "^3.2.45",
"eslint": "^8.48.0",
"mini-types": "^0.1.7",
Expand Down
2 changes: 1 addition & 1 deletion src/router/guards/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import { defineMiddleware } from '$uni-router/middleware'

export default (app, router) => {
permission(router, { app })
defineMiddleware(realname, { router, app })
defineMiddleware('realname', realname, { router, app })
}
4 changes: 2 additions & 2 deletions src/router/guards/realname/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const realname = (router) => {
const userStore = store.useUserStore()

router.beforeEach((to, from, next) => {
console.log('realname.beforeEach.to', to)
console.log('realname.beforeEach.from', from)
// console.log('realname.beforeEach.to', to)
// console.log('realname.beforeEach.from', from)

const realStatus = userStore.userInfo.realStatus

Expand Down
10 changes: 3 additions & 7 deletions src/utils/uni-router/middleware.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
/**
* 将路由守卫包装为中间件
*/
export function defineMiddleware(callback, { router, app, name } = {}) {
// callback.name 在使用匿名函数下可能取值不符合预期 这时请使用 name 参数手动命名
const middlewareName = name || callback.name
// console.log('middlewareName', middlewareName)

export function defineMiddleware(name, handler, { router, app } = {}) {
router.beforeEach((to, from, next) => {
const middleware = to.meta.middleware || []
if (!middleware.includes(middlewareName)) {
if (!middleware.includes(name)) {
next()
}
else {
callback(
handler(
{
...router,
beforeEach: func => func(to, from, next),
Expand Down

0 comments on commit 37515aa

Please sign in to comment.