From 302858f921bed077ab54dd31102f086bfe1add3c Mon Sep 17 00:00:00 2001
From: haoxuan <haoxuan>
Date: 星期三, 07 二月 2024 11:59:47 +0800
Subject: [PATCH] srm项目 系统参数设置的前端页面开发+增加相关路由+公共列表组件增加是否可以配置表头的逻辑

---
 src/common/untils/request.js |   86 +++++++++++++++++++++++++++++++++++++------
 1 files changed, 74 insertions(+), 12 deletions(-)

diff --git a/src/common/untils/request.js b/src/common/untils/request.js
index 9270bbc..1c10074 100644
--- a/src/common/untils/request.js
+++ b/src/common/untils/request.js
@@ -2,21 +2,55 @@
 import { Message } from "element-ui"
 
 // import router from '@/router'
+// 寮�鍙戠幆澧冧笅灏嗚嚜宸辩殑token澶嶅埗鍒拌繖閲�, 涔熷彲浠ュ湪娴忚鍣ㄤ腑鎵嬪姩娣诲姞token鍒癱ookie涓�,cookie涓殑token浼樺厛
+const DEV_TOKEN =  ''
+function environmentType(){
+  let type
+  if (location.href.includes('192.168.20.119')) {
+      type = 'test'
+  } else if (location.href.includes('192.168') || location.href.includes('localhost')) {
+      type = 'dev'
+  } else {
+      type = 'prod'
+  }
+
+  return type
+}
+
+const isDev = environmentType() === 'dev'
 
 const Axios = axios.create({
   responseType: "json",
   withCredentials: true // 鏄惁鍏佽甯ookie杩欎簺
 })
+const getApsPage = () => {
+  // 棣栭〉閮ㄧ讲鍦ㄥ悇涓幆澧冪殑绔彛
+  const loginPathMap = {
+      prod:`//${window.location.hostname}:9080`,
+      test:`//192.168.20.119:9080`,
+      dev: `//192.168.8.107:8080`
+  }
+  return loginPathMap[environmentType()]
+}
 
 /* //POST浼犲弬搴忓垪鍖�(娣诲姞璇锋眰鎷︽埅鍣�) */
 Axios.interceptors.request.use(
   (config) => {
-    // 鑻ユ槸鏈夊仛閴存潈token , 灏辩粰澶撮儴甯︿笂token
-    // let token = util.cookies.get("token");
-    // if (token != undefined) {
-    //   config.headers.Authorization = "Bearer " + token;
-    // }
-
+    // // 鑻ユ槸鏈夊仛閴存潈token , 灏辩粰澶撮儴甯︿笂token
+    // // let token = util.cookies.get("token");
+    // // if (token != undefined) {
+    // //   config.headers.Authorization = "Bearer " + token;
+    // // }
+    let token = document.cookie.replace(
+      /(?:(?:^|.*;\s*)token\s*=\s*([^;]*).*$)|^.*$/,
+      "$1",
+    );
+    if (isDev){
+      token = token || DEV_TOKEN
+    }
+    if (token) {
+      config.headers.Authorization = "Bearer " + token;
+    }
     return config
   },
   (error) => {
@@ -30,16 +64,44 @@
     /* //瀵瑰搷搴旀暟鎹仛浜涗簨 */
     if (res.data.code === 200) {
       return res.data ? res.data : {}
-    } else {
-      Message({
-        message: res.data.msg,
-        type: "error",
-        duration: 5 * 1000
-      })
+    } else if([2012,2013,2014,2015].includes(res.data.code)){
+      if (isDev){
+        alert("JWT澶辨晥,鍗冲皢璺宠浆鑷崇櫥褰曢〉..")
+        window.location = getApsPage()+'/login'
+      }else {
+        //   JWT閴存潈澶辨晥 璺宠浆鍒扮櫥褰曢〉
+        window.location = getApsPage()+'/login'
+      }
+      // Message({
+      //   message: res.data.msg,
+      //   type: "error",
+      //   duration: 5 * 1000
+      // })
       return Promise.reject(res.data)
+    }else if([2036].includes(res.data.code)){
+      if (isDev){
+        window.location = getApsPage()+'/commonWeb?resetPwd=true'
+      }else {
+        //   JWT閴存潈澶辨晥 璺宠浆鍒扮櫥褰曢〉
+        window.location = getApsPage()+'/commonWeb?resetPwd=true'
+      }
+      return Promise.reject(res.data)
+
+      // if(window.location.pathname && window.location.pathname !== '/login'){
+      //   window.location = window.location.origin+'/login'
+      // }
     }
   },
   (error) => {
+    if(error.response.status === 401){
+      if (isDev){
+        alert("JWT澶辨晥,鍗冲皢璺宠浆鑷崇櫥褰曢〉..")
+        window.location = getApsPage()+'/login'
+      }else {
+        //   JWT閴存潈澶辨晥 璺宠浆鍒扮櫥褰曢〉
+        window.location = getApsPage()+'/login'
+      }
+    }
     let { message } = error
     if (message === "Network Error") {
       message = "鍚庣鎺ュ彛杩炴帴寮傚父"

--
Gitblit v1.8.0