提交 43eb1d38 authored 作者: chscls@163.com's avatar chscls@163.com

修复bug

上级 d85cbb2e
...@@ -4,18 +4,18 @@ ...@@ -4,18 +4,18 @@
*/ */
import { extend } from 'umi-request'; import { extend } from 'umi-request';
import { notification } from 'antd'; import { notification } from 'antd';
import FormdataWrapper from "../utils/object-to-formdata-custom"; import { getToken } from './token';
import { getToken } from "./token"; import { queryApiActionPath, queryOauthActionPath, queryPermActionPath } from './queryConfig';
import { queryApiActionPath, queryOauthActionPath, queryPermActionPath } from "../utils/queryConfig"; import { openToast } from '@/components/Notification';
import { showToast } from '../location/Toast';
import { offline } from '../location/Toast' import { offline } from '../location/Toast';
const codeMessage = { const codeMessage = {
200: '服务器成功返回请求的数据。', 200: '服务器成功返回请求的数据。',
201: '新建或修改数据成功。', 201: '新建或修改数据成功。',
202: '一个请求已经进入后台排队(异步任务)。', 202: '一个请求已经进入后台排队(异步任务)。',
204: '删除数据成功。', 204: '删除数据成功。',
400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。', 400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
401: '用户没有权限(令牌、用户名、密码错误)。', 401: '登录已过期,请重新登录',
403: '用户得到授权,但是访问是被禁止的。', 403: '用户得到授权,但是访问是被禁止的。',
404: '发出的请求针对的是不存在的记录,服务器没有进行操作。', 404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
406: '请求的格式不可得。', 406: '请求的格式不可得。',
...@@ -26,23 +26,46 @@ const codeMessage = { ...@@ -26,23 +26,46 @@ const codeMessage = {
503: '服务不可用,服务器暂时过载或维护。', 503: '服务不可用,服务器暂时过载或维护。',
504: '网关超时。', 504: '网关超时。',
}; };
/**
* param 将要转为URL参数字符串的对象
* key URL参数字符串的前缀
* encode true/false 是否进行URL编码,默认为true
*
* return URL参数字符串
*/
const objectToQuery = function(param, key, encode) {
if (param == null) return '';
var paramStr = '';
var t = typeof param;
if (t == 'string' || t == 'number' || t == 'boolean') {
paramStr += '&' + key + '=' + (encode == null || encode ? encodeURIComponent(param) : param);
} else {
for (var i in param) {
var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
paramStr += objectToQuery(param[i], k, encode);
}
}
return paramStr;
};
/** /**
* 异常处理程序 * 异常处理程序
*/ */
var canReportError = true; var canReportError = true;
const errorHandler = error => { const errorHandler = error => {
const { response } = error; const { response } = error;
if (response && response.status) { if (response && response.status) {
const errorText = codeMessage[response.status] || response.statusText; const errorText = codeMessage[response.status] || response.statusText;
notification.error({ notification.error({
message: `请求错误`, message: `请求错误`,
description:errorText, description: errorText,
}); });
if (response.status === 401) { if (response.status === 401) {
// @HACK // @HACK
/* eslint-disable no-underscore-dangle */ /* eslint-disable no-underscore-dangle */
if (window.location.href.indexOf('/portal/previewModule') !== -1) {
return;
}
return window.g_app._store.dispatch({ return window.g_app._store.dispatch({
type: 'login/loginout', type: 'login/loginout',
}); });
...@@ -51,14 +74,13 @@ const errorHandler = error => { ...@@ -51,14 +74,13 @@ const errorHandler = error => {
if (canReportError) { if (canReportError) {
notification.error({ notification.error({
message: `网络故障`, message: `网络故障`,
description: "请检查网络链接或联系管理员", description: '请检查网络链接或联系管理员',
}); });
canReportError = false; canReportError = false;
} }
} }
}; };
/** /**
* 配置request请求时的默认参数 * 配置request请求时的默认参数
*/ */
...@@ -68,40 +90,144 @@ const umiRequest = extend({ ...@@ -68,40 +90,144 @@ const umiRequest = extend({
mode: 'cors', mode: 'cors',
}); });
const getUrl = url => url.startsWith("/") ? url : "/" + url; const getUrl = url => (url.startsWith('/') ? url : '/' + url);
export const request = (url, data, options = {}) => { export const request = (url, data, options = {}) => {
const pp = { ...data, token: getToken() } let version =
for(let i in pp){ localStorage.getItem('version') && localStorage.getItem('version') !== 'undefined'
if(pp[i]==null){ ? JSON.parse(localStorage.getItem('version'))
delete pp[i] : null;
}else if(typeof(pp[i])=='string'){
pp[i]=pp[i].trim() if (options.method === 'GET') {
const pp = { ...data, version_: version };
for (let i in pp) {
if (pp[i] == null) {
delete pp[i];
} else if (typeof pp[i] == 'string') {
pp[i] = pp[i].trim();
}
}
if (url.indexOf('?') !== -1) {
url = url + '&token=' + getToken() + objectToQuery(pp);
} else {
url = url + '?token=' + getToken() + objectToQuery(pp);
}
// 由于自动添加前缀 这里暂时修改getUrl(url)
return umiRequest(url, {
data: pp,
// method: "POST",
requestType: 'form',
...options,
}).then(response => {
if (response.errCode && response.errCode == '10000') {
const ul = window.location.href;
if (ul.indexOf('?') == -1) {
let x = JSON.parse(response.errMsg);
let time = +new Date();
localStorage.setItem('version', JSON.stringify(x.version));
if (ul.indexOf('/portal/') == -1 && ul.indexOf('/admin/') == -1) {
sessionStorage.removeItem('cacheMobile');
showToast('检查到最新版本,即将自动更新,更新完成后请重新操作');
window.location.href = x.mobileAdress
? mobileAdress + '?time=' + time
: ul + '?time=' + time;
} else {
sessionStorage.removeItem('cacheMobile');
openToast('error', '版本更新', '检查到最新版本,即将自动更新,更新完成后请重新操作');
window.location.href = x.webAdress
? x.webAdress + '?time=' + time
: ul + '?time=' + time;
} }
setTimeout(() => location.reload(), 3000);
} }
return;
}
if (window.location.href.indexOf('/mobile') != -1) {
if (response.errCode || response.errMsg) {
offline(response.errMsg, 2);
if (data.callback) data.callback(response);
return;
}
}
if (!response) return;
if (response.errCode || response.errMsg) {
notification.error({
message: `请求错误`,
description: response.errMsg,
});
return;
}
if (response.errcode) {
notification.error({
message: `请求错误`,
description: response.errmsg,
});
return;
}
canReportError = true;
return response;
});
} else {
const pp = { ...data, token: getToken(), version_: version };
for (let i in pp) {
if (pp[i] == null) {
delete pp[i];
} else if (typeof pp[i] == 'string') {
pp[i] = pp[i].trim();
}
}
// 由于自动添加前缀 这里暂时修改getUrl(url) // 由于自动添加前缀 这里暂时修改getUrl(url)
return umiRequest(url, { return umiRequest(url, {
data: pp, data: pp,
// method: "POST", // method: "POST",
requestType: "form", requestType: 'form',
...options, ...options,
}).then(response => { }).then(response => {
if (response.errCode && response.errCode == '10000') {
const ul = window.location.href;
if (ul.indexOf('?') == -1) {
let x = JSON.parse(response.errMsg);
let time = +new Date();
localStorage.setItem('version', JSON.stringify(x.version));
if (ul.indexOf('/portal/') == -1 && ul.indexOf('/admin/') == -1) {
showToast('检查到最新版本,即将自动更新,更新完成后请重新操作');
window.location.href = x.mobileAdress
? mobileAdress + '?time=' + time
: ul + '?time=' + time;
} else {
openToast('error', '版本更新', '检查到最新版本,即将自动更新,更新完成后请重新操作');
window.location.href = x.webAdress
? x.webAdress + '?time=' + time
: ul + '?time=' + time;
}
setTimeout(() => location.reload(), 3000);
}
return;
}
if (window.location.href.indexOf('/mobile') != -1) { if (window.location.href.indexOf('/mobile') != -1) {
if (response.errCode || response.errMsg) { if (response.errCode || response.errMsg) {
offline(response.errMsg, 2) offline(response.errMsg, 2);
if (data.callback) data.callback(response) if (data.callback) data.callback(response);
return return;
} }
} }
if (!response) return; if (!response) return;
if (response.errCode || response.errMsg) { if (response.errCode || response.errMsg) {
notification.error({ notification.error({
message: `请求错误`, message: `请求错误`,
description:response.errMsg, description: response.errMsg,
}); });
return; return;
} }
...@@ -112,53 +238,35 @@ export const request = (url, data, options = {}) => { ...@@ -112,53 +238,35 @@ export const request = (url, data, options = {}) => {
}); });
return; return;
} }
canReportError = true canReportError = true;
return response; return response;
}) });
}
}; };
const setRequestConfig = config => {
const setRequestConfig = (config) => {
return (url, data, options = {}) => { return (url, data, options = {}) => {
if (Array.isArray(url)) { if (Array.isArray(url)) {
return Promise.all(url.map((ags) => { return Promise.all(
url.map(ags => {
return request(ags[0], ags[1], Object.assign({}, ags[2], config)); return request(ags[0], ags[1], Object.assign({}, ags[2], config));
})) }),
} );
return request(url, data, { ...options, ...config })
} }
return request(url, data, { ...options, ...config });
};
}; };
const createServerRequest = (config = {}) => { const createServerRequest = (config = {}) => {
return setRequestConfig(config) return setRequestConfig(config);
}; };
export const uaaRequest = createServerRequest({ method: 'POST', prefix: queryOauthActionPath() });
export const uaaRequest = createServerRequest({ method: "POST", prefix: queryOauthActionPath() }); export const permRequest = createServerRequest({ method: 'POST', prefix: queryPermActionPath() });
export const permRequest = createServerRequest({ method: "POST", prefix: queryPermActionPath() });
export const apiRequest = createServerRequest({ method: "POST", prefix: queryApiActionPath() });
export const getRequest = createServerRequest({ method: "GET", prefix: queryApiActionPath() });
export const apiRequest = createServerRequest({ method: 'POST', prefix: queryApiActionPath() });
export const getRequest = createServerRequest({ method: 'GET', prefix: queryApiActionPath() });
export default request; export default request;
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论