Parcourir la source

refactor(router): 使用常量替换硬编码的登录页面路径

将多处硬编码的登录页面路径替换为统一的常量LOGIN_PAGE,提高代码可维护性
同时将注册页面路径也提取为常量REGISTER_PAGE
feige996 il y a 8 mois
Parent
commit
494c6452b5

+ 2 - 1
src/http/alova.ts

@@ -4,6 +4,7 @@ import AdapterUniapp from '@alova/adapter-uniapp'
 import { createAlova } from 'alova'
 import { createServerTokenAuthentication } from 'alova/client'
 import VueHook from 'alova/vue'
+import { LOGIN_PAGE } from '@/router/config'
 import { ContentTypeEnum, ResultEnum, ShowMessage } from './tools/enum'
 
 // 配置动态Tag
@@ -29,7 +30,7 @@ const { onAuthRequired, onResponseRefreshToken } = createServerTokenAuthenticati
       }
       catch (error) {
         // 切换到登录页
-        await uni.reLaunch({ url: '/pages/login/login' })
+        await uni.reLaunch({ url: LOGIN_PAGE })
         throw error
       }
     },

+ 3 - 2
src/http/http.ts

@@ -1,6 +1,7 @@
 import type { IDoubleTokenRes } from '@/api/types/login'
 import type { CustomRequestOptions } from '@/http/types'
 import { nextTick } from 'vue'
+import { LOGIN_PAGE } from '@/router/config'
 import { useTokenStore } from '@/store/token'
 import { isDoubleTokenMode } from '@/utils'
 
@@ -30,7 +31,7 @@ export function http<T>(options: CustomRequestOptions) {
           if (!isDoubleTokenMode) {
             // 未启用双token策略,清理用户信息,跳转到登录页
             tokenStore.logout()
-            uni.navigateTo({ url: '/pages/login/login' })
+            uni.navigateTo({ url: LOGIN_PAGE })
             return reject(res)
           }
           /* -------- 无感刷新 token ----------- */
@@ -76,7 +77,7 @@ export function http<T>(options: CustomRequestOptions) {
               await tokenStore.logout()
               // 跳转到登录页
               setTimeout(() => {
-                uni.navigateTo({ url: '/pages/login/login' })
+                uni.navigateTo({ url: LOGIN_PAGE })
               }, 2000)
             }
             finally {

+ 2 - 1
src/pages/index/index.vue

@@ -11,6 +11,7 @@
 </route>
 
 <script lang="ts" setup>
+import { LOGIN_PAGE } from '@/router/config'
 import { useThemeStore } from '@/store'
 
 defineOptions({
@@ -52,7 +53,7 @@ onLoad(() => {
 
 function toLogin() {
   uni.navigateTo({
-    url: '/pages/login/login',
+    url: LOGIN_PAGE,
   })
 }
 

+ 3 - 1
src/pages/login/register.vue

@@ -8,13 +8,15 @@
 </route>
 
 <script lang="ts" setup>
+import { LOGIN_PAGE } from '@/router/config'
+
 function doRegister() {
   uni.showToast({
     title: '注册成功',
   })
   // 注册成功后跳转到登录页
   uni.navigateTo({
-    url: '/pages/login/login',
+    url: LOGIN_PAGE,
   })
 }
 </script>

+ 3 - 2
src/pages/me/me.vue

@@ -12,6 +12,7 @@
 <script lang="ts" setup>
 import type { IUploadSuccessInfo } from '@/api/types/login'
 import { storeToRefs } from 'pinia'
+import { LOGIN_PAGE } from '@/router/config'
 import { useUserStore } from '@/store'
 import { useTokenStore } from '@/store/token'
 import { useUpload } from '@/utils/uploadFile'
@@ -43,7 +44,7 @@ async function handleLogin() {
   await tokenStore.wxLogin()
   // #endif
   // #ifndef MP-WEIXIN
-  uni.navigateTo({ url: '/pages/login/login' })
+  uni.navigateTo({ url: LOGIN_PAGE })
   // #endif
 }
 
@@ -94,7 +95,7 @@ function handleLogout() {
         // #endif
         // #ifndef MP-WEIXIN
         // 非微信小程序,去登录页
-        uni.navigateTo({ url: '/pages/login/login' })
+        uni.navigateTo({ url: LOGIN_PAGE })
         // #endif
       }
     },

+ 2 - 1
src/router/config.ts

@@ -7,7 +7,8 @@ export const LOGIN_STRATEGY = LOGIN_STRATEGY_MAP.DEFAULT_NO_NEED_LOGIN
 export const isNeedLogin = LOGIN_STRATEGY === LOGIN_STRATEGY_MAP.DEFAULT_NEED_LOGIN
 
 export const LOGIN_PAGE = '/pages/login/login'
-export const LOGIN_PAGE_LIST = [LOGIN_PAGE, '/pages/login/register']
+export const REGISTER_PAGE = '/pages/login/register'
+export const LOGIN_PAGE_LIST = [LOGIN_PAGE, REGISTER_PAGE]
 
 // 排除在外的列表,白名单策略指白名单列表,黑名单策略指黑名单列表
 export const EXCLUDE_PAGE_LIST = [

+ 0 - 1
src/store/token.ts

@@ -155,7 +155,6 @@ export const useTokenStore = defineStore(
     const logout = async () => {
       try {
         await _logout()
-
         // 清除存储的过期时间
         uni.removeStorageSync('accessTokenExpireTime')
         uni.removeStorageSync('refreshTokenExpireTime')