From 22b5859b62fa57710ca771ee59fe896442641914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=AB=A5=E5=B4=87?= <792998983@qq.com> Date: Tue, 7 Sep 2021 17:06:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E8=AF=B7=E6=B1=82=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E5=A4=84=E7=90=86=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/forge/SecuritySetting/notice/api.js | 46 ++++++++++ src/forge/SecuritySetting/notice/fetch.js | 10 +++ src/forge/javaFetch.js | 105 ++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 src/forge/SecuritySetting/notice/api.js create mode 100644 src/forge/SecuritySetting/notice/fetch.js create mode 100644 src/forge/javaFetch.js diff --git a/src/forge/SecuritySetting/notice/api.js b/src/forge/SecuritySetting/notice/api.js new file mode 100644 index 000000000..07fc1838a --- /dev/null +++ b/src/forge/SecuritySetting/notice/api.js @@ -0,0 +1,46 @@ +import fetch from './fetch'; + +// 获取消息列表 +export function noticePages(params) { + return fetch({ + url: '/api/notice/noticePages', + method: 'get', + params + }); +} + +// 获取单个notice +export function getNotice(params) { + return fetch({ + url: '/api/notice/getNotice', + method: 'get', + params + }); +} + +//新增notice +export function addNotice(data) { + return fetch({ + url: '/api/notice/createNotice', + method: 'post', + data: data + }); +} + +//更新notice +export function updateNotice(data) { + return fetch({ + url: '/api/notice/updateNotice', + method: 'PUT', + data: data + }); +} + +//删除notice +export function deleteNotice(data) { + return fetch({ + url: '/api/notice/deleteNotice', + method: 'DELETE', + data, + }); +} diff --git a/src/forge/SecuritySetting/notice/fetch.js b/src/forge/SecuritySetting/notice/fetch.js new file mode 100644 index 000000000..dba00b140 --- /dev/null +++ b/src/forge/SecuritySetting/notice/fetch.js @@ -0,0 +1,10 @@ + +import javaFetch from '../../javaFetch'; + +const developUrl = "https://test-search.trustie.net"; +const testUrl = "https://test-search.trustie.net"; +const productUrl = "https://wiki-api.trustie.net"; + +const { service, actionUrl } = javaFetch(productUrl, testUrl, developUrl); +export const httpUrl = actionUrl; +export default service; \ No newline at end of file diff --git a/src/forge/javaFetch.js b/src/forge/javaFetch.js new file mode 100644 index 000000000..7f5368960 --- /dev/null +++ b/src/forge/javaFetch.js @@ -0,0 +1,105 @@ +import { notification ,message} from 'antd'; +import axios from 'axios'; +import cookie from 'react-cookies'; +import Login from './Wiki/components/Login'; + +export const TokenKey = 'autologin_trustie'; +export default function javaFetch(productUrl,testUrl,developUrl ){ + let actionUrl=''; + if (window.location.href.indexOf('localhost') > -1) { + actionUrl = developUrl; + } else if(window.location.href.indexOf('testforgeplus')>-1){ + actionUrl = testUrl; + axios.defaults.withCredentials = true; + }else if (window.location.href.indexOf('forgeplus') > -1) { + actionUrl = productUrl; + axios.defaults.withCredentials = true; + } + + // 创建axios实例 + const service = axios.create({ + baseURL: actionUrl, + 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 token = sessionStorage.token; + if (config.url.indexOf('?') === -1) { + config.url = `${config.url}?token=${token}`; + } else { + config.url = `${config.url}&token=${token}`; + } + } + return config; + }, error => { + console.log(error); // for debug + // Promise.reject(error); + }); + // respone拦截器 + service.interceptors.response.use( + response => { + const res = response||{}; + if (res.status === 400) { + message.error(res.data.message || '操作失败'); + return Promise.reject('error'); + } + if (res.status === 401) { + message.error(res.data.message || '登录信息已过期'); + return Promise.reject('error'); + } + if (res.status === 403) { + message.error(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) { + message.error(res.data.message || '操作失败'); + return Promise.reject('error'); + } + if (res.status === 401) { + message.error(res.data.message || '登录信息已过期'); + Login(); + return Promise.reject('error'); + } + if (res.status === 403) { + message.error(res.data.message || '无权限!'); + return Promise.reject('error'); + } + notification.open({ + message: "提示", + description: error.message, + }); + return Promise.reject(error); + } + ); + + console.log(service); + console.log(typeof service); + + return {service,actionUrl}; +} \ No newline at end of file