shop.js 11.9 KB
'use strict';
const mRoot = '../models';
const utils = '../../../utils';
const shopModel = require(`${mRoot}/shop`);
const listModel = require(`${mRoot}/list`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const productProcess = require(`${utils}/product-process`);
const stringProcess = require(`${utils}/string-process`);
const shopPrcs = require(`${utils}/shop-process`);
const co = require('bluebird').coroutine;
const css = require('../css');
const camelCase = global.yoho.camelCase;
const shop = {
    /**
     * 解析店铺的参数
     */
    resolveShopParams(req, res, next) {
        if (req.params.shopParams) {
            req.query.shop_id = _.last(_.split(req.params.shopParams, '-'));
            req.query.domain = req.params.shopParams.replace(`-${req.query.shop_id}`, '');
        }

        return next();
    },

    /**
     * 店铺统一入口
     */
    entry(req, res, next) {
        const shopId = _.parseInt(stringProcess.paramsFilter(req.params.shop_id || req.query.shop_id));
        const domain = req.params.domain || req.query.domain;
        const uid = req.user.uid;

        req.shopInfo = {};

        return co(function* () {
            if (shopId) {
                let shopInfoApi = (yield req.ctx(shopModel).getShopInfo(shopId, uid)) || {};
                let shopInfo = _.get(shopInfoApi, 'data', {});
                let apiDomain = _.get(shopInfo, 'shop_domain');

                if (apiDomain && apiDomain !== domain) {
                    return res.redirect(301, `//m.yohobuy.com/mip/shop/${apiDomain}-${shopId}.html`);
                }

                // 红人店铺
                if (shopInfo && shopInfo.is_red_shop === 1) {
                    shopInfo.isRedShop = true;
                }

                // 店铺,非红人店铺,跳转店铺列表
                if (shopInfo && shopInfo.is_red_shop !== 1) {
                    shopInfo.isShop = true;
                }

                // 可能没有店铺信息,跳转店铺列表
                req.shopInfo = shopInfo;
            } else if (domain) {
                let domainInfo = (yield req.ctx(shopModel).getBrandLogoByDomain(domain)) || {}; // 通过域名查询店铺类型,或者品牌信息

                if (domainInfo.shopId && domainInfo.type === '2') { // 店铺
                    let shopInfoApi = yield req.ctx(shopModel).getShopInfo(domainInfo.shopId, uid);
                    let shopInfo = _.get(shopInfoApi, 'data', {});

                    // 红人店铺
                    if (shopInfo && shopInfo.is_red_shop === 1) {
                        shopInfo.isRedShop = true;
                    }

                    // 店铺,非红人店铺,跳转店铺列表
                    if (shopInfo && shopInfo.is_red_shop !== 1) {
                        shopInfo.isShop = true;
                    }

                    _.assign(req.shopInfo, domainInfo, shopInfo);
                } else { // 品牌
                    req.shopInfo = domainInfo;
                }
            }

            if (req.shopInfo.isRedShop) { // 店铺
                return shop.redShop(req, res, next);
            } else if (shopId || req.shopInfo.isShop) { // 非红人店铺的店铺
                return shop.list(req, res, next);
            } else { // 什么都没有,去首页
                return res.redirect(helpers.urlFormat('', {go: 1}));
            }
        })().catch(next);
    },

    /**
     * 红人店铺
     */
    redShop(req, res, next) {
        let shopId = req.shopInfo.shops_id;
        let domain = req.shopInfo.shop_domain || req.shopInfo.brandDomain || 'shop';
        let channel = req.yoho.channel || 'boys';
        let uid = req.user.uid || 0;
        let udid = req.cookies.udid || 'yoho';
        let isMip = true;

        if (!shopId) {
            return next();
        }

        co(function* () {
            let [bannerData, favCountData, decoratorsData, categoryData, defaultGoods, newGoods] = yield Promise.all([
                req.ctx(shopModel).getBanner(shopId),
                req.ctx(shopModel).favCount(shopId, uid, channel, udid),
                req.ctx(shopModel).getShopsdecorator(shopId),
                req.ctx(shopModel).getShopCategory(shopId, channel),
                req.ctx(listModel).getShopGoods({shop_id: shopId, order: '0', type: 'default'}),
                req.ctx(listModel).getShopGoods({shop_id: shopId, order: '0', type: 'new', end: false, page: 1})
            ]);

            let banner = _.get(bannerData, 'data.banner');
            let shopInfo = req.shopInfo;
            let favCount = _.get(favCountData, 'data[0].approximateCount', '2.1w');
            let decoratorsAll = shopPrcs.floor(_.get(decoratorsData, 'data.modules', []), isMip);
            let category = shopPrcs.category(_.get(categoryData, 'data', []), {
                shopId: shopId,
                domain: domain
            }, isMip);

            let goodsListBySkn = yield req.ctx(listModel).searchProductBySkn(decoratorsAll.skns);
            let decorators = shopPrcs.pushGoodsInfo(decoratorsAll.decorators, goodsListBySkn);

            shopInfo.shop_intro_link = helpers.urlFormat('/product/index/intro', {
                shop_id: shopId,
                union_type: '100000000013130'
            });

            let AppRecommendProduct_0 = _.find(decorators, {module_type: 'AppRecommendProduct', type: 0 });
            let AppRecommendProduct_1 = _.find(decorators, {module_type: 'AppRecommendProduct', type: 1 });

            if (AppRecommendProduct_0) {
                AppRecommendProduct_0 = yield req.ctx(listModel).getShopGoods({
                    shop_id: shopId,
                    order: AppRecommendProduct_0.order
                });
                AppRecommendProduct_0 = productProcess.processProductList(_.get(AppRecommendProduct_0, 'data.product_list', []));// eslint-disable-line

            }

            if (AppRecommendProduct_1) {
                AppRecommendProduct_1 = yield req.ctx(listModel).searchProductBySkn(AppRecommendProduct_1.skns);

                AppRecommendProduct_1 = productProcess.processProductList(AppRecommendProduct_1);

                _.forEach(AppRecommendProduct_1, (value, key) => {
                    AppRecommendProduct_1[key].tags = {};
                    AppRecommendProduct_1[key].is_soon_sold_out = false;
                    AppRecommendProduct_1[key].tags.is_hot = true;
                });
            }

            _.each(decorators, item => {
                if (item.module_type === 'AppRecommendProduct' && item.type === 0) {
                    item.list = camelCase(AppRecommendProduct_0) || [];
                }

                if (item.module_type === 'AppRecommendProduct' && item.type === 1) {
                    item.list = camelCase(AppRecommendProduct_1) || [];
                }
            });

            defaultGoods = productProcess.processProductList(_.get(defaultGoods, 'data.product_list', []));
            newGoods = productProcess.processProductList(_.get(newGoods, 'data.product_list', []));

            let shopRedsCss = yield css('shop/shop-reds.css');
            let chanpinCss = yield css('chanpin.css');
            let commonCss = yield css('common.css');

            res.render('shop/shop-reds', {
                css: shopRedsCss + chanpinCss + commonCss,
                mipTab: true,
                mipFooter: true,
                pageHead: {
                    text: '分类',
                    url: helpers.urlFormat('/product/index/category', {
                        shop_id: shopId,
                        domain: domain,
                        union_type: '100000000013130'
                    })
                },
                title: `${shopInfo.shop_name}_${shopInfo.shop_name}品牌官方旗舰店-YOHO!BUY 有货官网`,
                pageTitle: shopInfo.shop_name || '',
                allGoodsUrl: helpers.urlFormat('', {
                    shop_id: shopId,
                    title: '全部商品',
                    union_type: '100000000013130'
                }, 'list'),
                canonical: {
                    currentHref: `https://www.yohobuy.com${req.originalUrl}`
                },
                appPath: 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.shop","params":{"shop_template_type":"1","is_red_shop":"1","shop_id":"' +
                shopId + '","shop_name":"' + shopInfo.shop_name + '"}}',
                defaultGoods: {list: camelCase(defaultGoods) || []},
                newGoods: {list: camelCase(newGoods) || []},
                shopId,
                banner, shopInfo, favCount, decorators, category
            });
        })().catch(next);
    },

    /**
     * 非红人店铺的店铺页面
     */
    list(req, res, next) {
        co(function* () {
            let shopId = _.parseInt(stringProcess.paramsFilter(req.query.shop_id)) || req.shopInfo.shops_id;
            let shopInfo = req.shopInfo || {};
            let title = shopInfo.shop_name || '店铺商品列表';
            let searchParam = {
                isApp: req.yoho.isApp,
                shop_id: shopId,
                app_type: req.query.app_type,
                type: 'default',
                order: '0',
                page: 1
            };

            let [defaultGoods, newGoods] = yield Promise.all([
                req.ctx(listModel).getShopGoods(searchParam),
                req.ctx(listModel).getShopGoods(_.assign(searchParam, {type: 'new'}))
            ]);

            defaultGoods = productProcess.processProductList(_.get(defaultGoods, 'data.product_list', []));

            newGoods = productProcess.processProductList(_.get(newGoods, 'data.product_list', []));

            if (shopInfo.isShop) {
                let shopDecorator = yield req.ctx(shopModel).getShopDecorator(shopId);
                let shopDecoratorList = _.get(shopDecorator, 'list', []);

                if (shopDecoratorList) {
                    shopInfo.banner = shopPrcs.getShopBanner(shopDecoratorList);
                }
            }

            let commonCss = yield css('common.css');
            let shopListCss = yield css('/shop/shop-list.css');
            let chanpinCss = yield css('chanpin.css');

            res.render('shop/shop-list', _.assign(
                {
                    css: shopListCss + chanpinCss + commonCss,
                    title: `${shopInfo.shop_name}_${shopInfo.shop_name}品牌官方旗舰店-YOHO!BUY 有货官网`,
                    pageTitle: title,
                    mipFooter: true,
                    mipTab: true,
                    mipLightBox: true,
                    shopId: shopId,
                    shopInfo: shopInfo,
                    defaultGoods: camelCase(defaultGoods),
                    newGoods: camelCase(newGoods),
                    allGoodsUrl: helpers.urlFormat('', {
                        shop_id: shopId,
                        title: '全部商品',
                        union_type: '100000000013130'
                    }, 'list'),
                    appPath: 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.shop","params":{"shop_template_type":"1","shop_id":"' +
                    shopId + '","shop_name":"' + shopInfo.shop_name + '"}}',
                    canonical: {
                        currentHref: `https://www.yohobuy.com${req.originalUrl}`
                    }
                }
            ));

        })().catch(next);
    },

    _formatJumpUrl(curUrl) {
        let str = '';

        if (curUrl.indexOf('?') > -1) {
            str = '&';
        } else {
            str = '?';
        }

        return {
            default: `//m.yohobuy.com${curUrl}${str}type=default`,
            new: `//m.yohobuy.com${curUrl}${str}type=new`,
            popularity: `//m.yohobuy.com${curUrl}${str}type=popularity`,
            price: `//m.yohobuy.com${curUrl}${str}type=popularity&order=1`,
            discount0: `//m.yohobuy.com${curUrl}${str}type=discount&order=0`,
            discount1: `//m.yohobuy.com${curUrl}${str}type=discount&order=0`
        };
    }
};

module.exports = shop;