-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
162 lines (133 loc) · 4.13 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"use strict"
const Q = require("q");
const lazy = require("lazy.js");
const crypto = require("crypto");
const request = require("request");
class SeckenSDK {
constructor(options) {
this.app_id = options.app_id;
this.app_key = options.app_key;
this.base_url = "https://api.sdk.yangcong.com/";
}
sha1(text) {
return crypto.createHash("sha1").update(text, 'utf8').digest("hex");
}
getSignature(data, ignore) {
ignore = ignore || [];
ignore = lazy(["signature"]).union(ignore);
let string = "";
lazy(data)
.keys()
.without(ignore)
.sort()
.each(key => {
string += `${key}=${data[key]}`;
});
string += this.app_key;
return this.sha1(string);
}
request(uri, params, method, base_url) {
params = params || {};
method = method || "GET";
base_url = base_url || this.base_url;
let options = {
baseUrl: base_url,
uri: uri,
method: method,
qs: params,
};
let defer = Q.defer();
let _self = this;
request(options, (error, response, body) => {
let data = JSON.parse(body);
switch(data.status) {
case 200:
if(data.signature === _self.getSignature(data)) {
defer.resolve(data);
} else {
defer.reject(data);
}
break;
default:
defer.reject(data);
break;
}
});
return defer.promise;
}
getQrcode(options) {
options = options || {};
let params = lazy({app_id: this.app_id})
.assign(lazy(options).pick([
'auth_type',
'action_type',
'action_details',
'callback',
]))
.map((value, key) => {
switch(key) {
case "callback": return [key, encodeURIComponent(value)];
default: return [key, value];
}
})
.toObject();
params.signature = this.getSignature(params);
return this.request("/qrcode_for_auth", params);
}
getResult(event_id, time) {
let once = time === false ? true : false;
time = time || 2000;
let params = {
app_id: this.app_id,
event_id: event_id,
};
params.signature = this.getSignature(params);
let _self = this;
let defer = Q.defer();
let loop = () => {
_self.request("/event_result", params).then(data => {
defer.resolve(data);
}, data => {
switch(data.status){
case 201:
case 602:
if(!once) setTimeout(loop, time);
defer.notify(data);
break;
default:
defer.reject(data);
}
});
};
loop();
return defer.promise;
}
postAuthPush(options) {
let params = lazy({app_id: this.app_id})
.assign(lazy(options).pick([
'uid',
'auth_type',
'action_type',
'action_details',
'callback',
]))
.map((value, key) => {
switch(key) {
case "callback": return [key, encodeURIComponent(value)];
default: return [key, value];
}
})
.toObject();
params.signature = this.getSignature(params);
return this.request("/realtime_authorization", params, "POST");
}
checkAuthToken(options) {
let params = {
app_id: this.app_id,
auth_token: options.auth_token,
};
params.signature = this.getSignature(params);
return this.request("/query_auth_token", params);
}
}
module.exports = SeckenSDK;