Authored by htoooth

index backend

@@ -6,8 +6,6 @@ @@ -6,8 +6,6 @@
6 'use strict'; 6 'use strict';
7 const Promise = require('bluebird'); 7 const Promise = require('bluebird');
8 const co = Promise.coroutine; 8 const co = Promise.coroutine;
9 -const requestIp = require('request-ip');  
10 -const md5 = require('md5');  
11 9
12 const favoriteService = require('../models/favorite-service'); 10 const favoriteService = require('../models/favorite-service');
13 11
1 -/**  
2 - * 个人中心二维码 controller  
3 - * @author: weiqingting<qingting.wei@yoho.cn>  
4 - * @date: 2016/05/16  
5 - */  
6 'use strict'; 1 'use strict';
7 2
8 -const Promise = require('bluebird');  
9 -const co = Promise.coroutine; 3 +const indexService = require('../models/index-service');
10 4
11 -const OrderData = require('../models/orders-api');  
12 -const IndexModel = require('../models/index-service');  
13 5
14 6
15 const index = (req, res, next)=>{ 7 const index = (req, res, next)=>{
16 - let $uid = '8041246';// req.user.uid;  
17 - let $udid = 'abcdrf';// req.sessionID;  
18 -  
19 - co(function*() {  
20 - let items = yield Promise.all([OrderData.closeReasons(),  
21 - IndexModel.getInfoNumData($uid, $udid),  
22 - IndexModel.getFooterBanner(),  
23 - IndexModel.latestOrders($uid),  
24 - IndexModel.homeData()]);  
25 -  
26 - let cancelReason = items[0].data ? items[0].data : '';  
27 - res.render('index', {  
28 - cancelReason: cancelReason,  
29 - content: [  
30 - {messages: items[1]},  
31 - {  
32 - latestOrders: items[3],  
33 - favBrand: {  
34 - more: '/brands',  
35 - brands: items[4].brand  
36 - },  
37 - newArrival: items[4].new,  
38 - banner: items[2]  
39 - }  
40 - ],  
41 - helpUsUrl: ''  
42 - });  
43 - })().catch(next);  
44 -  
45 let uid = req.user.uid; 8 let uid = req.user.uid;
  9 + let udid = req.user.uid + req.yoho.udid;
  10 + let channel = req.yoho.channel;
  11 + let isStudent = req.user.isStudent;
  12 +
  13 + indexService.index(uid, udid, channel, isStudent).then((result) => {
  14 + return res.render('index', {
  15 + meIndexPage: true,
  16 + me: result
  17 + });
  18 + }).catch(next);
46 }; 19 };
47 20
48 module.exports = { 21 module.exports = {
1 -/**  
2 - * @author: weiqingting<qingting.wei@yoho.cn>  
3 - */  
4 -  
5 'use strict'; 1 'use strict';
6 2
7 -  
8 module.exports.xchangeDay = 15; 3 module.exports.xchangeDay = 15;
9 4
10 module.exports.refundDay = 7; 5 module.exports.refundDay = 7;
1 -/**  
2 - * @author: weiqingting<qingting.wei@yoho.cn>  
3 - */  
4 'use strict'; 1 'use strict';
5 2
6 const api = global.yoho.API; 3 const api = global.yoho.API;
7 4
8 -const getPendingOrderCount = uid=>{  
9 - let options = {  
10 - method: 'web.SpaceOrders.getPendingOrderCount', 5 +const pendingOrderCount = (uid) => {
  6 + return api.get('', {
  7 + method: 'web.SpaceOrders.pendingOrderCount',
11 uid: uid 8 uid: uid
12 - };  
13 - return api.get('', options); 9 + });
14 }; 10 };
15 -const infoNum = (uid, udid)=>{  
16 - let options = { 11 +
  12 +const unreadMessageCount = (uid, udid)=>{
  13 + return api.get('', {
17 method: 'app.home.getInfoNum', 14 method: 'app.home.getInfoNum',
18 uid: uid, 15 uid: uid,
19 udid: udid 16 udid: udid
20 - };  
21 - return api.get('', options); 17 + });
22 }; 18 };
23 -const notCommentRecordCount = uid=>{  
24 - let options = { 19 +
  20 +const needCommentCount = (uid) =>{
  21 + return api.get('', {
25 method: 'show.notCommentRecordCount', 22 method: 'show.notCommentRecordCount',
26 uid: uid 23 uid: uid
27 - };  
28 - return api.get('', options); 24 + });
  25 +};
  26 +
  27 +const guessBrand = () => {
  28 + return api.get('', {
  29 + method: 'web.search.favorBrand'
  30 + });
  31 +};
  32 +
  33 +const newArrival = () => {
  34 + return api.get('', {
  35 + method: 'web.search.search',
  36 + sales: 'Y',
  37 + outlets: 2,
  38 + stocknumber: 1,
  39 + new: 'Y',
  40 + order: 's_t_desc',
  41 + viewNum: 0
  42 + });
  43 +};
  44 +
  45 +/**
  46 + * 优选新品
  47 + *
  48 + * @param int $channel 频道,1代表男生,2代表女生,3代表潮童,4代表创意生活
  49 + * @param $uid 用户ID
  50 + * @param $udid 设备ID
  51 + * @param $rec_pos 位置码
  52 + * @param $limit 数量限制
  53 + * @return array 接口返回的数据
  54 + */
  55 +const recommend = (channelNum, uid, udid, pos, limit) => {
  56 + return api.get('', {
  57 + method: 'app.home.newPreference',
  58 + yh_channel: channelNum,
  59 + uid: uid,
  60 + udid: udid,
  61 + rec_pos: pos,
  62 + limit: limit
  63 + });
  64 +};
  65 +
  66 +/**
  67 + * 根据节点和运行模式选择静态内容
  68 + * @param $node 20141219-100447
  69 + * @param string $mode
  70 + * @return mixed
  71 + */
  72 +const getByNodeContent = (node, mode) => {
  73 + return api.get('', {
  74 + method: 'web.html.content',
  75 + mode: mode,
  76 + node: node
  77 + });
29 }; 78 };
30 79
31 module.exports = { 80 module.exports = {
32 - getPendingOrderCount,  
33 - infoNum,  
34 - notCommentRecordCount 81 + pendingOrderCount,
  82 + unreadMessageCount,
  83 + needCommentCount,
  84 + guessBrand,
  85 + newArrival,
  86 + recommend,
  87 + getByNodeContent
35 }; 88 };
1 -/**  
2 - * @author: weiqingting<qingting.wei@yoho.cn>  
3 - */  
4 'use strict'; 1 'use strict';
5 2
6 const Promise = require('bluebird'); 3 const Promise = require('bluebird');
7 const co = Promise.coroutine; 4 const co = Promise.coroutine;
8 -const OrderData = require('./orders-api');  
9 -const OrderModel = require('./orders-service'); 5 +const _ = require('lodash');
  6 +const Fn = require('lodash/fp');
  7 +
10 const helpers = global.yoho.helpers; 8 const helpers = global.yoho.helpers;
11 -const api = global.yoho.API;  
12 -const searchApi = global.yoho.SearchAPI;  
13 9
14 -const BrandData = require('./BrandData');  
15 -const IndexData = require('./index-api');  
16 -const SearchData = require('./SearchData'); 10 +const orderService = require('./orders-service');
  11 +const indexApi = require('./index-api');
17 12
18 -const HelperHome = require('./HelperHome'); 13 +const CHANNEL_NUM = {
  14 + boys: 1,
  15 + girls: 2,
  16 + kids: 3,
  17 + lifestyle: 4
  18 +};
19 19
20 -const _ = require('lodash'); 20 +const _channelNum = channel => CHANNEL_NUM[channel] || CHANNEL_NUM.boys;
  21 +
  22 +/**
  23 + * 处理品牌
  24 + */
  25 +const _handleBrand = (brands, needNum) => {
  26 + const handle = Fn.pipe(Fn.filter({is_hot: 'Y'}), Fn.take(needNum), Fn.map((brand) => ({
  27 + href: helpers.urlFormat(null, null, brand.brand_domain),
  28 + logo: helpers.getForceSourceUrl(brand.brand_ico),
  29 + name: brand.brand_name
  30 + })));
  31 +
  32 + return handle(brands);
  33 +};
  34 +
  35 +/**
  36 + * 处理商品
  37 + */
  38 +const _handleProduct = (products) => {
  39 + return products.map((product) => {
  40 + let img = helpers.image(_.get(product, 'default_images', ''), 100, 100);
  41 +
  42 + if (img.indexOf('imageView') !== -1) {
  43 + img = img.split('imageView', 1) +
  44 + 'imageMogr2/thumbnail/100x100/extent/100x100/background/d2hpdGU=/position/center/quality/90';
  45 + }
  46 +
  47 + return {
  48 + href: helpers.getUrlBySkc(product.product_id,
  49 + _.get(product, 'goods_list[0].product_skc', ''),
  50 + product.cn_alphabet
  51 + ),
  52 + thumb: img,
  53 + name: product.product_name,
  54 + price: product.sales_price,
  55 + productId: product.product_id
  56 + };
  57 + });
  58 +};
21 59
22 /** 60 /**
23 - * 个人中心——消息提示  
24 - * @param type uid  
25 - * @param type udid  
26 - * @return array 61 + * 消息数量提示
27 */ 62 */
28 -const getInfoNumData = (uid, udid)=>{ 63 +const _msgNumber = co(function * (uid, udid) {
29 let result = [ 64 let result = [
30 - {href: helpers.urlFormat('/home/orders'), name: '待处理订单', 'count': 0},  
31 - {href: helpers.urlFormat('/home/message'), name: '未读消息', 'count': 0},  
32 - {href: helpers.urlFormat('/home/comment'), name: '待评论商品', 'count': 0} 65 + {href: helpers.urlFormat('/home/orders'), name: '待处理订单', count: 0},
  66 + {href: helpers.urlFormat('/home/message'), name: '未读消息', count: 0},
  67 + {href: helpers.urlFormat('/home/comment'), name: '待评论商品', count: 0}
33 ]; 68 ];
34 - return co(function * () {  
35 - let getPendingOrderCount = yield IndexData.getPendingOrderCount(uid);// 待处理订单  
36 - let infoNumData = yield IndexData.infoNum(uid, udid); // 未读消息  
37 - let notCommentRecordCount = yield IndexData.notCommentRecordCount(uid);// 待评论商品  
38 - result[0]['count'] = getPendingOrderCount.data.count ? getPendingOrderCount.data.count : 0;  
39 - result[1]['count'] = infoNumData.data.inbox_total ? infoNumData.data.inbox_total : 0;  
40 - result[2]['count'] = notCommentRecordCount.data ? notCommentRecordCount.data : 0;  
41 - return result;  
42 - })();  
43 -};  
44 69
  70 + let reqData = yield Promise.props({
  71 + pending: indexApi.pendingOrderCount(uid), // 待处理订单
  72 + unread: indexApi.unreadMessageCount(uid, udid), // 未读消息
  73 + needComment: indexApi.needCommentCount(uid) // 待评论商品
  74 + });
  75 +
  76 + result[0].count = _.get(reqData, 'pending.data.count', 0);
  77 + result[1].count = _.get(reqData, 'unread.data.count', 0);
  78 + result[2].count = _.get(reqData, 'needComment.data.count', 0);
  79 +
  80 + return result;
  81 +});
45 82
46 /** 83 /**
47 * 个人中心——最新订单 84 * 个人中心——最新订单
48 - * @param type uid  
49 - * @return array  
50 */ 85 */
51 -const latestOrders = (uid)=>{  
52 - return co(function *() {  
53 - let orders = yield OrderModel.getOrders(uid, 1, 2, 1); 86 +const _recentOrder = co(function * (uid) {
  87 + let latestOrder = yield orderService.getOrders(orderService.ORDER_TYPE.all, uid, 1, 2);
  88 +
54 return { 89 return {
55 more: helpers.urlFormat('/home/orders'), 90 more: helpers.urlFormat('/home/orders'),
56 - orders: orders 91 + orders: latestOrder
57 }; 92 };
58 - })();  
59 -};  
60 -const homeData = ()=>{  
61 - return co(function * () {  
62 - let result = {};  
63 - let url = {};  
64 - url.fav_brand = SearchData.getBrandListUrl();  
65 - url.new = SearchData.getProductUrl({new: 'Y', viewNum: 10});  
66 - let data = yield Promise.all([searchApi.get(url.fav_brand, {}, {cache: true}), searchApi.get(url.new, {}, {cache: true})]);  
67 -  
68 - // 格式化数据  
69 - result['brand'] = data[0].data && data[0].data.length > 0 ? HelperHome.formatFavBrand(data[0].data, 6) : [];  
70 - result['new'] = data[1].data['product_list'] && data[1].data['product_list'].length > 0 ? HelperHome.formatNew(data[1].data['product_list']) : {};  
71 - return result;  
72 - })();  
73 -}; 93 +});
  94 +
  95 +/**
  96 + * 你喜欢的品牌
  97 + */
  98 +const _guessYouLikeBrand = co(function * () {
  99 + let brand = yield indexApi.guessBrand();
  100 + const NEED_BRAND_NUM = 6;
  101 +
  102 + return _handleBrand(_.get(brand, 'data', []), NEED_BRAND_NUM);
  103 +});
  104 +
  105 +/**
  106 + * 新品
  107 + */
  108 +const _newProduct = co(function * () {
  109 + let newProduct = yield indexApi.newArrival();
  110 +
  111 + return _handleProduct(_.get(newProduct, 'data.product_list', []));
  112 +});
  113 +
  114 +/**
  115 + * 为你优选
  116 + */
  117 +const _recommend = co(function * (channelNum, uid, udid) {
  118 + let resData = yield indexApi.recommend(channelNum, uid, udid, '100004', 30);
  119 +
  120 + return _handleProduct(_.get(resData, 'data.product_list', []));
  121 +});
74 122
75 /** 123 /**
76 * 底部banner 124 * 底部banner
77 - * @param string code  
78 - * @return mixed  
79 */ 125 */
80 -const getFooterBanner = (code)=>{  
81 -  
82 - code = code || '20110609-152143';  
83 - return co(function *() {  
84 - let result = '';  
85 - let banner = yield BrandData.getByNodeContent(code);  
86 - if (banner.code && banner.data) {  
87 - result = banner.data.replace('http://', '//'); 126 +const _footerBanner = co(function * () {
  127 + const CODE = '20110609-152143';
  128 + let banner = yield indexApi.getByNodeContent(CODE);
  129 +
  130 + return _.get(banner, 'data', '').replace('http://', '//');
  131 +});
  132 +
  133 +/**
  134 + * 取消订单
  135 + */
  136 +const _cancelReason = orderService.closeReason;
  137 +
  138 +const index = co(function * (uid, udid, channel, isStudent) {
  139 + let reqData = yield Promise.props({
  140 + msgNumber: _msgNumber(uid, udid),
  141 + recentOrder: _recentOrder(uid),
  142 + guessBrand: _guessYouLikeBrand(),
  143 + newProduct: _newProduct(),
  144 + recommendProduct: _recommend(_channelNum(channel), uid, udid),
  145 + footerBanner: _footerBanner(),
  146 + reason: _cancelReason()
  147 + });
  148 +
  149 + return {
  150 + certifiedName: isStudent ? '学生身份已验证' : '身份验证',
  151 + certifiedUrl: helpers.urlFormat('/product/students/'),
  152 + cancelReason: reqData.reason,
  153 + content: [
  154 + {
  155 + message: reqData.msgNumber
  156 + },
  157 + {
  158 + latestOrders: reqData.recentOrder,
  159 + favBrand: {
  160 + more: '/brands',
  161 + brands: reqData.guessBrand
  162 + },
  163 + newArrival: reqData.newProduct
88 } 164 }
89 - return result;  
90 - })();  
91 -}; 165 + ],
  166 + recommend: reqData.recommendProduct,
  167 + banner: reqData.footerBanner,
  168 + helpUsUrl: ''
  169 + };
  170 +});
  171 +
92 172
93 module.exports = { 173 module.exports = {
94 - getInfoNumData,  
95 - getFooterBanner,  
96 - latestOrders,  
97 - homeData 174 + index
98 }; 175 };
99 176
1 -/**  
2 - * @author: weiqingting<qingting.wei@yoho.cn>  
3 - */  
4 'use strict'; 1 'use strict';
5 2
6 const Promise = require('bluebird'); 3 const Promise = require('bluebird');
@@ -11,7 +8,7 @@ const helpers = global.yoho.helpers; @@ -11,7 +8,7 @@ const helpers = global.yoho.helpers;
11 8
12 const pager = require('./pager').handlePagerData; 9 const pager = require('./pager').handlePagerData;
13 const orderApi = require('./orders-api'); 10 const orderApi = require('./orders-api');
14 -const ChannelConfig = require('./ChannelConfig'); 11 +const ChannelConfig = require('./channel-config');
15 12
16 const ORDER_TYPE = { 13 const ORDER_TYPE = {
17 all: 1, // 全部 14 all: 1, // 全部
@@ -865,10 +862,16 @@ const confirm = orderApi.confirmUserOrder; @@ -865,10 +862,16 @@ const confirm = orderApi.confirmUserOrder;
865 862
866 const cancel = orderApi.cancelUserOrder; 863 const cancel = orderApi.cancelUserOrder;
867 864
  865 +const closeReason = () => {
  866 + return orderApi.closeReasons().then((result) => {
  867 + return _.get(result, 'data', []);
  868 + });
  869 +};
  870 +
868 const detail = co(function * (uid, orderId) { 871 const detail = co(function * (uid, orderId) {
869 let apiData = yield Promise.props({ 872 let apiData = yield Promise.props({
870 detailData: _getOrder(uid, orderId), 873 detailData: _getOrder(uid, orderId),
871 - reason: orderApi.closeReasons() 874 + reason: closeReason()
872 }); 875 });
873 876
874 if (apiData.detailData.code === 400) { 877 if (apiData.detailData.code === 400) {
@@ -878,7 +881,7 @@ const detail = co(function * (uid, orderId) { @@ -878,7 +881,7 @@ const detail = co(function * (uid, orderId) {
878 return { 881 return {
879 detail: apiData.detailData, 882 detail: apiData.detailData,
880 package: apiData.detailData.package, 883 package: apiData.detailData.package,
881 - cancelReason: _.get(apiData.reason, 'data', []) 884 + cancelReason: apiData.reason
882 }; 885 };
883 }); 886 });
884 887
@@ -890,5 +893,7 @@ module.exports = { @@ -890,5 +893,7 @@ module.exports = {
890 updateDeliveryAddress, 893 updateDeliveryAddress,
891 confirm, 894 confirm,
892 cancel, 895 cancel,
893 - detail 896 + detail,
  897 + getOrders,
  898 + closeReason
894 }; 899 };
@@ -20,21 +20,6 @@ const _getGender = (channel) => { @@ -20,21 +20,6 @@ const _getGender = (channel) => {
20 } 20 }
21 }; 21 };
22 22
23 -const udidRequired = (req, res, next) => {  
24 - let udid = req.cookies.udid;  
25 -  
26 - if (!udid) {  
27 - udid = md5(req.ip || requestIp.getClientIp(req));  
28 -  
29 - if (res && res.cookie) {  
30 - res.cookie('udid', udid);  
31 - }  
32 -  
33 - req.user.udid = udid;  
34 - }  
35 - next();  
36 -};  
37 -  
38 module.exports = () => { 23 module.exports = () => {
39 return (req, res, next) => { 24 return (req, res, next) => {
40 let yoho = { 25 let yoho = {