Authored by htoooth

Merge branch 'feature/searchTerms' into release/5.5

# Conflicts:
#	apps/product/controllers/search.js
/**
* Controller
* @author: gxh<xuhui.ge@yoho.cn>
* @date: 2017/02/28
*/
'use strict';
const mRoot = '../models';
const search = require(`${mRoot}/search`);
const _ = require('lodash');
/**
* search 搜索历史提示
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
const suggestHistory = (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}">` + // 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);
};
module.exports = {
suggestHistory
};
/**
* Models-api
* @author: gxh<xuhui.ge@yoho.cn>
* @date: 2017/02/28
*/
'use strict';
const _ = require('lodash');
const md5 = require('md5');
const api = global.yoho.API;
const config = global.yoho.config;
const getSuggest = (params) => {
let finalParams = {
method: 'app.search.fuzzy',
keyword: params.keyword || ''
};
return api.get('', finalParams, config.apiCache);
};
module.exports = {
getSuggest
};
/**
* Models-service
* @author: gxh<xuhui.ge@yoho.cn>
* @date: 2017/02/28
*/
'use strict';
const utils = '../../../utils';
const api = global.yoho.API;
const helpers = global.yoho.helpers;
const searchApi = require('./search-api');
const headerModel = require('../../../doraemon/models/header');
// const searchHandler = require('./search-handler');
const _ = require('lodash');
const needParams = ['query', 'msort', 'misort'];
/**
* 获取搜索数据
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
// exports.getSearchData = (params, channel) => {
// let searchParams = searchHandler.getSearchParams(params);
//
// switch (channel) {
// case 'boys':
// searchParams.physical_channel = 1;
// break;
// case 'girls':
// searchParams.physical_channel = 2;
// break;
// case 'kids':
// searchParams.physical_channel = 3;
// break;
// case 'lifestyle':
// searchParams.physical_channel = 4;
// break;
// default:
// break;
// }
//
// // 调用接口
// let apiMethod = [
// headerModel.requestHeaderData(channel),
// searchApi.getSortList(Object.assign({}, searchParams, {msort: '', misort: '', sort: ''})),
// searchApi.getProductList(searchParams),
// searchApi.getShopList(params)
// ];
//
// return api.all(apiMethod).then(result => {
// let finalResult = {
// headerData: Object.assign(result[0].headerData, {
// header: true
// }),
// search: {}
// };
//
// // 获取左侧类目数据
// if (result[1].code === 200) {
// let dps = {};
//
// _.forEach(needParams, (value) => {
// if (params[value]) {
// dps[value] = params[value];
// }
// });
//
// finalResult.search = {
// leftContent: searchHandler.handleSortData(result[1].data.sort, dps, params)
// };
// }
//
// // 获取商品数据和顶部筛选条件
// if (result[2].code === 200) {
//
// let filters = Object.assign(searchHandler.handleFilterDataAll(result[2].data, params),
// finalResult.search.leftContent.sort);
//
// filters.checkedConditions.conditions = _.concat(filters.checkedConditions.conditions,
// finalResult.search.leftContent.checked);
//
// Object.assign(finalResult.search,
// searchHandler.handlePathNavData({total: result[2].data.total }, params, 'search', channel),
// {
// filters: filters,
// opts: searchHandler.handleOptsData(params, result[2].data.total, result[2].data.filter),
// totalCount: result[2].data.total,
// footPager: searchHandler.handlePagerData(result[2].data.total, params),
// goods: productProcess.processProductList(result[2].data.product_list,
// Object.assign({showDiscount: false, from: {type: 'search', params: params}}, params)),
// latestWalk: 6,
// hasNextPage: searchHandler.handleNextPage(params, result[2].data.total),
// shopEntry: result[3]
// });
//
// finalResult.criteo = {skn: searchHandler.getCriteo(_.get(finalResult.search, 'goods'))};
// }
// return finalResult;
// });
// };
/**
* 处理搜索提示
* @type {[type]}
*/
const handleSuggest = (data) => {
let suggest = [];
_.forEach(data, function(value) {
suggest.push({
href: helpers.urlFormat('', {query: value.keyword}, 'search'),
keyword: value.keyword,
count: value.count
});
});
return suggest;
};
/**
* 搜索提示
*/
const getSuggest = (params) => {
return searchApi.getSuggest(params).then(result => {
let dest = [];
if (result.code === 200) {
dest = handleSuggest(result.data);
}
return dest;
});
};
module.exports = {
handleSuggest,
getSuggest
};
... ... @@ -20,7 +20,6 @@ const config = global.yoho.config;
*/
const index = (req, res, next) => {
let params = req.query;
let resData = {
title: '潮流商品搜索 | YOHO!BUY 有货',
keywords: 'Yoho! 有货,潮流,时尚,流行,购物,B2C,正品,购物网站,网上购物,货到付款,' +
... ... @@ -44,7 +43,6 @@ const index = (req, res, next) => {
return res.render('search/no-result', resData);
}
res.render('search/index', resData);
});
}).catch(next);
};
... ... @@ -97,7 +95,7 @@ const suggest4Old = (req, res, next) => {
}).catch(next);
};
const serachFilterBrands = (req, res, next) => {
const searchFilterBrands = (req, res, next) => {
search.getBrands4Filter(Object.assign({}, req.query, {
keyword: req.query.query || ''
})).then(result => {
... ... @@ -119,15 +117,10 @@ const serachFilterBrands = (req, res, next) => {
*/
const searchHistory = (req, res) => {
let history = req.cookies['_History'] || ''; //eslint-disable-line
let q = req.query.query ? _.trim(decodeURIComponent(req.query.query)) : '';
let history = _.trim(req.cookies['_History']); //eslint-disable-line
let q = _.trim(decodeURIComponent(req.query.query));
let data = [];
let ResData = {};
history = history ? history.split(',') : [];
history = _.reject(history, old => old === q ? true : false);
history = _.filter(history.split(','), old => old && old !== q);
if (q) {
history.unshift(q);
... ... @@ -137,20 +130,15 @@ const searchHistory = (req, res) => {
});
}
_.forEach(history, function(item) {
if (item !== 'undefined') {
let list = {};
list['keyword'] = item; //eslint-disable-line
list['href'] = helpers.urlFormat('', {query: list['keyword']}, 'search'); //eslint-disable-line
data.push(list);
}
let data = _.map(history, (item) => {
return {
keyword: item,
href: helpers.urlFormat('', {query: item}, 'search')
};
});
ResData['data'] = data; //eslint-disable-line
res.type('text/javascript');
res.send(req.query.callback + '(' + JSON.stringify(ResData) + ')');
res.send(req.query.callback + '(' + JSON.stringify({ data }) + ')');
};
... ... @@ -162,11 +150,6 @@ const searchHistory = (req, res) => {
*/
const searchRecommend = (req, res, next) => {
search.getSearchRecommend(req.yoho.channel, req.query).then(result => {
if (req.query.json === '1') {
return res.send(result);
}
let dest = {
code: 200,
message: 'recommend',
... ... @@ -179,32 +162,11 @@ const searchRecommend = (req, res, next) => {
}).catch(next);
};
/**
* 搜索少或无 有可能喜欢
**/
const searchLessRecommend = (req, res, next) => {
let channel = req.yoho.channel;
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(channel, uid, udid, page).then(result => {
return res.send(result);
}).catch(next);
};
module.exports = {
index,
suggest,
suggest4Old,
serachFilterBrands,
searchHistory,
searchRecommend,
searchLessRecommend
searchFilterBrands,
searchHistory, // 搜索历史记录
searchRecommend // 搜索框下方 热门搜索
};
... ...
... ... @@ -425,17 +425,14 @@ const getBrands4Filter = (params) => {
* @return
*/
const getSearchRecommend = (channel, params) => {
let finalParams = {
return api.get('', {
method: 'app.search.getTerms',
content: params.content || '',
yh_channel: channel,
sort: params.sort,
status: params.status,
type: params.type
};
return api.get('', finalParams, config.apiCache);
}, config.apiCache);
};
/**
... ...
... ... @@ -228,33 +228,28 @@ exports.getBrands4Filter = (params) => {
* @return {[type]} [description]
*/
exports.getSearchRecommend = (channel, params) => {
let channelNum = (function() {
switch (channel) {
case 'boys':
return 1;
case 'girls':
return 2;
case 'kids':
case 'lifestyle':
return 3;
default:
return 1;
}
}());
let channelNum = 1;
switch (channel) {
case 'boys':
channelNum = 1;
break;
case 'girls':
channelNum = 2;
break;
case 'kids':
case 'lifestyle':
channelNum = 3;
break;
default:
break;
}
return searchApi.getSearchRecommend(channelNum, params).then(result => {
return searchApi.getSearchRecommend(channelNum, params).then((result) => {
if (result.code === 200) {
let resData = {
code: 200,
hotTerms: []
};
resData.hotTerms = _.map(result.data.hotTerms, function(value) {
resData.hotTerms = _.map(result.data.hotTerms, (value) => {
return {
href: helpers.urlFormat('', {query: value.content}, 'search'),
content: value.content,
... ...
... ... @@ -85,7 +85,7 @@ router.post('/detail/notify/cancel', auth, notify.cancel); // 删é™
// 搜索
router.get('/search/index', gbk2utf, search.index);
router.get('/search/filter/brands', search.serachFilterBrands);
router.get('/search/filter/brands', search.searchFilterBrands);
router.get('/search/suggest', search.suggest); // 搜索提示
router.get('/api/suggest', search.suggest4Old);
router.get('/search/history', search.searchHistory); // 搜索历史提示
... ...
... ... @@ -928,7 +928,7 @@ function actionAddKeyWords() {
function searchSuggestHistory() {
var param = {
return_type: 'jsonp',
query: window.queryString().query
query: encodeURIComponent(window.queryString().query.replace(/\+/ig, ' '))
};
if ($searchKey.attr('alt')) {
... ... @@ -963,7 +963,6 @@ function searchSuggestHistory() {
});
}
/**
* 搜索 历史记录 dom
* @return {[type]} [description]
... ...