Authored by yyq

so keyword

... ... @@ -12,6 +12,7 @@ const searchModel = require(`${mRoot}/search`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const searchProcess = require(`${utils}/search-process`);
const stringCode = require(`${utils}/string-code`);
/**
* 搜索落地页
... ... @@ -274,10 +275,43 @@ let filter = (req, res, next) => {
}).catch(next);
};
// 关键词页
const keyword = (req, res, next) => {
let queryKey = stringCode.hexToUtf8(`${req.params.query}`);
let params = {
isSearch: true, // 搜索列表将最新改成默认的标识
cartUrl: helpers.urlFormat('/cart/index/index'),
query: queryKey
};
params.isApp = req.yoho.isApp;
params.physical_channel = req.yoho.channel && searchProcess.getChannelType(req.yoho.channel);
// 唤起 APP 的路径
res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.list","params":${JSON.stringify(params)}}`;
return searchModel.getSearchKeywordData(params, req.user.uid).then(result => {
res.render('search/list', {
_noLazy: true,
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: params.query
}),
title: params.query,
goodList: params,
firstPageGoods: result || [],
fuzzyWord: result.fuzzyWord,
pageFooter: true
});
}).catch(next);
};
module.exports = {
list,
filter,
search,
index,
fuzzyDatas
fuzzyDatas,
keyword
};
... ...
... ... @@ -7,6 +7,7 @@
const utils = '../../../utils';
const productProcess = require(`${utils}/product-process`);
const searchProcess = require(`${utils}/search-process`);
const stringCode = require(`${utils}/string-code`);
const _ = require('lodash');
const logger = global.yoho.logger;
const api = global.yoho.API;
... ... @@ -476,6 +477,46 @@ const getBrandDomain = (query) => {
}
};
const getSearchKeywordData = (params, uid) => {
// 获取第一页数据做服务端渲染
let initialData = _.assign({
type: 'default',
order: '0',
page: 1,
limit: 12
}, params);
if (uid) {
initialData.uid = uid;
}
return Promise.all([
getFuzzyDatas(params.query),
getSearchData(initialData)
]).then(result => {
let fuzzy = [];
_.forEach(result[0], value => {
if (value.keyword === params.query) {
return;
}
fuzzy.push({
name: value.keyword,
link: helpers.urlFormat(`/so/${stringCode.utf8ToHex(value.keyword)}.html`),
});
});
if (!_.isEmpty(fuzzy)) {
result[1].fuzzyWord = fuzzy;
}
return result[1];
});
};
module.exports = {
getSearchData,
getFilterData,
... ... @@ -485,5 +526,6 @@ module.exports = {
getSearchIndex,
getFuzzyDatas,
searchKeyActivity,
getBrandDomain
getBrandDomain,
getSearchKeywordData
};
... ...
... ... @@ -133,6 +133,9 @@ router.get('/seckill/get-product-list', seckill.getProductList); // 秒杀列表
router.get('/search/index', search.index);
// 搜索落地页
router.get('/search/keyword/:query', rewrite.sortParams, search.keyword);
// 搜索落地页
router.get('/search/list', rewrite.sortParams, search.list);
// filter
... ...
{{#if @root.fuzzyWord}}
<div class="word-content">
{{!--相关商品少于指定数据,显示关键词--}}
<div class="list-too-little">
{{#each @root.fuzzyWord}}
<span class="word"><a href="{{link}}">{{name}}</a></span>
{{/each}}
</div>
</div>
{{/if}}
... ...
... ... @@ -125,6 +125,9 @@
{{!--搜索推荐词模板--}}
{{> search/search-word-content}}
{{!--搜索相关词模板--}}
{{> search/fuzzy-word}}
{{#@root.firstPageGoods.list}}
{{> product/recommend-goods}}
{{/@root.firstPageGoods.list}}
... ...
... ... @@ -84,6 +84,13 @@ module.exports = () => {
}
break;
}
} else {
let soReg = /\/so\/(.*).html/;
if (soReg.test(req.path)) {
soReg.exec(req.url);
req.url = `/product/search/keyword/${RegExp.$1}`;
}
}
next();
};
... ...
'use strict';
const utf8ToHex = (string) => {
var buf = new Buffer(string, 'utf8');
return buf.toString('hex');
};
const hexToUtf8 = (string) => {
let buf = new Buffer(string, 'hex');
return buf.toString('utf8');
};
module.exports = {
utf8ToHex,
hexToUtf8
};
... ...