list.js 8.24 KB
/**
 * 品类|品牌|店铺 controller
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/7/21
 */

'use strict';
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const listModel = require(`${mRoot}/list`);
const _ = require('lodash');
const helpers = global.yoho.helpers;

/**
 * 店铺基础模板
 */
const _baseShop = (req, res, data) => {
    let params = Object.assign({
        cartUrl: helpers.urlFormat('/cart/index/index')
    }, req.query);

    Promise.all([listModel.getShopDecorator(data.shopsId), listModel.getShopBrands(data.shopsId)]).then((result) => {
        result[0] = result[0] || {};

        _.forEach(result[0], (item) => {
            if (item.resourceName === 'shopTopBanner_base') {
                let banner = JSON.parse(item.resourceData);

                params = _.assign({
                    shopBanner: {
                        banner: banner[0].shopSrc
                    }
                }, params);
            }
        });

        if (data.multBrandShopType === '2') {
            // 转义店铺
            params.shop_id = data.shopsId;
        } else {
            params.brand = result[1];
        }

        res.render('search/goods-list', {
            module: 'product',
            page: 'search-list',
            pageHeader: headerModel.setNav({
                navTitle: data.shopName
            }),
            title: data.shopName,
            goodList: params,
            showDownloadApp: true,
            pageFooter: true
        });
    });
};

/**
 * 店铺首页 || 若店铺使用基础模板就使用基础模板
 * @return int
 */
const _shop = (req, res, shopId) => {
    let isApp = req.query.app_version || req.query.appVersion || false;
    let pageHeader = {};
    let uid = 0;

    if (!isApp) {
        pageHeader = {
            pageHeader: _.assign({
                shopPage: {
                    text: '分类',
                    url: helpers.urlFormat('/product/index/category', {
                        shop_id: shopId
                    })
                }
            }, headerModel.setNav({
                navTitle: false
            }))
        };
        uid = req.user.uid;
    } else {
        uid = req.query.uid;
        req.session.appUid = uid;
        res.cookie('appUid', uid, {
            domain: 'yohobuy.com',
            expires: new Date(Date.now() + 2592000000) // 有效期一年
        });
    }

    listModel.getShopData(req, shopId, uid, isApp).then((result) => {
        if (result.goBrand) {
            // 跳转基础模板
            _baseShop(req, res, result.goBrand);
        } else {
            result = _.assign(result, pageHeader);

            res.render('shop/index', {
                module: 'product',
                page: 'shop',
                shopIndex: result,
                shopHeadHide: true,
                gender: req.query.gender,
                channel: req.query.channel
            });
        }
    });
};

// 品类落地页
const category = (req, res) => {
    let params = Object.assign({}, req.query);

    res.render('search/goods-list', {
        module: 'product',
        page: 'search-list',
        pageHeader: headerModel.setNav({
            navTitle: req.query.title
        }),
        goodList: params,
        showDownloadApp: true,
        pageFooter: true
    });
};

// 品牌|店铺落地页
const brand = (req, res, next) => {
    let params = Object.assign({}, req.query);
    let domain = req.query.domain;
    let uid = req.user.uid;
    let brandId = 0;
    let brandLogo = {};
    let title = '';

    /* tar add 160909 如果有 shop_id 直接跳转 */
    if (req.query.shop_id) {
        _shop(req, res, req.query.shop_id);
        return;
    }

    if (!domain) {
        res.redirect('/?go=1');
    }

    listModel.getBrandLogoByDomain(domain).then((result) => {
        brandLogo = result;

        title = brandLogo.name;

        if (brandLogo && brandLogo.id) {
            brandId = brandLogo.id;
        }

        params = _.assign({
            brand: brandId
        }, params);

        // 无店铺:0--->品牌页 无单品店有多品店:1--->搜索页 有单品店:2--->店铺页面
        if (brandLogo.type === '2' && brandLogo.shopId) {
            _shop(req, res, brandLogo.shopId);
        } else { // 获取品牌店铺信息
            listModel.getBrandShops(brandId).then((brandShop) => {
                if (brandId === 0) {
                    params.query = domain;
                }

                // 从搜索页过来的,显示搜索框, 和进入品牌引导信息 或者品牌关联多店铺
                if (req.query.from === 'search' || brandShop.length > 0) {
                    params = {
                        brandWay: brandShop ? brandShop : brandLogo,
                        search: {
                            default: req.query.query,
                            url: helpers.urlFormat('', null, 'search')
                        }
                    };
                } else if (brandId !== 0) { // 品牌一览过来的展示品牌介绍和LOGO
                    return Promise.all([listModel.getBrandIntro(brandId, uid), listModel.getBrandBanner(brandId)]).then((resData) => { //eslint-disable-line
                        title = resData[0].title;
                        delete resData[0].title;

                        return _.assign({
                            banner: resData[1]
                        }, resData[0]);
                    });
                }
            }).then((brandHome) => {
                params.brandHome = brandHome;

                res.render('search/goods-list', {
                    module: 'product',
                    page: 'search-list',
                    pageHeader: headerModel.setNav({
                        navTitle: title !== '' ? title : domain
                    }),
                    goodList: params,
                    showDownloadApp: true,
                    pageFooter: true
                });
            });
        }
    }).catch(next);
};

// 店铺简介
const shopIntro = (req, res, next) => {
    let shopId = req.query.shop_id;
    let appVersion = req.body.appVersion || false;
    let params = {};

    if (shopId) {
        listModel.getShopIntro(shopId).then((result) => {
            if (appVersion) {
                params = {
                    title: '店铺简介'
                };
            } else {
                params = {
                    pageHeader: headerModel.setNav({
                        navTitle: '店铺简介'
                    })
                };
            }

            res.render('shop/intro', _.assign(params, result));
        }).catch(next);
    } else {
        res.redirect('/?go=1');
    }
};

/**
 * 品牌[店铺]收藏/取消收藏
 * id 品牌ID
 * opt 操作标识("ok":表示收藏,"cancel":表示取消收藏)
 */
const favoriteBrand = (req, res, next) => {
    let id = req.body.id;
    let uid = req.user.uid;
    let opt = req.body.opt || 'ok';
    let type = req.body.type || 'product';
    let appVersion = req.body.appVersion || false;
    let url = helpers.urlFormat('/signin.html') + '?refer=' + decodeURI(req.cookies.refer);

    if (appVersion === 'true') {
        uid = req.cookies.appUid;
        url = `${url}&openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":${decodeURI(req.cookies.refer)},"param":{}},"requesturl":{"param":{},"url":""},"priority":"Y"}}`; // eslint-disable-line
    }

    if (_.isNumber(id)) {
        res.json({
            code: 401,
            message: '参数不正确',
            data: false
        });
        return false;
    } else if (!uid) {


        res.json({
            code: 400,
            message: '未登录',
            data: url
        });
        return false;
    } else if (opt !== 'ok') { // 取消收藏
        listModel.setFavoriteCancel(id, uid, type).then((data) => {
            res.json(data);
            return false;
        }).catch(next);
    }

    listModel.setFavorite(id, uid, type).then((result) => {
        if (!result.code) { // 收藏
            result = {
                code: 401,
                message: '参数不正确',
                data: false
            };
        }

        res.json(result);
    });
};

module.exports = {
    category,
    brand,
    favoriteBrand,
    shopIntro
};