Authored by 王水玲

搜索联动

@@ -70,6 +70,11 @@ const Query = { @@ -70,6 +70,11 @@ const Query = {
70 } 70 }
71 }).catch(next); 71 }).catch(next);
72 72
  73 + },
  74 + suggest: (req, res, next) => {
  75 + Search.getSuggest(req.query).then(result => {
  76 + res.json(result);
  77 + }).catch(next);
73 } 78 }
74 }; 79 };
75 80
@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 'use strict'; 5 'use strict';
6 6
7 const api = global.yoho.API; 7 const api = global.yoho.API;
  8 +const helpers = global.yoho.helpers;
8 const _ = require('lodash'); 9 const _ = require('lodash');
9 10
10 /** 11 /**
@@ -107,6 +108,24 @@ const Search = { @@ -107,6 +108,24 @@ const Search = {
107 return api.get('', _.assign({ 108 return api.get('', _.assign({
108 method: 'web.regular.groupsort' 109 method: 'web.regular.groupsort'
109 }, params), {code: 200}); 110 }, params), {code: 200});
  111 + },
  112 + getSuggest(params) {
  113 + return api.get('', _.assign({
  114 + method: 'app.search.fuzzy'
  115 + }, params), {code: 200}).then(result => {
  116 + let list = result.data || [];
  117 + let suggest = [];
  118 +
  119 + _.forEach(list, function(value) {
  120 + suggest.push({
  121 + href: helpers.urlFormat('/product/query', {query: value.keyword}),
  122 + keyword: value.keyword,
  123 + count: value.count
  124 + });
  125 + });
  126 +
  127 + return suggest;
  128 + });
110 } 129 }
111 }; 130 };
112 131
@@ -32,6 +32,7 @@ router.post('/shop/togglecollect', auth, fav.shop); @@ -32,6 +32,7 @@ router.post('/shop/togglecollect', auth, fav.shop);
32 router.post('/brand/togglecollect', auth, fav.brand); 32 router.post('/brand/togglecollect', auth, fav.brand);
33 33
34 router.get('/query', query.index); 34 router.get('/query', query.index);
  35 +router.get('/query/suggest', query.suggest);
35 36
36 router.get('/getRecommendProduct', recommend.getRecommendProduct); 37 router.get('/getRecommendProduct', recommend.getRecommendProduct);
37 router.get('/getRecommendShop', recommend.getRecommendShop); 38 router.get('/getRecommendShop', recommend.getRecommendShop);
@@ -22,15 +22,15 @@ module.exports = { @@ -22,15 +22,15 @@ module.exports = {
22 }, 22 },
23 cookieDomain: 'yohoblk.com', 23 cookieDomain: 'yohoblk.com',
24 domains: { 24 domains: {
25 - // singleApi: 'http://single.yoho.cn/',  
26 - // api: 'http://api.yoho.yohoops.org/',  
27 - // service: 'http://service.yoho.yohoops.org/',  
28 - // search: 'http://search.yohoops.org/yohosearch/' 25 + singleApi: 'http://single.yoho.cn/',
  26 + api: 'http://api.yoho.yohoops.org/',
  27 + service: 'http://service.yoho.yohoops.org/',
  28 + search: 'http://search.yohoops.org/yohosearch/'
29 29
30 - api: 'http://api-test3.yohops.com:9999/',  
31 - service: 'http://service-test3.yohops.com:9999/',  
32 - liveApi: 'http://testapi.live.yohops.com:9999/',  
33 - singleApi: 'http://api-test3.yohops.com:9999/' 30 + //api: 'http://api-test3.yohops.com:9999/',
  31 + //service: 'http://service-test3.yohops.com:9999/',
  32 + //liveApi: 'http://testapi.live.yohops.com:9999/',
  33 + //singleApi: 'http://api-test3.yohops.com:9999/'
34 }, 34 },
35 useOneapm: false, 35 useOneapm: false,
36 useCache: false, 36 useCache: false,
@@ -67,14 +67,7 @@ @@ -67,14 +67,7 @@
67 <span class="iconfont right clear-input">&#xe608;</span> 67 <span class="iconfont right clear-input">&#xe608;</span>
68 </form> 68 </form>
69 </div> 69 </div>
70 - <ul class="search-hint clearfix">  
71 - <li class="cur">  
72 - <a href="#">  
73 - <span>大衣</span>  
74 - <span class="right">约300个商品</span>  
75 - </a>  
76 - </li>  
77 - </ul> 70 + <ul class="search-hint clearfix"></ul>
78 <div class="hot-search hide"></div> 71 <div class="hot-search hide"></div>
79 </div> 72 </div>
80 </div> 73 </div>
@@ -11,9 +11,11 @@ var $miniBag = $('.mini-bag-box'), @@ -11,9 +11,11 @@ var $miniBag = $('.mini-bag-box'),
11 var $searchWrap = $('.search-wrapper'), 11 var $searchWrap = $('.search-wrapper'),
12 $searchForm = $('#search-form'), 12 $searchForm = $('#search-form'),
13 $searchKey = $('#search-key'), 13 $searchKey = $('#search-key'),
  14 + $searchHint = $('.search-hint'),
14 $clearInput = $('.clear-input'); 15 $clearInput = $('.clear-input');
15 16
16 var goodsTpl = require('../../tpl/common/bag-goods.hbs'); 17 var goodsTpl = require('../../tpl/common/bag-goods.hbs');
  18 +var searchHintTpl = require('../../tpl/header/search-hint.hbs');
17 var delayer; 19 var delayer;
18 20
19 require('yoho-jquery-nanoscroller'); 21 require('yoho-jquery-nanoscroller');
@@ -178,6 +180,62 @@ $searchKey.keyup(function(e) { @@ -178,6 +180,62 @@ $searchKey.keyup(function(e) {
178 } 180 }
179 }); 181 });
180 182
  183 +// 关键词搜索联想
  184 +function searchSuggest(key) {
  185 + $.get('//www.yohoblk.com/product/query/suggest', {
  186 + keyword: key
  187 + }, function(data) {
  188 + if (data.length >=1) {
  189 + $searchHint.html(searchHintTpl({list: data})).show();
  190 + } else {
  191 + $searchHint.hide();
  192 + }
  193 + });
  194 +}
  195 +
  196 +$searchKey.keyup(function(e) {
  197 + var val = $.trim($(this).val()),
  198 + $child = $searchHint.find('li'),
  199 + $act = $searchHint.find('.action'),
  200 + $focus;
  201 +
  202 + if (e.which > 36 && e.which < 41) {
  203 +
  204 + if (e.which === 38) {
  205 + $focus = $act.prev();
  206 + if (!$act.length || !$focus.length) {
  207 + $focus = $child.eq($child.length - 1);
  208 + }
  209 + } else if (e.which === 40) {
  210 + $focus = $act.next();
  211 + if (!$act.length || !$focus.length) {
  212 + $focus = $child.eq(0);
  213 + }
  214 + } else {
  215 + return;
  216 + }
  217 +
  218 + $child.removeClass('action');
  219 + $focus.addClass('action');
  220 + $(this).val($focus.find('.search-value').text());
  221 + } else if (e.which === 13) {
  222 + if (val) {
  223 + $searchForm.submit();
  224 + }
  225 + } else {
  226 + if ($searchHint && $searchHint.length) {
  227 + val = val.replace(new RegExp('\'', 'gm'), ''); // 去掉特殊字符
  228 + searchSuggest(val);
  229 + }
  230 + }
  231 +
  232 + if (val) {
  233 + $clearInput.show();
  234 + } else {
  235 + $clearInput.hide();
  236 + }
  237 +});
  238 +
181 // ie8输入框提示特殊处理 239 // ie8输入框提示特殊处理
182 // 应产品要求所有浏览器获得焦点提示文字隐藏 240 // 应产品要求所有浏览器获得焦点提示文字隐藏
183 // if (!!window.ActiveXObject && !!document.documentMode) { 241 // if (!!window.ActiveXObject && !!document.documentMode) {
@@ -193,6 +251,10 @@ $searchKey.focus(function() { @@ -193,6 +251,10 @@ $searchKey.focus(function() {
193 if ($.trim(key) === '') { 251 if ($.trim(key) === '') {
194 $searchKey.val('search').css('color', '#aaa'); 252 $searchKey.val('search').css('color', '#aaa');
195 } 253 }
  254 +
  255 + setTimeout(function() {
  256 + $searchHint.hide();
  257 + }, 200);
196 }).val('search').css('color', '#aaa'); 258 }).val('search').css('color', '#aaa');
197 259
198 // } 260 // }
@@ -394,12 +394,25 @@ @@ -394,12 +394,25 @@
394 padding: 0 20px; 394 padding: 0 20px;
395 } 395 }
396 396
  397 + li:first-child {
  398 + margin-top: 10px;
  399 + }
  400 +
397 .cur { 401 .cur {
398 background: #1b1b1b; 402 background: #1b1b1b;
399 } 403 }
400 404
  405 + .action {
  406 + background: #232323;
  407 + }
  408 +
  409 + .cur:hover {
  410 + background: #232323;
  411 + }
  412 +
401 a { 413 a {
402 color: #fff; 414 color: #fff;
  415 + display: block;
403 } 416 }
404 } 417 }
405 418
  1 +{{#each list}}
  2 + <li class="cur">
  3 + <a href="{{href}}" title="{{keyword}}">
  4 + <span class="search-value">{{keyword}}</span>
  5 + <span class="right">{{count}}个商品</span>
  6 + </a>
  7 + </li>
  8 +{{/each}}