yonge 1 неделя назад
Родитель
Сommit
c821365e4d
4 измененных файлов с 39 добавлено и 17 удалено
  1. 2 6
      src/queryParams/index.ts
  2. 1 1
      src/router/index.ts
  3. 3 10
      src/store/user.ts
  4. 33 0
      src/utils/urlParams.ts

+ 2 - 6
src/queryParams/index.ts

@@ -1,4 +1,5 @@
 import { reactive } from "vue"
+import { getCurrentUrlParams } from "@/utils/urlParams"
 
 /** 维护其他系统过来的特殊字段  这里用来记录,以免其他系统传过来的参数混乱 不好维护 */
 export type queryParamsType = {
@@ -34,11 +35,6 @@ export function initQueryParams() {
 }
 
 function getParamsFromUrl() {
-  const fullUrl = window.location.href
-  const queryIndex = fullUrl.indexOf("?")
-  if (queryIndex === -1) return undefined
-  const queryString = fullUrl.slice(queryIndex + 1)
-  const params = new URLSearchParams(queryString)
-  return params
+  return getCurrentUrlParams()
 }
 export default queryParams

+ 1 - 1
src/router/index.ts

@@ -1,7 +1,6 @@
 import { createRouter, createWebHashHistory } from "vue-router"
 import { constRoutes } from "./routers"
 import NProgress from "@/plugins/nprogress"
-import { ElMessage } from "element-plus"
 import { getToken, CODE401 } from "@/libs/auth"
 import userStore from "@/store/user"
 
@@ -28,6 +27,7 @@ const regWhiteList: RegExp[] = [] //正则白名单(用于系统不需要权限
 
 router.beforeEach((to, from, next) => {
   NProgress.start()
+  userStoreHook.login()
   const hasToken = getToken()
   //如果有token且不是白名单页面则加载info
   if (hasToken && !authWhiteList.includes(to.path)) {

+ 3 - 10
src/store/user.ts

@@ -1,9 +1,10 @@
 import { defineStore } from "pinia"
-import { removeToken, setToken, setCODE401 } from "@/libs/auth"
+import { removeToken, setToken } from "@/libs/auth"
 import router from "@/router"
 import { httpAjax } from "@/plugins/httpAjax"
 import { store } from "./index"
 import { getUserInfoApi } from "@/api/user"
+import { getCurrentUrlParams, normalizeAuthorization } from "@/utils/urlParams"
 interface userType {
   userInfo: {
     nickname?: string
@@ -22,15 +23,7 @@ const useStore = defineStore("user", {
   actions: {
     /** 登录 */
     async login() {
-      function getAuthorizationFromUrl() {
-        const fullUrl = window.location.href
-        const queryIndex = fullUrl.indexOf("?")
-        if (queryIndex === -1) return undefined
-        const queryString = fullUrl.slice(queryIndex + 1)
-        const params = new URLSearchParams(queryString)
-        return params.get("Authorization") || undefined
-      }
-      const token = getAuthorizationFromUrl()
+      const token = normalizeAuthorization(getCurrentUrlParams().get("Authorization"))
       token && setToken(token)
     },
     /** 获取用户信息 */

+ 33 - 0
src/utils/urlParams.ts

@@ -0,0 +1,33 @@
+function appendParams(target: URLSearchParams, rawQuery: string) {
+  if (!rawQuery) return
+  rawQuery
+    .replace(/^\?/, "")
+    .split("??")
+    .filter(Boolean)
+    .forEach(query => {
+      new URLSearchParams(query).forEach((value, key) => {
+        if (!target.has(key)) target.set(key, value)
+      })
+    })
+}
+
+export function getCurrentUrlParams() {
+  const params = new URLSearchParams()
+  appendParams(params, window.location.search)
+
+  const hash = window.location.hash || ""
+  const hashQueryIndex = hash.indexOf("?")
+  if (hashQueryIndex >= 0) appendParams(params, hash.slice(hashQueryIndex + 1))
+
+  const doubleQuestionQuery = window.location.href.split("??").slice(1).join("??")
+  appendParams(params, doubleQuestionQuery)
+
+  return params
+}
+
+export function normalizeAuthorization(token?: string | null) {
+  if (!token) return undefined
+  const trimmed = token.trim()
+  if (!trimmed) return undefined
+  return trimmed.replace(/^bearer\s+/i, "Bearer ")
+}