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

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

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

// 店铺页
const shop = (shopId, req, res, next) => {

    list.getShopInfo(shopId, req.user.id).then(shopInfo => {

        // 暂定2
        shopInfo.shopTemplateType = 2;


        if (shopInfo.shopTemplateType === 2) {

            // 经典模板
            list.getShopData(shopId, req.user.id, req.query, shopInfo).then(result => {
                Object.assign(result, {
                    page: 'shop'
                });
                res.render('list/shop-index', result);
            }).catch(next);

        } else {

            // 基础模板
            list.getBaseShopData(shopId, req.user.id, req.query).then(result => {
                res.render('list/index', result);
            }).catch(next);
        }
    }).catch(next);
};

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

    list.getListData(req.query).then(result => {
        Object.assign(resData, result);
        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)).then(result => {
        Object.assign(resData, result);
        res.render('list/index', resData);
    }).catch(next);

};

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

    // req.shopId存在,直接走店铺
    if (req.shopId) {
        return shop(req.shopId, req, res, next);
    }
    // let brandDomain = 'colormad';

    let brandDomain = req.brandDomain || 'shop$15';

    // 获取品牌信息
    list.getBrandInfo({domain: brandDomain}).then(brandInfo => {

        brandInfo.type = 2;
        brandInfo.shopId = 15;
        switch (parseInt(brandInfo.type, 10)) {
            case 1:
                // 搜索
                res.location(helpers.urlFormat('', {query: brandInfo.brandDomain}, 'search'));
                break;

            case 2:
                // 店铺
                return shop(brandInfo.shopId, req, res, next);

            default:

                // 品牌
                return list.getBrandData(req.query, Object.assign({uid: req.user.id}, brandInfo)).then(result => {
                    Object.assign(resData, result);
                    res.render('list/brand', resData);
                });
        }
    }).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.query).then(result => {
        Object.assign(result, {
            page: 'shop'
        });
        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 || !req.body.brandId) {
        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) => {

    if (!req.xhr || !req.body.brandId || req.user.id) {
        return next();
    }

    searchApi.isFavoriteBrand(req.user.id, req.body).then(result => {
        res.json(result);
    }).catch(next);
};