Authored by htoooth

search

... ... @@ -6,7 +6,6 @@
'use strict';
// TODO: ctx
const mRoot = '../models';
const search = require(`${mRoot}/search`);
const _ = require('lodash');
... ... @@ -26,14 +25,14 @@ const index = (req, res, next) => {
let params = req.query;
let resData = {};
search.getKeyActivity(params.query).then(activityUrl => {
req.ctx(search).getKeyActivity(params.query).then(activityUrl => {
if (activityUrl) {
activityUrl = encodeURI(activityUrl);
res.redirect(activityUrl);
return;
}
return search.getSearchData(Object.assign(params, {uid: req.user.uid, prid: req.user.prid}),
return req.ctx(search).getSearchData(Object.assign(params, {uid: req.user.uid, prid: req.user.prid}),
req.yoho.channel).then(result => {
let queryKey = req.query.query;
... ... @@ -65,7 +64,7 @@ const index = (req, res, next) => {
* @return {[type]} [description]
*/
const suggest = (req, res, next) => {
search.getSuggest(req.query).then(result => {
req.ctx(search).getSuggest(req.query).then(result => {
let dest = {
code: 200,
message: 'suggest',
... ... @@ -79,7 +78,7 @@ const suggest = (req, res, next) => {
};
const suggest4Old = (req, res, next) => {
search.getSuggest(Object.assign({}, req.query, { keyword: req.query.query })).then(result => {
req.ctx(search).getSuggest(Object.assign({}, req.query, { keyword: req.query.query })).then(result => {
let dest = {
code: 200,
message: 'suggest'
... ... @@ -107,7 +106,7 @@ const suggest4Old = (req, res, next) => {
};
const searchFilterBrands = (req, res, next) => {
search.getBrands4Filter(Object.assign({}, req.query, {
req.ctx(search).getBrands4Filter(Object.assign({}, req.query, {
keyword: req.query.query || ''
})).then(result => {
let dest = {
... ... @@ -165,7 +164,7 @@ const searchLessRecommend = (req, res, next) => {
page = 1;
}
search.getSearchLessProduct(channelNum, uid, udid, page).then(result => {
req.ctx(search).getSearchLessProduct(channelNum, uid, udid, page).then(result => {
return res.send(result);
}).catch(next);
... ... @@ -183,7 +182,7 @@ const keyword = (req, res, next) => {
let resData = {};
return search.getSearchKeywordData(Object.assign(params, {query: query}),
return req.ctx(search).getSearchKeywordData(Object.assign(params, {query: query}),
req.yoho.channel).then(result => {
Object.assign(resData, result, {
pageNoFollow: true,
... ... @@ -212,7 +211,7 @@ const keyId = (req, res, next) => {
let params = req.query;
let id = req.params.id;
return search.getSearchKeywordDataById(id, Object.assign({stocknumber: -1}, params),
return req.ctx(search).getSearchKeywordDataById(id, Object.assign({stocknumber: -1}, params),
req.yoho.channel).then(result => {
if (!result) {
... ...
... ... @@ -11,21 +11,23 @@ const Promise = require('bluebird');
const co = Promise.coroutine;
const helpers = global.yoho.helpers;
const redis = global.yoho.redis;
const saleApi = require('./sale-api');
const searchApi = require('./search-api');
const SaleApiModel = require('./ctx-sale-api');
const SearchApiModel = require('./ctx-search-api');
const headerModel = require('../../../doraemon/models/header');
const productProcess = require(`${utils}/product-process`);
const searchHandler = require('./search-handler');
const _ = require('lodash');
const needParams = ['query', 'msort', 'misort'];
exports.getKeyActivity = (query) => {
return searchApi.getKeyActivityAsync(query).then(result => {
function getKeyActivity(query) {
return this.searchApi.getKeyActivityAsync(query).then(result => {
return _.get(result, 'data.urlobj.pcUrl', '');
});
};
}
const setSearchData = (result, params, channel) => {
const _setSearchData = (result, params, channel) => {
let changeQuery = Object.assign({}, params);
let finalResult = {
headerData: Object.assign(result[0].headerData, {
... ... @@ -68,7 +70,7 @@ const setSearchData = (result, params, channel) => {
_.get(finalResult, 'search.leftContent.checked', []));
Object.assign(finalResult.search,
searchHandler.handlePathNavData({total: result[2].data.total }, params, 'search', channel),
searchHandler.handlePathNavData({total: result[2].data.total}, params, 'search', channel),
{
filters: filters,
opts: searchHandler.handleOptsData(changeQuery, result[2].data.total, result[2].data.filter),
... ... @@ -103,7 +105,7 @@ const setSearchData = (result, params, channel) => {
* @param float|string $price 价格
* @return float|string 转换之后的价格
*/
const transPrice = (price) => {
const _transPrice = (price) => {
return price ? (price * 1).toFixed(2) : '0.00';
};
... ... @@ -112,7 +114,7 @@ const transPrice = (price) => {
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.getSearchData = (params, channel) => {
function getSearchData(params, channel) {
let searchParams = searchHandler.getSearchParams(params);
switch (channel) {
... ... @@ -135,31 +137,33 @@ exports.getSearchData = (params, channel) => {
// 调用接口
let apiMethod = [
headerModel.requestHeaderData(channel),
searchApi.getSortList(Object.assign({}, searchParams, {msort: '', misort: '', sort: ''})),
searchApi.getProductList(searchParams, 'fuzzySearch'),
searchApi.getShopList(params)
this.searchApi.getSortList(Object.assign({}, searchParams, {msort: '', misort: '', sort: ''})),
this.searchApi.getProductList(searchParams, 'fuzzySearch'),
this.searchApi.getShopList(params)
];
return api.all(apiMethod).then(result => {
if (_.get(result, '[2].data.isChangedQuery', '') === 'Y') {
return searchApi.getSortList(Object.assign({}, searchParams, {msort: '',
return this.searchApi.getSortList(Object.assign({}, searchParams, {
msort: '',
query: _.get(result, '[2]data.suggestion.terms_suggestion[0]', ''),
misort: '', sort: ''})).then(subRes => {
result[1] = subRes;
misort: '', sort: ''
})).then(subRes => {
result[1] = subRes;
return setSearchData(result, params, channel);
});
return _setSearchData(result, params, channel);
});
}
return setSearchData(result, params, channel);
return _setSearchData(result, params, channel);
});
};
}
/**
* 搜索提示
*/
exports.getSuggest = (params) => {
return searchApi.getSuggest(params).then(result => {
function getSuggest(params) {
return this.searchApi.getSuggest(params).then(result => {
let dest = [];
if (result.code === 200) {
... ... @@ -168,10 +172,10 @@ exports.getSuggest = (params) => {
return dest;
});
};
}
exports.getListBrandsFilter = (params, nparams) => {
return searchApi.getBrands4Filter(Object.assign({}, params, nparams)).then(result => {
function getListBrandsFilter(params, nparams) {
return this.searchApi.getBrands4Filter(Object.assign({}, params, nparams)).then(result => {
let dest = [];
if (result.code === 200) {
... ... @@ -180,9 +184,9 @@ exports.getListBrandsFilter = (params, nparams) => {
return dest;
});
};
}
exports.getBrands4Filter = (params) => {
function getBrands4Filter(params) {
let nparams = {};
... ... @@ -206,7 +210,7 @@ exports.getBrands4Filter = (params) => {
}
if (params.specialsale_id) {
return saleApi.getSaleSpecialAsync(params.specialsale_id).then(special => {
return this.saleApi.getSaleSpecialAsync(params.specialsale_id).then(special => {
if (special && special.data) {
let data = special.data;
... ... @@ -243,7 +247,7 @@ exports.getBrands4Filter = (params) => {
}
return this.getListBrandsFilter(params, nparams);
};
}
/**
... ... @@ -253,8 +257,8 @@ exports.getBrands4Filter = (params) => {
* @return array
* */
exports.getSearchLessProduct = (channelNum, uid, udid, page) => {
return co(function * () {
function getSearchLessProduct(channelNum, uid, udid, page) {
return co(function* () {
let result = {
... ... @@ -274,7 +278,7 @@ exports.getSearchLessProduct = (channelNum, uid, udid, page) => {
return result;
}
let together = yield searchApi.lessRecommend(channelNum, uid, udid, '100003', 20);
let together = yield this.searchApi.lessRecommend(channelNum, uid, udid, '100003', 20);
if (page > 5) {
page = 1;
... ... @@ -295,9 +299,9 @@ exports.getSearchLessProduct = (channelNum, uid, udid, page) => {
title: it.product_name,
img: it.default_images ? helpers.image(it.default_images, 180, 240) : '',
alt: it.product_name,
price: '¥' + transPrice(it.sales_price),
marketPrice: transPrice(it.market_price) <= transPrice(it.sales_price) ?
false : ('¥' + transPrice(it.market_price))
price: '¥' + _transPrice(it.sales_price),
marketPrice: _transPrice(it.market_price) <= _transPrice(it.sales_price) ?
false : ('¥' + _transPrice(it.market_price))
};
item.href = helpers.getUrlBySkc(it.product_skn);
... ... @@ -321,9 +325,9 @@ exports.getSearchLessProduct = (channelNum, uid, udid, page) => {
})();
};
}
const setSearchKeywordData = (result, params, channel) => {
const _setSearchKeywordData = (result, params, channel) => {
let changeQuery = Object.assign({}, params);
let finalResult = {
headerData: Object.assign(result[0].headerData, {
... ... @@ -346,7 +350,7 @@ const setSearchKeywordData = (result, params, channel) => {
// 获取商品数据和顶部筛选条件
if (result[2].code === 200) {
Object.assign(finalResult.search,
searchHandler.handlePathNavData({total: result[2].data.total }, params, 'search', channel),
searchHandler.handlePathNavData({total: result[2].data.total}, params, 'search', channel),
{
opts: searchHandler.handleOptsData(changeQuery, result[2].data.total),
totalCount: result[2].data.total,
... ... @@ -368,7 +372,7 @@ const setSearchKeywordData = (result, params, channel) => {
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.getSearchKeywordData = (params, channel) => {
function getSearchKeywordData(params, channel) {
let searchParams = searchHandler.getSearchParams(params);
switch (channel) {
... ... @@ -393,15 +397,15 @@ exports.getSearchKeywordData = (params, channel) => {
// 调用接口
let apiMethod = [
headerModel.requestHeaderData(channel),
searchApi.getSuggest({keyword: searchParams.query.substring(0, 2)}),
searchApi.getSeoProductList(searchParams, 'fuzzySearch')
this.searchApi.getSuggest({keyword: searchParams.query.substring(0, 2)}),
this.searchApi.getSeoProductList(searchParams, 'fuzzySearch')
];
return api.all(apiMethod).then(result => {
return setSearchKeywordData(result, params, channel);
return _setSearchKeywordData(result, params, channel);
});
};
}
/**
* 获取搜索建议数据
... ... @@ -410,10 +414,8 @@ exports.getSearchKeywordData = (params, channel) => {
* @channel {[string]} origin [description]
* @return {[object]} [description]
*/
exports.getSearchKeywordDataById = (id, params, channel) => {
let that = this;
return co(function * () {
function getSearchKeywordDataById(id, params, channel) {
return co(function* () {
let redisData = yield redis.all([
['get', `golobal:yoho:seo:keywords:id:${id}`]
]);
... ... @@ -426,7 +428,7 @@ exports.getSearchKeywordDataById = (id, params, channel) => {
params.query = redisData.name;
let resData = yield that.getSearchKeywordData(params, channel);
let resData = yield this.getSearchKeywordData(params, channel);
resData.queryKey = params.query;
... ... @@ -447,4 +449,23 @@ exports.getSearchKeywordDataById = (id, params, channel) => {
return resData;
})();
}
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.searchApi = new SearchApiModel(ctx);
this.saleApi = new SaleApiModel(ctx);
this.getKeyActivity = getKeyActivity.bind(this);
this.getSearchData = getSearchData.bind(this);
this.getSuggest = getSuggest.bind(this);
this.getListBrandsFilter = getListBrandsFilter.bind(this);
this.getBrands4Filter = getBrands4Filter.bind(this);
this.getSearchLessProduct = getSearchLessProduct.bind(this);
this.getSearchKeywordData = getSearchKeywordData.bind(this);
this.getSearchKeywordDataById = getSearchKeywordDataById.bind(this);
}
};
... ...