-
Notifications
You must be signed in to change notification settings - Fork 0
/
jssdk.ts
501 lines (466 loc) · 14.4 KB
/
jssdk.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import crypto from 'crypto';
import fs from 'fs';
import https from 'https';
import redis, { ClientOpts, RedisClient } from 'redis';
import Redlock from 'redlock';
/**
* 持久化
* 1. file,保存到文件上
* 2. redis,保存到redis
* 3. 不保存
*/
type SaveType = 'file' | 'redis' | 'none';
/**
* 配置项
*/
interface IJSSDKOptions {
/**
* 是否企业号,默认false
*/
corp?: boolean;
/**
* 公众号appId
*/
appId: string;
/**
* 公众号secret
*/
secret: string;
/**
* 随机字符串长度,最长32位,默认16位
*/
nonceStrLength?: number;
/**
* ticket和token持久化类型,默认none,可选redis、file
*/
type?: SaveType;
/**
* redis host,type = redis时,必须提供
*/
redisHost?: string;
/**
* redis port,type = redis时,必须提供
*/
redisPort?: number;
/**
* redis password
*/
redisAuth?: string;
/**
* token 缓存文件,type = file时,必须提供
*/
tokenFilename?: string;
/**
* ticket 缓存文件,type = file时,必须提供
*/
ticketFilename?: string;
/**
* 是否缓存在进程内存,默认true
*/
cache?: boolean;
/**
* 打开调试,默认false
*/
debug?: boolean;
}
interface ITokenData {
accessToken?: string;
ticket?: string;
expireTime: number;
}
type Callback = (err: Error | null, ret: ITokenData | null) => void;
interface ISignResult {
/**
* error code
* 1. 4001,需要签名的url参数没有提供
* 2. 内部获取jssdk api ticket错误
* 3. 0正常签名
*/
errCode: 4001 | 4002 | 0;
msg?: string;
appId?: string;
nonceStr?: string;
timestamp?: number;
url?: string;
signature?: string;
}
interface IExpressResponse {
json: (body?: any) => void;
}
interface IExpressRequest {
query: any;
body: any;
}
const TAG = 'express-jssdk';
const NONCE_STR_MAX = 32;
const GET_TICKET_URL = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=';
const GET_TICKET_URL_CORP = 'https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=';
/**
* 票据
*/
const TYPE_TICKET = 1;
/**
* TOKEN
*/
const TYPE_TOKEN = 2;
/**
* 配置项
*/
const defOptions: IJSSDKOptions = {
corp: false, // 是否企业号
appId: '',
secret: '',
nonceStrLength: 16, // 随机字符串长度,最长32位
type: 'none', // ticket和token持久化类型,默认none,可选file、redis
redisHost: '127.0.0.1', // redis host
redisPort: 6379, // redis port
tokenFilename: __dirname + '/local-token.json',
ticketFilename: __dirname + '/local-ticket.json',
cache: true,
debug: false,
};
/**
* 票据缓存
*/
const ticketCache: ITokenData = {
expireTime: 0,
ticket: '',
};
/**
* token缓存
*/
const tokenCache: ITokenData = {
expireTime: 0,
accessToken: '',
};
/**
* redis 客户端
*/
let redisClient: RedisClient;
let options: IJSSDKOptions;
let redlock: any;
/**
* 导出中间件
* @param {object} _options
*/
const jssdk = (_options: IJSSDKOptions) => {
if (!_options.appId || !_options.secret) {
throw new Error('appId and secret must be provided!');
}
if (_options.type === 'file' && (!_options.tokenFilename || !_options.ticketFilename)) {
throw new Error('if type = file, tokenFilename and ticketFilename must be provided!');
}
if (_options.type === 'redis' && (!_options.redisHost || !_options.redisPort)) {
throw new Error('if type = redis, redis config must be provided!');
}
options = Object.assign({}, defOptions, _options);
// 兼容上个版本,修正type = mem为type = none
if ('mem' === options.type as string) { options.type = 'none'; }
// type 设置为 none时,强制使用进程内存
if ('none' === options.type) { options.cache = true; }
// reset cache
ticketCache.expireTime = 0;
ticketCache.ticket = '';
tokenCache.expireTime = 0;
tokenCache.accessToken = '';
// 票据token储到redis
if ('redis' === options.type) {
const conf: ClientOpts = {
host: options.redisHost,
port: options.redisPort,
};
if (options.redisAuth) {
conf.password = options.redisAuth;
}
redisClient = redis.createClient(conf);
redisClient.on('error', (err) => {
console.error(TAG, 'redis error:', err.message);
// 连接不上redis时,cache开启
options.cache = true;
});
redisClient.on('connect', () => {
if (options.debug) { console.log(TAG, 'redis connected to the server:', redisClient.connection_id); }
redlock = new Redlock(
[redisClient],
{
driftFactor: 0.01,
retryCount: 10,
retryDelay: 200,
retryJitter: 200,
});
});
}
return (req: IExpressRequest, res: IExpressResponse) => {
const url = (req && req.query && req.query.url) || (req && req.body && req.body.url);
sign(url, (ret) => {
res.json(ret);
});
};
};
// const disconnectRedis = () => {
// if (redisClient.connected) { redisClient.quit(); }
// };
export = jssdk;
function createNonceStr(length: number = 16) {
length = length || 16;
length = length > NONCE_STR_MAX ? NONCE_STR_MAX : length;
let str = '';
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
for (let i = 0; i < length; i++) {
str += chars[Math.floor(Math.random() * chars.length)];
}
return str;
}
function getAccessToken(cb: (err: Error | null, token: string | null) => void) {
readTokenOrTicket(options.type || 'none', TYPE_TOKEN, (err, data) => {
if (err || !data || !data.accessToken || data.expireTime < Date.now()) {
// 如果是企业号用以下URL获取access_token
let url = '';
if (options.corp) {
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='
+ options.appId + '&corpsecret=' + options.secret;
} else {
url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='
+ options.appId + '&secret=' + options.secret;
}
https.get(url, (res) => {
res.on('data', (d: Buffer) => {
const tokenData: ITokenData = {
accessToken: '',
expireTime: 0,
};
let ret;
try {
// 正常 {"access_token":"ACCESS_TOKEN","expires_in":7200}
// 错误 {"errcode":40013,"errmsg":"invalid appid"}
ret = JSON.parse(d.toString());
} catch (error) {
cb(new Error('get token parse error'), null);
return;
}
if (ret && ret.access_token) {
tokenData.accessToken = ret.access_token;
tokenData.expireTime = Date.now() + 7000000;
cb(null, tokenData.accessToken || '');
saveTokenOrTicket(options.type || 'none', TYPE_TOKEN, tokenData);
} else if (ret.errcode) {
cb(new Error(d.toString()), null);
} else {
cb(new Error('No token!'), null);
}
});
}).on('error', (e) => {
cb(e, null);
});
} else {
cb(null, data.accessToken);
}
});
}
function getJsApiTicket(cb: (err: Error | null, ticket: string | null) => void) {
readTokenOrTicket(options.type || 'none', TYPE_TICKET, (err, data) => {
if (err || !data || !data.expireTime || data.expireTime < Date.now()) {
getAccessToken((error, token) => {
if (error) {
cb(error, null);
return;
}
https.get(options.corp ? GET_TICKET_URL_CORP + token : GET_TICKET_URL + token, (res) => {
res.on('data', (d) => {
const ticketData: ITokenData = {
ticket: '',
expireTime: 0,
};
let ret;
try {
// 正常 {"errcode":0,"errmsg":"ok","ticket":"xx","expires_in":7200}
ret = JSON.parse(d);
} catch (error) {
cb(new Error('get ticket parse error'), null);
return;
}
if (ret && ret.errcode === 0 && ret.ticket) {
ticketData.ticket = ret.ticket;
ticketData.expireTime = Date.now() + 7000000;
cb(null, ret.ticket);
saveTokenOrTicket(options.type || 'none', TYPE_TICKET, ticketData);
} else {
cb(new Error('No ticket!'), null);
}
});
}).on('error', (_httpError: Error) => {
cb(error, null);
});
});
} else {
if (data.ticket) {
cb(null, data.ticket);
} else {
cb(new Error('Ticket not Found'), null);
}
}
});
}
function sign(url: string, cb: (ret: ISignResult) => void) {
if (!url) {
cb({
errCode: 4001,
msg: 'argument url must be provided!',
});
return;
}
getJsApiTicket((err, ticket) => {
if (err) {
cb({
errCode: 4002,
msg: err.message,
});
return;
}
const timestamp = Math.floor(Date.now() / 1000);
const nonceStr = createNonceStr(options.nonceStrLength);
const signStr = 'jsapi_ticket=' + ticket + '&noncestr=' + nonceStr + '×tamp=' + timestamp + '&url=' + url;
const signature = crypto.createHash('sha1').update(signStr).digest('hex');
cb({
errCode: 0,
appId: options.appId,
nonceStr,
timestamp,
signature,
});
});
}
/**
* 保存票据token
* @param {string} saveType token保存类型file本地文件,redis
* @param {number} tokenOrTicket
* @param {object} data
* @param {string} fileName
*/
function saveTokenOrTicket(saveType: SaveType, tokenOrTicket: number, data: ITokenData) {
if (options.cache) { refreshCache(tokenOrTicket, data); }
if ('redis' === saveType) {
if (!redisClient.connected) {
console.warn(TAG, 'redis disconnected');
// redis未建立连接,且未开启缓存,开启缓存并缓存数据
if (!options.cache) {
if (options.debug) { console.log(TAG, 'redis disconnected, set cache = true.'); }
options.cache = true;
refreshCache(tokenOrTicket, data);
}
return;
}
const key = tokenOrTicket === TYPE_TICKET ? REDIS_KEY_TICKET : REDIS_KEY_TOKEN;
const lockName = tokenOrTicket === TYPE_TICKET ? LOCK_TICKET : LOCK_TOKEN;
redlock.lock(lockName, 1000).then((lock: any) => {
redisClient.set(key, JSON.stringify(data));
return lock.unlock().catch((err: Error) => {
console.error(err);
});
});
} else if ('file' === saveType) {
const fileName = tokenOrTicket === TYPE_TICKET ? options.ticketFilename : options.tokenFilename;
if (!fileName) {
if (!options.cache) {
if (options.debug) { console.log(TAG, 'type = file and no filename, set cache = true.'); }
options.cache = true;
refreshCache(tokenOrTicket, data);
}
return;
}
fs.writeFile(fileName, JSON.stringify(data), (err) => {
if (err) {
// 保存到文件上失败,且未开启缓存,开启缓存并缓存数据
if (!options.cache) {
if (options.debug) { console.log(TAG, 'save to file error, set cache = true.'); }
options.cache = true;
refreshCache(tokenOrTicket, data);
}
console.error(TAG, 'Save to File (' + fileName + ') Error:', err.message);
}
});
}
}
function refreshCache(tokenOrTicket: number, data: ITokenData) {
if (options.debug) { console.log(TAG, ' refresh cache, TYPE =', tokenOrTicket, 'data =', data); }
if (tokenOrTicket === TYPE_TICKET) {
ticketCache.expireTime = data.expireTime;
ticketCache.ticket = data.ticket;
} else {
tokenCache.expireTime = data.expireTime;
tokenCache.accessToken = data.accessToken;
}
}
const REDIS_KEY_TOKEN = 'jssdk.token';
const REDIS_KEY_TICKET = 'jssdk.ticket';
const LOCK_TOKEN = 'locks:token';
const LOCK_TICKET = 'locks:ticket';
/**
* 读取缓存的票据token
* @param {string} saveType [file, redis, mem]
* @param {number} tokenOrTicket
* @param {function} cb
*/
function readTokenOrTicket(saveType: SaveType, tokenOrTicket: number, cb: Callback) {
if (options.cache) {
if (tokenOrTicket === TYPE_TICKET) {
if (ticketCache && ticketCache.ticket && ticketCache.expireTime > Date.now()) {
// 内存有缓存
cb(null, ticketCache);
if (options.debug) { console.log(TAG, 'readTokenOrTicket: read ticket from cache'); }
return;
}
} else {
if (tokenCache && tokenCache.accessToken && tokenCache.expireTime > Date.now()) {
if (options.debug) { console.log(TAG, 'readTokenOrTicket: read token from cache'); }
cb(null, tokenCache);
return;
}
}
}
if ('redis' === saveType) {
if (!redisClient.connected) {
if (options.debug) { console.log(TAG, 'readTokenOrTicket: redis disconnected'); }
cb(new Error('redis disconnected'), null);
return;
}
const key = tokenOrTicket === TYPE_TICKET ? REDIS_KEY_TICKET : REDIS_KEY_TOKEN;
redisClient.get(key, (err, reply) => {
if (err) {
if (options.debug) { console.log(TAG, 'readTokenOrTicket: read redis error =', err.message); }
cb(err, null);
} else {
try {
cb(err, JSON.parse(reply.toString()) as ITokenData);
if (options.debug) { console.log(TAG, 'readTokenOrTicket: reply =', reply); }
} catch (error) {
if (options.debug) { console.log(TAG, 'readTokenOrTicket: type = redis, parse data error =', error); }
cb(error, null);
}
}
});
} else if ('file' === saveType) {
const fileName = tokenOrTicket === TYPE_TICKET ? options.ticketFilename : options.tokenFilename;
if (!fileName) {
cb(new Error('No filename'), null);
return;
}
fs.readFile(fileName, (err, data) => {
if (err) {
if (options.debug) { console.log(TAG, 'readTokenOrTicket: read file error =', err.message); }
cb(err, null);
} else {
try {
cb(err, JSON.parse(data.toString()) as ITokenData);
if (options.debug) { console.log(TAG, 'readTokenOrTicket: buffer =', data); }
} catch (error) {
if (options.debug) { console.log(TAG, 'readTokenOrTicket: type = file, parse data error =', error); }
cb(error, null);
}
}
});
} else {
// 不是file也不是redis,返回空,让后面逻辑去微信服务器请求
cb(null, null);
}
}