chanpin.js 4.47 KB
/**
 * chanpin controller
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2017/09/22
 */

const _ = require('lodash');
const helpers = global.yoho.helpers;

const utils = '../../../utils';
const searchProcess = require(`${utils}/search-process`);
const stringCode = require(`${utils}/string-code`);
const chanpin = require('../models/chanpin');
const headerModel = require('../../../doraemon/models/header');

// 关键词页
const keyword = (req, res, next) => {
    let queryKey = stringCode.hexToUtf8(`${req.params.query}`);
    let params = {
        isSearch: true, // 搜索列表将最新改成默认的标识
        cartUrl: helpers.urlFormat('/cart/index/index'),
        query: queryKey
    };

    params.isApp = req.yoho.isApp;
    params.physical_channel = req.yoho.channel && searchProcess.getChannelType(req.yoho.channel);

    // 唤起 APP 的路径
    res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.list","params":${JSON.stringify(params)}}`;

    return req.ctx(chanpin).getSearchKeywordData(params, req.user.uid).then(result => {
        res.render('search/list', {
            _noLazy: true,
            module: 'product',
            page: 'chanpin',
            pageHeader: headerModel.setNav({
                navTitle: queryKey
            }),
            goodList: params,
            firstPageGoods: result || [],
            fuzzyWord: result.fuzzyWord,
            title: `${queryKey}价格_图片_品牌_怎么样-YOHO!BUY有货`,
            keywords: `${queryKey},${queryKey}价格,${queryKey}图片,${queryKey}怎么样,${queryKey}品牌,YOHO!BUY有货`,
            description: `YOHO!BUY有货网yohobuy.com是国内专业的${queryKey}网上潮流购物商城,为您找到${_.get(result,
                'total', 0)}${queryKey}、产品的详细参数,实时报价,价格行情,图片、评价、品牌等信息。买${queryKey},就上YOHO!BUY有货`,
            pageFooter: true
        });
    }).catch(next);
};

// 关键词页with id
const keyId = (req, res, next) => {
    let params = {
        isSearch: true, // 搜索列表将最新改成默认的标识
        cartUrl: helpers.urlFormat('/cart/index/index')
    };

    params.isApp = req.yoho.isApp;
    params.physical_channel = req.yoho.channel && searchProcess.getChannelType(req.yoho.channel);

    return req.ctx(chanpin).getSearchKeywordDataById(req.params.id, params, req.user.uid).then(result => {
        let queryKey = result.queryKey;

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

        // 唤起 APP 的路径
        res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.list","params":${JSON.stringify(params)}}`;

        res.render('search/list', {
            _noLazy: true,
            module: 'product',
            page: 'chanpin',
            pageHeader: headerModel.setNav({
                navTitle: queryKey
            }),
            goodList: params,
            firstPageGoods: result || [],
            fuzzyWord: result.fuzzyWord,
            title: `${queryKey}价格_图片_品牌_怎么样-YOHO!BUY有货`,
            keywords: `${queryKey},${queryKey}价格,${queryKey}图片,${queryKey}怎么样,${queryKey}品牌,YOHO!BUY有货`,
            description: `YOHO!BUY有货网yohobuy.com是国内专业的${queryKey}网上潮流购物商城,为您找到${_.get(result,
                'total', 0)}${queryKey}、产品的详细参数,实时报价,价格行情,图片、评价、品牌等信息。买${queryKey},就上YOHO!BUY有货`,
            pageFooter: true,
            miphtml: `https://m.yohobuy.com/mip/chanpin/${req.params.id}.html`,
            canonical: {
                currentHref: `https://www.yohobuy.com/chanpin/${req.params.id}.html`
            }
        });
    }).catch(next);
};

const searchGoods = (req, res, next) => {

    let params = Object.assign({}, req.query);
    let uid = req.user.uid || 0;

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

    params.isApp = req.yoho.isApp;
    params.limit = 24;

    return req.ctx(chanpin).getSeoSearchData(params).then((result) => {
        if (result.list && result.list.length > 0) {
            res.render('search/page', {
                layout: false,
                new: result.list,
                suggestion: result.suggestion || [],
                total: result.total,
                _noLazy: params.noLazy || false
            });
        } else {
            res.json(result);
        }
    }).catch(next);
};

module.exports = {
    keyword,
    keyId,
    searchGoods
};