瀏覽代碼

no message

linwenxin 1 月之前
父節點
當前提交
41c349cbc6
共有 2 個文件被更改,包括 16 次插入3 次删除
  1. 15 2
      src/common/http/index.js
  2. 1 1
      src/store/modules/user/index.js

+ 15 - 2
src/common/http/index.js

@@ -16,6 +16,19 @@ const program = {
 }
 const whiteCodes = [200, 201]
 
+function removeQueryParams(params) {
+  var url = new URL(window.location.href) // 获取当前 URL
+
+  // 遍历每个参数并删除
+  params.forEach(function (param) {
+    url.searchParams.delete(param)
+  })
+
+  // 使用新的 URL 更新地址栏,且不刷新页面
+  window.history.pushState({}, '', url)
+  location.reload() // 刷新页面,重新加载
+}
+
 export const $http = (url, method, data, json, loadingBool = false, isExecute = true, isForm = false) => {
   //设置请求前拦截器
   http.interceptor.request = config => {
@@ -60,7 +73,7 @@ export const $http = (url, method, data, json, loadingBool = false, isExecute =
         return Promise.reject(new Error(res.message || 'Error'))
       }
       if (res.code === 4444) {
-        location.reload()
+        removeQueryParams(['x-token', 'code'])
         return {}
       }
       if (res.code === 1001) {
@@ -271,7 +284,7 @@ export const axios = function (obj = {}) {
           }
 
           if (res.code === 4444) {
-            location.reload()
+            removeQueryParams(['x-token', 'code'])
             reject(new Error(res.message || 'Error'))
             return {}
           }

+ 1 - 1
src/store/modules/user/index.js

@@ -25,7 +25,7 @@ function getQueryVariable(variable) {
 
 const state = {
   openId: getQueryVariable('openId') || getStorage('openId') || '',
-  token: getStorage('token') || getQueryVariable('x-token') || '',
+  token: getQueryVariable('x-token') || getStorage('token') || '',
   name: getQueryVariable('username') || getStorage('name') || '',
   avatar: getQueryVariable('useravatar') || getStorage('avatar') || '',
   userId: getQueryVariable('userId') || getStorage('userId') || '',