Skip to content

Commit 85f7e11

Browse files
committed
feat: 区分钉钉小程序和支付宝小程序
1 parent a43aa34 commit 85f7e11

File tree

8 files changed

+130
-99
lines changed

8 files changed

+130
-99
lines changed

dist/index.aio.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
2-
* axios-miniprogram-adapter 0.3.1 (https://github.com/bigMeow/axios-miniprogram-adapter)
2+
* axios-miniprogram-adapter 1.0.0 (https://github.com/bigMeow/axios-miniprogram-adapter)
33
* API https://github.com/bigMeow/axios-miniprogram-adapter/blob/master/doc/api.md
4-
* Copyright 2018-2020 bigMeow. All Rights Reserved
4+
* Copyright 2018-2022 bigMeow. All Rights Reserved
55
* Licensed under MIT (https://github.com/bigMeow/axios-miniprogram-adapter/blob/master/LICENSE)
66
*/
77

@@ -43,28 +43,31 @@
4343
return output;
4444
}
4545

46-
var platFormName = 'wechat';
46+
var platFormName = "wechat" /* 微信 */;
4747
/**
4848
* 获取各个平台的请求函数
4949
*/
5050
function getRequest() {
5151
switch (true) {
5252
case typeof wx === 'object':
53-
platFormName = 'wechat';
53+
platFormName = "wechat" /* 微信 */;
5454
return wx.request.bind(wx);
5555
case typeof swan === 'object':
56-
platFormName = 'baidu';
56+
platFormName = "baidu" /* 百度 */;
5757
return swan.request.bind(swan);
58+
case typeof dd === 'object':
59+
platFormName = "dd" /* 钉钉 */;
60+
// https://open.dingtalk.com/document/orgapp-client/send-network-requests
61+
return dd.httpRequest.bind(dd);
5862
case typeof my === 'object':
5963
/**
6064
* remark:
6165
* 支付宝客户端已不再维护 my.httpRequest,建议使用 my.request。另外,钉钉客户端尚不支持 my.request。若在钉钉客户端开发小程序,则需要使用 my.httpRequest。
6266
* my.httpRequest的请求头默认值为{'content-type': 'application/x-www-form-urlencoded'}。
6367
* my.request的请求头默认值为{'content-type': 'application/json'}。
64-
* TODO: 区分支付宝和钉钉环境
65-
* 还有个 dd.httpRequest WFK!!! https://ding-doc.dingtalk.com/doc#/dev/httprequest
68+
* 还有个 dd.httpRequest
6669
*/
67-
platFormName = 'alipay';
70+
platFormName = "alipay" /* 支付宝 */;
6871
return (my.request || my.httpRequest).bind(my);
6972
default:
7073
return wx.request.bind(wx);
@@ -104,7 +107,7 @@
104107
*/
105108
function transformError(error, reject, config) {
106109
switch (platFormName) {
107-
case 'wechat':
110+
case "wechat" /* 微信 */:
108111
if (error.errMsg.indexOf('request:fail abort') !== -1) {
109112
// Handle request cancellation (as opposed to a manual cancellation)
110113
reject(createError('Request aborted', config, 'ECONNABORTED', ''));
@@ -118,21 +121,22 @@
118121
reject(createError('Network Error', config, null, ''));
119122
}
120123
break;
121-
case 'alipay':
124+
case "dd" /* 钉钉 */:
125+
case "alipay" /* 支付宝 */:
122126
// https://docs.alipay.com/mini/api/network
123127
if ([14, 19].includes(error.error)) {
124-
reject(createError('Request aborted', config, 'ECONNABORTED', ''));
128+
reject(createError('Request aborted', config, 'ECONNABORTED', '', error));
125129
}
126130
else if ([13].includes(error.error)) {
127131
// timeout
128-
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', ''));
132+
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', '', error));
129133
}
130134
else {
131135
// NetWordError
132-
reject(createError('Network Error', config, null, ''));
136+
reject(createError('Network Error', config, null, '', error));
133137
}
134138
break;
135-
case 'baidu':
139+
case "baidu" /* 百度 */:
136140
// TODO error.errCode
137141
reject(createError('Network Error', config, null, ''));
138142
break;
@@ -143,14 +147,18 @@
143147
* @param config
144148
*/
145149
function transformConfig(config) {
146-
if (platFormName === 'alipay') {
150+
var _a;
151+
if (["alipay" /* 支付宝 */, "dd" /* 钉钉 */].includes(platFormName)) {
147152
config.headers = config.header;
148153
delete config.header;
154+
if ("dd" /* 钉钉 */ === platFormName && ((_a = config.headers) === null || _a === void 0 ? void 0 : _a["Content-Type"]) === "application/json" && Object.prototype.toString.call(config.data) === '[object Object]') {
155+
// Content-Type为application/json时,data参数只支持json字符串,需要手动调用JSON.stringify进行序列化
156+
config.data = JSON.stringify(config.data);
157+
}
149158
}
150159
return config;
151160
}
152161

153-
var warn = console.warn;
154162
var isJSONstr = function (str) {
155163
try {
156164
return typeof str === 'string' && str.length && (str = JSON.parse(str)) && Object.prototype.toString.call(str) === '[object Object]';
@@ -171,6 +179,7 @@
171179
var mpRequestOption = {
172180
method: requestMethod,
173181
url: buildURL(buildFullPath(config.baseURL, config.url), config.params, config.paramsSerializer),
182+
timeout: config.timeout,
174183
// Listen for success
175184
success: function (mpResponse) {
176185
var response = transformResponse(mpResponse, config, mpRequestOption);
@@ -189,10 +198,6 @@
189198
var _a = [config.auth.username || '', config.auth.password || ''], username = _a[0], password = _a[1];
190199
requestHeaders.Authorization = 'Basic ' + encoder(username + ':' + password);
191200
}
192-
// Set the request timeout
193-
if (config.timeout !== 0) {
194-
warn('The "timeout" option is not supported by miniprogram. For more information about usage see "https://developers.weixin.qq.com/miniprogram/dev/framework/config.html#全局配置"');
195-
}
196201
// Add headers to the request
197202
utils.forEach(requestHeaders, function setRequestHeader(val, key) {
198203
var _header = key.toLowerCase();

dist/index.esm.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
2-
* axios-miniprogram-adapter 0.3.1 (https://github.com/bigMeow/axios-miniprogram-adapter)
2+
* axios-miniprogram-adapter 1.0.0 (https://github.com/bigMeow/axios-miniprogram-adapter)
33
* API https://github.com/bigMeow/axios-miniprogram-adapter/blob/master/doc/api.md
4-
* Copyright 2018-2020 bigMeow. All Rights Reserved
4+
* Copyright 2018-2022 bigMeow. All Rights Reserved
55
* Licensed under MIT (https://github.com/bigMeow/axios-miniprogram-adapter/blob/master/LICENSE)
66
*/
77

@@ -37,28 +37,31 @@ function encoder(input) {
3737
return output;
3838
}
3939

40-
var platFormName = 'wechat';
40+
var platFormName = "wechat" /* 微信 */;
4141
/**
4242
* 获取各个平台的请求函数
4343
*/
4444
function getRequest() {
4545
switch (true) {
4646
case typeof wx === 'object':
47-
platFormName = 'wechat';
47+
platFormName = "wechat" /* 微信 */;
4848
return wx.request.bind(wx);
4949
case typeof swan === 'object':
50-
platFormName = 'baidu';
50+
platFormName = "baidu" /* 百度 */;
5151
return swan.request.bind(swan);
52+
case typeof dd === 'object':
53+
platFormName = "dd" /* 钉钉 */;
54+
// https://open.dingtalk.com/document/orgapp-client/send-network-requests
55+
return dd.httpRequest.bind(dd);
5256
case typeof my === 'object':
5357
/**
5458
* remark:
5559
* 支付宝客户端已不再维护 my.httpRequest,建议使用 my.request。另外,钉钉客户端尚不支持 my.request。若在钉钉客户端开发小程序,则需要使用 my.httpRequest。
5660
* my.httpRequest的请求头默认值为{'content-type': 'application/x-www-form-urlencoded'}。
5761
* my.request的请求头默认值为{'content-type': 'application/json'}。
58-
* TODO: 区分支付宝和钉钉环境
59-
* 还有个 dd.httpRequest WFK!!! https://ding-doc.dingtalk.com/doc#/dev/httprequest
62+
* 还有个 dd.httpRequest
6063
*/
61-
platFormName = 'alipay';
64+
platFormName = "alipay" /* 支付宝 */;
6265
return (my.request || my.httpRequest).bind(my);
6366
default:
6467
return wx.request.bind(wx);
@@ -98,7 +101,7 @@ function transformResponse(mpResponse, config, mpRequestOption) {
98101
*/
99102
function transformError(error, reject, config) {
100103
switch (platFormName) {
101-
case 'wechat':
104+
case "wechat" /* 微信 */:
102105
if (error.errMsg.indexOf('request:fail abort') !== -1) {
103106
// Handle request cancellation (as opposed to a manual cancellation)
104107
reject(createError('Request aborted', config, 'ECONNABORTED', ''));
@@ -112,21 +115,22 @@ function transformError(error, reject, config) {
112115
reject(createError('Network Error', config, null, ''));
113116
}
114117
break;
115-
case 'alipay':
118+
case "dd" /* 钉钉 */:
119+
case "alipay" /* 支付宝 */:
116120
// https://docs.alipay.com/mini/api/network
117121
if ([14, 19].includes(error.error)) {
118-
reject(createError('Request aborted', config, 'ECONNABORTED', ''));
122+
reject(createError('Request aborted', config, 'ECONNABORTED', '', error));
119123
}
120124
else if ([13].includes(error.error)) {
121125
// timeout
122-
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', ''));
126+
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', '', error));
123127
}
124128
else {
125129
// NetWordError
126-
reject(createError('Network Error', config, null, ''));
130+
reject(createError('Network Error', config, null, '', error));
127131
}
128132
break;
129-
case 'baidu':
133+
case "baidu" /* 百度 */:
130134
// TODO error.errCode
131135
reject(createError('Network Error', config, null, ''));
132136
break;
@@ -137,14 +141,18 @@ function transformError(error, reject, config) {
137141
* @param config
138142
*/
139143
function transformConfig(config) {
140-
if (platFormName === 'alipay') {
144+
var _a;
145+
if (["alipay" /* 支付宝 */, "dd" /* 钉钉 */].includes(platFormName)) {
141146
config.headers = config.header;
142147
delete config.header;
148+
if ("dd" /* 钉钉 */ === platFormName && ((_a = config.headers) === null || _a === void 0 ? void 0 : _a["Content-Type"]) === "application/json" && Object.prototype.toString.call(config.data) === '[object Object]') {
149+
// Content-Type为application/json时,data参数只支持json字符串,需要手动调用JSON.stringify进行序列化
150+
config.data = JSON.stringify(config.data);
151+
}
143152
}
144153
return config;
145154
}
146155

147-
var warn = console.warn;
148156
var isJSONstr = function (str) {
149157
try {
150158
return typeof str === 'string' && str.length && (str = JSON.parse(str)) && Object.prototype.toString.call(str) === '[object Object]';
@@ -165,6 +173,7 @@ function mpAdapter(config) {
165173
var mpRequestOption = {
166174
method: requestMethod,
167175
url: buildURL(buildFullPath(config.baseURL, config.url), config.params, config.paramsSerializer),
176+
timeout: config.timeout,
168177
// Listen for success
169178
success: function (mpResponse) {
170179
var response = transformResponse(mpResponse, config, mpRequestOption);
@@ -183,10 +192,6 @@ function mpAdapter(config) {
183192
var _a = [config.auth.username || '', config.auth.password || ''], username = _a[0], password = _a[1];
184193
requestHeaders.Authorization = 'Basic ' + encoder(username + ':' + password);
185194
}
186-
// Set the request timeout
187-
if (config.timeout !== 0) {
188-
warn('The "timeout" option is not supported by miniprogram. For more information about usage see "https://developers.weixin.qq.com/miniprogram/dev/framework/config.html#全局配置"');
189-
}
190195
// Add headers to the request
191196
utils.forEach(requestHeaders, function setRequestHeader(val, key) {
192197
var _header = key.toLowerCase();

dist/index.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
2-
* axios-miniprogram-adapter 0.3.1 (https://github.com/bigMeow/axios-miniprogram-adapter)
2+
* axios-miniprogram-adapter 1.0.0 (https://github.com/bigMeow/axios-miniprogram-adapter)
33
* API https://github.com/bigMeow/axios-miniprogram-adapter/blob/master/doc/api.md
4-
* Copyright 2018-2020 bigMeow. All Rights Reserved
4+
* Copyright 2018-2022 bigMeow. All Rights Reserved
55
* Licensed under MIT (https://github.com/bigMeow/axios-miniprogram-adapter/blob/master/LICENSE)
66
*/
77

@@ -41,28 +41,31 @@ function encoder(input) {
4141
return output;
4242
}
4343

44-
var platFormName = 'wechat';
44+
var platFormName = "wechat" /* 微信 */;
4545
/**
4646
* 获取各个平台的请求函数
4747
*/
4848
function getRequest() {
4949
switch (true) {
5050
case typeof wx === 'object':
51-
platFormName = 'wechat';
51+
platFormName = "wechat" /* 微信 */;
5252
return wx.request.bind(wx);
5353
case typeof swan === 'object':
54-
platFormName = 'baidu';
54+
platFormName = "baidu" /* 百度 */;
5555
return swan.request.bind(swan);
56+
case typeof dd === 'object':
57+
platFormName = "dd" /* 钉钉 */;
58+
// https://open.dingtalk.com/document/orgapp-client/send-network-requests
59+
return dd.httpRequest.bind(dd);
5660
case typeof my === 'object':
5761
/**
5862
* remark:
5963
* 支付宝客户端已不再维护 my.httpRequest,建议使用 my.request。另外,钉钉客户端尚不支持 my.request。若在钉钉客户端开发小程序,则需要使用 my.httpRequest。
6064
* my.httpRequest的请求头默认值为{'content-type': 'application/x-www-form-urlencoded'}。
6165
* my.request的请求头默认值为{'content-type': 'application/json'}。
62-
* TODO: 区分支付宝和钉钉环境
63-
* 还有个 dd.httpRequest WFK!!! https://ding-doc.dingtalk.com/doc#/dev/httprequest
66+
* 还有个 dd.httpRequest
6467
*/
65-
platFormName = 'alipay';
68+
platFormName = "alipay" /* 支付宝 */;
6669
return (my.request || my.httpRequest).bind(my);
6770
default:
6871
return wx.request.bind(wx);
@@ -102,7 +105,7 @@ function transformResponse(mpResponse, config, mpRequestOption) {
102105
*/
103106
function transformError(error, reject, config) {
104107
switch (platFormName) {
105-
case 'wechat':
108+
case "wechat" /* 微信 */:
106109
if (error.errMsg.indexOf('request:fail abort') !== -1) {
107110
// Handle request cancellation (as opposed to a manual cancellation)
108111
reject(createError('Request aborted', config, 'ECONNABORTED', ''));
@@ -116,21 +119,22 @@ function transformError(error, reject, config) {
116119
reject(createError('Network Error', config, null, ''));
117120
}
118121
break;
119-
case 'alipay':
122+
case "dd" /* 钉钉 */:
123+
case "alipay" /* 支付宝 */:
120124
// https://docs.alipay.com/mini/api/network
121125
if ([14, 19].includes(error.error)) {
122-
reject(createError('Request aborted', config, 'ECONNABORTED', ''));
126+
reject(createError('Request aborted', config, 'ECONNABORTED', '', error));
123127
}
124128
else if ([13].includes(error.error)) {
125129
// timeout
126-
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', ''));
130+
reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', '', error));
127131
}
128132
else {
129133
// NetWordError
130-
reject(createError('Network Error', config, null, ''));
134+
reject(createError('Network Error', config, null, '', error));
131135
}
132136
break;
133-
case 'baidu':
137+
case "baidu" /* 百度 */:
134138
// TODO error.errCode
135139
reject(createError('Network Error', config, null, ''));
136140
break;
@@ -141,14 +145,18 @@ function transformError(error, reject, config) {
141145
* @param config
142146
*/
143147
function transformConfig(config) {
144-
if (platFormName === 'alipay') {
148+
var _a;
149+
if (["alipay" /* 支付宝 */, "dd" /* 钉钉 */].includes(platFormName)) {
145150
config.headers = config.header;
146151
delete config.header;
152+
if ("dd" /* 钉钉 */ === platFormName && ((_a = config.headers) === null || _a === void 0 ? void 0 : _a["Content-Type"]) === "application/json" && Object.prototype.toString.call(config.data) === '[object Object]') {
153+
// Content-Type为application/json时,data参数只支持json字符串,需要手动调用JSON.stringify进行序列化
154+
config.data = JSON.stringify(config.data);
155+
}
147156
}
148157
return config;
149158
}
150159

151-
var warn = console.warn;
152160
var isJSONstr = function (str) {
153161
try {
154162
return typeof str === 'string' && str.length && (str = JSON.parse(str)) && Object.prototype.toString.call(str) === '[object Object]';
@@ -169,6 +177,7 @@ function mpAdapter(config) {
169177
var mpRequestOption = {
170178
method: requestMethod,
171179
url: buildURL(buildFullPath(config.baseURL, config.url), config.params, config.paramsSerializer),
180+
timeout: config.timeout,
172181
// Listen for success
173182
success: function (mpResponse) {
174183
var response = transformResponse(mpResponse, config, mpRequestOption);
@@ -187,10 +196,6 @@ function mpAdapter(config) {
187196
var _a = [config.auth.username || '', config.auth.password || ''], username = _a[0], password = _a[1];
188197
requestHeaders.Authorization = 'Basic ' + encoder(username + ':' + password);
189198
}
190-
// Set the request timeout
191-
if (config.timeout !== 0) {
192-
warn('The "timeout" option is not supported by miniprogram. For more information about usage see "https://developers.weixin.qq.com/miniprogram/dev/framework/config.html#全局配置"');
193-
}
194199
// Add headers to the request
195200
utils.forEach(requestHeaders, function setRequestHeader(val, key) {
196201
var _header = key.toLowerCase();

0 commit comments

Comments
 (0)