Przeglądaj źródła

chore(router): 修复switchTab无法携带query参数的问题

修复uni-app中switchTab跳转时query参数丢失的问题,改为直接使用完整path跳转
优化路由拦截器中的query参数合并逻辑,确保参数正确传递
feige996 8 miesięcy temu
rodzic
commit
8e76de0313
2 zmienionych plików z 19 dodań i 19 usunięć
  1. 9 3
      src/pages/login/login.vue
  2. 10 16
      src/router/interceptor.ts

+ 9 - 3
src/pages/login/login.vue

@@ -39,15 +39,21 @@ function doLogin() {
     path = `/${path}`
   }
   const { path: _path, query } = parseRouteStr(path)
-  console.log('_path:', _path, 'query:', query)
+  console.log('_path:', _path, 'query:', query, 'path:', path)
   console.log('isPageTabbar(_path):', isPageTabbar(_path))
   if (isPageTabbar(_path)) {
+    // 经过我的测试 switchTab 不能带 query 参数, 不管是放到 url  还是放到 query ,
+    // 最后跳转过去的时候都会丢失 query 信息
     uni.switchTab({
-      url: _path,
-      query,
+      url: path,
     })
+    // uni.switchTab({
+    //   url: _path,
+    //   query,
+    // })
   }
   else {
+    console.log('redirectTo:', path)
     uni.redirectTo({
       url: path,
     })

+ 10 - 16
src/router/interceptor.ts

@@ -18,38 +18,32 @@ export const navigateToInterceptor = {
     if (url === undefined) {
       return
     }
+    let { path, query: _query } = parseRouteStr(url)
 
-    let path = decodeURIComponent(url).split('?')[0]
+    console.log('路由拦截器 1: url->', url, ', query ->', query)
+    const myQuery = { ..._query, ...query }
     // /pages/route-interceptor/index?name=feige&age=30
-    console.log('路由拦截器:url->', url, ', query ->', query, ', path ->', path)
+    console.log('路由拦截器 2: path->', path, ', _query ->', _query)
+    console.log('路由拦截器 3: myQuery ->', myQuery)
 
     // 处理相对路径
     if (!path.startsWith('/')) {
-      console.log(getCurrentPages())
-      if (getCurrentPages().length === 0) {
-        return
-      }
       const currentPath = getLastPage()?.route || ''
       const normalizedCurrentPath = currentPath.startsWith('/') ? currentPath : `/${currentPath}`
       const baseDir = normalizedCurrentPath.substring(0, normalizedCurrentPath.lastIndexOf('/'))
       path = `${baseDir}/${path}`
     }
 
-    const { path: _path, query: _query } = parseRouteStr(path)
-    console.log('_path:', _path, 'query:', _query)
-
     // 处理直接进入路由非首页时,tabbarIndex 不正确的问题
-    tabbarStore.setAutoCurIdx(_path)
+    tabbarStore.setAutoCurIdx(path)
 
-    if (LOGIN_PAGE_LIST.includes(_path)) {
+    if (LOGIN_PAGE_LIST.includes(path)) {
       console.log('命中了 LOGIN_PAGE_LIST')
       return
     }
 
-    console.log('拦截器中得到的 path:', path)
-    console.log('拦截器中得到的 query:', query)
-    if (query) {
-      path += `?${Object.keys(query).map(key => `${key}=${query[key]}`).join('&')}`
+    if (myQuery) {
+      path += `?${Object.keys(myQuery).map(key => `${key}=${myQuery[key]}`).join('&')}`
     }
     const redirectUrl = `${LOGIN_PAGE}?redirect=${encodeURIComponent(path)}`
 
@@ -62,7 +56,7 @@ export const navigateToInterceptor = {
         return
       }
       else {
-        if (EXCLUDE_PAGE_LIST.includes(_path)) {
+        if (EXCLUDE_PAGE_LIST.includes(path)) {
           return
         }
         else {