Authored by 周少峰

Merge branch 'feature/searchPlus' into release/5.1

@@ -16,6 +16,7 @@ const _ = require('lodash'); @@ -16,6 +16,7 @@ const _ = require('lodash');
16 * @return {[type]} [description] 16 * @return {[type]} [description]
17 */ 17 */
18 exports.index = (req, res, next) => { 18 exports.index = (req, res, next) => {
  19 + let params = req.query;
19 let resData = { 20 let resData = {
20 title: '潮流商品搜索 | YOHO!BUY 有货', 21 title: '潮流商品搜索 | YOHO!BUY 有货',
21 keywords: 'Yoho! 有货,潮流,时尚,流行,购物,B2C,正品,购物网站,网上购物,货到付款,' + 22 keywords: 'Yoho! 有货,潮流,时尚,流行,购物,B2C,正品,购物网站,网上购物,货到付款,' +
@@ -24,16 +25,23 @@ exports.index = (req, res, next) => { @@ -24,16 +25,23 @@ exports.index = (req, res, next) => {
24 description: '潮流商品搜索,上衣,衬衫,TEE,卫衣,冲锋衣,风衣,羽绒服,裤子,休闲鞋,板鞋,配饰,复古眼镜' 25 description: '潮流商品搜索,上衣,衬衫,TEE,卫衣,冲锋衣,风衣,羽绒服,裤子,休闲鞋,板鞋,配饰,复古眼镜'
25 }; 26 };
26 27
27 - search.getSearchData(req.query, req.yoho.channel).then(result => {  
28 - Object.assign(resData, result);  
29 -  
30 - if (!_.get(resData, 'search.goods') || !_.get(resData, 'search.goods').length) {  
31 - _.set(resData, 'search.keyWord', req.query.query);  
32 - return res.render('search/no-result', resData); 28 + search.getKeyActivity(params.query).then(activityUrl => {
  29 + if (activityUrl) {
  30 + activityUrl = encodeURI(activityUrl);
  31 + res.redirect(activityUrl);
  32 + return;
33 } 33 }
34 - res.render('search/index', resData);  
35 - }).catch(next);  
36 34
  35 + search.getSearchData(params, req.yoho.channel).then(result => {
  36 + Object.assign(resData, result);
  37 +
  38 + if (!_.get(resData, 'search.goods') || !_.get(resData, 'search.goods').length) {
  39 + _.set(resData, 'search.keyWord', req.query.query);
  40 + return res.render('search/no-result', resData);
  41 + }
  42 + res.render('search/index', resData);
  43 + });
  44 + }).catch(next);
37 }; 45 };
38 46
39 /** 47 /**
@@ -40,6 +40,16 @@ const getSearchCackeKey = params => { @@ -40,6 +40,16 @@ const getSearchCackeKey = params => {
40 40
41 const getProductListOrig = finalParams => api.get('', finalParams); 41 const getProductListOrig = finalParams => api.get('', finalParams);
42 42
  43 +const getKeyActivityAsync = (query) => {
  44 + return yohoApi.get('', {
  45 + method: 'app.search.word',
  46 + query: query
  47 + }, {
  48 + catch: true,
  49 + code: 200
  50 + });
  51 +};
  52 +
43 /** 53 /**
44 * 获取商品列表 54 * 获取商品列表
45 * @return 55 * @return
@@ -508,6 +518,7 @@ const getBrands4Filter = (params) => { @@ -508,6 +518,7 @@ const getBrands4Filter = (params) => {
508 518
509 module.exports = { 519 module.exports = {
510 getSortByConditionAsync, 520 getSortByConditionAsync,
  521 + getKeyActivityAsync,
511 getProductList, 522 getProductList,
512 getSortList, 523 getSortList,
513 getSortIntro, 524 getSortIntro,
@@ -13,6 +13,12 @@ const searchHandler = require('./search-handler'); @@ -13,6 +13,12 @@ const searchHandler = require('./search-handler');
13 const _ = require('lodash'); 13 const _ = require('lodash');
14 const needParams = ['query', 'msort', 'misort']; 14 const needParams = ['query', 'msort', 'misort'];
15 15
  16 +exports.getKeyActivity = (query) => {
  17 + return searchApi.getKeyActivityAsync(query).then(result => {
  18 + return _.get(result, 'data.urlobj.pcUrl', '');
  19 + });
  20 +};
  21 +
16 /** 22 /**
17 * 获取搜索数据 23 * 获取搜索数据
18 * @param {[type]} origin [description] 24 * @param {[type]} origin [description]
@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 8
9 const _ = require('lodash'); 9 const _ = require('lodash');
10 10
  11 +const api = global.yoho.API;
11 const serviceApi = global.yoho.ServiceAPI; 12 const serviceApi = global.yoho.ServiceAPI;
12 13
13 /** 14 /**
@@ -190,25 +191,44 @@ const setHeaderData = (resData, type) => ( @@ -190,25 +191,44 @@ const setHeaderData = (resData, type) => (
190 ); 191 );
191 192
192 193
  194 +const getHotSearchAsync = () => {
  195 + return api.get('', {method: 'app.search.getTerms'}, {
  196 + cache: 600,
  197 + code: 200
  198 + });
  199 +};
  200 +
  201 +const getHeaderNavAsync = () => {
  202 + return serviceApi.get('operations/api/v6/category/getCategory', {}, {
  203 + cache: true,
  204 + code: 200
  205 + });
  206 +};
  207 +
193 /** 208 /**
194 * 请求头部数据 209 * 请求头部数据
195 * @param {String} 频道类型 210 * @param {String} 频道类型
196 * @return {promise} 211 * @return {promise}
197 */ 212 */
198 -exports.requestHeaderData = (type, parentId) => {  
199 - let data = {  
200 - client_type: 'web',  
201 - parent_id: parentId || null  
202 - }; 213 +exports.requestHeaderData = (type) => {
  214 + let resData = {};
203 215
204 type = type || 'boys'; 216 type = type || 'boys';
205 217
206 - return serviceApi.get('operations/api/v6/category/getCategory', data, {  
207 - cache: true,  
208 - code: 200  
209 - }).then(res => {  
210 - return {  
211 - headerData: res ? setHeaderData(res.data, type) : {}  
212 - }; 218 + return Promise.all([
  219 + getHeaderNavAsync(),
  220 + getHotSearchAsync()
  221 + ]).then(res => {
  222 + resData.headerData = {};
  223 +
  224 + if (res[0] && res[0].data) {
  225 + Object.assign(resData.headerData, setHeaderData(res[0].data, type));
  226 + }
  227 +
  228 + if (res[1] && res[1].data) {
  229 + resData.headerData.defaultSearch = _.get(res[1], 'data.hotTerms[0].content', '');
  230 + }
  231 +
  232 + return resData;
213 }); 233 });
214 }; 234 };
@@ -84,8 +84,8 @@ @@ -84,8 +84,8 @@
84 <ul class="search-suggest"></ul> 84 <ul class="search-suggest"></ul>
85 <div class="search-2016"> 85 <div class="search-2016">
86 <form action="//search.yohobuy.com" method="get" id="search-form"> 86 <form action="//search.yohobuy.com" method="get" id="search-form">
87 - <input type="hidden" id="defaultsearch">  
88 - <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"> 87 + <input type="hidden" id="defaultsearch" value="{{defaultSearch}}">
  88 + <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">
89 <a class="search-btn" href="javascript:submitSearch();"></a> 89 <a class="search-btn" href="javascript:submitSearch();"></a>
90 </form> 90 </form>
91 </div> 91 </div>
@@ -12,6 +12,7 @@ var $tool = $('.tool-wrapper'), @@ -12,6 +12,7 @@ var $tool = $('.tool-wrapper'),
12 12
13 var $head = $('.head-wrapper'), 13 var $head = $('.head-wrapper'),
14 $searchForm = $('#search-form'), 14 $searchForm = $('#search-form'),
  15 + $searchKey = $searchForm.find('.search-key'),
15 $logotrans = $head.find('.main-logo'), 16 $logotrans = $head.find('.main-logo'),
16 $searchSug = $head.find('.search-suggest'), 17 $searchSug = $head.find('.search-suggest'),
17 $goCart = $head.find('.go-cart'), 18 $goCart = $head.find('.go-cart'),
@@ -23,6 +24,8 @@ var $subNav = $('.sub-nav-list.cure .contain-third'); @@ -23,6 +24,8 @@ var $subNav = $('.sub-nav-list.cure .contain-third');
23 24
24 var fetchUserInfoEvent = $.Callbacks('once'); // eslint-disable-line 25 var fetchUserInfoEvent = $.Callbacks('once'); // eslint-disable-line
25 26
  27 +var defaultSearch = $('#defaultsearch').val() || '';
  28 +
26 var thirdLineNum = 9, 29 var thirdLineNum = 9,
27 delayer, 30 delayer,
28 centerFn, 31 centerFn,
@@ -32,8 +35,6 @@ var thirdLineNum = 9, @@ -32,8 +35,6 @@ var thirdLineNum = 9,
32 35
33 var logoAngle = 0, 36 var logoAngle = 0,
34 loopTime = 500, 37 loopTime = 500,
35 - keyword = '',  
36 - link = '',  
37 reg = /^[\^\!\+\-\(\)\:\[\]\\\{\}\~\*\?\|\&\;\/]{0,}$/g; 38 reg = /^[\^\!\+\-\(\)\:\[\]\\\{\}\~\*\?\|\&\;\/]{0,}$/g;
38 39
39 var dataLayer = []; 40 var dataLayer = [];
@@ -295,6 +296,21 @@ function searchSuggest(key) { @@ -295,6 +296,21 @@ function searchSuggest(key) {
295 }); 296 });
296 } 297 }
297 298
  299 +function submitSearch() {
  300 + var searchKey = $searchKey.val();
  301 +
  302 + searchKey = $.trim(searchKey.toLowerCase());
  303 +
  304 + if (reg.test(searchKey) && searchKey !== '') {
  305 + location.href = 'http://search.yohobuy.com/error?query=' + searchKey;
  306 + } else {
  307 + if (searchKey === '') {
  308 + $('#query-key').val(defaultSearch);
  309 + }
  310 + $searchForm.submit();
  311 + }
  312 +}
  313 +
298 // 同步mini购物车数据 314 // 同步mini购物车数据
299 function syncCratInfo(strG) { 315 function syncCratInfo(strG) {
300 var info, total; 316 var info, total;
@@ -568,7 +584,7 @@ $yohoGroup.hover(function() { @@ -568,7 +584,7 @@ $yohoGroup.hover(function() {
568 $(this).text(data.en); 584 $(this).text(data.en);
569 }); 585 });
570 586
571 -$searchForm.on('keyup', '.search-key', function(e) { 587 +$searchKey.keyup(function(e) {
572 var val = $.trim($(this).val()), 588 var val = $.trim($(this).val()),
573 $child = $searchSug.find('li'), 589 $child = $searchSug.find('li'),
574 $act = $searchSug.find('.action'), 590 $act = $searchSug.find('.action'),
@@ -594,16 +610,27 @@ $searchForm.on('keyup', '.search-key', function(e) { @@ -594,16 +610,27 @@ $searchForm.on('keyup', '.search-key', function(e) {
594 $focus.addClass('action'); 610 $focus.addClass('action');
595 $(this).val($focus.find('.searchvalue').text()); 611 $(this).val($focus.find('.searchvalue').text());
596 } else if (e.which === 13) { 612 } else if (e.which === 13) {
597 - if (val !== '') {  
598 - $searchForm.submit();  
599 - } 613 + submitSearch();
600 } else { 614 } else {
601 val = val.replace(new RegExp('\'', 'gm'), ''); // 去掉特殊字符 615 val = val.replace(new RegExp('\'', 'gm'), ''); // 去掉特殊字符
602 616
603 $(this).val(val); 617 $(this).val(val);
604 searchSuggest(val); 618 searchSuggest(val);
605 } 619 }
606 -}).on('blur', '.search-key', function() { 620 +}).focus(function() {
  621 + var val = $.trim($(this).val());
  622 +
  623 + if (val === defaultSearch) {
  624 + $(this).val('');
  625 + }
  626 +
  627 + $(this).css('color', '#333');
  628 +}).blur(function() {
  629 + var val = $.trim($(this).val());
  630 +
  631 + if (val === '') {
  632 + $(this).val(defaultSearch).css('color', '#999');
  633 + }
607 setTimeout(function() { 634 setTimeout(function() {
608 $searchSug.hide(); 635 $searchSug.hide();
609 }, 200); 636 }, 200);
@@ -779,59 +806,19 @@ function actionCover() { @@ -779,59 +806,19 @@ function actionCover() {
779 } 806 }
780 } 807 }
781 808
782 -window.submitSearch = function() {  
783 - var $keywordinput = $('#query-key').val();  
784 - var $defaultsearch = $('#defaultsearch').val();  
785 - var column = 'Search';  
786 - var postition = 'Head Search';  
787 - var event = 'Search';  
788 - var $formatkeyword,  
789 - $formatkeywordinput;  
790 -  
791 - $formatkeyword = $.trim(keyword.toLowerCase());  
792 - $formatkeywordinput = $.trim($keywordinput.toLowerCase());  
793 - getSource(column, postition, event);  
794 - if (link !== '' && keyword !== '' && $formatkeyword === $formatkeywordinput) {  
795 - location.href = link.replace('\'', '');  
796 - } else {  
797 - if (reg.test($keywordinput) && $keywordinput !== '') {  
798 - location.href = 'http://search.yohobuy.com/error?query=' + $keywordinput + '&result=error';  
799 - } else {  
800 - if ($keywordinput === '') {  
801 - $('#query-key').val($defaultsearch);  
802 - }  
803 -  
804 - $searchForm.submit();  
805 - return false;  
806 - }  
807 - }  
808 -}; 809 +window.submitSearch = submitSearch;
809 810
810 /** 811 /**
811 * 查询跳转后保留关键字 812 * 查询跳转后保留关键字
812 * @return {[type]} [description] 813 * @return {[type]} [description]
813 */ 814 */
814 function actionAddKeyWords() { 815 function actionAddKeyWords() {
815 - var $keywords = $('#nav_keyword').text();  
816 - var defaultsearch = $('#defaultsearch').val();  
817 - var $querykey = $('#query-key'); 816 + var keywords = $('#nav_keyword').text();
818 817
819 - if ($keywords !== '') {  
820 - $querykey.css({  
821 - color: '#000'  
822 - });  
823 - $querykey.val($keywords); 818 + if (keywords !== '') {
  819 + $searchKey.val(keywords).css('color', '#333');
824 } else { 820 } else {
825 - $querykey.css({  
826 - color: '#e0e0e0'  
827 - });  
828 - $querykey.val(defaultsearch);  
829 - $querykey.on('focus', function() {  
830 - $querykey.css({  
831 - color: '#000'  
832 - });  
833 - $querykey.val('');  
834 - }); 821 + $searchKey.val(defaultSearch).css('color', '#999');
835 } 822 }
836 $(document).click(function(e) { 823 $(document).click(function(e) {
837 if (!$(e.target).closest('.searchspan, .search-list').length) { 824 if (!$(e.target).closest('.searchspan, .search-list').length) {