1
0
tangning 15 часов назад
Родитель
Сommit
528a74e2a3
2 измененных файлов с 19 добавлено и 21 удалено
  1. 2 2
      src/App.vue
  2. 17 19
      src/util/index.ts

+ 2 - 2
src/App.vue

@@ -15,8 +15,8 @@ import { strToParams } from "@/util";
 import { ElMessageBox } from "element-plus";
 import { browser } from "@/util/browser.ts";
 const url = window.location.href.split('?')[1];
-const params = strToParams(url);
-console.log("params", params, url);
+const urlWithoutHash = window.location.href.split('#')[0]
+const params = strToParams(urlWithoutHash);
 if (params.token) {
   setToken(params.token);
 }

+ 17 - 19
src/util/index.ts

@@ -327,25 +327,23 @@ export const mixEnum = <T, K>(enum1: T, enum2: K): T | K => {
   };
 };
 
-// 字符串转params对象
-export const strToParams = (str: string) => {
-  if (!str) return {};
-  if (str?.[0] === "?") {
-    str = str.substr(1);
-  }
-
-  const result: { [key: string]: string } = {};
-  const splitRG = /([^=&]+)(?:=([^&]*))?&?/;
-
-  let rgRet;
-  while ((rgRet = str.match(splitRG))) {
-    result[rgRet[1]] = rgRet[2] === undefined ? "" : rgRet[2];
-    str = str.substr(rgRet[0].length);
-  }
-
-  return result;
-};
-
+/**
+ * 解析 URL 中的所有参数,返回对象
+ * @param {string} url 可选,不传默认取当前页面 url
+ * @returns {object} 参数对象
+ */
+export const strToParams = (url = window.location.href) => {
+  // 取出 ? 后面的参数部分
+  const queryStr = url.split('?')[1] || ''
+  if (!queryStr) return {}
+
+  // 解析成键值对
+  const params = {}
+  new URLSearchParams(queryStr).forEach((val, key) => {
+    params[key] = val
+  })
+  return params
+}
 export const getDomMatrix = (dom: HTMLElement) => {
   const str = getComputedStyle(dom, null).getPropertyValue("transform");
   const matrix2d = str