diff --git a/src/military/fetch.js b/src/military/fetch.js index bede3805..f9ec73f8 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 1f80ba34..d2d8962c 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 00000000..bede3805 --- /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;