wiz-frameworks
Version:
wizlong react framework
177 lines (155 loc) • 6.71 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
/**
*
* @overview 请求
* @author mll
* @date 2019-9-5
*/
import { utils_tool, component_tool } from '../tool';
var $$ = utils_tool.$$,
Storage = utils_tool.Storage;
var WizNotice = component_tool.WizNotice;
export var post = function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(api, payload, requestConfig) {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
return _context.abrupt('return', requestConfig ? global.request.post(api, payload, _extends(defaultConfig, requestConfig)) : global.request.post(api, payload));
case 1:
case 'end':
return _context.stop();
}
}
}, _callee, this);
}));
return function post(_x, _x2, _x3) {
return _ref.apply(this, arguments);
};
}();
export var get = function () {
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(api, payload, requestConfig) {
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
return _context2.abrupt('return', requestConfig ? global.request.get(api, payload, _extends(defaultConfig, requestConfig)) : global.request.get(api, payload));
case 1:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));
return function get(_x4, _x5, _x6) {
return _ref2.apply(this, arguments);
};
}();
export var put = function () {
var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(api, payload, requestConfig) {
var put;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
put = global.request.put;
return _context3.abrupt('return', requestConfig ? put(api, payload, _extends(defaultConfig, requestConfig)) : put(api, payload));
case 2:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
return function put(_x7, _x8, _x9) {
return _ref3.apply(this, arguments);
};
}();
export var _delete = function () {
var _ref4 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(api, payload, requestConfig) {
var _delete;
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_delete = global.request['delete'];
return _context4.abrupt('return', requestConfig ? _delete(api, payload, _extends(defaultConfig, requestConfig)) : _delete(api, payload));
case 2:
case 'end':
return _context4.stop();
}
}
}, _callee4, this);
}));
return function _delete(_x10, _x11, _x12) {
return _ref4.apply(this, arguments);
};
}();
var defaultConfig = {
prefix: '/api',
timeout: 3600 * 1000,
credentials: 'omit',
headers: {
'content-type': 'application/json',
'endpoint': global.endpoint ? global.endpoint : 'admin'
},
//redirect:'manual',
// 每次请求头部都会带着这些参数
withHeaders: function withHeaders() {
return {
'access-token': Storage.getStorage("token")
};
},
/**
* 因为modelEnhance需要知道服务器反回的数据,
* 什么样的是成功,什么样的是失败,如
* {success: true, data: ...} // 代表成功
* {success: false, message: ...} // 代表失败
* 实际中应该通过服务端反回的response中的
* 成功失败标识来进行区分
*/
afterResponse: function afterResponse(response) {
if (response instanceof Blob) {
return response;
}
var success = response.success,
code = response.code,
msg = response.msg;
// if (code === 100009) {//100009代表未登录
// if (constants.env.sys_login_path) {
// window.location.replace(location.origin + '/#' + constants.env.sys_login_path);
// } else {
// window.location.replace(location.origin + '/#/sign/login');
// }
// location.reload();
// }
if (success || code != undefined && code != null) {
// if (code === '401' || code === 401) { //401代表登录失败
// //WizNotice.warn(msg || '您的登录已超时,请重新登录')
// if (constants.env.sys_login_path) {
// window.location.replace(location.origin + '/#' + constants.env.sys_login_path);
// } else {
// window.location.replace(location.origin + '/#/sign/login');
// }
// //location.reload();
// }
return response;
} else {
throw new Error(msg);
}
},
errorHandle: function errorHandle(err) {
// 请求错误全局拦截
console.log('Error:', err);
if (err.name === 'RequestError') {
WizNotice.error(err.text || err.message);
} else {
WizNotice.error({
key: 'errorHandle',
description: err.text || err.message || err.msg
});
}
}
};
export default defaultConfig;