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

Commit bc5702f

Browse files
author
jonnyshao
committed
fixed alipay mini-program of bug
1 parent 6301b32 commit bc5702f

12 files changed

Lines changed: 117 additions & 66 deletions

File tree

exmaple/package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exmaple/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13-
"wefetch": "^1.2.5"
13+
"wefetch": "^1.2.6"
1414
}
1515
}

src/lib/core/addMethods.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import utils from "../utils";
22
import WeFetch from "./Wefetch";
33
import {UPLOAD_CONTENT_TYPE, DOWNLOAD_CONTENT_TYPE} from '../defaults'
44
import platform from "./platform";
5-
5+
import promisify from "./promisify";
66
['options', 'get', 'head', 'post', 'put', 'delete', 'trace', 'connect', 'postJson'].forEach(function (method) {
77
WeFetch.prototype[method] = function (url, config) {
88
return this.request(utils.merge(config || {}, {
@@ -14,8 +14,6 @@ import platform from "./platform";
1414
WeFetch.prototype.download = function (url, config) {
1515
// init
1616
config = config || {};
17-
config.createRequest = platform.getDownload();
18-
1917
// check user is input header param
2018
if (config.header) {
2119
config.header['Content-Type'] = config.header['Content-Type'] || DOWNLOAD_CONTENT_TYPE
@@ -25,18 +23,18 @@ WeFetch.prototype.download = function (url, config) {
2523

2624
// wf.download({}) support
2725
if (utils.type.isObject(url)) {
28-
return this.request(utils.merge(config, url))
26+
return this.request(utils.merge(config, url,{ method: 'download' }))
2927
}
3028
// default
3129
return this.request(utils.merge(config,{
32-
url: url
30+
url: url,
31+
method: 'download'
3332
}))
3433
};
3534

3635
WeFetch.prototype.upload = function (url, config) {
3736
// init
3837
config = config || {};
39-
config.createRequest = platform.getUpload();
4038
// check user is input header param
4139
if (config.header) {
4240
config.header['Content-Type'] = config.header['Content-Type'] || UPLOAD_CONTENT_TYPE;
@@ -46,9 +44,13 @@ WeFetch.prototype.upload = function (url, config) {
4644

4745
// upload({}) support
4846
if (utils.type.isObject(url)) {
49-
return this.request(config, url)
47+
return this.request(config, url,{ method: 'upload' })
5048
}
5149
return this.request(utils.merge(config, {
52-
url: url
50+
url: url,
51+
method: 'upload'
5352
}))
5453
};
54+
WeFetch.prototype.login = function () {
55+
return promisify(platform.getPlatform().login)();
56+
};

src/lib/core/dispatchRequest.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
import platform from "./platform";
12

23
function dispatchRequest(config) {
4+
if (platform.platform === 'my' && config.method !== 'download' && config.method !== 'upload') {
5+
config.headers = config.header;
6+
delete config.header
7+
}
8+
if (config.method === 'download') {
9+
config.method = 'get';
10+
config.createRequest = platform.getDownload()
11+
}
12+
if (config.method === 'upload'){
13+
config.method = 'post';
14+
config.createRequest = platform.getUpload()
15+
}
316
var request = config.createRequest;
417
return request(config).then(function (response) {
518
return response;
@@ -8,4 +21,4 @@ function dispatchRequest(config) {
821
})
922
}
1023

11-
export default dispatchRequest;
24+
export default dispatchRequest;

src/lib/core/functional.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import platform from'./platform'
2+
import promisify from "./promisify";
13
export function retry(times,request,timeout) {
24
timeout = timeout || 1000;
35
if (!times && times !== 0 || !request)throw new Error('request and times params is required');
@@ -16,4 +18,16 @@ export function retry(times,request,timeout) {
1618
}
1719
return p;
1820
}
19-
21+
export function getUserInfo(type) {
22+
var p = platform.getPlatform();
23+
var get_setting = promisify(p.getSetting);
24+
var get_user_info = promisify(p.getUserInfo);
25+
if (type){
26+
return get_setting().then(function (res) {
27+
if (res.authSetting['scope.userInfo']) {
28+
return get_user_info()
29+
}
30+
})
31+
}
32+
return get_user_info()
33+
}

src/lib/core/platform.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ function Platform() {
66
Platform.prototype.getRequest = function () {
77
try {
88
if (wx.request) {
9-
this.platform = 'wechat';
9+
this.platform = 'wx';
1010
return promisify(wx.request)
1111
}
1212
} catch (e) {
1313
try {
14-
if (my.httpRequest) {
15-
this.platform = 'ali';
16-
return promisify(my.httpRequest)
14+
if (my.request) {
15+
this.platform = 'my';
16+
return promisify(my.request)
17+
} else if (my.httpRequest){
18+
this.platform = 'my';
19+
return promisify(my.httpRequest)
1720
}
1821
}catch (e) {
1922
if (swan.request) {
@@ -22,15 +25,16 @@ Platform.prototype.getRequest = function () {
2225
}
2326
}
2427
}
25-
}
28+
};
2629
Platform.prototype.getUpload = function () {
27-
if (this.platform === 'wechat')return promisify(wx.uploadFile);
28-
if (this.platform === 'ali')return promisify(my.uploadFile);
29-
if (this.platform === 'swan')return promisify(swan.uploadFile);
30-
}
30+
return promisify(this.getPlatform().uploadFile);
31+
};
3132
Platform.prototype.getDownload = function () {
32-
if (this.platform === 'wechat')return promisify(wx.downloadFile)
33-
if (this.platform === 'ali')return promisify(my.downloadFile)
34-
if (this.platform === 'swan')return promisify(swan.downloadFile)
35-
}
33+
return promisify(this.getPlatform().downloadFile);
34+
};
35+
Platform.prototype.getPlatform = function () {
36+
if (this.platform === 'wx')return wx;
37+
if (this.platform === 'my')return my;
38+
if (this.platform === 'swan')return swan;
39+
};
3640
export default new Platform();

src/lib/core/promisify.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import e from './Events'
22
function promisify (api) {
33
return function (options) {
44
options = options || {};
5+
options.config = options.config || {};
56
for (var len = arguments.length, params = Array(len > 1 ? len - 1 : 0), key = 1; key < len; key++) {
67
params[key - 1] = arguments[key];
78
}
@@ -12,4 +13,4 @@ function promisify (api) {
1213
};
1314
};
1415

15-
export default promisify;
16+
export default promisify;

src/lib/core/request.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import utils from "../utils";
2-
import platform from './platform.js'
32
import {JSON_CONTENT_TYPE} from "../defaults";
43

54
import dispatchRequest from "./dispatchRequest";
@@ -22,14 +21,6 @@ function request (config) {
2221
config.url = config.baseUrl + config.url
2322
}
2423
}
25-
if (config.method === 'download') {
26-
config.method = 'get';
27-
config.createRequest = platform.getDownload()
28-
}
29-
if (config.method === 'upload'){
30-
config.method = 'post';
31-
config.createRequest = platform.getUpload()
32-
}
3324
var chain = [dispatchRequest, undefined];
3425
config.config = config.config || {};
3526
var promise = Promise.resolve(config);

src/lib/wefetch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {defaults} from './defaults'
33
import utils from './utils'
44
import WeFetch from './core/Wefetch'
55
import bind from './core/bind'
6-
import {retry} from './core/functional'
6+
import {retry, getUserInfo} from './core/functional'
77
Promise.prototype.finally = function (cb) {
88
var p = this.constructor;
99
return this.then(function (value) {
@@ -29,6 +29,7 @@ var wf = createInstance(defaults);
2929
wf.all = function (promises) {
3030
return Promise.all(promises)
3131
};
32+
wf.getUserInfo = getUserInfo;
3233
wf.retry = retry;
3334
wf.create = function (instanceConfig) {
3435
return createInstance(utils.merge(defaults, instanceConfig))

0 commit comments

Comments
 (0)