list.js 8.93 KB
/*
 * @Author: sefon
 * @Date:   2016-07-24 11:40:21
 */

'use strict';
const mRoot = '../models';
const list = require(`${mRoot}/list`);
const listSeoMap = require(`${global.middleware}/seo/listSeoMap`);
const helpers = global.yoho.helpers;
const _ = require('lodash');

// 搜索相关接口
const searchApi = require(`${mRoot}/search-api`);

// 店铺页
const shop = (shopId, req, res, next, brandInfo) => {
    let domain = req.query.domain,
        params = req.query;

    _.unset(params, 'domain');
    shopId = parseInt(shopId, 10);
    Object.assign(params, {shopId: shopId});

    list.getShopInfo(shopId, req.user.uid).then(shopInfo => {
        let pjax = params._pjax;

        // 比较品牌域名与店铺域名是否一致,不一致跳转至店铺域名
        if (!pjax && shopInfo.domain && domain && domain !== _.toLower(shopInfo.domain)) {
            res.redirect(helpers.urlFormat('', params, shopInfo.domain));
            return;
        }

        if (+shopInfo.shopTemplateType === 2) { // 经典模板

            if (pjax) {
                list.getShopGoodsData(shopId, req.yoho.channel, params, shopInfo).then(result => {
                    Object.assign(result, {
                        shopId: shopId,
                        layout: false
                    });
                    res.render('list/goods-list', result);
                }).catch(next);
                return;
            }

            list.getShopData(shopId, req.yoho.channel, params, shopInfo).then(result => {
                Object.assign(result, {
                    page: 'shop',
                    shopId: shopId
                });

                // 店铺装修为空则不cache
                if (!result.shopTopBanner) {
                    res.set('Cache-Control', 'no-cache');
                }
                res.render('list/shop-index', result);
            }).catch(next);
        } else { // 基础模板
            list.getBaseShopData(params, Object.assign({uid: req.user.uid}, brandInfo),
                req.yoho.channel, shopId).then(result => {
                    Object.assign(result, {page: 'list'});

                    // 基础店铺装修为空则不cache
                    if (!result.brand || !result.brand.shopBanner) {
                        res.set('Cache-Control', 'no-cache');
                    }
                    res.render('list/brand', result);
                }).catch(next);
        }
    }).catch(next);
};

/**
 * 商品分类列表页
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.index = (req, res, next) => {
    let resData = {};
    let qs = req._parsedOriginalUrl.query || '';

    list.getListData(req.query, req.yoho.channel).then(result => {
        Object.assign(resData, result);

        if (qs) {
            Object.assign(resData, listSeoMap[qs] || {});
        }

        // 查询结果为空则不cache
        if (_.isEmpty(_.get(resData, 'list.goods', []))) {
            res.set('Cache-Control', 'no-cache');
        }

        // 商品详情页跳转过来带参数
        if (req.query.phrase) {
            resData.list.phrase = decodeURIComponent(req.query.phrase);
        }

        res.render('list/index', resData);
    }).catch(next);
};

/**
 * 新品到着
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.new = (req, res, next) => {
    let resData = {};

    list.getListNewData(Object.assign({order: 's_t_desc'}, req.query), req.yoho.channel).then(result => {
        Object.assign(resData, result, {
            hideInfo: {from: 'newProduct'}
        });

        // 查询结果为空则不cache
        if (_.isEmpty(_.get(resData, 'list.goods', []))) {
            res.set('Cache-Control', 'no-cache');
        }

        res.render('list/index', resData);
    }).catch(next);

};

/**
 * 品牌页
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.brand = (req, res, next) => {
    let brandDomain = req.query.domain;
    let shopId = req.query.shopId;
    let resData = {};

    // shopId存在,直接走店铺
    if (shopId) {
        return shop(shopId, req, res, next);
    }

    if (!brandDomain) {
        return next();
    }

    // 获取品牌信息
    list.getBrandInfo({domain: brandDomain}).then(brandInfo => {
        if (!brandInfo.hasOwnProperty('type')) {
            return res.redirect(helpers.urlFormat(''));
        }

        switch (parseInt(brandInfo.type, 10)) {
            case 1: // 搜索
                res.redirect(helpers.urlFormat('', {query: brandInfo.brandDomain}, 'search'));
                break;
            case 2: // 店铺
                return shop(brandInfo.shopId, req, res, next, brandInfo);
            default: // 品牌
                return list.getBrandData(req.query, Object.assign({uid: req.user.uid}, brandInfo),
                    req.yoho.channel).then(result => {
                        Object.assign(resData, result, {
                            page: 'list'
                        });
                        res.render('list/brand', resData);
                    });
        }
    }).catch(next);
};

/**
 * 品牌介绍页
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.brandAbout = (req, res, next) => {
    let brandDomain = req.query.domain;
    let shopId = req.query.shopId;

    if (!brandDomain) {
        return res.redirect(helpers.urlFormat(''));
    }

    if (shopId) {
        list.getShopAbout(shopId, req.user.uid).then(result => {
            res.render('list/brand', Object.assign(result, {page: 'list'}));
        }).catch(next);
    } else {
        list.getBrandAbout(brandDomain, req.user.uid, req.yoho.channel).then(result => {
            res.render('list/brand', Object.assign(result, {page: 'list'}));
        }).catch(next);
    }
};

/**
 * 店铺商品列表页
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.shopList = (req, res, next) => {
    let shopId = req.query.shopId;

    if (!shopId) {
        return next();
    }

    list.getShopListData(req.yoho.channel, req.query, req.user.uid).then(result => {
        Object.assign(result, {
            page: 'shop',
            shopId: shopId
        });
        if (req.query.query) {
            result.shopKey = req.query.query;
        }

        // 店铺装修为空则不cache
        if (!result.shopTopBanner) {
            res.set('Cache-Control', 'no-cache');
        }
        res.render('list/shop-list', result);
    }).catch(next);
};

/**
 * ajax调用品牌页左侧水牌
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.getNodeContent = (req, res, next) => {

    if (!req.xhr || !req.body.node) {
        return next();
    }

    list.getNodeContentData(req.body).then(result => {
        res.json(result);
    }).catch(next);
};

/**
 * ajax调用品牌页左侧series folder
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.getAdnav = (req, res, next) => {

    if (!req.xhr) {
        return next();
    }

    list.getAdnav(req.body).then(result => {
        res.json(result);
    }).catch(next);
};



/**
 * 判断用户是否收藏品牌页
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.isFavoriteBrand = (req, res, next) => {
    let uid = req.user.uid;
    let brandId = req.body.brandId;

    if (!req.xhr) {
        return next();
    }

    if (!req.body.brandId || !uid) {
        return res.json({code: 400, message: '用户未登录或缺少参数'});
    }

    searchApi.isFavoriteBrand(uid, brandId).then(result => {
        res.json(result);
    }).catch(next);
};


/**
 * 用户店铺优惠券领取状态
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.shopCouponSync = (req, res, next) => {
    let uid = req.user.uid;
    let id = req.query.id;

    if (!req.xhr) {
        return next();
    }

    if (!id || !uid) {
        return res.json({code: 400, message: '用户未登录或缺少参数'});
    }

    list.getUserCoupunStatus(id, uid, 'shop').then(result => {
        res.json(result);
    });
};

/**
 * 用户品牌优惠券领取状态
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.brandCouponSync = (req, res, next) => {
    let uid = req.user.uid;
    let id = req.query.id;

    if (!req.xhr) {
        return next();
    }

    if (!id || !uid) {
        return res.json({code: 400, message: '用户未登录或缺少参数'});
    }

    list.getUserCoupunStatus(id, uid, 'brand').then(result => {
        res.json(result);
    });

};