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

修复bug

上级 d85cbb2e
......@@ -4,18 +4,18 @@
*/
import { extend } from 'umi-request';
import { notification } from 'antd';
import FormdataWrapper from "../utils/object-to-formdata-custom";
import { getToken } from "./token";
import { queryApiActionPath, queryOauthActionPath, queryPermActionPath } from "../utils/queryConfig";
import { offline } from '../location/Toast'
import { getToken } from './token';
import { queryApiActionPath, queryOauthActionPath, queryPermActionPath } from './queryConfig';
import { openToast } from '@/components/Notification';
import { showToast } from '../location/Toast';
import { offline } from '../location/Toast';
const codeMessage = {
200: '服务器成功返回请求的数据。',
201: '新建或修改数据成功。',
202: '一个请求已经进入后台排队(异步任务)。',
204: '删除数据成功。',
400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
401: '用户没有权限(令牌、用户名、密码错误)。',
401: '登录已过期,请重新登录',
403: '用户得到授权,但是访问是被禁止的。',
404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
406: '请求的格式不可得。',
......@@ -26,23 +26,46 @@ const codeMessage = {
503: '服务不可用,服务器暂时过载或维护。',
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;
const errorHandler = error => {
const { response } = error;
if (response && response.status) {
const errorText = codeMessage[response.status] || response.statusText;
notification.error({
message: `请求错误`,
description:errorText,
description: errorText,
});
if (response.status === 401) {
// @HACK
/* eslint-disable no-underscore-dangle */
if (window.location.href.indexOf('/portal/previewModule') !== -1) {
return;
}
return window.g_app._store.dispatch({
type: 'login/loginout',
});
......@@ -51,14 +74,13 @@ const errorHandler = error => {
if (canReportError) {
notification.error({
message: `网络故障`,
description: "请检查网络链接或联系管理员",
description: '请检查网络链接或联系管理员',
});
canReportError = false;
}
}
};
/**
* 配置request请求时的默认参数
*/
......@@ -68,40 +90,144 @@ const umiRequest = extend({
mode: 'cors',
});
const getUrl = url => url.startsWith("/") ? url : "/" + url;
const getUrl = url => (url.startsWith('/') ? url : '/' + url);
export const request = (url, data, options = {}) => {
const pp = { ...data, token: getToken() }
for(let i in pp){
if(pp[i]==null){
delete pp[i]
}else if(typeof(pp[i])=='string'){
pp[i]=pp[i].trim()
let version =
localStorage.getItem('version') && localStorage.getItem('version') !== 'undefined'
? JSON.parse(localStorage.getItem('version'))
: null;
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)
return umiRequest(url, {
data: pp,
// method: "POST",
requestType: "form",
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) {
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 (response.errCode || response.errMsg) {
offline(response.errMsg, 2)
if (data.callback) data.callback(response)
return
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,
description: response.errMsg,
});
return;
}
......@@ -112,53 +238,35 @@ export const request = (url, data, options = {}) => {
});
return;
}
canReportError = true
canReportError = true;
return response;
})
});
}
};
const setRequestConfig = (config) => {
const setRequestConfig = config => {
return (url, data, options = {}) => {
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(url, data, { ...options, ...config })
}),
);
}
return request(url, data, { ...options, ...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 apiRequest = createServerRequest({ method: "POST", prefix: queryApiActionPath() });
export const getRequest = createServerRequest({ method: "GET", prefix: queryApiActionPath() });
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 default request;
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论