Authored by 郭成尧

Merge branch 'hotfix/wx-share' into 'master'

微信分享appi 改成活动的

把微信分享的appId 和appSecret 修改成了可配置的 默认值是原先的值(女生志)

See merge request !59
@@ -6,9 +6,13 @@ @@ -6,9 +6,13 @@
6 const wechatModel = require('../models/wechat'); 6 const wechatModel = require('../models/wechat');
7 7
8 exports.wechatShare = (req, res, next) => { 8 exports.wechatShare = (req, res, next) => {
9 - wechatModel.calcSignature({ 9 + let params = {
  10 + appId: req.query.appId || 'wxb52ec6a352f0b090', //此处使用的是 女生志 的appId
  11 + secret: req.query.secret || '9fe6bedb0b7f30986a168c7fc44f34c0',
10 url: req.query.url || 'http://www.yohobuy.com/' 12 url: req.query.url || 'http://www.yohobuy.com/'
11 - }).then((result) => { 13 + };
  14 +
  15 + wechatModel.calcSignature(params).then((result) => {
12 res.jsonp(result); 16 res.jsonp(result);
13 }).catch(next); 17 }).catch(next);
14 }; 18 };
@@ -12,8 +12,8 @@ const logger = global.yoho.logger; @@ -12,8 +12,8 @@ const logger = global.yoho.logger;
12 const cache = global.yoho.cache; 12 const cache = global.yoho.cache;
13 13
14 // 此处请勿使用有货公众号的 appId, 此处使用的是 女生志 的appId 14 // 此处请勿使用有货公众号的 appId, 此处使用的是 女生志 的appId
15 -const appId = 'wxb52ec6a352f0b090';  
16 -const secret = '9fe6bedb0b7f30986a168c7fc44f34c0'; 15 +// const appId = 'wxb52ec6a352f0b090';
  16 +// const secret = '9fe6bedb0b7f30986a168c7fc44f34c0';
17 17
18 const sha1 = (str) => { 18 const sha1 = (str) => {
19 const generator = crypto.createHash('sha1'); 19 const generator = crypto.createHash('sha1');
@@ -27,7 +27,7 @@ const ticketCacheKey = 'wechatShare:ticket'; @@ -27,7 +27,7 @@ const ticketCacheKey = 'wechatShare:ticket';
27 27
28 // 微信 JS 接口签名校验工具 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 28 // 微信 JS 接口签名校验工具 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
29 29
30 -let _getAccessToken = Promise.coroutine(function* () { 30 +let _getAccessToken = Promise.coroutine(function* (appId, secret) {
31 let accessToken = yield cache.get(accessTokenCacheKey); 31 let accessToken = yield cache.get(accessTokenCacheKey);
32 32
33 if (accessToken) { 33 if (accessToken) {
@@ -35,6 +35,7 @@ let _getAccessToken = Promise.coroutine(function* () { @@ -35,6 +35,7 @@ let _getAccessToken = Promise.coroutine(function* () {
35 } 35 }
36 36
37 logger.info('get accessToken from wechat API'); 37 logger.info('get accessToken from wechat API');
  38 +
38 return request({ 39 return request({
39 url: 'https://api.weixin.qq.com/cgi-bin/token', 40 url: 'https://api.weixin.qq.com/cgi-bin/token',
40 qs: { 41 qs: {
@@ -55,7 +56,7 @@ let _getAccessToken = Promise.coroutine(function* () { @@ -55,7 +56,7 @@ let _getAccessToken = Promise.coroutine(function* () {
55 }); 56 });
56 }); 57 });
57 58
58 -let _getTicket = Promise.coroutine(function* () { 59 +let _getTicket = Promise.coroutine(function* (appId, secret) {
59 let ticket = yield cache.get(ticketCacheKey); 60 let ticket = yield cache.get(ticketCacheKey);
60 61
61 if (ticket) { 62 if (ticket) {
@@ -66,7 +67,7 @@ let _getTicket = Promise.coroutine(function* () { @@ -66,7 +67,7 @@ let _getTicket = Promise.coroutine(function* () {
66 return request({ 67 return request({
67 url: 'https://api.weixin.qq.com/cgi-bin/ticket/getticket', 68 url: 'https://api.weixin.qq.com/cgi-bin/ticket/getticket',
68 qs: { 69 qs: {
69 - access_token: yield _getAccessToken(), 70 + access_token: yield _getAccessToken(appId, secret),
70 type: 'jsapi' 71 type: 'jsapi'
71 }, 72 },
72 json: true 73 json: true
@@ -86,8 +87,7 @@ let calcSignature = Promise.coroutine(function* (data) { @@ -86,8 +87,7 @@ let calcSignature = Promise.coroutine(function* (data) {
86 data = Object.assign({ 87 data = Object.assign({
87 nonceStr: Math.random().toString(36).substr(2, 15), 88 nonceStr: Math.random().toString(36).substr(2, 15),
88 timestamp: Math.floor(Date.now() / 1000) + '', 89 timestamp: Math.floor(Date.now() / 1000) + '',
89 - ticket: yield _getTicket(),  
90 - appId: appId 90 + ticket: yield _getTicket(data.appId, data.secret)
91 }, data); 91 }, data);
92 92
93 const str = `jsapi_ticket=${data.ticket}&noncestr=${data.nonceStr}&timestamp=${data.timestamp}&url=${data.url}`; 93 const str = `jsapi_ticket=${data.ticket}&noncestr=${data.nonceStr}&timestamp=${data.timestamp}&url=${data.url}`;