shop.js 2.74 KB

'use strict';

const mRoot = '../models';
const shopModel = require(`${mRoot}/shop-service`);
const tdk = require('../../../utils/getTDK');

// 店铺首页(经典&基础)
exports.index = (req, res, next) => {
    let domain = req.query.domain;
    let shopId = req.query.shopId;

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

    if (req.xhr && req.query._pjax && shopId) {
        return req.ctx(shopModel).getShopGoodsData(shopId, req.yoho.channel, req.query).then(result => {
            Object.assign(result, {
                shopId: shopId,
                layout: false
            });
            res.render('list/goods-list', result);
        });
    }

    return Promise.all([
        tdk('shop', shopId, req),
        req.ctx(shopModel).getShopInfoAsync(domain, req.yoho.channel, req.query)
    ]).then(result => {
        let TDKObj = result[0],
            shopObj = result[1];

        if (TDKObj && TDKObj[0]) {
            req.tdk = {
                title: TDKObj[1],
                keywords: TDKObj[2],
                description: TDKObj[3]
            };
        }

        // 数据异常,重定向
        if (shopObj.redirect) {
            return res.redirect(shopObj.redirect);
        }

        if (shopObj.templateType === 2) { // 经典模板
            Object.assign(shopObj, {page: 'shop'});

            // 店铺装修为空则不cache
            if (!shopObj.shopTopBanner) {
                res.set('Cache-Control', 'no-cache');
            }

            res.render('shop/index', shopObj);
        } else { // 基础模版
            Object.assign(shopObj, {page: 'list'});

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

            res.render('list/brand', shopObj);
        }
    }).catch(next);
};

// 经典店铺列表
exports.list = (req, res, next) => {
    let shopId = req.query.shopId;

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

    return req.ctx(shopModel).getShopListInfoAsync(req.yoho.channel, req.query).then(result => {
        Object.assign(result, {
            page: 'shop',
            shopId: shopId,
            shopKey: req.query.query || ''
        });

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

// 经典店铺推荐文章
exports.article = (req, res, next) => {
    let brands = req.query.brands;

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

    return req.ctx(shopModel).getShopArticleByBrandsAsync(brands).then(result => {
        res.render('shop/article', Object.assign(result, {layout: false}));
    }).catch(next);
};