From 5de621e943d9605d10e2ac5d46658c0626a68caa Mon Sep 17 00:00:00 2001
From: yangfeng <wanwan926_4@163.com>
Date: 星期四, 07 三月 2024 10:11:05 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/web/SRM

---
 src/common/untils/request.js |   68 +++++++++++++++++++++++++++++++---
 1 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/src/common/untils/request.js b/src/common/untils/request.js
index a69a0ae..1c10074 100644
--- a/src/common/untils/request.js
+++ b/src/common/untils/request.js
@@ -2,11 +2,36 @@
 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(
@@ -20,6 +45,9 @@
       /(?:(?:^|.*;\s*)token\s*=\s*([^;]*).*$)|^.*$/,
       "$1",
     );
+    if (isDev){
+      token = token || DEV_TOKEN
+    }
     if (token) {
       config.headers.Authorization = "Bearer " + token;
     }
@@ -36,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