-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwechat_cordova.js
83 lines (75 loc) · 2.86 KB
/
wechat_cordova.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
MeteorWeChat = {};
// Request WeChat credentials for the user
// @param options {optional}
// @param credentialRequestCompleteCallback {Function} Callback function to call on
// completion. Takes one argument, credentialToken on success, or Error on
// error.
MeteorWeChat.requestCredential = function (options, credentialRequestCompleteCallback) {
// support both (options, callback) and (callback).
if (!credentialRequestCompleteCallback && typeof options === 'function') {
credentialRequestCompleteCallback = options;
options = {};
} else if (!options) {
options = {};
}
var config = ServiceConfiguration.configurations.findOne({service: 'wechat'});
if (!config) {
credentialRequestCompleteCallback && credentialRequestCompleteCallback(
new ServiceConfiguration.ConfigError()
);
return;
}
var credentialToken = Random.secret();
var scope = (options && options.requestPermissions) || ['snsapi_userinfo'];
scope = _.map(scope, encodeURIComponent).join(',');
var loginStyle = OAuth._loginStyle('wechat', config, options);
//var state = OAuth._stateParam(loginStyle, credentialToken);
var state = credentialToken;
if (!Wechat) {
credentialRequestCompleteCallback && credentialRequestCompleteCallback(
new Meteor.Error("unsupported", "WeChat corodova plugin is not found.")
);
return;
}
Wechat.isInstalled(function (installed) {
if (!installed) {
credentialRequestCompleteCallback && credentialRequestCompleteCallback(
new Meteor.Error("unsupported", "WeChat is not installed.")
);
return;
}
Wechat.auth(scope, state, function(response) {
// send the 3rd party response directly to the server for processing
Meteor.call('handleWeChatOauthRequest', response, function(err, credentials) {
OAuth._handleCredentialSecret(credentials.credentialToken, credentials.credentialSecret);
credentialRequestCompleteCallback && credentialRequestCompleteCallback(
credentialToken
);
});
}, function(reason) {
credentialRequestCompleteCallback && credentialRequestCompleteCallback(
new Meteor.Error("unauthroized", "WeChat authorization failed: " + reason)
);
});
}, function (reason) {
credentialRequestCompleteCallback && credentialRequestCompleteCallback(
new Meteor.Error("unsupported", "Cannot detect whether WeChat is installed or not: " + reason)
);
return;
});
};
MeteorWeChat.appInstalled = function(callback) {
if (!Wechat) {
callback && callback(
new Meteor.Error("unsupported", "WeChat corodova plugin is not found.")
);
return;
}
Wechat.isInstalled(function (installed) {
callback && callback(null, installed);
}, function(reason) {
callback && callback(
new Meteor.Error("unsupported", "Cannot detect whether WeChat is installed or not: " + reason)
);
});
};