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

'use strict';

const _ = require('lodash');
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.title = result.name;
                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 = productData.data;

                data.paginationData = {
                    page: 1,
                    limit: 16,
                    total: ret.total,
                    pageTotal: ret.page_total,
                    queryParams: req.query
                };
                data.products = DataHelper.handleProductList(ret.product_list);
                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;
            data.title = result.name;
            if (data.banner.banner) {
                data.banner.banner = data.banner.banner.split('?')[0];
            }

            q.brand = result.brandId;
            q.shop_id = result.shopId;
            nav.push({
                name: result.name
            });
            return result.brandId;
        }).then((brandId) => {
            return Promise.all([Search.queryAllSort({
                brand: q.brand,
                shop: q.shop_id,
                small_sort: 1
            }), Search.queryProductOfBrand(q)]).then(allResult => {
                let allSort = allResult[0];
                let result = allResult[1];

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

                    if (ret.filter) {
                        ret.filter.standard = ret.standard;
                        ret.filter.group_sort = 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.page_total,
                        queryParams: req.query
                    };

                    data.navPath = {
                        nav: nav
                    };

                    res.display('shop-list', _.assign(data, {
                        products: DataHelper.handleProductList(ret.product_list, q, false),
                        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 = result.data;

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

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

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

module.exports = shop;