UNPKG

dareway-rui

Version:

247 lines (209 loc) 7.99 kB
'use strict'; var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault'); Object.defineProperty(exports, '__esModule', { value: true }); exports.default = request; exports.requestBlob = requestBlob; var _typeof2 = _interopRequireDefault(require('@babel/runtime/helpers/typeof')); var _crossFetch = _interopRequireDefault(require('cross-fetch')); var _exception = require('../exception'); var _URL = _interopRequireDefault(require('./URL')); var _DataConvertUtil = _interopRequireDefault(require('../dataUtil/DataConvertUtil')); var _LoadingMask = _interopRequireDefault(require('./LoadingMask')); var DELAY_SHOW_TIME = 1500; function checkHttpStatus(response) { if (response.status >= 200 && response.status < 300) { return response; } throw new _exception.NetworkException( '' .concat(response.status, ' ') .concat(response.statusText, ' ') .concat(response.url) ); } function checkFrameworkException(data) { if (data && data._rui_err_type && data._rui_err_type == 2) { throw new _exception.BusinessException(data._rui_err_text); } else if (data && data._rui_err_type && data._rui_err_type == 1) { throw new _exception.AppException(data._rui_err_text + '\n\r' + data._rui_err_stack); } else if (data && data._rui_err_type && data._rui_err_type == 3) { throw new _exception.SessionOrUserNotExistException(data._rui_err_text + '\n\r' + data._rui_err_stack); } return data; } function isFrameworkException(error) { if ( error instanceof _exception.AppException || error instanceof _exception.BusinessException || error instanceof _exception.SessionOrUserNotExistException ) { return true; } return false; } function request(urlInst) { var isShowLoadingMask = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (!urlInst || !(urlInst instanceof _URL.default)) { throw new Error( 'request(url) \u5165\u53C2 url \u5FC5\u987B\u4E3A URL \u5BF9\u8C61\u5B9E\u4F8B\uFF0C\u5B9E\u9645\u4F20\u5165\u3010' .concat(urlInst, '\uFF0C') .concat((0, _typeof2.default)(urlInst), '\u3011\uFF0C\u8BF7\u68C0\u67E5!') ); } urlInst.addPara('_', Math.random()); var reqUrl = urlInst.getUrl(); var reqData = urlInst.getData(); var loadingMaskObj = new _LoadingMask.default(); if (typeof reqData == 'string') { if (isShowLoadingMask) { loadingMaskObj.show(DELAY_SHOW_TIME); } return (0, _crossFetch.default)(reqUrl, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }, body: reqData, credentials: 'include' }) .then(function(response) { if (isShowLoadingMask) { loadingMaskObj.hidden(); } return response; }) .then(checkHttpStatus) .then(function(response) { return response.json(); }) .then(checkFrameworkException) .then(function(_ref) { var payload = _ref.payload; return _DataConvertUtil.default.convertData(payload); }) .catch(function(error) { if (isShowLoadingMask) { loadingMaskObj.hidden(); } console.error(reqUrl, error); if (isFrameworkException(error)) { throw error; } else { throw new _exception.NetworkException( '\u7F51\u7EDC\u8BF7\u6C42\u3010'.concat(reqUrl, '\u3011\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5!') ); } }); } else if (reqData instanceof FormData) { if (isShowLoadingMask) { loadingMaskObj.show(DELAY_SHOW_TIME); } return (0, _crossFetch.default)(reqUrl, { method: 'POST', body: reqData, credentials: 'include' }) .then(function(response) { if (isShowLoadingMask) { loadingMaskObj.hidden(); } return response; }) .then(checkHttpStatus) .then(function(response) { return response.json(); }) .then(checkFrameworkException) .then(function(_ref2) { var payload = _ref2.payload; return _DataConvertUtil.default.convertData(payload); }) .catch(function(error) { if (isShowLoadingMask) { loadingMaskObj.hidden(); } console.error(reqUrl, error); if (isFrameworkException(error)) { throw error; } else { throw new _exception.NetworkException( '\u7F51\u7EDC\u8BF7\u6C42\u3010'.concat(reqUrl, '\u3011\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5!') ); } }); } } function requestBlob(urlInst) { var isShowLoadingMask = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (!urlInst || !(urlInst instanceof _URL.default)) { throw new Error( 'requestBlob(url) \u5165\u53C2 url \u5FC5\u987B\u4E3A URL \u5BF9\u8C61\u5B9E\u4F8B\uFF0C\u5B9E\u9645\u4F20\u5165\u3010' .concat(urlInst, '\uFF0C') .concat((0, _typeof2.default)(urlInst), '\u3011\uFF0C\u8BF7\u68C0\u67E5!') ); } urlInst.addPara('_', Math.random()); var reqUrl = urlInst.getUrl(); var reqData = urlInst.getData(); var loadingMaskObj = new _LoadingMask.default(); if (typeof reqData == 'string') { if (isShowLoadingMask) { loadingMaskObj.show(DELAY_SHOW_TIME); } return (0, _crossFetch.default)(reqUrl, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }, body: reqData, credentials: 'include' }) .then(function(response) { if (isShowLoadingMask) { loadingMaskObj.hidden(); } return response; }) .then(checkHttpStatus) .then(checkFrameworkException4Blob) .then(function(response) { return { contentDisposition: response.headers.get('Content-Disposition'), blob: response.blob() }; }) .catch(function(error) { if (isShowLoadingMask) { loadingMaskObj.hidden(); } console.error(reqUrl, error); if (isFrameworkException(error)) { throw error; } else { throw new _exception.NetworkException( '\u7F51\u7EDC\u8BF7\u6C42\u3010'.concat(reqUrl, '\u3011\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5!') ); } }); } else if (reqData instanceof FormData) { throw new Error('这里也先暂时不支持,感觉是可以支持的...'); } } function checkFrameworkException4Blob(res4Text) { var res4Blob = res4Text.clone(); return res4Text .json() .then(function(data) { checkFrameworkException(data); return res4Blob; }) .catch(function(error) { if (isFrameworkException(error)) { throw error; } return res4Blob; }); } //@ sourceMappingURL=request.js.map