shop.js 5.65 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 Editorial = require('../models/editorial');

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;
                data.shopId = result.shopId;
                data.mores = {};
                result.menus.forEach(m => {
                    if (m.id) {
                        data.mores[m.id] = m;
                    }
                });

                return Promise.all([Search.queryProductOfBrand({
                    shop_id: result.shopId,
                    limit: 16,
                    page: 1
                }), Editorial.getByBrand(result.brandId)]);
            } else {
                return false;
            }
        }).then(result => {
            let productData = result[0];
            let editorial = result[1];

            data.showEditorial = editorial && editorial.length === 3;
            data.editorial = editorial;

            if (productData && productData.code === 200 && productData.data) {
                let ret = camelCase(productData.data);

                data.paginationData = {
                    page: 1,
                    limit: 16,
                    total: ret.total,
                    pageTotal: ret.pageTotal,
                    queryParams: req.query
                };
                data.products = DataHelper.handleProductList(ret.productList);
                res.display('shop-index', data);
            } else {
                shop.list(req, res, next);
            }
        }).catch(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 = parseInt(q.page || 1, 10);

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

            data.banner = result;

            if (data.banner.banner) {
                data.banner.banner = data.banner.banner.split('?')[0];
            }

            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: DataHelper.handleProductList(ret.productList),
                        order: q.order
                    }));
                } else {
                    return Promise.reject('query shop index error');
                }
            });
        }).catch(next);
    },

    // 首页ajax加载所有商品
    indexQuery(req, res, next) {
        let shopId = req.query.shopId;
        let page = parseInt(req.query.page || 1, 10);
        let query = {
            shop_id: shopId,
            limit: 16,
            page: page,
            order: req.query.order || ''
        };

        let data = {};

        return Search.queryProductOfBrand(query).then(result => {
            if (result && result.code === 200 && result.data) {
                let ret = camelCase(result.data);

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

                let products = DataHelper.handleProductList(ret.productList);

                products.forEach(p => {
                    p.defaultImages = global.yoho.helpers.image(p.defaultImages, 270, 366);
                    p.salesPrice = global.yoho.helpers.round(p.salesPrice, 2);
                });
                res.json(_.assign(data, {
                    products: products
                }));
            } else {
                return Promise.reject('query shop index error');
            }
        }).catch(next);
    }
};

module.exports = shop;