From 5888e32fb99675ac64a4c25ee66d1909083b1c02 Mon Sep 17 00:00:00 2001 From: zuozhengqing <a13193816592@163.com> Date: 星期三, 20 十二月 2023 19:34:24 +0800 Subject: [PATCH] jwt为空时跳转至aps登录页 --- src/common/untils/request.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/common/untils/request.js b/src/common/untils/request.js index a69a0ae..3e507c4 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,32 @@ /* //瀵瑰搷搴旀暟鎹仛浜涗簨 */ 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) } }, (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