@deppon/deppon-request 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/es/_virtual/_rollup-plugin-inject-process-env.js +11 -0
- package/es/index.d.ts +2 -0
- package/es/index.js +3 -0
- package/es/request/adapterAjax.d.ts +1 -0
- package/es/request/adapterAjax.js +94 -0
- package/es/request/cancel.d.ts +1 -0
- package/es/request/cancel.js +118 -0
- package/es/request/headers.d.ts +15 -0
- package/es/request/headers.js +131 -0
- package/es/request/index.d.ts +2 -0
- package/es/request/index.js +3 -0
- package/es/request/instance.d.ts +1 -0
- package/es/request/instance.js +21 -0
- package/es/request/reqInterceptor.d.ts +2 -0
- package/es/request/reqInterceptor.js +189 -0
- package/es/request/request.d.ts +2 -0
- package/es/request/request.js +95 -0
- package/es/request/resInterceptor.d.ts +2 -0
- package/es/request/resInterceptor.js +68 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 xingxing
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
(function() {
|
|
2
|
+
const env = {};
|
|
3
|
+
try {
|
|
4
|
+
if (process) {
|
|
5
|
+
process.env = Object.assign({}, process.env);
|
|
6
|
+
Object.assign(process.env, env);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
} catch (e) {} // avoid ReferenceError: process is not defined
|
|
10
|
+
globalThis.process = { env:env };
|
|
11
|
+
})();
|
package/es/index.d.ts
ADDED
package/es/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function optionsAdapter(options: any, instance: any, resolveOptions: any): any;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import '../_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
|
+
import { Loading } from '@deppon/deppon-ui';
|
|
3
|
+
|
|
4
|
+
var isFuntion = function isFuntion(func) {
|
|
5
|
+
return func && typeof func === 'function';
|
|
6
|
+
};
|
|
7
|
+
var DEFAULT_METHOD = 'POST';
|
|
8
|
+
var whiteApiUrl = ['/gw/home/v6/goods/recommend.html'];
|
|
9
|
+
function optionsAdapter(options, instance, resolveOptions) {
|
|
10
|
+
// 适配method用法
|
|
11
|
+
typeToMethod(options);
|
|
12
|
+
// 适配datatype
|
|
13
|
+
dataTypeForAxios(options);
|
|
14
|
+
// 适配beforeSend
|
|
15
|
+
beforeSendForAxios(options);
|
|
16
|
+
resolveOptions(options);
|
|
17
|
+
return instance(options).then(function (res) {
|
|
18
|
+
// // 兼容老的传参方式
|
|
19
|
+
isFuntion(options.success) && options.success(res);
|
|
20
|
+
isFuntion(options.complete) && options.complete();
|
|
21
|
+
// return options.catchCode ? res : res ? res.data : null;
|
|
22
|
+
return options.catchCode || whiteApiUrl.includes(options === null || options === void 0 ? void 0 : options.url) ? res : res ? res.data : null;
|
|
23
|
+
})["catch"](function (e) {
|
|
24
|
+
e.status === 'success' ? isFuntion(options.success) && options.success(e.data) : isFuntion(options.error) && options.error(e.data);
|
|
25
|
+
isFuntion(options.complete) && options.complete();
|
|
26
|
+
return Promise.reject(e.data || e);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function typeToMethod(options) {
|
|
30
|
+
var type = options.type,
|
|
31
|
+
method = options.method;
|
|
32
|
+
if (!method) {
|
|
33
|
+
var _method = type || DEFAULT_METHOD;
|
|
34
|
+
options.method = _method.toLowerCase();
|
|
35
|
+
}
|
|
36
|
+
delete options.type;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 赋予axios 修改dataType的能力
|
|
40
|
+
function dataTypeForAxios(options) {
|
|
41
|
+
var accepts = {
|
|
42
|
+
'*': '*/*',
|
|
43
|
+
text: 'text/plain',
|
|
44
|
+
html: 'text/html',
|
|
45
|
+
xml: 'application/xml, text/xml',
|
|
46
|
+
json: 'application/json, text/javascript'
|
|
47
|
+
};
|
|
48
|
+
var _options$headers = options.headers,
|
|
49
|
+
headers = _options$headers === void 0 ? {} : _options$headers,
|
|
50
|
+
_options$dataType = options.dataType,
|
|
51
|
+
dataType = _options$dataType === void 0 ? '' : _options$dataType;
|
|
52
|
+
var dataTypes = null;
|
|
53
|
+
if (dataType && !headers.accept) {
|
|
54
|
+
dataTypes = dataType.toLowerCase().match(/[^\x20\t\r\n\f]+/g);
|
|
55
|
+
if (dataTypes && accepts[dataTypes[0]]) {
|
|
56
|
+
headers.accept = accepts[dataTypes[0]];
|
|
57
|
+
options.headers = headers;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* jquery中beforeSend有3个参数
|
|
64
|
+
* callbackContext
|
|
65
|
+
* jqXHR
|
|
66
|
+
* s
|
|
67
|
+
* return false 会终止请求
|
|
68
|
+
* 对应axios有2个
|
|
69
|
+
* data
|
|
70
|
+
* headers
|
|
71
|
+
*
|
|
72
|
+
* return false的话不会终止请求。但是ajax会终止,不过目前没有人用这个规则
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
function beforeSendForAxios(options) {
|
|
76
|
+
var transformRequest = options.transformRequest,
|
|
77
|
+
beforeSend = options.beforeSend;
|
|
78
|
+
if (!transformRequest) {
|
|
79
|
+
transformRequest = [];
|
|
80
|
+
}
|
|
81
|
+
if (typeof transformRequest === 'function') {
|
|
82
|
+
transformRequest = [transformRequest];
|
|
83
|
+
}
|
|
84
|
+
transformRequest.push(function (data) {
|
|
85
|
+
if (!options.unload) {
|
|
86
|
+
Loading.start();
|
|
87
|
+
}
|
|
88
|
+
beforeSend && typeof beforeSend === 'function' && beforeSend();
|
|
89
|
+
return data;
|
|
90
|
+
});
|
|
91
|
+
options.transformRequest = transformRequest;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { optionsAdapter as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function _default(options: any): void;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import '../_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
3
|
+
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
|
|
4
|
+
import _createClass from '@babel/runtime/helpers/createClass';
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
|
|
7
|
+
var CancelToken = axios.CancelToken;
|
|
8
|
+
var CLEAR_TIME = 60000;
|
|
9
|
+
var Cancel = /*#__PURE__*/function () {
|
|
10
|
+
function Cancel() {
|
|
11
|
+
_classCallCheck(this, Cancel);
|
|
12
|
+
this.cancelMap = {};
|
|
13
|
+
this.keyUrlMap = {};
|
|
14
|
+
this.keyOptionsMap = {};
|
|
15
|
+
this.keyTsMap = {};
|
|
16
|
+
this.clearTime = null;
|
|
17
|
+
}
|
|
18
|
+
_createClass(Cancel, [{
|
|
19
|
+
key: "autoClear",
|
|
20
|
+
value: function autoClear() {
|
|
21
|
+
var _this = this;
|
|
22
|
+
if (!this.clearTime) {
|
|
23
|
+
this.clearTime = +new Date();
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
var newClearTime = +new Date();
|
|
27
|
+
if (newClearTime - this.clearTime > CLEAR_TIME) {
|
|
28
|
+
// 超过刷新时间的话,就执行刷新- use micro task
|
|
29
|
+
Promise.resolve(true).then(function () {
|
|
30
|
+
var catchTime = _this.clearTime;
|
|
31
|
+
_this.clearTime = newClearTime;
|
|
32
|
+
Object.entries(_this.keyTsMap).map(function (_ref) {
|
|
33
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
34
|
+
key = _ref2[0],
|
|
35
|
+
value = _ref2[1];
|
|
36
|
+
if (value && value - catchTime > CLEAR_TIME) {
|
|
37
|
+
_this.clearByKey(key);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}, {
|
|
44
|
+
key: "clearUrl",
|
|
45
|
+
value: function clearUrl(url) {
|
|
46
|
+
return typeof url === 'string' ? url.split('?')[0] : url;
|
|
47
|
+
}
|
|
48
|
+
}, {
|
|
49
|
+
key: "set",
|
|
50
|
+
value: function set(options, cancel) {
|
|
51
|
+
this.autoClear();
|
|
52
|
+
var url = this.clearUrl(options.url);
|
|
53
|
+
var rmKey = Math.random() * Math.random() * 1000;
|
|
54
|
+
this.keyUrlMap[rmKey] = url;
|
|
55
|
+
this.cancelMap[rmKey] = cancel;
|
|
56
|
+
this.keyTsMap[rmKey] = +new Date();
|
|
57
|
+
this.keyOptionsMap[rmKey] = options; // 用来以后做精细化对比。目前只使用URL做初步对比
|
|
58
|
+
}
|
|
59
|
+
}, {
|
|
60
|
+
key: "clear",
|
|
61
|
+
value: function clear(options) {
|
|
62
|
+
var url = this.clearUrl(options.url);
|
|
63
|
+
this.findKeysByUrl(url).forEach(this.clearByKey);
|
|
64
|
+
}
|
|
65
|
+
}, {
|
|
66
|
+
key: "clearByKey",
|
|
67
|
+
value: function clearByKey(key) {
|
|
68
|
+
delete this.cancelMap[key];
|
|
69
|
+
delete this.keyUrlMap[key];
|
|
70
|
+
delete this.keyOptionsMap[key];
|
|
71
|
+
}
|
|
72
|
+
}, {
|
|
73
|
+
key: "findKeysByUrl",
|
|
74
|
+
value: function findKeysByUrl(url) {
|
|
75
|
+
var matchKeys = Object.entries(this.keyUrlMap);
|
|
76
|
+
return matchKeys.reduce(function (list, _ref3) {
|
|
77
|
+
var _ref4 = _slicedToArray(_ref3, 2),
|
|
78
|
+
key = _ref4[0],
|
|
79
|
+
value = _ref4[1];
|
|
80
|
+
if (value === url) {
|
|
81
|
+
list.push(key);
|
|
82
|
+
}
|
|
83
|
+
return list;
|
|
84
|
+
}, []);
|
|
85
|
+
}
|
|
86
|
+
}, {
|
|
87
|
+
key: "cancel",
|
|
88
|
+
value: function cancel(options) {
|
|
89
|
+
var _this2 = this;
|
|
90
|
+
var url = this.clearUrl(options.url);
|
|
91
|
+
var cancelMap = this.cancelMap;
|
|
92
|
+
this.findKeysByUrl(url).forEach(function (rmKey) {
|
|
93
|
+
if (rmKey && cancelMap[rmKey]) {
|
|
94
|
+
cancelMap[rmKey].cancel();
|
|
95
|
+
_this2.clearByKey(rmKey);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}]);
|
|
100
|
+
return Cancel;
|
|
101
|
+
}();
|
|
102
|
+
var cancelController = new Cancel();
|
|
103
|
+
if (!window.cancelController) {
|
|
104
|
+
window.cancelController = cancelController;
|
|
105
|
+
}
|
|
106
|
+
function cancelApi (options) {
|
|
107
|
+
function getNewToken() {
|
|
108
|
+
var source = CancelToken.source();
|
|
109
|
+
if (options.cancelHistory) {
|
|
110
|
+
cancelController.cancel(options);
|
|
111
|
+
}
|
|
112
|
+
cancelController.set(options, source);
|
|
113
|
+
return source.token;
|
|
114
|
+
}
|
|
115
|
+
options.cancelToken = getNewToken();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export { cancelApi as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default function setHeaders(options: any): void;
|
|
2
|
+
/**
|
|
3
|
+
* 获取head信息
|
|
4
|
+
* @param {string} name 要匹配的关键字
|
|
5
|
+
* @param {boolean} vague 是否模糊搜索
|
|
6
|
+
* @return object
|
|
7
|
+
*
|
|
8
|
+
**/
|
|
9
|
+
export function getHeaderItem(name: string, vague?: boolean): {};
|
|
10
|
+
/**
|
|
11
|
+
* 不重复随机数
|
|
12
|
+
* @params toString参数范围(2-36)
|
|
13
|
+
*
|
|
14
|
+
**/
|
|
15
|
+
export function random(): string;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import '../_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
3
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
4
|
+
import md5 from 'md5';
|
|
5
|
+
import { browser, queryCode } from '@deppon/deppon-utils';
|
|
6
|
+
|
|
7
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
8
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
9
|
+
|
|
10
|
+
// 设置header信息
|
|
11
|
+
function setHeaders(options) {
|
|
12
|
+
var headers = options.headers;
|
|
13
|
+
var UA = browser.parseUA,
|
|
14
|
+
versions = browser.versions;
|
|
15
|
+
var _headers = {};
|
|
16
|
+
var isInApp = UA.isInApp;
|
|
17
|
+
var isInWechat = versions.wechat;
|
|
18
|
+
headers && Object.assign(_headers, {
|
|
19
|
+
'x-app-env': getEnvHeader()
|
|
20
|
+
}, {
|
|
21
|
+
'x-app-appId': getAppId()
|
|
22
|
+
}, isInApp && getHeaderItem('x-app-', true), browser.parseUA.version && {
|
|
23
|
+
'x-app-version': browser.parseUA.version
|
|
24
|
+
}, isInWechat && {
|
|
25
|
+
'x-app-referer': location.href
|
|
26
|
+
}, headers);
|
|
27
|
+
options.headers = _objectSpread(_objectSpread({}, _headers), getEncryptHeader());
|
|
28
|
+
}
|
|
29
|
+
function getAppId() {
|
|
30
|
+
var appId = queryCode('appId') || '';
|
|
31
|
+
|
|
32
|
+
// 被加密后的AppId长度不合规 优先从缓存中获取
|
|
33
|
+
if (appId && (appId === null || appId === void 0 ? void 0 : appId.length) > 30) {
|
|
34
|
+
return sessionStorage.getItem('appId') || '';
|
|
35
|
+
}
|
|
36
|
+
if ((browser.parseUA.isMiniSync || browser.parseUA.isWxWorkMini || browser.parseUA.isTTWebView) && appId) {
|
|
37
|
+
// 设置新的 appId
|
|
38
|
+
sessionStorage.setItem('appId', appId);
|
|
39
|
+
}
|
|
40
|
+
return appId || sessionStorage.getItem('appId') || '';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// WECHAT APP H5 MINI 高德 抖音小程序
|
|
44
|
+
function getEnvHeader() {
|
|
45
|
+
if (browser.parseUA.isMiniSync || browser.parseUA.isWxWorkMini || browser.parseUA.isTTWebView) {
|
|
46
|
+
return 'MINI';
|
|
47
|
+
} else if (browser.parseUA.isApp) {
|
|
48
|
+
return 'APP';
|
|
49
|
+
}
|
|
50
|
+
//注意:该判断异常 parseUA下面没有wechat方法
|
|
51
|
+
else if (browser.parseUA.wechat) {
|
|
52
|
+
return 'WECHAT';
|
|
53
|
+
} else if (browser.parseUA.isAmapApp) {
|
|
54
|
+
return 'GAODE';
|
|
55
|
+
} else {
|
|
56
|
+
return 'H5';
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 获取head信息
|
|
62
|
+
* @param {string} name 要匹配的关键字
|
|
63
|
+
* @param {boolean} vague 是否模糊搜索
|
|
64
|
+
* @return object
|
|
65
|
+
*
|
|
66
|
+
**/
|
|
67
|
+
function getHeaderItem(name) {
|
|
68
|
+
var vague = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
69
|
+
if (!window._headers) {
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
var list = {};
|
|
73
|
+
var _headers = window._headers;
|
|
74
|
+
var newHeaders = _headers;
|
|
75
|
+
for (var _i = 0, _Object$entries = Object.entries(newHeaders); _i < _Object$entries.length; _i++) {
|
|
76
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
77
|
+
key = _Object$entries$_i[0],
|
|
78
|
+
value = _Object$entries$_i[1];
|
|
79
|
+
// 查询name 返回当前name的value
|
|
80
|
+
if (!vague && key === name) {
|
|
81
|
+
list = newHeaders[key];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 模糊匹配 返回object
|
|
85
|
+
if (vague && key.indexOf(name) !== -1) {
|
|
86
|
+
list[key] = value;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return list;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 加密逻辑
|
|
94
|
+
* 随机数:rm 平台号:mstatic 时间戳:time 密钥:key
|
|
95
|
+
* x-app-nonce: 随机字符串
|
|
96
|
+
* x-app-timestamp: 时间戳
|
|
97
|
+
* DEV、PRE:(@vs9jV]ykdnbjE7M) pro:(pro:8Wfy(kX7Gj$uhEUT)
|
|
98
|
+
* x-app-encrypt-text: 随机字符串 + 时间戳 + mstatic + md5
|
|
99
|
+
*/
|
|
100
|
+
function getEncryptHeader() {
|
|
101
|
+
var devpre = '@vs9jV]ykdnbjE7M';
|
|
102
|
+
var production = '8Wfy(kX7Gj$uhEUT';
|
|
103
|
+
var encryptText;
|
|
104
|
+
var headers = {};
|
|
105
|
+
var nonce = random();
|
|
106
|
+
var timestamp = new Date().getTime();
|
|
107
|
+
if (process.env.NODE_ENV === 'production') {
|
|
108
|
+
encryptText = md5(nonce + timestamp + 'static' + production);
|
|
109
|
+
} else {
|
|
110
|
+
encryptText = md5(nonce + timestamp + 'static' + devpre);
|
|
111
|
+
}
|
|
112
|
+
if (browser.parseUA.isTTWebView) {
|
|
113
|
+
headers['x-app-channel'] = 'card_bytedance_mini';
|
|
114
|
+
}
|
|
115
|
+
return _objectSpread({
|
|
116
|
+
'x-app-nonce': nonce,
|
|
117
|
+
'x-app-timestamp': timestamp,
|
|
118
|
+
'x-app-encrypt-text': encryptText
|
|
119
|
+
}, headers);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 不重复随机数
|
|
124
|
+
* @params toString参数范围(2-36)
|
|
125
|
+
*
|
|
126
|
+
**/
|
|
127
|
+
function random() {
|
|
128
|
+
return (Math.random() * 10000000).toString(16).substr(0, 4) + new Date().getTime() + Math.random().toString().substr(2, 5);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export { setHeaders as default, getHeaderItem, random };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function appInstance(options: any): import("axios").AxiosPromise<any>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import '../_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import { res, resErr } from './resInterceptor.js';
|
|
4
|
+
import { req, reqErr } from './reqInterceptor.js';
|
|
5
|
+
|
|
6
|
+
function appInstance(options) {
|
|
7
|
+
var App = axios.create();
|
|
8
|
+
App.defaults.timeout = process.env.NODE_ENV === 'production' ? 10000 : 300000;
|
|
9
|
+
App.defaults.withCredentials = true;
|
|
10
|
+
App.interceptors.request.use(function (_req) {
|
|
11
|
+
return req(_req, options);
|
|
12
|
+
}, reqErr);
|
|
13
|
+
App.interceptors.response.use(function (_res) {
|
|
14
|
+
return res(_res, options);
|
|
15
|
+
}, function (e) {
|
|
16
|
+
return resErr(e, options);
|
|
17
|
+
});
|
|
18
|
+
return App(options);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { appInstance as default };
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import '../_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
|
+
import qs from 'qs';
|
|
4
|
+
import { browser, queryCode } from '@deppon/deppon-utils';
|
|
5
|
+
import { mb_setLocalData } from '@deppon/deppon-bridge';
|
|
6
|
+
import setHeaders from './headers.js';
|
|
7
|
+
|
|
8
|
+
var isAmapApp = browser.parseUA.isAmapApp || false;
|
|
9
|
+
|
|
10
|
+
// 参数拼接
|
|
11
|
+
var replaceTrace = function replaceTrace(targetUrl, value) {
|
|
12
|
+
var queryUrl = targetUrl || window.location.href;
|
|
13
|
+
var url = queryUrl.substr(queryUrl.indexOf('?') + 1);
|
|
14
|
+
var pairs = url.split('&');
|
|
15
|
+
var map = [];
|
|
16
|
+
var params = {};
|
|
17
|
+
|
|
18
|
+
// 如果没有参数
|
|
19
|
+
if (pairs[0] === location.href) {
|
|
20
|
+
return '?trace=' + value;
|
|
21
|
+
}
|
|
22
|
+
for (var i = 0, len = pairs.length; i < len; i++) {
|
|
23
|
+
map = pairs[i].split('=');
|
|
24
|
+
if (map[0] in params) {
|
|
25
|
+
if (Object.prototype.toString.call(params[map[0]]) === '[object Array]') {
|
|
26
|
+
params[map[0]].push(map[1]);
|
|
27
|
+
} else {
|
|
28
|
+
params[map[0]] = [params[map[0]], map[1]];
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
params[map[0]] = map[1];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
var newSearch = '';
|
|
35
|
+
if (params && Object.keys(params).length > 0) {
|
|
36
|
+
params['trace'] = value;
|
|
37
|
+
Object.entries(params).map(function (item, index) {
|
|
38
|
+
if (index === 0) {
|
|
39
|
+
newSearch += '?' + item[0] + '=' + item[1] + '&';
|
|
40
|
+
} else {
|
|
41
|
+
newSearch += item[0] + '=' + item[1] + '&';
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return newSearch.substr(newSearch, newSearch.length - 1) || '';
|
|
45
|
+
} else {
|
|
46
|
+
return '?trace=' + value;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// check列表中是否包含pathname
|
|
51
|
+
var checkPathName = function checkPathName(pathname) {
|
|
52
|
+
var PATHS = ['ts/hotel/item',
|
|
53
|
+
// 酒店
|
|
54
|
+
'ts/groupon/item',
|
|
55
|
+
// 拼团
|
|
56
|
+
'ts/agentpurchasing/item',
|
|
57
|
+
// 盖亚
|
|
58
|
+
'ts/pn/item',
|
|
59
|
+
// 票牛
|
|
60
|
+
'ts/performance/item',
|
|
61
|
+
// 演出
|
|
62
|
+
'ts/platform/item',
|
|
63
|
+
// 平台
|
|
64
|
+
'ts/ticket/item',
|
|
65
|
+
// 门票
|
|
66
|
+
'ts/shopcard/item',
|
|
67
|
+
// 老商户卡
|
|
68
|
+
'ts/hotelcard/index',
|
|
69
|
+
// 酒店卡
|
|
70
|
+
'kt/bargain/item',
|
|
71
|
+
// 砍价一元拿
|
|
72
|
+
'ts/groupon/detail',
|
|
73
|
+
// 参团详情
|
|
74
|
+
'm/club/index',
|
|
75
|
+
// club首页
|
|
76
|
+
'm/club/detail',
|
|
77
|
+
// club详情
|
|
78
|
+
'm/bekka/coin',
|
|
79
|
+
// club充值
|
|
80
|
+
'h5/product/detail',
|
|
81
|
+
// 新商户卡
|
|
82
|
+
'h5/item/detail',
|
|
83
|
+
// 新酒店
|
|
84
|
+
'h5/product/package/detail', 'h5/product/groupon/package/detail', 'h5/product/groupon/detail',
|
|
85
|
+
// 新拼团
|
|
86
|
+
'cps/partner/groupon/item',
|
|
87
|
+
// 分销拼团
|
|
88
|
+
'cps/partner/hotel/item' // 分销酒店
|
|
89
|
+
];
|
|
90
|
+
var isIncludes = false;
|
|
91
|
+
for (var i in PATHS) {
|
|
92
|
+
if (pathname.indexOf(PATHS[i]) > -1) {
|
|
93
|
+
isIncludes = true;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return isIncludes;
|
|
98
|
+
};
|
|
99
|
+
function RequestInterceptor(config, options) {
|
|
100
|
+
setHeaders(config);
|
|
101
|
+
|
|
102
|
+
// 小程序中处理ut (微信小程序、企业微信小程序、抖音小程序)
|
|
103
|
+
if (browser.parseUA.isMiniSync || browser.parseUA.isWxWorkMini || browser.parseUA.isTTWebView) {
|
|
104
|
+
var _ut = window.ut; // 避免同一个页面多次写入ut
|
|
105
|
+
var ut = queryCode('ut') || '';
|
|
106
|
+
if (ut && ut !== _ut) {
|
|
107
|
+
// 如果链接上没有ut 那么沿用之前的ut
|
|
108
|
+
document.cookie = "ut=".concat(ut, "; path=/; domain=").concat(location.host.split('.').slice(1).join('.'));
|
|
109
|
+
window.ut = ut;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 检查trace并设置默认值
|
|
114
|
+
var trace = queryCode('trace');
|
|
115
|
+
if (checkPathName(location.pathname) && (trace === 'null' || !trace)) {
|
|
116
|
+
trace = 'deppon';
|
|
117
|
+
if (isAmapApp) {
|
|
118
|
+
trace = 'amap';
|
|
119
|
+
}
|
|
120
|
+
var newUrl = window.location.origin + window.location.pathname + replaceTrace(window.location.href, trace);
|
|
121
|
+
window.history.replaceState({}, '', newUrl);
|
|
122
|
+
}
|
|
123
|
+
if (trace && !window.trace && checkPathName(location.pathname)) {
|
|
124
|
+
window.trace = trace;
|
|
125
|
+
if (!browser.versions.android && !browser.versions.wechat) {
|
|
126
|
+
mb_setLocalData('trace', trace);
|
|
127
|
+
}
|
|
128
|
+
document.cookie = "trace=".concat(trace, "; path=/; domain=").concat(location.host.split('.').slice(1).join('.'));
|
|
129
|
+
}
|
|
130
|
+
var actCode = queryCode('actCode');
|
|
131
|
+
if (actCode) {
|
|
132
|
+
// 这个用来缓存口令的actCode避免所有页面都要透传
|
|
133
|
+
if (!window.actCode) {
|
|
134
|
+
sessionStorage.setItem('actCode', actCode);
|
|
135
|
+
window.actCode = actCode;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// 验签
|
|
139
|
+
if (queryCode('sIgN')) {
|
|
140
|
+
if (config.method === 'get') {
|
|
141
|
+
if (!config.params || Object.prototype.toString(config.params) !== '[object Object]') {
|
|
142
|
+
config.params = {};
|
|
143
|
+
}
|
|
144
|
+
config.params.sIgN = queryCode('sIgN');
|
|
145
|
+
} else {
|
|
146
|
+
if (!config.data || Object.prototype.toString(config.data) !== '[object Object]') {
|
|
147
|
+
config.data = {};
|
|
148
|
+
}
|
|
149
|
+
config.data.sIgN = queryCode('sIgN');
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (config.method === 'get') {
|
|
153
|
+
var _data = config.data,
|
|
154
|
+
params = config.params;
|
|
155
|
+
if (_data && !params) {
|
|
156
|
+
config.params = _data;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
var _config$headers = config.headers,
|
|
160
|
+
headers = _config$headers === void 0 ? {} : _config$headers,
|
|
161
|
+
data = config.data;
|
|
162
|
+
var reqDataType = options.reqDataType;
|
|
163
|
+
if (config.method === 'post') {
|
|
164
|
+
switch (reqDataType) {
|
|
165
|
+
case 'json':
|
|
166
|
+
if (options.jsonPrefix) {
|
|
167
|
+
config.data = JSON.stringify(_defineProperty({}, options.jsonPrefix, data));
|
|
168
|
+
} else {
|
|
169
|
+
config.data = JSON.stringify(data);
|
|
170
|
+
}
|
|
171
|
+
config.headers['Content-Type'] = 'application/json; charset=UTF-8';
|
|
172
|
+
break;
|
|
173
|
+
default:
|
|
174
|
+
if (!headers['Content-Type']) {
|
|
175
|
+
config.data = qs.stringify(data);
|
|
176
|
+
config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return config;
|
|
182
|
+
}
|
|
183
|
+
function RequestInterceptorError(e) {
|
|
184
|
+
return e;
|
|
185
|
+
}
|
|
186
|
+
var req = RequestInterceptor;
|
|
187
|
+
var reqErr = RequestInterceptorError;
|
|
188
|
+
|
|
189
|
+
export { req, reqErr };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import '../_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
|
+
import _typeof from '@babel/runtime/helpers/typeof';
|
|
4
|
+
import appInstance from './instance.js';
|
|
5
|
+
import optionsAdapter from './adapterAjax.js';
|
|
6
|
+
import cancelApi from './cancel.js';
|
|
7
|
+
|
|
8
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
9
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
10
|
+
/**
|
|
11
|
+
* cbk-request说明
|
|
12
|
+
* @params options.url 请求的地址
|
|
13
|
+
* @params options.data 请求数据
|
|
14
|
+
* @params options.async 是否同步(废弃)
|
|
15
|
+
* @params options.headers 自定义请求头
|
|
16
|
+
*/
|
|
17
|
+
if (!window.onceList) {
|
|
18
|
+
window.onceList = [];
|
|
19
|
+
document.addEventListener('visibilitychange', function () {
|
|
20
|
+
if (document.visibilityState === 'visible') {
|
|
21
|
+
window.onceList = [];
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function CBKRquest(options) {
|
|
26
|
+
// 适配当前ajax已有的功能
|
|
27
|
+
// resove cancel token
|
|
28
|
+
if (options.once) {
|
|
29
|
+
// 控制接口只执行一次
|
|
30
|
+
if (window.onceList.includes(options.url)) {
|
|
31
|
+
return new Promise(function () {}); // 返回一个pending状态的Promise,后面的catch和then都不会执行了
|
|
32
|
+
}
|
|
33
|
+
window.onceList.push(options.url);
|
|
34
|
+
}
|
|
35
|
+
cancelApi(options);
|
|
36
|
+
return optionsAdapter(options, appInstance, resolveOptins)["catch"](function (e) {
|
|
37
|
+
if (options.once) {
|
|
38
|
+
window.onceList = window.onceList.filter(function (i) {
|
|
39
|
+
return i !== options.url;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return Promise.reject(e);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function checkAutoParse(autoParse) {
|
|
46
|
+
if (!autoParse) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
if (Array.isArray(autoParse)) {
|
|
50
|
+
return autoParse.length > 0 ? autoParse : false;
|
|
51
|
+
}
|
|
52
|
+
switch (_typeof(autoParse)) {
|
|
53
|
+
case 'string':
|
|
54
|
+
return [autoParse];
|
|
55
|
+
case 'boolean':
|
|
56
|
+
return [];
|
|
57
|
+
default:
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function resolveOptins(config) {
|
|
62
|
+
// 自动截取参数
|
|
63
|
+
autoParseParams(config);
|
|
64
|
+
|
|
65
|
+
// // 处理
|
|
66
|
+
// resolveBI(config);
|
|
67
|
+
|
|
68
|
+
// // 测试验签
|
|
69
|
+
// resolveSign(config);
|
|
70
|
+
}
|
|
71
|
+
function autoParseParams(config) {
|
|
72
|
+
var checkResult = checkAutoParse(config.autoParse);
|
|
73
|
+
var params = location.href.split('?')[1];
|
|
74
|
+
if (!checkResult || !params) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
var allowAll = checkResult.length === 0; // 不代表传空数组就可以匹配全部
|
|
78
|
+
var dataFromUrl = params.split('&').filter(function (item) {
|
|
79
|
+
return item.indexOf('=') > 0;
|
|
80
|
+
}).reduce(function (result, item) {
|
|
81
|
+
var _split = item.split('=');
|
|
82
|
+
if (allowAll || checkResult.indexOf(_split[0]) >= 0) {
|
|
83
|
+
result[_split[0]] = _split[1];
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}, {});
|
|
87
|
+
if (config.method === 'post') {
|
|
88
|
+
// 如果是get 就没有必要截取了
|
|
89
|
+
config.data = _objectSpread(_objectSpread({}, dataFromUrl), config.data || {});
|
|
90
|
+
} else if (config.method === 'get') {
|
|
91
|
+
config.params = _objectSpread(_objectSpread({}, dataFromUrl), config.params || {});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export { CBKRquest as default };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import '../_virtual/_rollup-plugin-inject-process-env.js';
|
|
2
|
+
import { Loading } from '@deppon/deppon-ui';
|
|
3
|
+
import { login } from '@deppon/deppon-utils';
|
|
4
|
+
|
|
5
|
+
function ResponseInterceptor(res, options) {
|
|
6
|
+
var resCode = res.status;
|
|
7
|
+
var resData = res.data || {
|
|
8
|
+
message: '请求异常'
|
|
9
|
+
};
|
|
10
|
+
var resSuccess = resCode >= 200 && resCode < 300 || resCode === 304; // jq
|
|
11
|
+
// 控制loading的隐藏
|
|
12
|
+
if (!options.unload) {
|
|
13
|
+
Loading.end();
|
|
14
|
+
}
|
|
15
|
+
// 请求失败
|
|
16
|
+
var requestError = function requestError() {
|
|
17
|
+
return Promise.reject({
|
|
18
|
+
status: 'error',
|
|
19
|
+
// 这个表示整个请求都失败了
|
|
20
|
+
data: resData
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
if (resSuccess) {
|
|
24
|
+
/**
|
|
25
|
+
* 捕获code
|
|
26
|
+
* 如果传true的话会捕获所有code
|
|
27
|
+
* 传数组或者Number类型的code也可以捕获指定code
|
|
28
|
+
* 但凡传了options.catchCode,就一定会捕获,并且不走默认校验流程
|
|
29
|
+
*/
|
|
30
|
+
if (options.catchCode) {
|
|
31
|
+
if (typeof options.catchCode === 'boolean' || Array.isArray(options.catchCode) && options.catchCode.indexOf(resData.code) >= 0 || options.catchCode === resData.code) {
|
|
32
|
+
return Promise.resolve(resData);
|
|
33
|
+
}
|
|
34
|
+
return Promise.reject(resData);
|
|
35
|
+
}
|
|
36
|
+
// 如果传入了success回调,默认是使用了ajax,方便适配
|
|
37
|
+
// code 没有办法统一处理除了201
|
|
38
|
+
switch (resData.code) {
|
|
39
|
+
case 201:
|
|
40
|
+
login();
|
|
41
|
+
return Promise.reject({
|
|
42
|
+
status: 'success',
|
|
43
|
+
// 这个表示请求成功了,但是返回信息失败了 因为在success回调里面需要在请求发送成功之后知道异常的code和data来处理信息
|
|
44
|
+
data: resData
|
|
45
|
+
});
|
|
46
|
+
case 200:
|
|
47
|
+
return Promise.resolve(resData);
|
|
48
|
+
default:
|
|
49
|
+
return Promise.reject({
|
|
50
|
+
status: 'success',
|
|
51
|
+
// 这个表示请求成功了,但是返回信息失败了 因为在success回调里面需要在请求发送成功之后知道异常的code和data来处理信息
|
|
52
|
+
data: resData
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
return requestError();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function ResponseInterceptorError(res, options) {
|
|
60
|
+
if (!options.unload) {
|
|
61
|
+
Loading.end();
|
|
62
|
+
}
|
|
63
|
+
return Promise.reject(res);
|
|
64
|
+
}
|
|
65
|
+
var res = ResponseInterceptor;
|
|
66
|
+
var resErr = ResponseInterceptorError;
|
|
67
|
+
|
|
68
|
+
export { res, resErr };
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@deppon/deppon-request",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "德邦前端 http 请求包",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"request",
|
|
9
|
+
"react-library"
|
|
10
|
+
],
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "",
|
|
13
|
+
"email": ""
|
|
14
|
+
},
|
|
15
|
+
"main": "es/index.js",
|
|
16
|
+
"module": "es/index.js",
|
|
17
|
+
"typings": "es/index.d.ts",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "ts-node build.ts"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public",
|
|
24
|
+
"registry": "https://registry.npmjs.org"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"es"
|
|
28
|
+
],
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": ""
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@babel/runtime": "^7.17.7",
|
|
35
|
+
"@deppon/deppon-bridge": "1.0.3",
|
|
36
|
+
"@deppon/deppon-ui": "1.0.3",
|
|
37
|
+
"@deppon/deppon-utils": "1.0.3",
|
|
38
|
+
"axios": "^0.26.0",
|
|
39
|
+
"md5": "^2.3.0",
|
|
40
|
+
"qs": "^6.10.3"
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "f885b11aa852bb3d9c3f23e3d42649ba0c695b20"
|
|
43
|
+
}
|