Authored by 陈峰

品牌搜索页pagecache

... ... @@ -70,7 +70,6 @@ let search = (req, res, next) => {
channel: req.query.channel || '1'
};
indexModel.branchSearch(param).then(result => {
res.render('brand/search', Object.assign(responseData, result));
}).catch(next);
... ... @@ -114,10 +113,26 @@ let delBrandHistory = (req, res, next) => {
res.json(result);
}).catch(next);
};
/**
* [品牌搜索异步数据]
*/
let searchAsync = (req, res, next) => {
if (!req.xhr) {
return next();
}
let uid = req.user.uid;
if (!uid) {
return res.json({code: 200, data: {}})
}
return indexModel.branchSearchHistoryAsync(uid).then((result) => {
return res.json({code: 200, data: { history: result }})
})
}
module.exports = {
index,
search,
delBrandHistory,
addBrandSearch
addBrandSearch,
searchAsync
};
... ...
... ... @@ -323,8 +323,111 @@ const branchSearchHot = () => {
});
};
// /**
// * 获取搜索的品牌列表
// *
// * @param int $channel 1表示男生频道, 2表示女生频道, 3表示潮童频道, 4表示创意生活频道
// * @return array
// */
// const branchSearch = (params) => {
// return api.all([
// getBrandForSearch(params.channel),
// branchSearchHistory(params),
// branchSearchHot(),
// ]).then(result => {
// let resu = {
// brandPage: true,
// showDownloadApp: true,
// pageFooter: true,
// hot: [],
// history: [],
// brandList: []
// };
// if (result && result[0] && result[0].data) {
// let brandList = result[0].data.brands;
// let obj = {};
// for (let key in brandList) {
// let build = [];
// brandList[key].forEach(row => {
// build.push({
// name: row.brand_name,
// isHot: row.is_hot === 'Y' ? true : false,
// isNew: row.is_show_new === 'Y' ? true : false,
// url: helpers.urlFormat('', null, row.brand_domain),
// brandId: row.id,
// brandDomain: row.brand_domain,
// searchName: row.brand_name_en + row.brand_name_cn
// });
// obj[key] = build;
// });
// }
// resu.brandList = JSON.stringify(obj);
// }
// if (result && result[2] && result[2].data) {
// let hotList = result[2].data;
// for (let key in hotList) {
// let obj = {};
// obj = Object.assign(obj, {name: hotList[key].brandName});
// resu.hot.push(obj);
// }
// }
// if (result && result[1] && result[1].data) {
// let hisList = result[1].data;
// let obj = [];
// let build = {
// list: []
// };
// for (let key in hisList) {
// obj.push({
// name: hisList[key].searchTerms,
// searchTime: hisList[key].searchTime
// });
// }
// build.list = obj;
// resu.history = build;
// }
// return resu;
// });
// };
/**
* 获取搜索的品牌列表
* 获取搜索的品牌列表 pagecache重构
*
* @param int $channel 1表示男生频道, 2表示女生频道, 3表示潮童频道, 4表示创意生活频道
* @return array
... ... @@ -333,7 +436,7 @@ const branchSearch = (params) => {
return api.all([
getBrandForSearch(params.channel),
branchSearchHistory(params),
// branchSearchHistory(params),
branchSearchHot(),
]).then(result => {
... ... @@ -379,9 +482,9 @@ const branchSearch = (params) => {
}
if (result && result[2] && result[2].data) {
if (result && result[1] && result[1].data) {
let hotList = result[2].data;
let hotList = result[1].data;
for (let key in hotList) {
... ... @@ -395,32 +498,32 @@ const branchSearch = (params) => {
}
if (result && result[1] && result[1].data) {
// if (result && result[1] && result[1].data) {
let hisList = result[1].data;
// let hisList = result[1].data;
let obj = [];
// let obj = [];
let build = {
// let build = {
list: []
// list: []
};
// };
for (let key in hisList) {
// for (let key in hisList) {
obj.push({
name: hisList[key].searchTerms,
searchTime: hisList[key].searchTime
});
// obj.push({
// name: hisList[key].searchTerms,
// searchTime: hisList[key].searchTime
// });
}
// }
build.list = obj;
// build.list = obj;
resu.history = build;
// resu.history = build;
}
// }
return resu;
... ... @@ -439,10 +542,40 @@ const delBrandSearchHistory = (param) => {
});
};
/**
* [异步获取用户最近搜索词]
*/
const branchSearchHistoryAsync = (uid) => {
return branchSearchHistory({uid}).then((result) => {
let hisList = result.data;
let obj = [];
let build = {
list: []
};
for (let key in hisList) {
obj.push({
name: hisList[key].searchTerms,
searchTime: hisList[key].searchTime
});
}
build.list = obj;
return build;
});
}
module.exports = {
getBrandByChannel,
branchSearch,
delBrandSearchHistory,
addSearchHistory
addSearchHistory,
branchSearchHistoryAsync
};
... ...
... ... @@ -23,6 +23,7 @@ router.get('/channel/bottomBanner', channel.bottomBanner);
router.get('/brands', brandController.index); // 品牌首页
router.get('/brands/search', brandController.search); // 品牌搜索页
router.post('/brands/searchAsync', brandController.searchAsync); // 品牌搜索异步数据
router.post('/brands/addBrandSearch', brandController.addBrandSearch); // 添加品牌搜索记录
router.get('/brands/delBrandHistory', brandController.delBrandHistory); // 删除品牌搜索记录
... ...
... ... @@ -15,21 +15,15 @@
<div class="search-brand-page">
<div class="search-items">
{{#if history}}
<div class="net-history hide">
<div class="history-search net-search hide">
<ul class="history clearfix net">
{{# history}}
{{# list}}
<li>
<a href="{{url}}" brandName="{{name}}" collectTime="{{searchTime}}">{{name}}</a>
</li>
{{/ list}}
{{/ history}}
</ul>
</div>
<div class="net-history hide">
<div class="history-search net-search hide">
<ul class="history clearfix net">
<li>
<a href="{{url}}" brandName="{{name}}" collectTime="{{searchTime}}">{{name}}</a>
</li>
</ul>
</div>
{{/if}}
</div>
<div class="history-search local-search">
<h3><span class="iconfont left-icon">&#xe64a;</span>最近搜索
<button id="clear-history" class="clear-icon iconfont">&#xe64c;</button>
... ...
... ... @@ -16,9 +16,9 @@ module.exports = {
siteUrl: '//m.yohobuy.com',
assetUrl: '//localhost:5001',
domains: {
api: 'http://api.yoho.yohoops.org/',
service: 'http://service.yoho.yohoops.org/',
liveApi: 'http://api.live.yoho.cn/'
api: 'http://api.yoho.cn/',
service: 'http://service.yoho.cn/',
liveApi: 'http://testapi.live.yohops.com:9999/'
},
subDomains: {
host: '.m.yohobuy.com',
... ...
... ... @@ -187,7 +187,7 @@ gulp.task('webpack-dev-server', () => {
new WebpackDevServer(webpack(devConfig), {
contentBase: '.',
publicPath: '//localhost:5001/',
publicPath: 'http://localhost:5001/',
hot: true,
stats: {
colors: true
... ...
var $ = require('yoho-jquery'),
dbClass = 'data-bind';
module.exports = function(callback) {
$.ajax({
type: 'POST',
url: '/brands/searchAsync',
success: function(data) {
render(data.data);
callback && callback(data);
}
});
};
function render(data) {
console.log(data)
if (data.history && data.history.list && data.history.list.length) {
var hisEl = $('.history>li').remove();
$.each(data.history.list, function(i, his) {
var itemEl = hisEl.clone();
itemEl.find('a')
.attr('href', his.url)
.attr('brandName', his.name)
.attr('collectTime', his.searchTime)
.text(his.name);
$('.history').append(itemEl);
})
$('.net-history').removeClass(dbClass);
} else {
$('.net-history').remove();
}
}
\ No newline at end of file
... ...
... ... @@ -430,145 +430,147 @@ function removeHistory() {
}
// 初始化历史搜索的内容
require('./brand-render')(function() {
$netHistory = $('.net-history')
if ($('.brand-search-page').length > 0) {
chHammer = new Hammer($clearHistory[0]);
chHammer.on('tap', function() {
if (isLocalStorageSupported()) {
localStorage.removeItem('historys-brand');
}
$history.html('');
$historySearch.hide();
$clearHistory.hide();
if ($('.brand-search-page').length > 0) {
chHammer = new Hammer($clearHistory[0]);
chHammer.on('tap', function() {
if (isLocalStorageSupported()) {
localStorage.removeItem('historys-brand');
}
$history.html('');
$historySearch.hide();
$clearHistory.hide();
if ($('.net-search').length > 0) {
removeHistory();
}
window.rePosFooter();
});
if ($('.net-search').length > 0) {
removeHistory();
}
// 搜索页面
(function() {
var html = '',
history,
historys, i,
localHistory,
brand, data,
time, netArr, dict, dictc, localArr,
combinArr, key, date, temp;
localArr = [];
combinArr = [];
netArr = [];
$netHistory.find('li').each(function() {
brand = $(this).find('a').attr('brandName');
time = $(this).find('a').attr('collectTime');
dict = {
brand: brand,
time: time
}, dictc, dict;
netArr.push(dict);
window.rePosFooter();
});
// 搜索页面
(function() {
var html = '',
history,
historys, i,
localHistory,
brand, data,
time, netArr, dict, dictc, localArr,
combinArr, key, date, temp;
localArr = [];
combinArr = [];
netArr = [];
$netHistory.find('li').each(function() {
brand = $(this).find('a').attr('brandName');
time = $(this).find('a').attr('collectTime');
dict = {
brand: brand,
time: time
}, dictc, dict;
netArr.push(dict);
});
if (localStorage) {
if (isLocalStorageSupported()) {
historys = localStorage.getItem('historys-brand');
}
if (historys && historys.length > 0) {
historys = historys.split(ranToken);
for (i = historys.length; i > 0; i--) {
history = historys[i - 1];
if (localStorage) {
if (isLocalStorageSupported()) {
historys = localStorage.getItem('historys-brand');
}
if (history === '') {
continue;
}
localHistory = history.split(ranToken2);
if (historys && historys.length > 0) {
historys = historys.split(ranToken);
for (i = historys.length; i > 0; i--) {
history = historys[i - 1];
brand = localHistory[0];
time = localHistory[1];
if (history === '') {
continue;
}
localHistory = history.split(ranToken2);
dict = {
brand: brand,
time: time
};
brand = localHistory[0];
time = localHistory[1];
localArr.push(dict);
dict = {
brand: brand,
time: time
};
localArr.push(dict);
}
}
}
}
if (localArr.length === 0 && netArr.length === 0) {
$historySearch.addClass('hide');
return;
}
if (localArr.length === 0 && netArr.length === 0) {
$historySearch.addClass('hide');
return;
}
date = {};
temp = {};
if (localArr.length > 0) {
for (dictc in localArr) {
if (localArr.hasOwnProperty(dictc)) {
temp = localArr[dictc];
if (!date[temp.brand] || date[temp.brand] < temp.time) {
date[temp.brand] = temp.time;
date = {};
temp = {};
if (localArr.length > 0) {
for (dictc in localArr) {
if (localArr.hasOwnProperty(dictc)) {
temp = localArr[dictc];
if (!date[temp.brand] || date[temp.brand] < temp.time) {
date[temp.brand] = temp.time;
}
}
}
}
}
if (netArr.length > 0) {
for (dict in netArr) {
if (netArr.hasOwnProperty(dict)) {
temp = netArr[dict];
if (!date[temp.brand] || date[temp.brand] < temp.time) {
date[temp.brand] = temp.time;
if (netArr.length > 0) {
for (dict in netArr) {
if (netArr.hasOwnProperty(dict)) {
temp = netArr[dict];
if (!date[temp.brand] || date[temp.brand] < temp.time) {
date[temp.brand] = temp.time;
}
}
}
}
}
data = {};
for (key in date) {
if (date.hasOwnProperty(key)) {
data = {
brand: key,
time: date[key]
};
combinArr.push(data);
data = {};
for (key in date) {
if (date.hasOwnProperty(key)) {
data = {
brand: key,
time: date[key]
};
combinArr.push(data);
}
}
}
combinArr.sort(sequence);
combinArr.sort(sequence);
if (combinArr.length > 0) {
for (i = combinArr.length - 1; i >= ((combinArr.length > 10) ? (combinArr.length - 10) : 0); i--) {
if (combinArr.length > 0) {
for (i = combinArr.length - 1; i >= ((combinArr.length > 10) ? (combinArr.length - 10) : 0); i--) {
brand = combinArr[i].brand;
time = combinArr[i].time;
brand = combinArr[i].brand;
time = combinArr[i].time;
html += '<li><a brandName = "' + brand +
'">' + brand + '</li>';
$history.removeClass('hide');
html += '<li><a brandName = "' + brand +
'">' + brand + '</li>';
$history.removeClass('hide');
$history.html(html);
$clearHistory.removeClass('hide');
$history.html(html);
$clearHistory.removeClass('hide');
$hotSearch.removeClass('hide');
window.rePosFooter();
$hotSearch.removeClass('hide');
window.rePosFooter();
}
} else {
$history.addClass('hide');
}
} else {
$history.addClass('hide');
}
}());
}());
}
})
}
... ...
.data-bind {
display: none;
}
\ No newline at end of file
... ...