Authored by 王水玲

搜索相关代码优化

... ... @@ -29,7 +29,7 @@ const _getResources = (page) => {
if (result && result.code === 200) {
return resourcesProcess(result.data);
} else {
logger.error('星潮教室页面资源位返回 code 不是 200');
logger.error('star index resources return code is not 200');
return [];
}
});
... ... @@ -37,7 +37,7 @@ const _getResources = (page) => {
/**
* 星潮教室首页数据处理
* @param {[array]} list
* @param {[array]} dataList
* @return {[array]}
*/
const _processIndexData = (dataList) => {
... ... @@ -158,7 +158,7 @@ const getIndexData = () => {
if (result && result.code === 200) {
return _processIndexData(result);
} else {
logger.error('星潮教室首页数据返回 code 不是 200');
logger.error('get star index data return is not 200');
return {};
}
});
... ... @@ -183,7 +183,7 @@ const getDetailData = (params, uid) => {
return _processGuangData(result.data.list, true);
}
} else {
logger.error('明星专题文章数据返回 code 不是 200');
logger.error('get star feature article data return code is not 200');
return [];
}
});
... ... @@ -227,7 +227,7 @@ const getCollocationListData = (params, uid) => {
if (result && result.code === 200) {
return _processGuangData(result.data.list.artList);
} else {
logger.error('获取星搭配文章列表返回 code 不是 200');
logger.error('get Collocation article list return code is not 200');
return [];
}
});
... ...
... ... @@ -198,7 +198,6 @@ const _formShopData = (data, shopId, isApp) => {
// 店铺下面的所有分类
if (data.shopCategory) {
console.log(data.shopCategory);
let total = data.shopCategory.length;
let shopCategory = {
list: []
... ... @@ -278,8 +277,6 @@ const _baseShop = (req, res, data) => {
Promise.all([listModel.getShopDecorator(data.shopsId), listModel.getShopBrands(data.shopsId)]).then((result) => {
result[0] = result[0] || {};
console.log(result[0], result[1]);
_.forEach(result[0], (item) => {
if (item.resourceName === 'shopTopBanner_base') {
let banner = JSON.parse(item.resourceData);
... ... @@ -383,8 +380,8 @@ const category = (req, res) => {
// 品牌|店铺落地页
const brand = (req, res, next) => {
let params = Object.assign({}, req.query);
let domain = 'colormad';
let uid = req.user.uid || 20000032;
let domain = req.query.domain || 'colormad';
let uid = req.user.uid;
let brandId = 0;
let brandLogo = {};
let title = '';
... ... @@ -486,13 +483,15 @@ const shopIntro = (req, res, next) => {
*/
const favoriteBrand = (req, res, next) => {
let id = req.body.id;
let uid = req.user.uid || 20000032;
let uid = req.user.uid;
let opt = req.body.opt || 'ok';
let type = req.body.type || 'product';
let appVersion = req.body.appVersion || false;
let url = helpers.urlFormat('/signin.html') + '?refer=' + decodeURI(req.cookies.refer);
if (appVersion === 'true') {
uid = req.cookies.appUid;
url = `${url}&openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":${decodeURI(req.cookies.refer)},"param":{}},"requesturl":{"param":{},"url":""},"priority":"Y"}}`; // eslint-disable-line
}
if (_.isNumber(id)) {
... ... @@ -503,12 +502,12 @@ const favoriteBrand = (req, res, next) => {
});
return false;
} else if (!uid) {
res.json({
code: 400,
message: '未登录',
data: helpers.urlFormat('/signin.html', {
refer: decodeURI(req.cookies.refer)
})
data: url
});
return false;
} else if (opt !== 'ok') { // 取消收藏
... ...
... ... @@ -11,19 +11,23 @@ const searchModel = require(`${mRoot}/search`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
// 搜索落地页
/**
* 搜索落地页
*/
const list = (req, res, next) => {
let params = Object.assign({}, req.query);
let params = Object.assign({
isSearch: true, // 搜索列表将最新改成默认的标识
cartUrl: helpers.urlFormat('/cart/index/index')
}, req.query);
let title = '';
let query = req.query.query;
let isQueryFirstClass = false; // 标识用户搜的是不是一级品类
let isQuerySecondClass = false; // 标识用户搜的是不是二级品类
let domain = null;
params = _.assign({
isSearch: true,
cartUrl: helpers.urlFormat('/cart/index/index')
}, params);
if (params.shop_id) {
params.shopId = params.shop_id;
}
/* 判断是不是品牌, 是品牌跳到品牌列表页(显示搜索框),判断是不是品类, 是品类加导航标题(不显示搜索框) */
Promise.all([searchModel.getAllBrandNames(), searchModel.getClassNames()]).then((result) => {
... ... @@ -85,28 +89,33 @@ const list = (req, res, next) => {
} else if (isQuerySecondClass) { // 搜索是二级品类
title = query;
} else { // 搜索其它内容
if (query) {
if (query || params.form) {
params.search = {
default: query === '' ? false : query,
url: ''
url: helpers.urlFormat('', null, 'search')
};
}
title = '搜索';
}
title = params.title ? params.title : title;
res.render('search/list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: title
}),
title: title,
goodList: params,
pageFooter: true
});
}).catch(next);
};
// ajax 商品数据请求
/**
* ajax 商品数据请求
*/
const search = (req, res, next) => {
let params = Object.assign({}, req.query);
... ... @@ -120,9 +129,6 @@ const search = (req, res, next) => {
/**
* 筛选
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let filter = (req, res, next) => {
let params = Object.assign({}, req.query);
... ...
... ... @@ -10,59 +10,6 @@ const _ = require('lodash');
const helpers = global.yoho.helpers;
const api = global.yoho.API;
/**
* 品牌名称处理
* @param {[object]} list
* @return {[object]}
*/
const _processBrandNames = (list) => {
const formatData = [];
list = list || [];
list = camelCase(list);
_.forEach(list, (item) => {
_.forEach(item, (obj) => {
formatData.push({
brandDomain: obj.brandDomain,
brandName: obj.brandName
});
});
});
return formatData;
};
/**
* 品牌名称处理
* @param {[object]} list
* @return {[object]}
*/
const _processClassNames = (list) => {
const formatData = {
first: {},
second: {}
};
list = list || [];
list = camelCase(list);
_.forEach(list, (item) => {
_.forEach(item, (obj) => {
formatData.first[obj.categoryId] = obj.categoryName;
if (obj.sub) {
_.forEach(obj.sub, (sub) => {
formatData.second[sub.categoryId] = sub.categoryName;
});
}
});
});
return formatData;
};
/* 多品牌店铺列表数据信息处理*/
const _processBrandShops = (list) => {
const formatDat = [];
... ... @@ -147,42 +94,6 @@ const getBrandBanner = (brandId) => {
};
/**
* 获取所有的品牌名称
**/
const getAllBrandNames = () => {
return api.get('', {
method: 'app.brand.brandlist'
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return _processBrandNames(result.data.brands);
} else {
logger.error('get brand all name data api return code is not 200');
return {};
}
});
};
/**
* 获取所有的品类名称
**/
const getClassNames = () => {
return api.get('', {
method: 'app.sort.get'
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return _processClassNames(result.data);
} else {
logger.error('get category name api return code is not 200');
return {};
}
});
};
/**
* 根据品牌域名获取品牌LOGO
* @param {string} domain 品牌域名
* @return array | false
... ... @@ -375,8 +286,6 @@ const getShopBrands = (shopId) => {
module.exports = {
getAllBrandNames,
getClassNames,
getBrandLogoByDomain,
getBrandIntro,
getBrandShops,
... ...
... ... @@ -250,7 +250,7 @@ const _getBreakingSort = (yhChannel) => {
if (result && result.code === 200) {
return _processBreakingSort(result.data);
} else {
logger.error('get BreakingSort classify return code is not 200');
logger.error('get sale BreakingSort classify return code is not 200');
return {};
}
});
... ... @@ -276,7 +276,7 @@ const getSearchData = (params, uid) => {
if (result && result.code === 200) {
return result.data.vip_info ? camelCase(result.data.vip_info) : {};
} else {
logger.error('获取用户信息返回 code 不是 200');
logger.error('get user info return code is not 200');
return {};
}
})
... ... @@ -296,7 +296,7 @@ const getFilterData = (params) => {
hideSort: params.saleType === '1'
});
} else {
logger.error('SALE 商品搜索返回 code 不是 200');
logger.error('get filter data return code is not 200');
return [];
}
});
... ... @@ -350,7 +350,7 @@ const getDiscountData = (yhChannel) => {
list: _processDiscount(result.data)
};
} else {
logger.error('折扣专场专题列表返回 code 不是 200');
logger.error('get sale discount data return code is not 200');
return {};
}
});
... ... @@ -383,7 +383,7 @@ const getDiscountDetailData = (id, yhChannel) => {
}
};
} else {
logger.error('折扣专场专题详情返回 code 不是 200');
logger.error('get sale discount detail return code is not 200');
return {};
}
});
... ...
... ... @@ -5,21 +5,64 @@
*/
'use strict';
const utils = '../../../utils';
const logger = global.yoho.logger;
const productProcess = require(`${utils}/product-process`);
const searchProcess = require(`${utils}/search-process`);
const _ = require('lodash');
const logger = global.yoho.logger;
const api = global.yoho.API;
const camelCase = global.yoho.camelCase;
/**
* 排序转换
* 品牌名称处理
* @param {[object]} list
* @return {[object]}
*/
const typeCont = {
price: ['s_p_desc', 's_p_asc'],
discount: ['p_d_desc', 'p_d_asc'],
sale: ['s_n_desc', 's_n_asc'],
newest: ['s_t_desc', 's_t_asc'],
stock: ['s_s_desc', 's_s_asc'],
all: ['d_s_desc', 'd_s_asc'],
category: ['s_t_desc', 's_t_asc']
const _processBrandNames = (list) => {
const formatData = [];
list = list || [];
list = camelCase(list);
_.forEach(list, (item) => {
_.forEach(item, (obj) => {
formatData.push({
brandDomain: obj.brandDomain,
brandName: obj.brandName
});
});
});
return formatData;
};
/**
* 品牌名称处理
* @param {[object]} list
* @return {[object]}
*/
const _processClassNames = (list) => {
const formatData = {
first: {},
second: {}
};
list = list || [];
list = camelCase(list);
_.forEach(list, (item) => {
_.forEach(item, (obj) => {
formatData.first[obj.categoryId] = obj.categoryName;
if (obj.sub) {
_.forEach(obj.sub, (sub) => {
formatData.second[sub.categoryId] = sub.categoryName;
});
}
});
});
return formatData;
};
/**
... ... @@ -37,9 +80,12 @@ const _searchGoods = (params) => {
}
}
// params.yh_channel = channelType[params.yh_channel];
if (params.channel) {
params.yh_channel = searchProcess.getChannelType(params.channel);
delete params.channel;
}
params = Object.assign({
params = _.assign({
limit: '60',
status: 1,
sales: 'Y',
... ... @@ -47,11 +93,11 @@ const _searchGoods = (params) => {
attribute_not: 2
}, params);
if (typeCont[params.type]) {
params.order = typeCont[params.type][params.order];
if (params.order) {
params.order = searchProcess.getTypeCont(params.type || '', params.order);
}
return api.get('', Object.assign({
return api.get('', _.assign({
method: method
}, params), {
cache: true
... ... @@ -88,7 +134,45 @@ const getFilterData = (params) => {
});
};
/**
* 获取所有的品类名称
**/
const getClassNames = () => {
return api.get('', {
method: 'app.sort.get'
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return _processClassNames(result.data);
} else {
logger.error('get category name api return code is not 200');
return {};
}
});
};
/**
* 获取所有的品牌名称
**/
const getAllBrandNames = () => {
return api.get('', {
method: 'app.brand.brandlist'
}, {
cache: true
}).then((result) => {
if (result && result.code === 200) {
return _processBrandNames(result.data.brands);
} else {
logger.error('get brand all name data api return code is not 200');
return {};
}
});
};
module.exports = {
getSearchData,
getFilterData
getFilterData,
getAllBrandNames,
getClassNames
};
... ...
@import "suggest";
... ...
... ... @@ -38,7 +38,30 @@ const getChannelType = (channel) => {
}
};
// 排序转换
const getTypeCont = (type, order) => {
switch (type) {
case 'price':
return order === '0' ? 's_p_desc' : 's_p_asc';
case 'discount':
return order === '0' ? 'p_d_desc' : 'p_d_asc';
case 'sale':
return order === '0' ? 's_n_desc' : 's_n_asc';
case 'newest':
return order === '0' ? 's_t_desc' : 's_t_asc';
case 'stock':
return order === '0' ? 's_s_desc' : 's_s_asc';
case 'all':
return order === '0' ? 'd_s_desc' : 'd_s_asc';
case 'category':
return order === '0' ? 's_t_desc' : 's_t_asc';
default:
return order === '0' ? 's_t_desc' : 's_t_asc';
}
};
module.exports = {
getGenderByChannel,
getChannelType
getChannelType,
getTypeCont
};
... ...