Skip to content
This repository was archived by the owner on Aug 15, 2025. It is now read-only.

Commit 37246f2

Browse files
author
jonnyshao
committed
fixed the header config bug of DingDing Mini-Program
1 parent 37c76a6 commit 37246f2

6 files changed

Lines changed: 123 additions & 116 deletions

File tree

exmaple/api/wf.js

Lines changed: 112 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/*
22
Promise based wx.request api for Mini Program
33
@Github https://github.com/jonnyshao/wechat-fetch
4-
wefetch beta v1.2.10 |(c) 2018-2019 By Jonny Shao
4+
wefetch beta v1.3.01 |(c) 2018-2019 By Jonny Shao
55
*/
66
(function (global, factory) {
77
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
8-
typeof define === 'function' && define.amd ? define(factory) :
9-
(global = global || self, global.wefetch = factory());
10-
}(this, function () { 'use strict';
8+
typeof define === 'function' && define.amd ? define(factory) :
9+
(global = global || self, global.wefetch = factory());
10+
}(this, function () {
11+
'use strict';
1112

1213
function Events() {
1314
this.listeners = {};
@@ -31,16 +32,16 @@
3132

3233
var e = new Events();
3334

34-
function promisify (api) {
35-
return function (options) {
35+
function promisify(api) {
36+
return function (options) {
3637
options = options || {};
3738
options.config = options.config || {};
3839
for (var len = arguments.length, params = Array(len > 1 ? len - 1 : 0), key = 1; key < len; key++) {
3940
params[key - 1] = arguments[key];
4041
}
4142
return new Promise(function (resolve, reject) {
42-
options.config.eventType ? e.emit(options.config.eventType, api.apply(undefined, [Object.assign({}, options, {success: resolve, fail: reject})].concat(params)))
43-
: api.apply(undefined, [Object.assign({}, options, {success: resolve, fail: reject})].concat(params));
43+
options.config.eventType ? e.emit(options.config.eventType, api.apply(undefined, [Object.assign({}, options, { success: resolve, fail: reject })].concat(params)))
44+
: api.apply(undefined, [Object.assign({}, options, { success: resolve, fail: reject })].concat(params));
4445
})
4546
};
4647
}
@@ -57,38 +58,38 @@
5758
} catch (e) {
5859
try {
5960
if (my.request) {
60-
this.platform = 'my';
61-
return promisify(my.request)
62-
} else if (my.httpRequest){
63-
this.platform = 'my';
64-
return promisify(my.httpRequest)
61+
this.platform = 'my';
62+
return promisify(my.request)
63+
} else if (my.httpRequest) {
64+
this.platform = 'my';
65+
return promisify(my.httpRequest)
6566
}
66-
}catch (e) {
67-
try{
68-
if (tt.request) {
69-
this.platform = 'tt';
70-
return promisify(tt.request)
71-
}
72-
}catch (e) {
73-
if (swan.request) {
74-
this.platform = 'swan';
75-
return promisify(swan.request)
76-
}
67+
} catch (e) {
68+
try {
69+
if (tt.request) {
70+
this.platform = 'tt';
71+
return promisify(tt.request)
72+
}
73+
} catch (e) {
74+
if (swan.request) {
75+
this.platform = 'swan';
76+
return promisify(swan.request)
77+
}
7778
}
7879
}
7980
}
8081
};
8182
Platform.prototype.getUpload = function () {
82-
return promisify(this.getPlatform().uploadFile);
83+
return promisify(this.getPlatform().uploadFile);
8384
};
8485
Platform.prototype.getDownload = function () {
85-
return promisify(this.getPlatform().downloadFile);
86+
return promisify(this.getPlatform().downloadFile);
8687
};
8788
Platform.prototype.getPlatform = function () {
88-
if (this.platform === 'wx')return wx;
89-
if (this.platform === 'my')return my;
90-
if (this.platform === 'swan')return swan;
91-
if (this.platform === 'tt')return tt;
89+
if (this.platform === 'wx') return wx;
90+
if (this.platform === 'my') return my;
91+
if (this.platform === 'swan') return swan;
92+
if (this.platform === 'tt') return tt;
9293
};
9394
var platform = new Platform();
9495

@@ -101,7 +102,7 @@
101102
};
102103

103104
function bind(fn, context) {
104-
return function wf (){
105+
return function wf() {
105106
var args = new Array(arguments.length);
106107
for (var i = 0, l = args.length; i < l; i++) {
107108
args[i] = arguments[i];
@@ -118,7 +119,7 @@
118119
for (var i = 0, len = typeAry.length; i < len; i++) {
119120
(function (name) {
120121
type['is' + name] = function (obj) {
121-
return tostring.call(obj) === '[object' +' '+ name + ']';
122+
return tostring.call(obj) === '[object' + ' ' + name + ']';
122123
};
123124
})(typeAry[i]);
124125
}
@@ -157,48 +158,48 @@
157158
}
158159
return result;
159160
},
160-
deepMerge: function() {
161+
deepMerge: function () {
161162
var result = {};
162-
function assignValue(val, key) {
163-
if (typeof result[key] === 'object' && typeof val === 'object') {
164-
result[key] = utils.deepMerge(result[key], val);
165-
} else if (typeof val === 'object'){
166-
result[key] = utils.deepMerge({},val);
167-
} else {
168-
result[key] = val;
169-
}
170-
}
171-
for (var i = 0, l = arguments.length; i < l; i++) {
172-
this.forEach(arguments[i], assignValue);
163+
function assignValue(val, key) {
164+
if (typeof result[key] === 'object' && typeof val === 'object') {
165+
result[key] = utils.deepMerge(result[key], val);
166+
} else if (typeof val === 'object') {
167+
result[key] = utils.deepMerge({}, val);
168+
} else {
169+
result[key] = val;
173170
}
174-
return result;
171+
}
172+
for (var i = 0, l = arguments.length; i < l; i++) {
173+
this.forEach(arguments[i], assignValue);
174+
}
175+
return result;
175176
},
176-
mergeConfig: function(source,target){
177-
var c = {}; target = target || {};
178-
['url','method', 'data','config'].forEach(function (prop) {
179-
if (target[prop]){
177+
mergeConfig: function (source, target) {
178+
var c = {}; target = target || {};
179+
['url', 'method', 'data', 'config'].forEach(function (prop) {
180+
if (target[prop]) {
180181
c[prop] = target[prop];
181182
}
182183
});
183-
['header'].forEach(function (prop) {
184-
if (utils.type.isObject(target[prop])){
185-
c[prop] = utils.deepMerge(source[prop],target[prop]);
186-
} else if(target[prop]){
184+
['header', 'headers'].forEach(function (prop) {
185+
if (utils.type.isObject(target[prop])) {
186+
c[prop] = utils.deepMerge(source[prop], target[prop]);
187+
} else if (target[prop]) {
187188
c[prop] = target[prop];
188-
} else if(utils.type.isObject(source[prop])){
189+
} else if (utils.type.isObject(source[prop])) {
189190
c[prop] = utils.deepMerge(source[prop]);
190-
} else if (source[prop]){
191+
} else if (source[prop]) {
191192
c[prop] = source[prop];
192193
}
193194
});
194-
['baseUrl','timeout','eventType','createRequest'].forEach(function (prop) {
195-
if (target[prop] !== undefined){
195+
['baseUrl', 'timeout', 'eventType', 'createRequest'].forEach(function (prop) {
196+
if (target[prop] !== undefined) {
196197
c[prop] = target[prop];
197-
} else if (source[prop] !== undefined){
198+
} else if (source[prop] !== undefined) {
198199
c[prop] = source[prop];
199200
}
200201
});
201-
return c
202+
return c
202203
},
203204
extends: function (extendObj, copyObj, thisArg) {
204205
this.forEach(copyObj, function (val, key) {
@@ -235,17 +236,13 @@
235236
};
236237

237238
function dispatchRequest(config) {
238-
if (platform.platform === 'my' && config.method !== 'download' && config.method !== 'upload') {
239-
config.headers = config.header;
240-
delete config.header;
241-
}
242239
if (config.method === 'download') {
243-
config.method = 'get';
244-
config.createRequest = platform.getDownload();
240+
config.method = 'get';
241+
config.createRequest = platform.getDownload();
245242
}
246-
if (config.method === 'upload'){
247-
config.method = 'post';
248-
config.createRequest = platform.getUpload();
243+
if (config.method === 'upload') {
244+
config.method = 'post';
245+
config.createRequest = platform.getUpload();
249246
}
250247
var request = config.createRequest;
251248
return request(config).then(function (response) {
@@ -255,20 +252,26 @@
255252
})
256253
}
257254

258-
function request (config) {
255+
function request(config) {
259256
if (typeof config === 'string') {
260-
config = arguments[1] || {};
261-
config.url = arguments[0];
257+
config = arguments[1] || {};
258+
config.url = arguments[0];
262259
}
263260
config = utils.mergeConfig(this.defaults, config);
264261
if (config.url.indexOf('http') === -1) {
265-
if (config.downloadUrl && config.method === 'download') {
266-
config.url = config.downloadUrl + config.url;
267-
} else if (config.uploadUrl && config.method === 'upload') {
268-
config.url = config.uploadUrl + config.url;
269-
} else { //(config.baseUrl)
270-
config.url = config.baseUrl + config.url;
271-
}
262+
if (config.downloadUrl && config.method === 'download') {
263+
config.url = config.downloadUrl + config.url;
264+
} else if (config.uploadUrl && config.method === 'upload') {
265+
config.url = config.uploadUrl + config.url;
266+
} else { //(config.baseUrl)
267+
config.url = config.baseUrl + config.url;
268+
}
269+
}
270+
if (platform.platform === 'my') {
271+
if (config.method !== 'download' && config.method !== 'upload') {
272+
config.headers = Object.assign({}, config.header);
273+
delete config.header;
274+
}
272275
}
273276
var chain = [dispatchRequest, undefined];
274277
var promise = Promise.resolve(config);
@@ -295,10 +298,10 @@
295298
WeFetch.prototype.download = function (url, config) {
296299
// wf.download({}) support
297300
if (utils.type.isObject(url)) {
298-
return this.request(utils.merge(url,{ method: 'download' }))
301+
return this.request(utils.merge(url, { method: 'download' }))
299302
}
300303
// default
301-
return this.request(utils.merge(config || {},{
304+
return this.request(utils.merge(config || {}, {
302305
url: url,
303306
method: 'download'
304307
}))
@@ -307,7 +310,7 @@
307310
WeFetch.prototype.upload = function (url, config) {
308311
// upload({}) support
309312
if (utils.type.isObject(url)) {
310-
return this.request(url,{ method: 'upload' })
313+
return this.request(url, { method: 'upload' })
311314
}
312315
return this.request(utils.merge(config || {}, {
313316
url: url,
@@ -316,44 +319,44 @@
316319
};
317320

318321
function WeFetch(instanceConfig) {
319-
this.defaults = instanceConfig;
320-
this.before = new InterceptorManager();
321-
this.after = new InterceptorManager();
322+
this.defaults = instanceConfig;
323+
this.before = new InterceptorManager();
324+
this.after = new InterceptorManager();
322325
}
323326

324327
WeFetch.prototype.on = function (event, cb) {
325-
e.on(event, cb);
328+
e.on(event, cb);
326329
};
327330
WeFetch.prototype.abort = function (event, cb) {
328-
this.on(event, function (t) {
329-
t.abort();
330-
cb && cb();
331-
});
331+
this.on(event, function (t) {
332+
t.abort();
333+
cb && cb();
334+
});
332335
};
333336
WeFetch.prototype.onProcess = function (event, cb) {
334-
this.on(event, function (t) {
335-
t.onProgressUpdate(cb);
336-
});
337+
this.on(event, function (t) {
338+
t.onProgressUpdate(cb);
339+
});
337340
};
338341
WeFetch.prototype.promisify = promisify;
339342
WeFetch.prototype.request = request;
340343

341-
function retry(times,request,timeout) {
342-
timeout = timeout || 1000;
343-
if (!times && times !== 0 || !request)throw new Error('request and times params is required');
344-
if (typeof request !== 'function') throw new Error('request must be a function, but got a\n'+ typeof request);
345-
var p = request();
346-
if (times > 1) {
347-
times --;
348-
return new Promise(function(resolve, reject) {
349-
p.then(resolve).catch(function () {
350-
setTimeout(function() {
351-
resolve(retry(times, request, timeout));
352-
}, timeout);
353-
});
354-
})
355-
}
356-
return p;
344+
function retry(times, request, timeout) {
345+
timeout = timeout || 1000;
346+
if (!times && times !== 0 || !request) throw new Error('request and times params is required');
347+
if (typeof request !== 'function') throw new Error('request must be a function, but got a\n' + typeof request);
348+
var p = request();
349+
if (times > 1) {
350+
times--;
351+
return new Promise(function (resolve, reject) {
352+
p.then(resolve).catch(function () {
353+
setTimeout(function () {
354+
resolve(retry(times, request, timeout));
355+
}, timeout);
356+
});
357+
})
358+
}
359+
return p;
357360
}
358361

359362
Promise.prototype.finally = function (cb) {
@@ -369,7 +372,7 @@
369372
})
370373
};
371374

372-
function createInstance (defaultConfig){
375+
function createInstance(defaultConfig) {
373376
var context = new WeFetch(defaultConfig);
374377
var instance = bind(WeFetch.prototype.request, context);
375378
utils.extends(instance, WeFetch.prototype, context);

src/lib/core/dispatchRequest.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import platform from "./platform";
22

33
function dispatchRequest(config) {
4-
if (platform.platform === 'my' && config.method !== 'download' && config.method !== 'upload') {
5-
config.headers = config.header;
6-
delete config.header
7-
}
84
if (config.method === 'download') {
95
config.method = 'get';
106
config.createRequest = platform.getDownload()

src/lib/core/request.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import utils from "../utils";
2-
32
import dispatchRequest from "./dispatchRequest";
3+
import platform from "./platform";
44
function request (config) {
55
if (typeof config === 'string') {
66
config = arguments[1] || {};
@@ -16,6 +16,13 @@ function request (config) {
1616
config.url = config.baseUrl + config.url
1717
}
1818
}
19+
// 阿里系的header是复数
20+
if (platform.platform === 'my' ) {
21+
if (config.method !== 'download' && config.method !== 'upload') {
22+
config.headers = Object.assign({}, config.header);
23+
delete config.header
24+
}
25+
}
1926
var chain = [dispatchRequest, undefined];
2027
var promise = Promise.resolve(config);
2128
this.before.forEach(function (interceptor) {

src/lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var utils = {
6969
c[prop] = target[prop]
7070
}
7171
});
72-
['header'].forEach(function (prop) {
72+
['header','headers'].forEach(function (prop) {
7373
if (utils.type.isObject(target[prop])){
7474
c[prop] = utils.deepMerge(source[prop],target[prop])
7575
} else if(target[prop]){

0 commit comments

Comments
 (0)