diff --git a/locales/en_US.json b/locales/en_US.json index 984e5d0..ca16936 100644 --- a/locales/en_US.json +++ b/locales/en_US.json @@ -114,6 +114,7 @@ "documentsVue": "Vue", "documentsVueuse": "VueUse (external link)", "documentsNova": "Nova docs", + "documentsPublic": "Public page (external link)", "echarts": "Echarts", "editor": "Editor", "editorMd": "MarkDown editor", diff --git a/locales/zh_CN.json b/locales/zh_CN.json index 5b600b2..e519c66 100644 --- a/locales/zh_CN.json +++ b/locales/zh_CN.json @@ -144,6 +144,7 @@ "documentsVite": "Vite", "documentsVueuse": "VueUse(外链)", "documentsNova": "Nova 文档", + "documentsPublic": "公共示例页(外链)", "permission": "权限", "permissionDemo": "权限示例", "justSuper": "super可见", diff --git a/src/router/guard.ts b/src/router/guard.ts index 48cf92f..820c2c6 100644 --- a/src/router/guard.ts +++ b/src/router/guard.ts @@ -13,22 +13,30 @@ export function setupRouterGuard(router: Router) { // 判断是否是外链,如果是直接打开网页并拦截跳转 if (to.meta.href) { window.open(to.meta.href) - return false + next(false) // 取消当前导航 + return } // 开始 loadingBar appStore.showProgress && window.$loadingBar?.start() // 判断有无TOKEN,登录鉴权 const isLogin = Boolean(local.get('accessToken')) - if (!isLogin) { - if (to.name === 'login') - next() - if (to.name !== 'login') { - const redirect = to.name === '404' ? undefined : to.fullPath - next({ path: '/login', query: { redirect } }) - } - return false + // 如果是login路由,直接放行 + if (to.name === 'login') { + // login页面不需要任何认证检查,直接放行 + // 继续执行后面的逻辑 + } + // 如果路由明确设置了requiresAuth为false,直接放行 + else if (to.meta.requiresAuth === false) { + // 明确设置为false的路由直接放行 + // 继续执行后面的逻辑 + } + // 如果路由设置了requiresAuth为true,且用户未登录,重定向到登录页 + else if (to.meta.requiresAuth === true && !isLogin) { + const redirect = to.name === '404' ? undefined : to.fullPath + next({ path: '/login', query: { redirect } }) + return } // 判断路由有无进行初始化 @@ -43,14 +51,14 @@ export function setupRouterGuard(router: Router) { query: to.query, hash: to.hash, }) - return false + return } } - // 判断当前页是否在login,则定位去首页 - if (to.name === 'login') { + // 如果用户已登录且访问login页面,重定向到首页 + if (to.name === 'login' && isLogin) { next({ path: '/' }) - return false + return } next() diff --git a/src/router/routes.inner.ts b/src/router/routes.inner.ts index cce97df..0de59fa 100644 --- a/src/router/routes.inner.ts +++ b/src/router/routes.inner.ts @@ -18,6 +18,16 @@ export const routes: RouteRecordRaw[] = [ withoutTab: true, }, }, + { + path: '/public', + name: 'publicAccess', + component: () => import('@/views/demo/publicAccess/index.vue'), + meta: { + title: '公共访问示例', + requiresAuth: false, + withoutTab: true, + }, + }, { path: '/403', name: '403', diff --git a/src/router/routes.static.ts b/src/router/routes.static.ts index c0d8f95..25f4bf8 100644 --- a/src/router/routes.static.ts +++ b/src/router/routes.static.ts @@ -59,7 +59,7 @@ export const staticRoutes: AppRoute.RowRoute[] = [ name: 'multi2Detail', path: '/multi/multi2/detail', title: '菜单详情页', - requiresAuth: true, + requiresAuth: false, icon: 'icon-park-outline:list', hide: true, activeMenu: '/multi/multi2', @@ -445,4 +445,15 @@ export const staticRoutes: AppRoute.RowRoute[] = [ id: 44, pid: 10, }, + { + name: 'documentsPublic', + path: '/documents/public', + title: '公共示例页(外链)', + requiresAuth: true, + icon: 'local:logo', + href: '/public', + componentPath: 'null', + id: 45, + pid: 24, + }, ] diff --git a/src/views/demo/multi/multi2/detail/index.vue b/src/views/demo/multi/multi2/detail/index.vue index 61d6c00..2a75270 100644 --- a/src/views/demo/multi/multi2/detail/index.vue +++ b/src/views/demo/multi/multi2/detail/index.vue @@ -15,6 +15,9 @@ modifyTab(fullPath, (target) => { 这是详情子页,他不会出现在侧边栏,他其实是上个页面的同级,并不是下级,这个要注意 + + 这个页面不需要登陆也可以访问,复制地址到其他浏览器打开查看 + 详情页id:{{ query.id }} diff --git a/src/views/demo/publicAccess/index.vue b/src/views/demo/publicAccess/index.vue new file mode 100644 index 0000000..04159fb --- /dev/null +++ b/src/views/demo/publicAccess/index.vue @@ -0,0 +1,520 @@ + + + + + diff --git a/src/views/login/index.vue b/src/views/login/index.vue index f4b4163..8b1c6e3 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -19,7 +19,7 @@ const appName = import.meta.env.VITE_APP_NAME