Authored by htoooth

index backend

... ... @@ -6,8 +6,6 @@
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const requestIp = require('request-ip');
const md5 = require('md5');
const favoriteService = require('../models/favorite-service');
... ...
/**
* 个人中心二维码 controller
* @author: weiqingting<qingting.wei@yoho.cn>
* @date: 2016/05/16
*/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const indexService = require('../models/index-service');
const OrderData = require('../models/orders-api');
const IndexModel = require('../models/index-service');
const index = (req, res, next)=>{
let $uid = '8041246';// req.user.uid;
let $udid = 'abcdrf';// req.sessionID;
co(function*() {
let items = yield Promise.all([OrderData.closeReasons(),
IndexModel.getInfoNumData($uid, $udid),
IndexModel.getFooterBanner(),
IndexModel.latestOrders($uid),
IndexModel.homeData()]);
let cancelReason = items[0].data ? items[0].data : '';
res.render('index', {
cancelReason: cancelReason,
content: [
{messages: items[1]},
{
latestOrders: items[3],
favBrand: {
more: '/brands',
brands: items[4].brand
},
newArrival: items[4].new,
banner: items[2]
}
],
helpUsUrl: ''
});
})().catch(next);
let uid = req.user.uid;
let udid = req.user.uid + req.yoho.udid;
let channel = req.yoho.channel;
let isStudent = req.user.isStudent;
indexService.index(uid, udid, channel, isStudent).then((result) => {
return res.render('index', {
meIndexPage: true,
me: result
});
}).catch(next);
};
module.exports = {
... ...
/**
* @author: weiqingting<qingting.wei@yoho.cn>
*/
'use strict';
module.exports.xchangeDay = 15;
module.exports.refundDay = 7;
... ...
/**
* @author: weiqingting<qingting.wei@yoho.cn>
*/
'use strict';
const api = global.yoho.API;
const getPendingOrderCount = uid=>{
let options = {
method: 'web.SpaceOrders.getPendingOrderCount',
const pendingOrderCount = (uid) => {
return api.get('', {
method: 'web.SpaceOrders.pendingOrderCount',
uid: uid
};
return api.get('', options);
});
};
const infoNum = (uid, udid)=>{
let options = {
const unreadMessageCount = (uid, udid)=>{
return api.get('', {
method: 'app.home.getInfoNum',
uid: uid,
udid: udid
};
return api.get('', options);
});
};
const notCommentRecordCount = uid=>{
let options = {
const needCommentCount = (uid) =>{
return api.get('', {
method: 'show.notCommentRecordCount',
uid: uid
};
return api.get('', options);
});
};
const guessBrand = () => {
return api.get('', {
method: 'web.search.favorBrand'
});
};
const newArrival = () => {
return api.get('', {
method: 'web.search.search',
sales: 'Y',
outlets: 2,
stocknumber: 1,
new: 'Y',
order: 's_t_desc',
viewNum: 0
});
};
/**
* 优选新品
*
* @param int $channel 频道,1代表男生,2代表女生,3代表潮童,4代表创意生活
* @param $uid 用户ID
* @param $udid 设备ID
* @param $rec_pos 位置码
* @param $limit 数量限制
* @return array 接口返回的数据
*/
const recommend = (channelNum, uid, udid, pos, limit) => {
return api.get('', {
method: 'app.home.newPreference',
yh_channel: channelNum,
uid: uid,
udid: udid,
rec_pos: pos,
limit: limit
});
};
/**
* 根据节点和运行模式选择静态内容
* @param $node 20141219-100447
* @param string $mode
* @return mixed
*/
const getByNodeContent = (node, mode) => {
return api.get('', {
method: 'web.html.content',
mode: mode,
node: node
});
};
module.exports = {
getPendingOrderCount,
infoNum,
notCommentRecordCount
pendingOrderCount,
unreadMessageCount,
needCommentCount,
guessBrand,
newArrival,
recommend,
getByNodeContent
};
... ...
/**
* @author: weiqingting<qingting.wei@yoho.cn>
*/
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const OrderData = require('./orders-api');
const OrderModel = require('./orders-service');
const _ = require('lodash');
const Fn = require('lodash/fp');
const helpers = global.yoho.helpers;
const api = global.yoho.API;
const searchApi = global.yoho.SearchAPI;
const BrandData = require('./BrandData');
const IndexData = require('./index-api');
const SearchData = require('./SearchData');
const orderService = require('./orders-service');
const indexApi = require('./index-api');
const HelperHome = require('./HelperHome');
const CHANNEL_NUM = {
boys: 1,
girls: 2,
kids: 3,
lifestyle: 4
};
const _ = require('lodash');
const _channelNum = channel => CHANNEL_NUM[channel] || CHANNEL_NUM.boys;
/**
* 个人中心——消息提示
* @param type uid
* @param type udid
* @return array
* 处理品牌
*/
const getInfoNumData = (uid, udid)=>{
let result = [
{href: helpers.urlFormat('/home/orders'), name: '待处理订单', 'count': 0},
{href: helpers.urlFormat('/home/message'), name: '未读消息', 'count': 0},
{href: helpers.urlFormat('/home/comment'), name: '待评论商品', 'count': 0}
];
return co(function * () {
let getPendingOrderCount = yield IndexData.getPendingOrderCount(uid);// 待处理订单
let infoNumData = yield IndexData.infoNum(uid, udid); // 未读消息
let notCommentRecordCount = yield IndexData.notCommentRecordCount(uid);// 待评论商品
result[0]['count'] = getPendingOrderCount.data.count ? getPendingOrderCount.data.count : 0;
result[1]['count'] = infoNumData.data.inbox_total ? infoNumData.data.inbox_total : 0;
result[2]['count'] = notCommentRecordCount.data ? notCommentRecordCount.data : 0;
return result;
})();
};
const _handleBrand = (brands, needNum) => {
const handle = Fn.pipe(Fn.filter({is_hot: 'Y'}), Fn.take(needNum), Fn.map((brand) => ({
href: helpers.urlFormat(null, null, brand.brand_domain),
logo: helpers.getForceSourceUrl(brand.brand_ico),
name: brand.brand_name
})));
return handle(brands);
};
/**
* 个人中心——最新订单
* @param type uid
* @return array
* 处理商品
*/
const latestOrders = (uid)=>{
return co(function *() {
let orders = yield OrderModel.getOrders(uid, 1, 2, 1);
const _handleProduct = (products) => {
return products.map((product) => {
let img = helpers.image(_.get(product, 'default_images', ''), 100, 100);
if (img.indexOf('imageView') !== -1) {
img = img.split('imageView', 1) +
'imageMogr2/thumbnail/100x100/extent/100x100/background/d2hpdGU=/position/center/quality/90';
}
return {
more: helpers.urlFormat('/home/orders'),
orders: orders
href: helpers.getUrlBySkc(product.product_id,
_.get(product, 'goods_list[0].product_skc', ''),
product.cn_alphabet
),
thumb: img,
name: product.product_name,
price: product.sales_price,
productId: product.product_id
};
})();
};
const homeData = ()=>{
return co(function * () {
let result = {};
let url = {};
url.fav_brand = SearchData.getBrandListUrl();
url.new = SearchData.getProductUrl({new: 'Y', viewNum: 10});
let data = yield Promise.all([searchApi.get(url.fav_brand, {}, {cache: true}), searchApi.get(url.new, {}, {cache: true})]);
// 格式化数据
result['brand'] = data[0].data && data[0].data.length > 0 ? HelperHome.formatFavBrand(data[0].data, 6) : [];
result['new'] = data[1].data['product_list'] && data[1].data['product_list'].length > 0 ? HelperHome.formatNew(data[1].data['product_list']) : {};
return result;
})();
});
};
/**
* 消息数量提示
*/
const _msgNumber = co(function * (uid, udid) {
let result = [
{href: helpers.urlFormat('/home/orders'), name: '待处理订单', count: 0},
{href: helpers.urlFormat('/home/message'), name: '未读消息', count: 0},
{href: helpers.urlFormat('/home/comment'), name: '待评论商品', count: 0}
];
let reqData = yield Promise.props({
pending: indexApi.pendingOrderCount(uid), // 待处理订单
unread: indexApi.unreadMessageCount(uid, udid), // 未读消息
needComment: indexApi.needCommentCount(uid) // 待评论商品
});
result[0].count = _.get(reqData, 'pending.data.count', 0);
result[1].count = _.get(reqData, 'unread.data.count', 0);
result[2].count = _.get(reqData, 'needComment.data.count', 0);
return result;
});
/**
* 个人中心——最新订单
*/
const _recentOrder = co(function * (uid) {
let latestOrder = yield orderService.getOrders(orderService.ORDER_TYPE.all, uid, 1, 2);
return {
more: helpers.urlFormat('/home/orders'),
orders: latestOrder
};
});
/**
* 你喜欢的品牌
*/
const _guessYouLikeBrand = co(function * () {
let brand = yield indexApi.guessBrand();
const NEED_BRAND_NUM = 6;
return _handleBrand(_.get(brand, 'data', []), NEED_BRAND_NUM);
});
/**
* 新品
*/
const _newProduct = co(function * () {
let newProduct = yield indexApi.newArrival();
return _handleProduct(_.get(newProduct, 'data.product_list', []));
});
/**
* 为你优选
*/
const _recommend = co(function * (channelNum, uid, udid) {
let resData = yield indexApi.recommend(channelNum, uid, udid, '100004', 30);
return _handleProduct(_.get(resData, 'data.product_list', []));
});
/**
* 底部banner
* @param string code
* @return mixed
*/
const getFooterBanner = (code)=>{
code = code || '20110609-152143';
return co(function *() {
let result = '';
let banner = yield BrandData.getByNodeContent(code);
if (banner.code && banner.data) {
result = banner.data.replace('http://', '//');
}
return result;
})();
};
const _footerBanner = co(function * () {
const CODE = '20110609-152143';
let banner = yield indexApi.getByNodeContent(CODE);
return _.get(banner, 'data', '').replace('http://', '//');
});
/**
* 取消订单
*/
const _cancelReason = orderService.closeReason;
const index = co(function * (uid, udid, channel, isStudent) {
let reqData = yield Promise.props({
msgNumber: _msgNumber(uid, udid),
recentOrder: _recentOrder(uid),
guessBrand: _guessYouLikeBrand(),
newProduct: _newProduct(),
recommendProduct: _recommend(_channelNum(channel), uid, udid),
footerBanner: _footerBanner(),
reason: _cancelReason()
});
return {
certifiedName: isStudent ? '学生身份已验证' : '身份验证',
certifiedUrl: helpers.urlFormat('/product/students/'),
cancelReason: reqData.reason,
content: [
{
message: reqData.msgNumber
},
{
latestOrders: reqData.recentOrder,
favBrand: {
more: '/brands',
brands: reqData.guessBrand
},
newArrival: reqData.newProduct
}
],
recommend: reqData.recommendProduct,
banner: reqData.footerBanner,
helpUsUrl: ''
};
});
module.exports = {
getInfoNumData,
getFooterBanner,
latestOrders,
homeData
index
};
... ...
/**
* @author: weiqingting<qingting.wei@yoho.cn>
*/
'use strict';
const Promise = require('bluebird');
... ... @@ -11,7 +8,7 @@ const helpers = global.yoho.helpers;
const pager = require('./pager').handlePagerData;
const orderApi = require('./orders-api');
const ChannelConfig = require('./ChannelConfig');
const ChannelConfig = require('./channel-config');
const ORDER_TYPE = {
all: 1, // 全部
... ... @@ -865,10 +862,16 @@ const confirm = orderApi.confirmUserOrder;
const cancel = orderApi.cancelUserOrder;
const closeReason = () => {
return orderApi.closeReasons().then((result) => {
return _.get(result, 'data', []);
});
};
const detail = co(function * (uid, orderId) {
let apiData = yield Promise.props({
detailData: _getOrder(uid, orderId),
reason: orderApi.closeReasons()
reason: closeReason()
});
if (apiData.detailData.code === 400) {
... ... @@ -878,7 +881,7 @@ const detail = co(function * (uid, orderId) {
return {
detail: apiData.detailData,
package: apiData.detailData.package,
cancelReason: _.get(apiData.reason, 'data', [])
cancelReason: apiData.reason
};
});
... ... @@ -890,5 +893,7 @@ module.exports = {
updateDeliveryAddress,
confirm,
cancel,
detail
detail,
getOrders,
closeReason
};
... ...
... ... @@ -20,21 +20,6 @@ const _getGender = (channel) => {
}
};
const udidRequired = (req, res, next) => {
let udid = req.cookies.udid;
if (!udid) {
udid = md5(req.ip || requestIp.getClientIp(req));
if (res && res.cookie) {
res.cookie('udid', udid);
}
req.user.udid = udid;
}
next();
};
module.exports = () => {
return (req, res, next) => {
let yoho = {
... ...