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

'use strict';

const _ = require('lodash');
const camelCase = global.yoho.camelCase;
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 = req.user.uid;
        let shopId = req.query.shopId;
        let channel = req.query._Channel || req.cookies._Channel || 'men';
        let data = {
            module: 'product',
            page: 'shop',
            title: domain
        };

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

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

                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;

            _.forEach(editorial, (e) => {
                e.title = e.title.length > 40 ? (e.title.substring(0, 40) + '...') : e.title;
                e.url = `/editorial/${e.id}.html`;
            });

            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 channel = req.query._Channel || req.cookies._Channel || 'men';
        let nav = [DataHelper.getChannelNav(channel)];
        let domain = req.params.domain;
        let uid = req.user.uid;
        let shopId = req.query.shopId;
        let q = req.query;

        // let hasBrand = !!q.brand;

        q.order = q.order || 's_n_desc';
        q.page = parseInt(q.page || 1, 10);
        // q.yh_channel = channel;

        ShopData.getShopHeadData(domain, shopId, 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
                });
            }
            return result.brandId;
        }).then((brandId) => {
            if (brandId) {
                return Promise.all([Search.queryAllSort({
                    brand: q.brand,
                    shop: q.shop_id,
                    small_sort: 1
                }), Search.queryProductOfBrand(q)]).then(allResult => {
                    let allSort = camelCase(allResult[0]);
                    let result = allResult[1];

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

                        if (ret.filter) {
                            ret.filter.standard = ret.standard;
                            ret.filter.groupSort = DataHelper.sortConvert(allSort.data.sort);
                            ret.filter.singleBrand = true;
                            data.filter = DataHelper.filterHandle(ret.filter, q);
                            data.filter.showPrice = ret.total > 10;
                            data.filter.showInfo = (data.filter.style && data.filter.style.length > 0)
                                || (ret.standard && ret.standard.length > 0);  //eslint-disable-line

                        }

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