diff --git a/src/pages.json b/src/pages.json index 0f7ead6..917f867 100644 --- a/src/pages.json +++ b/src/pages.json @@ -6,13 +6,18 @@ "style": { "navigationBarTitleText": "Home" }, - "meta": {} + "meta": { + "tabBar": true + } }, { "name": "Demo", "path": "pages/demo/index", "style": { "navigationBarTitleText": "Demo" + }, + "meta": { + "tabBar": true } }, { @@ -22,7 +27,8 @@ "navigationBarTitleText": "关于" }, "meta": { - "ignoreAuth": true + "ignoreAuth": true, + "tabBar": true } }, { diff --git a/src/pages/login/index.vue b/src/pages/login/index.vue index 8a07611..48b1a3b 100644 --- a/src/pages/login/index.vue +++ b/src/pages/login/index.vue @@ -28,11 +28,16 @@ const submit = (e: any) => { setTimeout(() => { if (unref(pageQuery)?.redirect) { // 如果有存在redirect(重定向)参数,登录成功后直接跳转 - // 这里replace方法无法跳转tabbar页面故改为replaceAll - router.replaceAll({ name: unref(pageQuery).redirect, params: omit(unref(pageQuery), ['redirect']) }); + const params = omit(unref(pageQuery), ['redirect', 'tabBar']); + if (unref(pageQuery)?.tabBar) { + // 这里replace方法无法跳转tabbar页面故改为replaceAll + router.replaceAll({ name: unref(pageQuery).redirect, params }); + } else { + router.replace({ name: unref(pageQuery).redirect, params }); + } } else { - // 不存在则回到首页 - router.replaceAll({ name: 'Home' }); + // 不存在则返回上一页 + router.back(); } }, 1500); }); diff --git a/src/router/guard.ts b/src/router/guard.ts index e97a8eb..4b41975 100644 --- a/src/router/guard.ts +++ b/src/router/guard.ts @@ -16,7 +16,7 @@ function createBeforeEachGuard(router: Router) { } else if (!authStore.isLogin && to && to.name !== 'Login') { // 如果没有登录且目标路由不是登录页面则跳转到登录页面 // 将目标路由和参数传入登录页面,登录成功后直接跳转到目标路由,优化体验 - next({ name: 'Login', params: { redirect: to.name, ...to.query }, navType: 'replaceAll' }); + next({ name: 'Login', params: { redirect: to.name!, tabBar: to?.meta?.tabBar, ...to.query }, navType: 'push' }); } else if (authStore.isLogin && to && to.name === 'Login') { // 如果已经登录且目标页面是登录页面则跳转至首页 next({ name: 'Home', navType: 'replaceAll' }); @@ -28,10 +28,13 @@ function createBeforeEachGuard(router: Router) { function createAfterEachGuard(router: Router) { router.afterEach((to) => { + console.log('afterEach', to); + // @ts-ignore + if (to && to?.meta?.ignoreAuth) return; const authStore = useAuthStore(); if (!authStore.isLogin && to && to.name !== 'Login') { // 如果没有登录且目标路由不是登录页面则跳转到登录页面 - router.replaceAll({ name: 'Login' }); + router.push({ name: 'Login', tabBar: to?.meta?.tabBar, ...to.query }); } else if (authStore.isLogin && to && to.name === 'Login') { // 如果已经登录且目标页面是登录页面则跳转至首页 router.replaceAll({ name: 'Home' });