Authored by ccbikai

微信获取 Token 功能实现

'use strict';
/*
* 生成微信分享所需的签名
* bikai <kai.bi@yoho.cn>
* 2016.6.15
*/
const request = require('request-promise');
const Promise = require('bluebird');
const crypto = require('crypto');
const logger = require('../../../library/logger');
const cache = require('../../../library/cache');
const appId = 'wx75e5a7c0c88e45c2';
const secret = 'ce21ae4a3f93852279175a167e54509b';
const sha1 = (etr) => {
const generator = crypto.createHash('sha1');
generator.update(etr);
return generator.digest('hex');
};
const accessTokenCacheKey = 'wechatShare:accessToken';
const ticketCacheKey = 'wechatShare:ticket';
const appId = '';
const secret = '';
// 微信 JS 接口签名校验工具 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
const wechat = {
getAccessToken: Promise.coroutine(function* () {
let accessToken = yield cache.get(accessTokenCacheKey);
if (accessToken) {
return accessToken;
}
logger.info('调用微信 API 获取 accessToken');
return request({
url: 'https://api.weixin.qq.com/cgi-bin/token',
qs: {
grant_type: 'client_credential',
appid: appId,
secret: secret
}
},
json: true
}).then((res) => {
// accessToken 有效期 7200s,缓存 7100s
cache.set(accessTokenCacheKey, res.access_token, 7100).catch((err) => {
logger.error('微信分享 Token, 缓存 accessToken 时出错', JSON.stringify(err));
});
return res.access_token;
}).catch((err) => {
logger.error('微信分享 Token, 获取 accessToken 时出错', JSON.stringify(err));
});
}),
getTicket: Promise.coroutine(function* (accessToken) {
let ticket = yield cache.get(ticketCacheKey);
if (ticket) {
return ticket;
}
logger.info('调用微信 API 获取 ticket');
return request({
url: 'https://api.weixin.qq.com/cgi-bin/ticket/getticket',
qs: {
access_token: accessToken,
type: 'jsapi'
}
},
json: true
}).then(res => {
// ticket 有效期 7200s,缓存 7100s
cache.set(ticketCacheKey, res.ticket, 7100).catch((err) => {
logger.error('微信分享 Token, 缓存 ticket 时出错', JSON.stringify(err));
});
return res.ticket;
}).catch((err) => {
logger.error('微信分享 Token, 获取 ticket 时出错', JSON.stringify(err));
});
}),
calcSignature: Promise.coroutine(function* () {
return yield Promise.resolve(true);
calcSignature: Promise.coroutine(function* (data) {
let accessToken = yield this.getAccessToken();
data = Object.assign({
noncestr: Math.random().toString(36).substr(2, 15),
timestamp: Math.floor(Date.now() / 1000) + '',
ticket: yield this.getTicket(accessToken)
}, data);
const str = `jsapi_ticket=${data.ticket}&noncestr=${data.noncestr}&timestamp=${data.timestamp}&url=${data.url}`;
return sha1(str);
})
};
// 测试
// wechat.calcSignature({
// url: 'http://www.yohobuy.com/boys'
// }).then(console.log);
module.exports = wechat;
... ...