shop.js 2.96 KB
/**
 * 店铺相关页面
 *
 * 首页、列表页
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 */

'use strict';

const _ = require('lodash');
const camelCase = global.yoho.camelCase;

const cookie = global.yoho.cookie;
const Search = require('../models/search');
const DataHelper = require('../models/helpers');
const ShopData = require('../models/shop-service');

const shop = {
    index(req, res, next) {
        let domain = req.params.domain;
        let uid = cookie.getUid(req);
        let data = {
            module: 'product',
            page: 'shop',
            title: domain
        };

        ShopData.getShopHeadData(domain, uid).then(result => {

            if (result.shopId) {
                let nav = [DataHelper.getChannelNav()];

                nav.push({
                    name: result.name
                });
                data.navPath = {
                    nav: nav
                };
                data.banner = result;
                res.display('shop-index', data);
            } else {
                shop.list(req, res, next);
            }
        });

    },
    list(req, res, next) {
        let data = {
            module: 'product',
            page: 'shop-list',
            title: '店铺列表'
        };
        let nav = [DataHelper.getChannelNav()];
        let domain = req.params.domain;

        let uid = cookie.getUid(req);
        let q = req.query;

        q.page = q.page || 1;

        ShopData.getShopHeadData(domain, uid).then(result => {

            data.banner = result;
            if (result.brandId) {
                q.brand = result.brandId;
                q.shop_id = result.shopId;
                nav.push({
                    name: result.name
                });
            } else {
                res.status(404);
                return Promise.reject('brand not found');
            }
        }).then(() => {
            return Search.queryProductOfBrand(q).then(result => {
                if (result && result.code === 200 && result.data) {
                    let ret = camelCase(result.data);

                    if (ret.filter) {
                        delete q.brand;
                        data.filter = DataHelper.filterHandle(ret.filter, req.query);
                    }

                    data.paginationData = {
                        page: q.page,
                        limit: ret.limit || 45,
                        total: ret.total,
                        pageTotal: ret.pageTotal,
                        queryParams: req.query
                    };

                    data.navPath = {
                        nav: nav
                    };

                    res.display('shop-list', _.assign(data, {
                        products: ret.productList,
                        order: q.order
                    }));
                } else {
                    return Promise.reject('query shop index error');
                }
            });
        }).catch(next);
    }
};

module.exports = shop;