search.js 8.73 KB
/**
 * 商品搜索controller
 * @author: sefon
 * @date: 2016-7-12 10:09:25
 */

'use strict';

const mRoot = '../models';
const search = require(`${mRoot}/search`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const utils = '../../../utils';
const stringCode = require(`${utils}/string-code`);
const parameter = require(`${global.utils}/parameter`);

const config = global.yoho.config;

/**
 * search 首页
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
const index = (req, res, next) => {
    let params = req.query;
    let resData = {};

    req.ctx(search).getKeyActivity(params.query).then(activityUrl => {
        if (activityUrl) {
            activityUrl = encodeURI(activityUrl);
            res.redirect(activityUrl);
            return;
        }

        return req.ctx(search).getSearchDataPre(Object.assign(params, {uid: req.user.uid, prid: req.user.prid}),
            req.yoho.channel).then(result => {
                let queryKey = req.query.query;

                Object.assign(resData, result);

                if (!_.get(resData, 'search.goods') || !_.get(resData, 'search.goods').length) {
                    _.set(resData, 'search.keyWord', queryKey);
                    return res.render('search/no-result', resData);
                }

                if (queryKey) {
                    Object.assign(resData, {
                        title: `${queryKey}价格_图片_品牌_怎么样-YOHO!BUY有货`,
                        keywords: `${queryKey},${queryKey}价格,${queryKey}图片,${queryKey}怎么样,${queryKey}品牌,YOHO!BUY有货`,
                        description: `YOHO!BUY有货网yohobuy.com是国内专业的${queryKey}网上潮流购物商城,为您找到${_.get(resData, 'search.totalCount', 0)}${queryKey}、产品的详细参数,实时报价,价格行情,图片、评价、品牌等信息。买${queryKey},就上YOHO!BUY有货` // eslint-disable-line
                    });
                }

                res.render('search/index', resData);

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

/**
 * search 提示
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
const suggest = (req, res, next) => {
    req.ctx(search).getSuggest(req.query).then(result => {
        let dest = {
            code: 200,
            message: 'suggest',
            data: result || ''
        };

        res.type('text/javascript');
        res.send(req.query.callback + '(' + JSON.stringify(dest) + ')');

    }).catch(next);
};

const suggest4Old = (req, res, next) => {
    req.ctx(search).getSuggest(Object.assign({}, req.query, { keyword: req.query.query })).then(result => {
        let dest = {
            code: 200,
            message: 'suggest'
        };

        let data = [];

        if (result) {
            for (let it of result) {

                let item = '<li>' +
                        `<a style="display: block;" href="${it.href}" class="clearfix clear search-item" title="${it.keyword}" act="${it.href}">` + // eslint-disable-line
                            `<span class="searchvalue" >${it.keyword}</span><span class="valuenum">约${it.count}个商品</span>` + // eslint-disable-line
                        '</a>' +
                    '</li>';

                data.push(item);
            }
        }

        dest.data = data;
        res.send(req.query.callback + '(' + JSON.stringify(dest) + ')');

    }).catch(next);
};

const searchFilterBrands = (req, res, next) => {
    let params = req.query;
    const callback = req.query.callback;

    _.unset(params, 'callback');
    _.unset(params, '_');

    if (params.pathname) {
        let path = _.last(_.compact(_.split(params.pathname, '/')));

        _.unset(params, 'pathname');
        _.assign(params, parameter.minPathToFullParam(path));
    }

    req.ctx(search).getBrands4Filter(Object.assign({}, params, {
        keyword: req.query.query || ''
    })).then(result => {
        let dest = {
            code: 200,
            data: result || ''
        };

        res.type('text/javascript');
        res.send(callback + '(' + JSON.stringify(dest) + ')');
    }).catch(next);
};

/**
 * search 历史记录提示
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */

const searchHistory = (req, res) => {
    let history = req.cookies['_History'] || ''; //eslint-disable-line
    let q = _.trim(req.query.query || '');

    try {
        q = decodeURIComponent(q);
    } catch (e) { //eslint-disable-line
        q = '';
    }

    history = _.filter(history.split(','), old => old && old !== q);

    if (q) {
        history.unshift(q);
        res.cookie('_History', _.take(history, 9).join(','), {
            domain: config.cookieDomain,
            maxAge: 60 * 60 * 24 * 1000 * 30 // 一月
        });
    }

    let data = _.map(history, (item) => {
        return {
            keyword: item,
            href: helpers.urlFormat('', {query: item}, 'search')
        };
    });

    res.type('text/javascript');
    res.send(req.query.callback + '(' + JSON.stringify({ data }) + ')');
};

/**
 * 搜索少或无 有可能喜欢
 **/
const searchLessRecommend = (req, res, next) => {
    let channelNum = req.yoho.channelNum;
    let uid = req.user.uid;
    let udid = req.yoho.udid;
    let page = Number(req.query.page || 1);

    if (page <= 0 || page >= 6) {
        page = 1;
    }

    req.ctx(search).getSearchLessProduct(channelNum, uid, udid, page).then(result => {
        return res.send(result);
    }).catch(next);

};

/**
 * 搜索推荐列表页
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
const keyword = (req, res, next) => {
    let params = req.query;
    let query = stringCode.hexToUtf8(req.params.id || '');

    let resData = {};

    return req.ctx(search).getSearchKeywordData(Object.assign(params, {query: query}),
        req.yoho.channel).then(result => {
            Object.assign(resData, result, {
                pageNoFollow: true,
                title: `${query}价格_图片_品牌_怎么样-YOHO!BUY有货`,
                keywords: `${query},${query}价格,${query}图片,${query}怎么样,${query}品牌,YOHO!BUY有货`,
                description: `YOHO!BUY有货网yohobuy.com是国内专业的${query}网上潮流购物商城,为您找到${_.get(result,
                    'search.totalCount', 0)}${query}、产品的详细参数,实时报价,价格行情,图片、评价、品牌等信息。买${query},就上YOHO!BUY有货`
            });

            if (!_.get(resData, 'search.goods') || !_.get(resData, 'search.goods').length) {
                _.set(resData, 'search.keyWord', query);
                return res.render('search/no-result', resData);
            }
            res.render('search/index', resData);

        }).catch(next);
};

/**
 * 搜索推荐列表页
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
const keyId = (req, res, next) => {
    let params = req.query;
    let id = req.params.id;

    return req.ctx(search).getSearchKeywordDataById(id, Object.assign({stocknumber: -1}, params),
        req.yoho.channel).then(result => {

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

            let query = result.queryKey;

            Object.assign(result, {
                pageNoFollow: true,
                title: `${query}价格_图片_品牌_怎么样-YOHO!BUY有货`,
                keywords: `${query},${query}价格,${query}图片,${query}怎么样,${query}品牌,YOHO!BUY有货`,
                description: `YOHO!BUY有货网yohobuy.com是国内专业的${query}网上潮流购物商城,为您找到${_.get(result,
                'search.totalCount', 0)}${query}、产品的详细参数,实时报价,价格行情,图片、评价、品牌等信息。买${query},就上YOHO!BUY有货`,
                pageFooterSeo: {
                    description: `YOHO!BUY有货网yohobuy.com是国内专业的<b>${query}</b>网上潮流购物商城,为您找到<b>${_.get(result,
                        'search.totalCount', 0)}</b>条<b>${query}</b>、产品的详细参数,实时报价,价格行情,图片、评价、品牌等信息。买<b>` +
                        `${query}</b>,就上YOHO!BUY有货!`,
                    queryKey: query,
                    wapUrl: helpers.urlFormat(`/chanpin/${id}.html`, null, 'm')
                }
            });

            res.render('search/index', result);
        }).catch(next);
};

module.exports = {
    index,
    suggest,
    suggest4Old,
    searchFilterBrands,
    searchHistory,   // 搜索历史记录
    searchLessRecommend,
    keyword,
    keyId
};