Authored by 周少峰

Merge branch 'feature/searchPlus' into release/5.1

... ... @@ -16,6 +16,7 @@ const _ = require('lodash');
* @return {[type]} [description]
*/
exports.index = (req, res, next) => {
let params = req.query;
let resData = {
title: '潮流商品搜索 | YOHO!BUY 有货',
keywords: 'Yoho! 有货,潮流,时尚,流行,购物,B2C,正品,购物网站,网上购物,货到付款,' +
... ... @@ -24,7 +25,14 @@ exports.index = (req, res, next) => {
description: '潮流商品搜索,上衣,衬衫,TEE,卫衣,冲锋衣,风衣,羽绒服,裤子,休闲鞋,板鞋,配饰,复古眼镜'
};
search.getSearchData(req.query, req.yoho.channel).then(result => {
search.getKeyActivity(params.query).then(activityUrl => {
if (activityUrl) {
activityUrl = encodeURI(activityUrl);
res.redirect(activityUrl);
return;
}
search.getSearchData(params, req.yoho.channel).then(result => {
Object.assign(resData, result);
if (!_.get(resData, 'search.goods') || !_.get(resData, 'search.goods').length) {
... ... @@ -32,8 +40,8 @@ exports.index = (req, res, next) => {
return res.render('search/no-result', resData);
}
res.render('search/index', resData);
});
}).catch(next);
};
/**
... ...
... ... @@ -40,6 +40,16 @@ const getSearchCackeKey = params => {
const getProductListOrig = finalParams => api.get('', finalParams);
const getKeyActivityAsync = (query) => {
return yohoApi.get('', {
method: 'app.search.word',
query: query
}, {
catch: true,
code: 200
});
};
/**
* 获取商品列表
* @return
... ... @@ -508,6 +518,7 @@ const getBrands4Filter = (params) => {
module.exports = {
getSortByConditionAsync,
getKeyActivityAsync,
getProductList,
getSortList,
getSortIntro,
... ...
... ... @@ -13,6 +13,12 @@ const searchHandler = require('./search-handler');
const _ = require('lodash');
const needParams = ['query', 'msort', 'misort'];
exports.getKeyActivity = (query) => {
return searchApi.getKeyActivityAsync(query).then(result => {
return _.get(result, 'data.urlobj.pcUrl', '');
});
};
/**
* 获取搜索数据
* @param {[type]} origin [description]
... ...
... ... @@ -8,6 +8,7 @@
const _ = require('lodash');
const api = global.yoho.API;
const serviceApi = global.yoho.ServiceAPI;
/**
... ... @@ -190,25 +191,44 @@ const setHeaderData = (resData, type) => (
);
const getHotSearchAsync = () => {
return api.get('', {method: 'app.search.getTerms'}, {
cache: 600,
code: 200
});
};
const getHeaderNavAsync = () => {
return serviceApi.get('operations/api/v6/category/getCategory', {}, {
cache: true,
code: 200
});
};
/**
* 请求头部数据
* @param {String} 频道类型
* @return {promise}
*/
exports.requestHeaderData = (type, parentId) => {
let data = {
client_type: 'web',
parent_id: parentId || null
};
exports.requestHeaderData = (type) => {
let resData = {};
type = type || 'boys';
return serviceApi.get('operations/api/v6/category/getCategory', data, {
cache: true,
code: 200
}).then(res => {
return {
headerData: res ? setHeaderData(res.data, type) : {}
};
return Promise.all([
getHeaderNavAsync(),
getHotSearchAsync()
]).then(res => {
resData.headerData = {};
if (res[0] && res[0].data) {
Object.assign(resData.headerData, setHeaderData(res[0].data, type));
}
if (res[1] && res[1].data) {
resData.headerData.defaultSearch = _.get(res[1], 'data.hotTerms[0].content', '');
}
return resData;
});
};
... ...
... ... @@ -84,8 +84,8 @@
<ul class="search-suggest"></ul>
<div class="search-2016">
<form action="//search.yohobuy.com" method="get" id="search-form">
<input type="hidden" id="defaultsearch">
<input class="search-key" type="text" name="query" id="query-key" autocomplete="off" x-webkit-speech="" lang="zh-CN" x-webkit-grammar="builtin:translate" value="" onfocus="if(this.value==this.defaultValue){this.value='';this.style.color='#333'}" onblur="if(this.value==''){this.value=this.defaultValue;this.style.color='#999'}" maxlength="30">
<input type="hidden" id="defaultsearch" value="{{defaultSearch}}">
<input class="search-key" type="text" name="query" id="query-key" autocomplete="off" x-webkit-speech="" lang="zh-CN" x-webkit-grammar="builtin:translate" value="" maxlength="30">
<a class="search-btn" href="javascript:submitSearch();"></a>
</form>
</div>
... ...
... ... @@ -12,6 +12,7 @@ var $tool = $('.tool-wrapper'),
var $head = $('.head-wrapper'),
$searchForm = $('#search-form'),
$searchKey = $searchForm.find('.search-key'),
$logotrans = $head.find('.main-logo'),
$searchSug = $head.find('.search-suggest'),
$goCart = $head.find('.go-cart'),
... ... @@ -23,6 +24,8 @@ var $subNav = $('.sub-nav-list.cure .contain-third');
var fetchUserInfoEvent = $.Callbacks('once'); // eslint-disable-line
var defaultSearch = $('#defaultsearch').val() || '';
var thirdLineNum = 9,
delayer,
centerFn,
... ... @@ -32,8 +35,6 @@ var thirdLineNum = 9,
var logoAngle = 0,
loopTime = 500,
keyword = '',
link = '',
reg = /^[\^\!\+\-\(\)\:\[\]\\\{\}\~\*\?\|\&\;\/]{0,}$/g;
var dataLayer = [];
... ... @@ -295,6 +296,21 @@ function searchSuggest(key) {
});
}
function submitSearch() {
var searchKey = $searchKey.val();
searchKey = $.trim(searchKey.toLowerCase());
if (reg.test(searchKey) && searchKey !== '') {
location.href = 'http://search.yohobuy.com/error?query=' + searchKey;
} else {
if (searchKey === '') {
$('#query-key').val(defaultSearch);
}
$searchForm.submit();
}
}
// 同步mini购物车数据
function syncCratInfo(strG) {
var info, total;
... ... @@ -568,7 +584,7 @@ $yohoGroup.hover(function() {
$(this).text(data.en);
});
$searchForm.on('keyup', '.search-key', function(e) {
$searchKey.keyup(function(e) {
var val = $.trim($(this).val()),
$child = $searchSug.find('li'),
$act = $searchSug.find('.action'),
... ... @@ -594,16 +610,27 @@ $searchForm.on('keyup', '.search-key', function(e) {
$focus.addClass('action');
$(this).val($focus.find('.searchvalue').text());
} else if (e.which === 13) {
if (val !== '') {
$searchForm.submit();
}
submitSearch();
} else {
val = val.replace(new RegExp('\'', 'gm'), ''); // 去掉特殊字符
$(this).val(val);
searchSuggest(val);
}
}).on('blur', '.search-key', function() {
}).focus(function() {
var val = $.trim($(this).val());
if (val === defaultSearch) {
$(this).val('');
}
$(this).css('color', '#333');
}).blur(function() {
var val = $.trim($(this).val());
if (val === '') {
$(this).val(defaultSearch).css('color', '#999');
}
setTimeout(function() {
$searchSug.hide();
}, 200);
... ... @@ -779,59 +806,19 @@ function actionCover() {
}
}
window.submitSearch = function() {
var $keywordinput = $('#query-key').val();
var $defaultsearch = $('#defaultsearch').val();
var column = 'Search';
var postition = 'Head Search';
var event = 'Search';
var $formatkeyword,
$formatkeywordinput;
$formatkeyword = $.trim(keyword.toLowerCase());
$formatkeywordinput = $.trim($keywordinput.toLowerCase());
getSource(column, postition, event);
if (link !== '' && keyword !== '' && $formatkeyword === $formatkeywordinput) {
location.href = link.replace('\'', '');
} else {
if (reg.test($keywordinput) && $keywordinput !== '') {
location.href = 'http://search.yohobuy.com/error?query=' + $keywordinput + '&result=error';
} else {
if ($keywordinput === '') {
$('#query-key').val($defaultsearch);
}
$searchForm.submit();
return false;
}
}
};
window.submitSearch = submitSearch;
/**
* 查询跳转后保留关键字
* @return {[type]} [description]
*/
function actionAddKeyWords() {
var $keywords = $('#nav_keyword').text();
var defaultsearch = $('#defaultsearch').val();
var $querykey = $('#query-key');
var keywords = $('#nav_keyword').text();
if ($keywords !== '') {
$querykey.css({
color: '#000'
});
$querykey.val($keywords);
if (keywords !== '') {
$searchKey.val(keywords).css('color', '#333');
} else {
$querykey.css({
color: '#e0e0e0'
});
$querykey.val(defaultsearch);
$querykey.on('focus', function() {
$querykey.css({
color: '#000'
});
$querykey.val('');
});
$searchKey.val(defaultSearch).css('color', '#999');
}
$(document).click(function(e) {
if (!$(e.target).closest('.searchspan, .search-list').length) {
... ...