list.js 4.3 KB
'use strict';
const css = require('../css');
const _ = require('lodash');
const co = require('bluebird').coroutine;
const mRoot = '../models';
const utils = '../../../utils';
const productProcess = require(`${utils}/product-process`);
const searchProcess = require(`${utils}/search-process`);
const stringProcess = require(`${utils}/string-process`);
const listParamsProcess = require(`${utils}/list-params-process`);
const listModel = require(`${mRoot}/list`);
const camelCase = global.yoho.camelCase;

/**
 * 封面图
 * @type {{boys: string, gilrs: string}}
 */
const _coverChannel = {
    boys: '1,3',
    gilrs: '2,3'
};

const _formatJumpUrl = (params) => {
    let opt = _.merge({}, params);
    let order = 1;

    delete opt[0];

    if (opt.type === 'price') {
        order = opt.order === '0' ? 1 : 0;
    }

    opt.union_type = '100000000013130';

    return {
        default: listParamsProcess.generatePathUrl(_.assign(opt, {type: 'default'})),
        new: listParamsProcess.generatePathUrl(_.assign(opt, {type: 'new'})),
        popularity: listParamsProcess.generatePathUrl(_.assign(opt, {type: 'popularity'})),
        price: listParamsProcess.generatePathUrl(_.assign(opt, {type: 'price', order: order})),
        discount0: listParamsProcess.generatePathUrl(_.assign(opt, {type: 'discount', order: 0})),
        discount1: listParamsProcess.generatePathUrl(_.assign(opt, {type: 'discount', order: 1})),
        curUrl: listParamsProcess.generatePathUrl(opt)
    };
};

const _formatFilterUrl = (params, filter) => {
    let opt = _.merge({}, params);

    delete opt[0];
    delete opt.from;

    opt.union_type = '100000000013130';

    _.each(filter.classify, (item) => {
        _.each(item.subs, sub => {
            sub.url = listParamsProcess.generatePathUrl(_.merge({}, opt, {[item.dataType]: sub.dataId}));
        });
    });

    return filter;
};

const index = (req, res, next) => {
    let params = _.assign({}, req.query);
    let uid = req.user.uid;

    // 获取第一页数据做服务端渲染
    let initialData = _.assign({
        gender: params.gender,
        type: 'default',
        order: '0',
        page: 1,
        limit: 24,
        isApp: params.app_version
    }, params);

    let allowOrigin = _.get(req, 'headers.origin', null) ?
        req.headers.origin : req.protocol + '://' + req.headers.host;

    res.setHeader('Access-Control-Allow-Origin', allowOrigin);
    res.setHeader('Access-Control-Allow-Credentials', 'true');

    if (uid) {
        initialData.uid = uid;
    }

    co(function* () {
        let result = yield req.ctx(listModel).getCategoryGoods(initialData);
        let filter = yield req.ctx(listModel).getFilterData(params);
        let responseResult = {
            list: productProcess.processProductList(_.get(result, 'data.product_list', []), {
                isApp: params.isApp || (params.appVersion && params.appVersion !== 'false'),
                gender: _coverChannel[params.coverChannel],
                showSimilar: params.shop_id || params.material === 'true' ? false : true
            })
        };
        let seoParams = searchProcess.getFilterValueForSeo(initialData, _.get(result, 'data', {}));
        let seoRenderData = searchProcess.getListSeoData(seoParams);
        let seoTitle = _.get(seoParams, 'sort');
        let paramsTitle = params.title || params.sort_name; // 可能会配置的标题,优先级最高

        if (paramsTitle) {
            seoTitle = stringProcess.decodeURIComponent(paramsTitle);
        }

        if (!params.type) {
            params.type = 'default';
        }

        let jumpUrls = _formatJumpUrl(params);

        res.render('list', Object.assign({
            css: yield css('chanpin.css'),
            commonCss: yield css('common.css'),
            title: seoRenderData.title || '商品列表',
            mipFooter: true,
            mipTab: true,
            canonical: {
                currentHref: `https://www.yohobuy.com${req.url}`
            },
            goodList: params,
            pageTitle: seoTitle || '商品列表',
            appPath: `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.list","params":${JSON.stringify(params)}}`,
            jumpUrls: jumpUrls,
            filter: _formatFilterUrl(params, filter)
        }, camelCase(responseResult)));
    })().catch(next);
};

module.exports = {
    index
};