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

'use strict';
const mRoot = '../models';
const search = require(`${mRoot}/search`);

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

    search.getSearchData(req.query, req.yoho.channel).then(result => {
        Object.assign(resData, result);
        res.render('search/index', resData);
    }).catch(next);

};

/**
 * search 提示
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.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);
};

exports.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}">`+
                                    `<span class="searchvalue" >${it.keyword}</span><span class="valuenum">约${it.count}个商品</span>`+
                                `</a>`+
                            `</li>`;

                data.push(item);
            }
        }

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

    }).catch(next);
}