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

'use strict';

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

// 搜索相关接口
const searchApi = require('../models/search-api');
const list = require('../models/list');

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

    req.ctx(list).getListDataPre(Object.assign(req.query, {uid: req.user.uid, prid: req.user.prid}),
        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) {
                try {
                    resData.list.phrase = decodeURIComponent(req.query.phrase);
                } catch (e) {
                    resData.list.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 = {};

    req.ctx(list).getListNewDataPre(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.newWithChannel = (req, res, next) => {
    let channel = req.params[0];

    req.query = req.query || {};

    // 根据 XXXX-new 中的频道处理查询参数
    switch (channel) {
        case 'boys':
            req.yoho.channel = 'boys';
            req.query = Object.assign({gender: '1,3', category_id: '1'}, req.query);
            break;
        case 'girls':
            req.yoho.channel = 'girls';
            req.query = Object.assign({gender: '2,3', category_id: '2'}, req.query);
            break;
        case 'kids':
            req.yoho.channel = 'kids';
            req.query = Object.assign({gender: '1,2,3', category_id: '3'}, req.query);
            break;
        case 'lifestyle':
            req.yoho.channel = 'lifestyle';
            req.query = Object.assign({gender: '1,2,3', category_id: '4'}, req.query);
            break;
        default:
            break;
    }

    this.new(req, res, 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) {
        req.ctx(list).getShopAbout(shopId, req.user.uid).then(result => {
            res.render('list/brand', Object.assign(result, {page: 'list'}));
        }).catch(next);
    } else {
        req.ctx(list).getBrandAbout(brandDomain, req.user.uid, req.yoho.channel).then(result => {
            res.render('list/brand', Object.assign(result, {page: 'list'}));
        }).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();
    }

    req.ctx(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();
    }

    req.ctx(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) {
        return res.json({code: 400, message: '缺少参数'});
    }

    req.ctx(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) {
        return res.json({code: 400, message: '缺少参数'});
    }

    req.ctx(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: '用户未登录或缺少参数'});
    }

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

};