import config from '../../config/systemconfig'
|
|
let mask; //遮罩层对象
|
|
/**
|
* 打开遮罩层(待实现)
|
* @param {String} msg 提示消息
|
* @param {Menu} type 默认0(半透明),1、不透明,2、完全透明
|
*/
|
let maskOn = function (msg, type) {
|
|
}
|
|
/**
|
* 关闭遮罩层(待实现)
|
*/
|
let maskOff = function () {
|
|
}
|
|
/**
|
*
|
* @param {Json Object} options 请求参数
|
* @returns 处理后的参数或设置了默认值的参数
|
*/
|
let setRequestParam = function (options) {
|
let requestConfig = {
|
baseUrl: config.Host,
|
url: '',
|
data: {},
|
async: config.RequestAsync,
|
timeout: config.RequestTimeOut,
|
responseType: 'json',
|
contentType: config.ContentType,
|
headers: {},
|
success: function (data) { },
|
error: function (xhr, response, msg) { },
|
before: function () { },
|
after: function () { }
|
};
|
|
//baseUrl
|
if (!!options.baseUrl) {
|
requestConfig.baseUrl = options.baseUrl;
|
}
|
//baseUrl end
|
|
//Content-Type
|
if (!!options.contentType) {
|
requestConfig.contentType = options.contentType;
|
}
|
//Content-Type end
|
|
//url
|
if (!options.url) {
|
console.error('url参数不能为空');
|
}
|
else {
|
requestConfig.url = options.url;
|
}
|
//url end
|
|
//data
|
if (!!options.data) {
|
requestConfig.data = options.data;
|
}
|
//data end
|
|
//headers
|
if (!!options.headers) {
|
requestConfig.headers = options.headers;
|
}
|
//header end
|
|
//responseType
|
if (!!options.responseType) {
|
requestConfig.responseType = options.response;
|
}
|
//responseType end
|
|
//before
|
if (!!options.before) {
|
requestConfig.before = function () {
|
options.before();
|
}
|
}
|
//before end
|
|
//after
|
if (!!options.after) {
|
requestConfig.after = function () {
|
options.after();
|
}
|
}
|
//after end
|
|
//async
|
if (options.async != undefined && options.async != null) {
|
requestConfig.async = options.async;
|
}
|
//async end
|
|
//success
|
if (!!options.success) {
|
requestConfig.success = function (data, xhr) {
|
options.success(data, xhr);
|
}
|
}
|
else {
|
requestConfig.success = function (data, xhr) {
|
console.log(xhr.responseText);
|
}
|
}
|
//success end
|
|
//error
|
if (!!options.error) {
|
requestConfig.error = function (xhr, response, msg) {
|
options.error(xhr, response, msg);
|
}
|
}
|
else {
|
requestConfig.error = function (xhr) {
|
if (xhr.status % 100 > 10 && xhr.status != 415) {
|
//后端自定义返回的错误码
|
try {
|
let json = JSON.parse(xhr.responseText);
|
if (!!json.msg) {
|
_alert(xhr.msg, "error");
|
}
|
}
|
catch (err) {
|
_alert(xhr.responseText, "error");
|
}
|
}
|
else {
|
//预期之外的
|
_alert("网络连接错误,请刷新页面!", "error");
|
}
|
}
|
}
|
//error end
|
|
return requestConfig;
|
}
|
|
/**
|
* 实例化一个xhr对象
|
* @param {String} type GET/POST等
|
* @param {Json Object} requestConfig 处理后的请求配置
|
* @returns 一个即将可以发送的xhr对象
|
*/
|
let setXhrRequest = function (type, requestConfig) {
|
let xhr = new XMLHttpRequest();
|
|
//异步才存在超时,同步无超时限制
|
if (requestConfig.async) {
|
xhr.timeout = requestConfig.timeout;
|
} else { }
|
//异步才存在超时,同步无超时限制 end
|
|
xhr.onreadystatechange = function () { //当请求状态改变时触发此事件
|
if (xhr.readyState == 4) { //请求结束后
|
if (xhr.status >= 200 && xhr.status < 300) {
|
//成功,
|
if (requestConfig.responseType.toLowerCase() == 'json') {
|
let data = JSON.parse(xhr.responseText);
|
requestConfig.success(data, xhr);
|
}
|
else if (requestConfig.responseType.toLowerCase() == 'text'
|
|| requestConfig.responseType.toLowerCase() == 'string') {
|
requestConfig.success(xhr.responseText, xhr);
|
}
|
else {
|
requestConfig.success(xhr.responseText, xhr);
|
}
|
|
}
|
else if (xhr.status >= 300 && xhr.status < 400) {
|
//重定向,待遇到再处理
|
}
|
else if (xhr.status >= 100 && xhr < 200) {
|
//连接中,待遇到再处理
|
}
|
else {
|
//其他,包含400系列、500系列
|
requestConfig.error(xhr, xhr.response, xhr.responseText);
|
}
|
}
|
}
|
if (type.toLowerCase() == "get") {
|
let url = requestConfig.baseUrl + requestConfig.url;
|
if(requestConfig.url.indexOf("://")){//url包含有一定的协议时,只取url,不取baseUrl
|
url = requestConfig.url;
|
}
|
if (!!requestConfig.data && JSON.stringify(requestConfig.data) != "{}") {
|
if (JSON.stringify(requestConfig.data).length > 4000) {
|
throw "Get请求限制最大传输4000字节!";
|
}
|
url = url + "?" + convertToUrl(requestConfig.data);
|
}
|
xhr.open(type, url, requestConfig.async);
|
}
|
else {
|
xhr.setRequestHeader("Content-Type", requestConfig.contentType);
|
xhr.open(type, requestConfig.baseUrl + requestConfig.url, requestConfig.async);
|
}
|
//设置headers
|
if (!!requestConfig.headers) {
|
for (var key in requestConfig.headers) {
|
xhr.setRequestHeader(key, requestConfig.headers[key]);
|
}
|
}
|
if (!!config.RequestHeaders) {
|
for (var key in config.RequestHeaders) {
|
xhr.setRequestHeader(key, config.RequestHeaders[key]);
|
}
|
}
|
//设置headers end
|
return xhr;
|
}
|
|
/**
|
* 将对象参数转成url参数
|
* @param {Object} obj 待转换的对象
|
* @returns a=1&b=2&c=%7B%22a%22:1,%22b%22:2,%22c%22:%5B%7B%22aa%22:11,%22bb%22:22%7D%5D%7D
|
*/
|
let convertToUrl = function (obj) {
|
if (Object.prototype.toString.call(obj) == '[object Array]') {
|
return "=" + encodeURIComponent(JSON.stringify(obj));
|
}
|
else if (Object.prototype.toString.call(obj) == '[object Undefined]'
|
|| Object.prototype.toString.call(obj) == '[object Function]') {
|
return "=";
|
}
|
else if (Object.prototype.toString.call(obj) == '[object Number]'
|
|| Object.prototype.toString.call(obj) == '[object Boolean]') {
|
return "=" + obj;
|
}
|
else if (Object.prototype.toString.call(obj) == '[object String]'
|
&& obj.indexOf('&') > -1) {
|
return encodeURI(obj);
|
}
|
else if (Object.prototype.toString.call(obj) == '[object String]'
|
&& obj.indexOf('?') == 0) {
|
return encodeURI(obj)
|
}
|
else if (Object.prototype.toString.call(obj) == '[object String]'
|
&& obj.indexOf('&') == -1) {
|
return "=" + encodeURI(obj);
|
}
|
var arr = [];
|
for (var p in obj)
|
if (obj.hasOwnProperty(p)) {
|
if (Object.prototype.toString.call(obj[p]) == '[object Array]'
|
|| Object.prototype.toString.call(obj[p]) == '[object Object]') {
|
arr.push(encodeURI(p) + "=" + encodeURI(JSON.stringify(obj)));
|
} else {
|
arr.push(encodeURI(p) + "=" + encodeURI(obj[p]));
|
}
|
}
|
return arr.join("&");
|
}
|
|
|
let out = {
|
setRequestParam,
|
setXhrRequest,
|
convertToUrl,
|
maskOn,
|
maskOff
|
}
|
|
export default out;
|