Authored by htoooth

refactor

'use strict';
const headerModel = require('../../../doraemon/models/header');
const msgModel = require('../models/message');
const helpers = global.yoho.helpers;
const _ = require('lodash');
const homeNav = (req) => {
return [
{
title: '交易管理',
subNav: [
{name: '我的订单', href: '/home/orders', catchs: ['/home/orders', '/home/index', '/home/orders/detail']},
{name: '我的收藏', href: '/home/favorite', catchs: ['/home/favorite/reduction']},
{name: '我的有货币', href: '/home/currency'},
{name: '我的红包', href: '/home/redenvelopes'},
{name: '我的优惠券', href: '/home/coupons'},
{name: '我的VIP', href: '/home/vip'}
]
},
{
title: '服务中心',
subNav: [
{name: '我的退/换货', href: '/home/returns'},
{name: '我的咨询', href: '/home/consult'},
{name: '我的评论', href: '/home/comment'},
// {name: '我的投诉', href: '/home/complaints'},
{name: '我的信息', href: '/home/message', count: 0},
{
name: '在线客服',
href: _.get(req.app.locals.pc, 'clientService.new', false) ?
'/service/client' : 'http://chat8.live800.com/live800/chatClient/chatbox.jsp?companyID=620092&configID=149091&jid=8732423409',
isBlank: true
}
]
},
{
title: '个人信息管理',
subNav: [
{name: '编辑个人资料', href: '/home/user'},
{name: '账号安全', href: '/home/account', catchs: [
'/home/account/userpwd',
'/home/account/email',
'/home/account/mobile',
'/home/account/checkverifycode',
'/home/account/checkpassword',
'/home/account/verifypassword',
'/home/account/modifypwd',
'/home/account/sendemail',
'/home/account/checkemail',
'/home/account/modifyemail',
'/home/account/sendemailsuccess',
'/home/account/mailresult',
'/home/account/checkmobile',
'/home/account/checkmobilemsg',
'/home/account/sendmobilemsg',
'/home/account/modifymobile'
]},
{name: '地址管理', href: '/home/address'},
{name: '兑换礼品卡', href: '/home/gift'}
]
}
];
};
const getActiveNav = (req, count)=>{
let mHomeNav = _.cloneDeep(homeNav(req));
let activeNav = null;
mHomeNav = mHomeNav.map((item) => {
item.subNav = item.subNav.map((nav) => {
let curMatchPath = req.originalUrl;
if (!nav.matchQuery && curMatchPath.indexOf('?') >= 0) { // 严格的路径匹配,包含后面的参数
curMatchPath = curMatchPath.substr(0, curMatchPath.indexOf('?'));
}
if (curMatchPath === nav.href) {
nav.active = true;
}
if (nav.name === '我的信息') {
nav.count = +count;
}
if (nav.catchs) {
let index = nav.catchs.indexOf(curMatchPath);
if (index > -1) {
nav.active = true;
nav.curHref = nav.catchs[index];
}
}
if (nav.active) {
activeNav = nav;
}
nav.href = nav.href.indexOf('http://') > -1 ? nav.href : helpers.urlFormat(nav.href);
return nav;
});
return item;
});
return {
homeNav: mHomeNav,
activeNav: activeNav
};
};
const getHomeNav = (req, res, next) => {
msgModel.unreadTotal(req.user.uid).then(result => {
let navs = getActiveNav(req, _.get(result, 'data.total', 0));
let activeNav = navs.activeNav;
let bread = [{href: helpers.urlFormat('/'), name: 'YOHO!BUY 有货首页'}];
if (activeNav) {
bread.push({
name: '个人中心',
href: helpers.urlFormat('/home')
});
bread.push({
name: activeNav.name
});
// 订单详情
if (activeNav.curHref === '/home/orders/detail') {
Object.assign(_.last(bread), {
href: helpers.urlFormat('/home/orders')
});
bread.push({
name: '订单详情'
});
}
} else {
bread.push({
name: '个人中心'
});
}
res.locals.path = bread; // [{href: helpers.urlFormat('/'), name: 'YOHO!BUY 有货首页'}, {name: '个人中心'}];
res.locals.homeNav = navs.homeNav;
res.locals.userThumb = '//img10.static.yhbimg.com/headimg/' +
'2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif?imageView/2/w/100/h/100';
next();
});
};
const getCommonHeader = (req, res, next) => {
let channel = req.query.channel ? req.query.channel : 'boys';
headerModel.requestHeaderData(channel).then((result)=>{
_.merge(res.locals, result);
next();
});
};
module.exports = {
homeNav,
getActiveNav,
getHomeNav,
getCommonHeader
};
... ...
... ... @@ -8,11 +8,8 @@
const express = require('express');
const router = express.Router(); // eslint-disable-line
const cRoot = './controllers';
const headerModel = require('../../doraemon/models/header');
const msgModel = require('./models/message');
const _ = require('lodash');
const helpers = global.yoho.helpers;
const tabsMiddleware = require(`${cRoot}/general-tabs`);
const indexController = require(`${cRoot}/index`);
const commentController = require(`${cRoot}/comment`);
... ... @@ -42,183 +39,30 @@ const AddressController = require(`${cRoot}/address`);
const giftController = require(`${cRoot}/gift`);
const homeNav = (req) => {
return [
{
title: '交易管理',
subNav: [
{name: '我的订单', href: '/home/orders', catchs: ['/home/orders', '/home/index', '/home/orders/detail']},
{name: '我的收藏', href: '/home/favorite', catchs: ['/home/favorite/reduction']},
{name: '我的有货币', href: '/home/currency'},
{name: '我的红包', href: '/home/redenvelopes'},
{name: '我的优惠券', href: '/home/coupons'},
{name: '我的VIP', href: '/home/vip'}
]
},
{
title: '服务中心',
subNav: [
{name: '我的退/换货', href: '/home/returns'},
{name: '我的咨询', href: '/home/consult'},
{name: '我的评论', href: '/home/comment'},
// {name: '我的投诉', href: '/home/complaints'},
{name: '我的信息', href: '/home/message', count: 0},
{
name: '在线客服',
href: _.get(req.app.locals.pc, 'clientService.new', false) ?
'/service/client' : 'http://chat8.live800.com/live800/chatClient/chatbox.jsp?companyID=620092&configID=149091&jid=8732423409',
isBlank: true
}
]
},
{
title: '个人信息管理',
subNav: [
{name: '编辑个人资料', href: '/home/user'},
{name: '账号安全', href: '/home/account', catchs: [
'/home/account/userpwd',
'/home/account/email',
'/home/account/mobile',
'/home/account/checkverifycode',
'/home/account/checkpassword',
'/home/account/verifypassword',
'/home/account/modifypwd',
'/home/account/sendemail',
'/home/account/checkemail',
'/home/account/modifyemail',
'/home/account/sendemailsuccess',
'/home/account/mailresult',
'/home/account/checkmobile',
'/home/account/checkmobilemsg',
'/home/account/sendmobilemsg',
'/home/account/modifymobile'
]},
{name: '地址管理', href: '/home/address'},
{name: '兑换礼品卡', href: '/home/gift'}
]
}
];
};
const getActiveNav = (req, count)=>{
let mHomeNav = _.cloneDeep(homeNav(req));
let activeNav = null;
mHomeNav = mHomeNav.map((item) => {
item.subNav = item.subNav.map((nav) => {
let curMatchPath = req.originalUrl;
if (!nav.matchQuery && curMatchPath.indexOf('?') >= 0) { // 严格的路径匹配,包含后面的参数
curMatchPath = curMatchPath.substr(0, curMatchPath.indexOf('?'));
}
if (curMatchPath === nav.href) {
nav.active = true;
}
if (nav.name === '我的信息') {
nav.count = +count;
}
if (nav.catchs) {
let index = nav.catchs.indexOf(curMatchPath);
if (index > -1) {
nav.active = true;
nav.curHref = nav.catchs[index];
}
}
if (nav.active) {
activeNav = nav;
}
nav.href = nav.href.indexOf('http://') > -1 ? nav.href : helpers.urlFormat(nav.href);
return nav;
});
return item;
});
return {
homeNav: mHomeNav,
activeNav: activeNav
};
};
const getHomeNav = (req, res, next) => {
msgModel.unreadTotal(req.user.uid).then(result => {
let navs = getActiveNav(req, _.get(result, 'data.total', 0));
let activeNav = navs.activeNav;
let bread = [{href: helpers.urlFormat('/'), name: 'YOHO!BUY 有货首页'}];
if (activeNav) {
bread.push({
name: '个人中心',
href: helpers.urlFormat('/home')
});
bread.push({
name: activeNav.name
});
// 订单详情
if (activeNav.curHref === '/home/orders/detail') {
Object.assign(_.last(bread), {
href: helpers.urlFormat('/home/orders')
});
bread.push({
name: '订单详情'
});
}
} else {
bread.push({
name: '个人中心'
});
}
res.locals.path = bread; // [{href: helpers.urlFormat('/'), name: 'YOHO!BUY 有货首页'}, {name: '个人中心'}];
res.locals.homeNav = navs.homeNav;
res.locals.userThumb = '//img10.static.yhbimg.com/headimg/' +
'2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif?imageView/2/w/100/h/100';
next();
});
};
const getCommonHeader = (req, res, next) => {
let channel = req.query.channel ? req.query.channel : 'boys';
headerModel.requestHeaderData(channel).then((result)=>{
_.merge(res.locals, result);
next();
});
};
// 首页
router.get(['/index', '/'], [getHomeNav, getCommonHeader], indexController.index);
router.get(['/index', '/'], [tabsMiddleware.getHomeNav, tabsMiddleware.getCommonHeader], indexController.index);
// 查看二维码
router.get('/QRcode', [getHomeNav, getCommonHeader], personalController.QRcode);
router.get('/QRcode', [tabsMiddleware.getHomeNav, tabsMiddleware.getCommonHeader], personalController.QRcode);
// 我的评论
router.get('/comment', [getCommonHeader, getHomeNav], commentController.index);
router.get('/comment', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], commentController.index);
router.post('/comment/saveComment', commentController.saveComment);
router.get('/comment/order', [getCommonHeader, getHomeNav], commentController.commentList4Order);
router.get('/comment/order', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], commentController.commentList4Order);
// 我的咨询
// router.get('/consult', [getCommonHeader, getHomeNav], consultController.index);
// router.get('/consult', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], consultController.index);
// 我的投诉
// router.get('/complaints', [getCommonHeader, getHomeNav], complaintsController.index);
// router.get('/complaints', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], complaintsController.index);
// router.post('/complaints/submit', complaintsController.submit);
// router.post('/complaints/cancel', complaintsController.cancel);
// 我的消息
router.get('/message', [getCommonHeader, getHomeNav], messageController.index);
router.get('/message/detail', [getCommonHeader, getHomeNav], messageController.detail);
router.get('/message', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], messageController.index);
router.get('/message/detail', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], messageController.detail);
router.get('/message/del', messageController.delMsg);
router.get('/message/read', messageController.readMsg);
router.get('/message/pickCoupon', messageController.pickCoupon);
... ... @@ -231,30 +75,29 @@ router.get('/message/pickCoupon', messageController.pickCoupon);
// router.get('/exchange/detail', returnsController.refundDetail);
// router.get('/returns/success', returnsController.index);
// router.get('/index', [getCommonHeader, getHomeNav], IndexController.index);
// router.get('/index', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], IndexController.index);
/* 我的YOHO币 */
router.get('/currency', [getCommonHeader, getHomeNav], currencyController.index);
router.get('/currency', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], currencyController.index);
/* 我的红包 */
router.get('/redenvelopes', [getCommonHeader, getHomeNav], redenvelopesController.index);
router.get('/redenvelopes', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], redenvelopesController.index);
/* 我的VIP */
router.get('/vip', [getCommonHeader, getHomeNav], vipController.index);
router.get('/vip', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], vipController.index);
/* 我的优惠卷 */
router.get('/coupons', [getCommonHeader, getHomeNav], couponsController.index);
// router.get('/vip', VipController.index);
router.get('/coupons', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], couponsController.index);
/********************************* 我的收藏开始 *****************************************/
// 我的收藏
router.get('/favorite', [getCommonHeader, getHomeNav], favoriteController.index);
router.get('/favorite', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], favoriteController.index);
// 品牌页新品到着
router.get('/favorite/newproduct', favoriteController.newProduct);
// 降价提醒
router.get('/favorite/reduction', [getCommonHeader, getHomeNav], favoriteController.reduction);
router.get('/favorite/reduction', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], favoriteController.reduction);
// 设置降价提醒
router.get('/favorite/notice', favoriteController.notice);
... ... @@ -268,7 +111,7 @@ router.get('/favorite/cancel', favoriteController.cancel);
/********************************* 我的收藏结束 *****************************************/
/********************************* 我的订单开始 *****************************************/
router.get('/orders', [getCommonHeader, getHomeNav], ordersController.index);
router.get('/orders', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], ordersController.index);
// 重新加入购物车
router.post('/orders/readd', ordersController.reBuy);
... ... @@ -286,7 +129,7 @@ router.post('/orders/confirmOrder', ordersController.confirm);
router.post('/orders/cancelOrder', ordersController.cancel);
// 订单详情
router.get('/orders/detail', [getCommonHeader, getHomeNav], ordersController.detail);
router.get('/orders/detail', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], ordersController.detail);
// 物流信息
router.get('/orders/express', ordersController.express);
... ... @@ -296,7 +139,7 @@ router.get('/orders/express', ordersController.express);
// router.get('/coupons', CouponsController.index);
router.get('/user', [getCommonHeader, getHomeNav], UserController.index);
router.get('/user', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], UserController.index);
router.post('/user/edituserinfo', UserController.editUserInfo);
... ... @@ -340,31 +183,31 @@ router.get('/bind/renren/callback', bindController.renren.callback);
router.post('/cancelbind/:type', bindController.cancelBind);
// 账号安全
router.get('/account', [getCommonHeader, getHomeNav], AccountController.index);
router.get('/account', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.index);
router.get('/account/userpwd', [getCommonHeader, getHomeNav], AccountController.userPwd);
router.get('/account/userpwd', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.userPwd);
router.get('/account/email', [getCommonHeader, getHomeNav], AccountController.userEmail);
router.get('/account/email', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.userEmail);
router.get('/account/mobile', [getCommonHeader, getHomeNav], AccountController.userMobile);
router.get('/account/mobile', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.userMobile);
router.post('/account/checkverifycode', [getCommonHeader, getHomeNav], AccountController.checkVerifyCode);
router.post('/account/checkverifycode', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.checkVerifyCode);
router.post('/account/checkpassword', [getCommonHeader, getHomeNav], AccountController.checkPassword);
router.post('/account/checkpassword', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.checkPassword);
router.post('/account/verifypassword', [getCommonHeader, getHomeNav], AccountController.verifyPassword);
router.post('/account/verifypassword', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.verifyPassword);
router.post('/account/modifypwd', [getCommonHeader, getHomeNav], AccountController.modifyPwd);
router.post('/account/modifypwd', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.modifyPwd);
router.post('/account/sendemail', AccountController.sendEmail);
router.post('/account/checkemail', [getCommonHeader, getHomeNav], AccountController.checkEmail);
router.post('/account/checkemail', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.checkEmail);
router.post('/account/modifyemail', [getCommonHeader, getHomeNav], AccountController.modifyEmail);
router.post('/account/modifyemail', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.modifyEmail);
router.get('/account/sendemailsuccess', [getCommonHeader, getHomeNav], AccountController.sendEmailSuccess);
router.get('/account/sendemailsuccess', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.sendEmailSuccess);
router.get('/account/mailresult', [getCommonHeader, getHomeNav], AccountController.mailResult);
router.get('/account/mailresult', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AccountController.mailResult);
router.post('/account/checkmobile', AccountController.checkMobile);
... ... @@ -376,7 +219,7 @@ router.post('/account/modifymobile', AccountController.modifyMobile);
// 地址管理
router.get('/address', [getCommonHeader, getHomeNav], AddressController.index);
router.get('/address', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], AddressController.index);
router.get('/address/area', UserController.getProviceList);
... ... @@ -390,7 +233,7 @@ router.get('/address/defaultAddress', AddressController.defaultAddress);
// 兑换礼品卡
router.get('/gift', [getCommonHeader, getHomeNav], giftController.index);
router.get('/gift', [tabsMiddleware.getCommonHeader, tabsMiddleware.getHomeNav], giftController.index);
router.post('/gift/exchange', giftController.exchange);
... ...