Authored by ccbikai

Merge branch 'develop' into release/4.6

'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');
// 此处请勿使用有货公众号的 appId, 此处使用的是 女生志 的appId
const appId = 'wxb52ec6a352f0b090';
const secret = '9fe6bedb0b7f30986a168c7fc44f34c0';
const sha1 = (str) => {
const generator = crypto.createHash('sha1');
generator.update(str);
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* () {
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: yield this.getAccessToken(),
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) {
data = Object.assign({
noncestr: Math.random().toString(36).substr(2, 15),
timestamp: Math.floor(Date.now() / 1000) + '',
ticket: yield this.getTicket()
}, 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;
... ...
... ... @@ -8,7 +8,6 @@ const library = '../../../library';
const _ = require('lodash');
const channelModel = require('../models/channel');
const helpers = require(`${library}/helpers`);
const log = require(`${library}/logger`);
const renderData = {
module: 'channel',
... ... @@ -27,11 +26,6 @@ const renderData = {
pageFooter: true
};
const channelLogger = (err, res) => {
log.error('频道页面渲染错误:' + JSON.stringify(err));
res.send('error');
};
/**
* 频道页生成函数
* @param {[object]} req
... ... @@ -45,15 +39,13 @@ const channelPage = (req, res, data) => {
uid: _.toString(req.user.uid)
}).then(result => {
res.render('channel', Object.assign({}, renderData, data, result));
}).catch((err) => {
channelLogger(err, res);
});
};
/**
* 频道选择页
*/
exports.index = (req, res) => {
exports.index = (req, res, next) => {
channelModel.getChannelSwitchData().then((result) => {
res.render('index', {
module: 'channel',
... ... @@ -64,7 +56,7 @@ exports.index = (req, res) => {
channelList: result[0],
background: result[1]
});
});
}).catch(next);
};
/**
... ... @@ -101,35 +93,35 @@ exports.boys = (req, res, next) => {
/**
* 女生首页
*/
exports.girls = (req, res) => {
exports.girls = (req, res, next) => {
channelPage(req, res, {
gender: 'girls',
title: '女生首页',
girlsHomePage: true
});
}).catch(next);
};
/**
* 潮童首页
*/
exports.kids = (req, res) => {
exports.kids = (req, res, next) => {
channelPage(req, res, {
gender: 'kids',
title: '潮童首页',
kidsHomePage: true
});
}).catch(next);
};
/**
* 创意生活首页
*/
exports.lifestyle = (req, res) => {
exports.lifestyle = (req, res, next) => {
channelPage(req, res, {
gender: 'lifestyle',
title: '创意生活首页',
lifestyleHomePage: true
});
}).catch(next);
};
/**
... ... @@ -138,12 +130,10 @@ exports.lifestyle = (req, res) => {
* @param {[object]} res
* @return {[type]}
*/
exports.bottomBanner = (req, res) => {
exports.bottomBanner = (req, res, next) => {
let gender = req.query.gender || 'boys';
channelModel.getBottomBannerData(gender).then(result => {
res.send(result);
}).catch((err) => {
channelLogger(err, res);
});
}).catch(next);
};
... ...
... ... @@ -60,7 +60,7 @@ exports.index = (req, res) => {
}, processPublicData(req, '星潮教室')));
};
exports.getIndexHtml = (req, res) => {
exports.getIndexHtml = (req, res, next) => {
starModel.getIndexData().then((result) => {
res.render('star/index-html', _.assign({
layout: false,
... ... @@ -71,7 +71,7 @@ exports.getIndexHtml = (req, res) => {
starAvatar: result.starAvatar,
articles: result.articles
}));
});
}).catch(next);
};
/**
... ... @@ -80,7 +80,7 @@ exports.getIndexHtml = (req, res) => {
* @param {[object]} res
* @return {[type]}
*/
exports.special = (req, res) => {
exports.special = (req, res, next) => {
starModel.getSpecialData().then((result) => {
const pageHeadTab = _.cloneDeep(headTab);
... ... @@ -91,7 +91,7 @@ exports.special = (req, res) => {
resources: result,
headTab: pageHeadTab
}));
});
}).catch(next);
};
... ... @@ -119,7 +119,7 @@ exports.collocation = (req, res) => {
* @param {[object]} res
* @return {[type]}
*/
exports.collocationList = (req, res) => {
exports.collocationList = (req, res, next) => {
let params = req.query;
let uid = req.user.uid || 0;
... ... @@ -130,7 +130,7 @@ exports.collocationList = (req, res) => {
list: result,
isApp: req.query.app_version || req.query.appVersion || false
}));
});
}).catch(next);
};
/**
... ... @@ -139,7 +139,7 @@ exports.collocationList = (req, res) => {
* @param {[type]} res [description]
* @return {[type]}
*/
exports.setFavorite = (req, res) => {
exports.setFavorite = (req, res, next) => {
let params = req.body;
let uid = req.user.uid || 0;
let isApp = req.query.app_version || req.query.appVersion || false;
... ... @@ -158,7 +158,7 @@ exports.setFavorite = (req, res) => {
}
}
res.json(result);
});
}).catch(next);
};
/**
... ... @@ -180,7 +180,7 @@ exports.detail = (req, res) => {
* @param {[object]} res
* @return {[type]}
*/
exports.detailList = (req, res) => {
exports.detailList = (req, res, next) => {
let params = req.query;
starModel.getDetailData(params).then((result) => {
... ... @@ -190,5 +190,5 @@ exports.detailList = (req, res) => {
list: result,
isApp: req.query.app_version || req.query.appVersion || false
}));
});
}).catch(next);
};
... ...
... ... @@ -30,7 +30,7 @@ function doPassportCallback(openId, nickname, sourceType, req, res) {
refer = `${config.siteUrl}/home`;
}
if (openId && nickname) {
AuthHelper.signinByOpenID(nickname, openId, sourceType, shoppingKey).then((result) => {
return AuthHelper.signinByOpenID(nickname, openId, sourceType, shoppingKey).then((result) => {
if (result.data['is_bind'] && result.data['is_bind'] === 'N') { //eslint-disable-line
return helpers.urlFormat('/passport/bind/index', {
openId: openId,
... ... @@ -44,9 +44,6 @@ function doPassportCallback(openId, nickname, sourceType, req, res) {
}
}).then((redirectTo) => {
return res.redirect(redirectTo);
}).catch((e) => {
log.error('频道页面渲染错误:' + JSON.stringify(e));
return res.send('error');
});
}
}
... ... @@ -77,7 +74,7 @@ const wechat = {
let nickname = user._json.nickname || user.displayName;
let openId = user._json.unionid || user.id;
doPassportCallback(openId, nickname, 'wechat', req, res);
doPassportCallback(openId, nickname, 'wechat', req, res).catch(next);
})(req, res, next);
}
};
... ...
... ... @@ -49,7 +49,7 @@ class Auth {
res.cookie('_TOKEN', token, {
domain: 'yohobuy.com'
}); // esline-disable-line
}).catch(console.log);
});
}
}
... ...
... ... @@ -5,12 +5,10 @@
*/
'use strict';
const library = '../../../library';
const mRoot = '../models';
// const cookie = require(`${library}/cookie`);
const headerModel = require('../../../doraemon/models/header');
const log = require(`${library}/logger`);
const saleModel = require(`${mRoot}/sale`);
const queryParam = {
... ... @@ -22,12 +20,6 @@ const queryParam = {
pD: '0.1,0.9'
};
const saleLogger = (err, res) => {
log.error('sale页面渲染错误:' + JSON.stringify(err));
res.send('error');
};
/**
* 公共数据处理
* @param {[object]} req
... ... @@ -60,7 +52,7 @@ const processPublicData = (req, title, page) => {
* @param {[object]} res
* @return {[type]}
*/
exports.index = (req, res) => {
exports.index = (req, res, next) => {
let params = processPublicData(req, 'SALE', 'sale');
saleModel.getSaleData(params.channel).then((result) => {
... ... @@ -72,9 +64,7 @@ exports.index = (req, res) => {
}
}
}));
}).catch((err) => {
saleLogger(err, res);
});
}).catch(next);
};
/**
... ... @@ -83,16 +73,14 @@ exports.index = (req, res) => {
* @param {[object]} res
* @return {[type]}
*/
exports.breakingYards = (req, res) => {
exports.breakingYards = (req, res, next) => {
let params = processPublicData(req, '断码区', 'break-code');
saleModel.getBreakCodeData({
yhChannel: params.channel
}).then((result) => {
res.render('sale/break-code', Object.assign(params.renderData, result));
}).catch((err) => {
saleLogger(err, res);
});
}).catch(next);
};
/**
... ... @@ -101,14 +89,12 @@ exports.breakingYards = (req, res) => {
* @param {[object]} res
* @return {[type]}
*/
exports.discount = (req, res) => {
exports.discount = (req, res, next) => {
let params = processPublicData(req, '折扣专场', 'discount');
saleModel.getDiscountData(params.channel).then((result) => {
res.render('sale/discount', Object.assign(params.renderData, result));
}).catch((err) => {
saleLogger(err, res);
});
}).catch(next);
};
... ... @@ -118,16 +104,14 @@ exports.discount = (req, res) => {
* @param {[object]} res
* @return {[type]}
*/
exports.discountDetail = (req, res) => {
exports.discountDetail = (req, res, next) => {
let id = req.query.id;
let params = processPublicData(req, '', 'discount-detail');
saleModel.getDiscountDetailData(id).then((result) => {
params.renderData.pageHeader.navTitle = result.title;
res.render('sale/discount-detail', Object.assign(params.renderData, result));
}).catch((err) => {
saleLogger(err, res);
});
}).catch(next);
};
/**
... ... @@ -136,16 +120,14 @@ exports.discountDetail = (req, res) => {
* @param {[object]} res
* @return {[type]}
*/
exports.vip = (req, res) => {
exports.vip = (req, res, next) => {
let params = processPublicData(req, '会员专享', 'vip');
saleModel.getVipData(params.channel).then((result) => {
res.render('sale/vip', Object.assign(params.renderData, {
content: result
}));
}).catch((err) => {
saleLogger(err, res);
});
}).catch(next);
};
/**
... ... @@ -154,7 +136,7 @@ exports.vip = (req, res) => {
* @param {[object]} res
* @return {[type]}
*/
exports.filter = (req, res) => {
exports.filter = (req, res, next) => {
let params = Object.assign({}, req.query);
saleModel.getFilterData(params).then((result) => {
... ... @@ -163,7 +145,7 @@ exports.filter = (req, res) => {
params: params,
filter: result
});
});
}).catch(next);
};
... ... @@ -173,7 +155,7 @@ exports.filter = (req, res) => {
* @param {[object]} res
* @return {[type]}
*/
exports.search = (req, res) => {
exports.search = (req, res, next) => {
let params = Object.assign({}, req.query);
// uid = 9239279
... ... @@ -199,5 +181,5 @@ exports.search = (req, res) => {
params: params,
goods: result[0]
}, vipObj));
});
}).catch(next);
};
... ...
No preview for this file type
No preview for this file type