From 82e27d0cadbddfc73e8b978e778b802dc8e51fa1 Mon Sep 17 00:00:00 2001 From: yangfeng <wanwan926_4@163.com> Date: 星期五, 29 三月 2024 10:51:36 +0800 Subject: [PATCH] 报工入参修改和工序ID修改 --- src/common/utils/request.ts | 28 ++++++++++++++++++++-------- 1 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/common/utils/request.ts b/src/common/utils/request.ts index 0522895..82f6c31 100644 --- a/src/common/utils/request.ts +++ b/src/common/utils/request.ts @@ -1,8 +1,10 @@ -import axios from 'axios' +import axiosClient from 'axios' +import type { AxiosRequestConfig } from 'axios' import { ElMessage } from 'element-plus' import { getToken } from '@/common/utils/index' +import router from '../../router/index' -const Axios = axios.create({ +const instance = axiosClient.create({ responseType: 'json', withCredentials: true // 鍏佽鎼哄甫 cookie }) @@ -10,11 +12,15 @@ /** * 璇锋眰鎷︽埅鍣� */ -Axios.interceptors.request.use( +instance.interceptors.request.use( (config) => { // 鑻ユ槸鏈夊仛閴存潈token , 灏辩粰澶撮儴甯︿笂token const token = getToken() - if (token) { + if ((token == null || token == '' || token == 'undefined') && config.url != '/api/base/login') { + router.push('/login') + return config + // window.location = `//${window.location.hostname}:9080`+'/login' + } else if (token) { config.headers.Authorization = 'Bearer ' + token } @@ -28,20 +34,24 @@ /** * 鍝嶅簲鎷︽埅鍣� */ -Axios.interceptors.response.use( +instance.interceptors.response.use( (res) => { if (res.data.code == 200) { - return res.data ?? {} + return res.data } else { ElMessage({ message: res.data.msg, type: 'error', duration: 3 * 1000 }) - return res.data ?? {} + return Promise.reject(res?.data) } }, (error) => { + if (error.response.status === 401) { + // alert('JWT澶辨晥,鍗冲皢璺宠浆鑷崇櫥褰曢〉..') + router.push('/login') + } let { message } = error if (message === 'Network Error') { message = '鍚庣鎺ュ彛杩炴帴寮傚父' @@ -59,4 +69,6 @@ } ) -export default Axios +const axios = <T>(cfg: AxiosRequestConfig) => instance.request<any, T>(cfg) + +export default axios -- Gitblit v1.8.0