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

'use strict';

const _ = require('lodash');
const camelCase = global.yoho.camelCase;
const BrandData = require('../models/brand-service');
const Search = require('../models/search');
const DateHelper = require('../models/helpers');

function bannerFormat(banner) {
    return {
        bgImg: banner.brandBanner,
        brandIntro: {
            text: '品牌介绍'
        },
        height: 150
    };
}

function shopMenu() {
    let menus = [
        {name: '店铺首页', href: '/'},
        {name: '全部商品', href: '/shop', icon: '&#xe60a;'},
        {name: '人气单品', href: '/list?order=xxx'},
        {name: '新品上架', href: '/list?order=s_t_desc'}
    ];

    return menus;
}

const shop = {

    list(req, res, next) {
        let data = {
            module: 'product',
            page: 'shop-list',
            title: '店铺列表'
        };

        let domain = req.params.domain;
        let q = req.query;

        q.page = q.page || 1;
        data.shopMenu = shopMenu();

        BrandData.getBrandByDomainAsync(domain).then(result => {
            data.brandBanner = bannerFormat(result);
            q.brand = result.id;
            q.shop_id = q.shopId || '';
        }).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 = DateHelper.filterHandle(ret.filter, req.query);
                    }

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

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

module.exports = shop;