Răsfoiți Sursa

fix: 修改跳页方法

liangan 2 săptămâni în urmă
părinte
comite
b1f1b31cbf
3 a modificat fișierele cu 22 adăugiri și 40 ștergeri
  1. 6 6
      src/store/user.ts
  2. 3 1
      src/utils/http.ts
  3. 13 33
      src/utils/index.ts

+ 6 - 6
src/store/user.ts

@@ -1,7 +1,7 @@
 import { defineStore } from 'pinia'
 import { ref } from 'vue'
 import { getUserInfo as _getUserInfo, login as _login } from '@/api/login'
-import { goBack, toPage } from '@/utils/page'
+import { toPage } from '@/utils/page'
 import { toast } from '@/utils/toast'
 
 // 初始化状态
@@ -41,9 +41,9 @@ export const useUserStore = defineStore(
     /**
      * 用户登录
      * @param loginData 登录数据
-     * @param redirectUrl 登录成功后的跳转地址,默认跳转到首页
+     * @param redirect 登录成功后的跳转地址,默认跳转到首页
      */
-    const login = async (loginData: any, redirectUrl?: string) => {
+    const login = async (loginData: any, redirect?: string) => {
       try {
         // 显示加载状态
         uni.showLoading({
@@ -69,13 +69,13 @@ export const useUserStore = defineStore(
 
         // 跳转到指定页面或默认首页
         setTimeout(() => {
-          if (redirectUrl) {
+          if (redirect) {
             // 使用 toPage 方法,自动判断是否为 tabBar 页面
-            toPage({ url: decodeURIComponent(redirectUrl), isRedirect: true })
+            toPage({ url: decodeURIComponent(redirect), isRedirect: true })
           }
           else {
             // 默认跳转到首页
-            goBack()
+            toPage({ url: '/pages/index/index', isReLaunch: true })
           }
         }, 1500)
 

+ 3 - 1
src/utils/http.ts

@@ -1,5 +1,6 @@
 import type { CustomRequestOptions } from '@/interceptors/request'
 import { toPage } from '@/utils/page'
+import { getLastPageUrl } from './index'
 import { toast } from './toast'
 
 export function http<T>(options: CustomRequestOptions) {
@@ -21,7 +22,8 @@ export function http<T>(options: CustomRequestOptions) {
           }
           else if (res.data.code === '598') {
             toast.error((res.data as IResData<T>).message)
-            toPage({ url: '/pages/login/login' })
+            const redirect = getLastPageUrl()
+            toPage({ url: '/pages/login/login', params: { redirect } })
           }
           else {
             toast.error((res.data as IResData<T>).message || '请求错误')

+ 13 - 33
src/utils/index.ts

@@ -1,5 +1,6 @@
 import { pages, subPackages } from '@/pages.json'
 import { isMpWeixin } from './platform'
+import { stringifyQuery } from './queryString'
 
 export function getLastPage() {
   // getCurrentPages() 至少有1个元素,所以不再额外判断
@@ -9,6 +10,18 @@ export function getLastPage() {
   return pages[pages.length - 1]
 }
 
+export function getLastPageUrl() {
+  const { route, options }: any = getLastPage()
+  // 拼接完整的页面路径和参数
+  let fullPath = `/${route}`
+  if (options && Object.keys(options).length > 0) {
+    const strParams = stringifyQuery(options)
+    fullPath = `${fullPath}?${strParams}`
+  }
+  console.log(fullPath)
+  return fullPath
+}
+
 /**
  * 获取当前页面路由的 path 路径和 redirectPath 路径
  * path 如 '/pages/login/index'
@@ -196,39 +209,6 @@ export function qs(params) {
     .join('&')
 }
 
-/**
- * 解析 URL 查询字符串为对象
- * @param {string} queryString - 需要解析的查询字符串(如 "a=1&b=2")
- * @returns {object} 解析后的参数对象
- */
-export function parseQs(queryString) {
-  if (!queryString)
-    return {}
-
-  // 移除开头的问号(如果有)
-  const cleanQuery = queryString.startsWith('?')
-    ? queryString.slice(1)
-    : queryString
-
-  return cleanQuery.split('&')
-    .reduce((acc, pair) => {
-      const [encodedKey, encodedValue] = pair.split('=')
-
-      if (encodedKey === undefined)
-        return acc
-
-      const key = decodeURIComponent(encodedKey)
-      const value = encodedValue === undefined
-        ? undefined
-        : decodeURIComponent(encodedValue)
-
-      // 处理空字符串值
-      acc[key] = value === '' ? null : value
-
-      return acc
-    }, {})
-}
-
 /**
  * 数字格式化,加逗号分隔符,支持自定义小数位数
  * @param num 要格式化的数字,支持 number 或 string 类型