search.js 6.41 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 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 = {
        title: '潮流商品搜索 | YOHO!BUY 有货',
        keywords: 'Yoho! 有货,潮流,时尚,流行,购物,B2C,正品,购物网站,网上购物,货到付款,' +
                  '品牌服饰,男士护肤,黑框眼镜,匡威,板鞋,i.t,izzue,5cm,eastpak,vans,lylescott,g-shock,' +
                  'new balance,lacoste,melissa,casio,卡西欧手表,舒雅,jasonwood,odm,AAAA,香港购物,日本潮流',
        description: '潮流商品搜索,上衣,衬衫,TEE,卫衣,冲锋衣,风衣,羽绒服,裤子,休闲鞋,板鞋,配饰,复古眼镜'
    };

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

        return search.getSearchData(Object.assign(params, {uid: req.user.uid, prid: req.user.prid}),
            req.yoho.channel).then(result => {
                Object.assign(resData, result);

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

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

/**
 * search 提示
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
const suggest = (req, res, next) => {
    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) => {
    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) => {
    search.getBrands4Filter(Object.assign({}, req.query, {
        keyword: req.query.query || ''
    })).then(result => {
        let dest = {
            code: 200,
            data: result || ''
        };

        res.type('text/javascript');
        res.send(req.query.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 = req.query.query ? _.trim(decodeURIComponent(req.query.query)) : '';

    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;
    }

    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 suggestIndex = (req, res, next) => {
    let params = req.params.id || '';

    let resData = {
        title: '潮流商品搜索 | YOHO!BUY 有货',
        keywords: 'Yoho! 有货,潮流,时尚,流行,购物,B2C,正品,购物网站,网上购物,货到付款,' +
        '品牌服饰,男士护肤,黑框眼镜,匡威,板鞋,i.t,izzue,5cm,eastpak,vans,lylescott,g-shock,' +
        'new balance,lacoste,melissa,casio,卡西欧手表,舒雅,jasonwood,odm,AAAA,香港购物,日本潮流',
        description: '潮流商品搜索,上衣,衬衫,TEE,卫衣,冲锋衣,风衣,羽绒服,裤子,休闲鞋,板鞋,配饰,复古眼镜'
    };

    // return search.getSearchSuggestData{query: stringCode.hexToUtf8(params)},
    return search.getSearchSuggestData({query: '卫衣'},
        req.yoho.channel).then(result => {
            Object.assign(resData, result);

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

        }).catch(next);
};

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