...
|
...
|
@@ -13,7 +13,6 @@ const config = global.yoho.config; |
|
|
const helpers = global.yoho.helpers;
|
|
|
const cache = global.yoho.cache;
|
|
|
const logger = global.yoho.logger;
|
|
|
const images = require('../../../utils/images.js');
|
|
|
|
|
|
const getSortByConditionAsync = (condition) => {
|
|
|
return api.get('', Object.assign({
|
...
|
...
|
@@ -288,191 +287,49 @@ const getBrandCouponAsync = (brandId, uid) => { |
|
|
*/
|
|
|
const getBrandShop = (query) => {
|
|
|
let finalParams = {
|
|
|
// method: 'web.regular.groupsort'
|
|
|
method: 'web.search.shopInfo'
|
|
|
method: 'web.search.shopNewInfo'
|
|
|
};
|
|
|
|
|
|
Object.assign(finalParams, {keyword: query});
|
|
|
|
|
|
let cKey = getSearchCacheKey(finalParams);
|
|
|
|
|
|
return cache.get(cKey)
|
|
|
.catch(err => logger.debug(`product query save cache data fail:${err.toString()}`))
|
|
|
.then(cdata => {
|
|
|
let retObj;
|
|
|
|
|
|
try {
|
|
|
if (cdata) {
|
|
|
retObj = JSON.parse(cdata);
|
|
|
}
|
|
|
} catch (e) {
|
|
|
logger.debug('getBrandShop cache data parse fail.');
|
|
|
}
|
|
|
|
|
|
if (retObj) {
|
|
|
return retObj;
|
|
|
} else {
|
|
|
return api.get('', finalParams).then(ret => {
|
|
|
if (ret && ret.code === 200) {
|
|
|
|
|
|
saveCache(cKey, ret.data, CACHE_TIME_S);
|
|
|
return ret.data;
|
|
|
}
|
|
|
return null;
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 根据品牌ID获取品牌下的店铺
|
|
|
* @return
|
|
|
*/
|
|
|
const getShopsByBrandId = bid => {
|
|
|
let finalParams = {
|
|
|
method: 'app.shop.queryShopsByBrandId',
|
|
|
brand_id: bid
|
|
|
};
|
|
|
|
|
|
let cKey = getSearchCacheKey(finalParams);
|
|
|
|
|
|
return cache.get(cKey)
|
|
|
.catch(err => logger.debug(`product query save cache data fail:${err.toString()}`))
|
|
|
.then(cdata => {
|
|
|
let cdataObj;
|
|
|
|
|
|
if (cdata) {
|
|
|
try {
|
|
|
cdataObj = JSON.parse(cdata);
|
|
|
} catch (e) {
|
|
|
logger.debug('getShopsByBrandId cache data parse fail.');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (cdataObj) {
|
|
|
return cdataObj;
|
|
|
} else {
|
|
|
return api.get('', finalParams).then(ret => {
|
|
|
if (ret && ret.code === 200) {
|
|
|
|
|
|
saveCache(cKey, ret.data, CACHE_TIME_S);
|
|
|
return ret.data;
|
|
|
}
|
|
|
return null;
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
|
|
|
const shopFormat = shopDatas => {
|
|
|
let shopEntry = [];
|
|
|
|
|
|
_.forEach(shopDatas, val => {
|
|
|
let sorts = [];
|
|
|
let shopId = _.get(val, 'shop.shop_id', '');
|
|
|
let url = helpers.urlFormat('', {shopId: shopId}, val.shop.brand_domain);
|
|
|
|
|
|
if (val.shopSort) {
|
|
|
for (let ss of val.shopSort) {
|
|
|
if (!ss.sub) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
let isFull = false;
|
|
|
|
|
|
for (let misort of ss.sub) {
|
|
|
if (sorts.length >= 10) {
|
|
|
isFull = true;
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
sorts.push({
|
|
|
href: `${url}&msort=${ss.sort_id}&misort=${misort.sort_id}`,
|
|
|
name: misort.sort_name
|
|
|
});
|
|
|
}
|
|
|
if (isFull) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
shopEntry.push({
|
|
|
home: url,
|
|
|
logo: images.getImageUrl(val.shop.brand_ico, 80, 50, 1, 'brandLogo'),
|
|
|
shopName: val.shop.brand_name,
|
|
|
sort: sorts
|
|
|
});
|
|
|
});
|
|
|
|
|
|
return shopEntry;
|
|
|
return api.get('', Object.assign(finalParams, {keyword: query}), config.apiCache);
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 根据搜索得到店铺/品牌 列表
|
|
|
* 根据搜索得到店铺/品牌
|
|
|
*/
|
|
|
const getShopList = params => {
|
|
|
|
|
|
if (!params || !params.query) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
let brandShop;
|
|
|
let brandShops;
|
|
|
|
|
|
return getBrandShop(params.query).then(shop => { // 获取品牌,并根据品牌获取品牌下的所有店铺
|
|
|
|
|
|
if (!shop || !shop.id) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
brandShop = shop;
|
|
|
return getShopsByBrandId(shop.id).then(shops => {
|
|
|
if (shops && shops.length) {
|
|
|
return shops;
|
|
|
} else {
|
|
|
return shop;
|
|
|
}
|
|
|
});
|
|
|
}).then(shops => { // 获取品牌/品牌店下所有的分类
|
|
|
|
|
|
if (_.isArray(shops)) {
|
|
|
brandShops = shops;
|
|
|
|
|
|
let promises = [];
|
|
|
|
|
|
for (let s of shops) {
|
|
|
promises.push(getSortList({
|
|
|
brand: s.brand_id || brandShop.id
|
|
|
}));
|
|
|
}
|
|
|
return getBrandShop(params.query).then(shops => {
|
|
|
let shopEntry = {},
|
|
|
sort = _.get(shops, 'data.sortInfo.sort', {}),
|
|
|
shopInfo = _.get(shops, 'data.shopBrandInfo.shopInfo', {}) ||
|
|
|
_.get(shops, 'data.shopBrandInfo.brandInfo', {});
|
|
|
|
|
|
return Promise.all(promises);
|
|
|
} else if (shops && shops.id) {
|
|
|
return getSortList({
|
|
|
brand: shops.id
|
|
|
});
|
|
|
if (_.isEmpty(shopInfo)) {
|
|
|
return shopEntry;
|
|
|
}
|
|
|
|
|
|
}).then(sorts => { // 数组则为品牌店列表,否则为品牌
|
|
|
let shopData = [];
|
|
|
|
|
|
if (_.isArray(sorts)) {
|
|
|
shopEntry = {
|
|
|
home: helpers.urlFormat('', '', shopInfo.shop_domain || shopInfo.brand_domain),
|
|
|
logo: shopInfo.shop_logo || shopInfo.brand_ico,
|
|
|
shopName: shopInfo.shop_name || shopInfo.brand_name,
|
|
|
sort: []
|
|
|
};
|
|
|
|
|
|
_.forEach(sorts, (val, index) => {
|
|
|
shopData.push({
|
|
|
shop: brandShops[index],
|
|
|
shopSort: val && val.data && !_.isFunction(val.data.sort) && val.data.sort
|
|
|
// 店铺/品牌的小分类
|
|
|
_.forEach(sort, (sortInfo) => {
|
|
|
_.forEach(_.get(sortInfo, 'sub', []), (subSort) => {
|
|
|
shopEntry.sort.push({
|
|
|
href: helpers.urlFormat('', {sort: subSort.sort_id}, 'list'),
|
|
|
name: subSort.sort_name
|
|
|
});
|
|
|
});
|
|
|
} else if (sorts && sorts.code === 200 && sorts.data) {
|
|
|
shopData.push({
|
|
|
shop: brandShop,
|
|
|
shopSort: !_.isFunction(sorts.data.sort) && sorts.data.sort
|
|
|
|
|
|
});
|
|
|
}
|
|
|
|
|
|
return shopFormat(shopData);
|
|
|
return shopEntry;
|
|
|
});
|
|
|
};
|
|
|
|
...
|
...
|
|