Authored by ccbikai

Merge branch 'feature/wechatShare' into develop

'use strict';
const wechatModel = require('../models/wechat');
exports.wechatShare = (req, res, next) => {
wechatModel.calcSignature({
url: req.query.url || 'http://www.yohobuy.com/'
}).then((result) => {
res.jsonp(result);
}).catch(next);
};
... ...
'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';
// 微信 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* (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}`;
data.signature = sha1(str);
return data;
})
};
// 测试
// wechat.calcSignature({
// url: 'http://www.yohobuy.com/'
// }).then(console.log);
module.exports = wechat;
... ...
... ... @@ -6,10 +6,11 @@
'use strict';
const router = require('express').Router();
const router = require('express').Router(); // eslint-disable-line
const cRoot = './controllers';
const coupon = require(`${cRoot}/coupon`);
const wechat = require(`${cRoot}/wechat`);
// routers
... ... @@ -19,4 +20,6 @@ router.get('/coupon/phone', coupon.getCoupon);
router.get('/coupon/verify', coupon.verify);
router.get('/wechat/share', wechat.wechatShare);
module.exports = router;
... ...