From 6fc31c0bb8d6e7cefffe6139f2012e3462ded089 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=AB=A5=E5=B4=87?= <792998983@qq.com> Date: Sun, 26 Sep 2021 18:31:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=EF=BC=8C=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E8=B0=83=E7=94=A8=E4=B8=8D=E5=90=8C=E7=9A=84?= =?UTF-8?q?API=EF=BC=8C=E6=9A=82=E6=97=B6=E7=94=A8=E4=B8=AA=E7=AE=80?= =?UTF-8?q?=E6=98=93=E7=89=88=E5=90=8E=E7=BB=AD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/military/fetch.js | 2 +- src/military/notice/api.js | 2 +- src/military/notice/fetch.js | 122 +++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 src/military/notice/fetch.js diff --git a/src/military/fetch.js b/src/military/fetch.js index bede3805d..f9ec73f82 100644 --- a/src/military/fetch.js +++ b/src/military/fetch.js @@ -14,7 +14,7 @@ if (window.location.href.indexOf('localhost') > -1) { actionUrl="https://taskapi.osredm.com"; axios.defaults.withCredentials = true; }else if(window.location.href.indexOf('forge.osredm.com')>-1){ - actionUrl="https://info.osredm.com"; + actionUrl="https://task.osredm.com"; axios.defaults.withCredentials = true; } export const httpUrl = actionUrl; diff --git a/src/military/notice/api.js b/src/military/notice/api.js index 1f80ba34b..d2d8962c4 100644 --- a/src/military/notice/api.js +++ b/src/military/notice/api.js @@ -1,4 +1,4 @@ -import fetch from '../fetch'; +import fetch from './fetch'; import { notification } from 'antd'; diff --git a/src/military/notice/fetch.js b/src/military/notice/fetch.js new file mode 100644 index 000000000..bede3805d --- /dev/null +++ b/src/military/notice/fetch.js @@ -0,0 +1,122 @@ +import { notification,message } from 'antd'; +import axios from 'axios'; +import cookie from 'react-cookies'; + + +let actionUrl = ''; +if (window.location.href.indexOf('localhost') > -1) { + actionUrl='https://taskapi.osredm.com'; + // actionUrl='http://192.168.31.47:8081'; +}else if(window.location.href.indexOf('192.168.31.48') > -1){ + actionUrl='https://taskapi.osredm.com'; + axios.defaults.withCredentials = true; +}else if(window.location.href.indexOf('noticeweb.osredm') > -1){ + actionUrl="https://taskapi.osredm.com"; + axios.defaults.withCredentials = true; +}else if(window.location.href.indexOf('forge.osredm.com')>-1){ + actionUrl="https://info.osredm.com"; + axios.defaults.withCredentials = true; +} +export const httpUrl = actionUrl; + +const TokenKey = 'autologin_forge_military'; + +// 创建axios实例 +const service = axios.create({ + baseURL: httpUrl, + timeout: 10000, // 请求超时时间 +}); + +// request拦截器 +service.interceptors.request.use(config => { + if (cookie.load(TokenKey)) { + console.log(cookie.load(TokenKey)); + config.headers['Authorization'] = cookie.load(TokenKey); // 让每个请求携带自定义token 请根据实际情况自行修改 + } + if (window.location.port === "3007") { + // 模拟token为登录用户 + const taskToken = sessionStorage.taskToken; + if (config.url.indexOf('?') === -1) { + config.url = `${config.url}?token=${taskToken}`; + } else { + config.url = `${config.url}&token=${taskToken}`; + } + } + return config; +}, error => { + // Do something with request error + console.log(error); // for debug + Promise.reject(error); +}); +// respone拦截器 +service.interceptors.response.use( + response => { + const res = response; + if (res.status === 400) { + notification.open({ + message: "提示", + description: res.data.message || '验证失败', + }); + return Promise.reject('error'); + } + if (res.status === 401) { + notification.open({ + message: "提示", + description: res.data.message || '未授权,请登录!', + }); + return Promise.reject('error'); + } + if (res.status === 403) { + notification.open({ + message: "提示", + description: res.data.message || '无权限', + }); + return Promise.reject('error'); + } + if (res.status === 40001) { + notification.open({ + message: "提示", + description: '账户或密码错误!', + }); + return Promise.reject('error'); + } + if (response.status !== 200 && res.status !== 200) { + notification.open({ + message: "提示", + description: res.message, + }); + } else { + return response.data; + } + }, + error => { + console.log(error); + let res = error.response||{}; + if (res.status === 400) { + notification.open({ + message: "提示", + description: res.data.message || '操作失败', + }); + return Promise.reject('error'); + } + if (res.status === 401) { + notification.open({ + message: "提示", + description: res.data.message || '登录信息已过期', + }); + return Promise.reject('error'); + } + if (res.status === 403) { + notification.open({ + message: "提示", + description: res.data.message || '无权限!', + }); + window.location.href="/403"; + return Promise.reject('error'); + } + + return Promise.reject(error); + } +); + +export default service;