Authored by biao

update for module format

... ... @@ -20,7 +20,8 @@ const searchApi = global.yoho.SearchAPI;
const headerModel = require('../../../doraemon/models/header');
const getShelveTime = duration => {
// 获取上线时间
const _getShelveTime = duration => {
let today = new Date(),
todayMil = today.getTime().toString().substr(0, 10),
startDayMil = (today.setMonth(today.getMonth() - duration)).toString().substr(0, 10);
... ... @@ -29,7 +30,7 @@ const getShelveTime = duration => {
};
// 获取可用的标题
const getText = data => {
const _getText = data => {
let text = data.split(' ')[0];
const regResult = /\w+/.exec(text);
... ... @@ -49,27 +50,16 @@ const channelMap = dataMap.channel;
const sortMap = dataMap.sort;
// 构建url
const httpBuildQuery = data => {
const _httpBuildQuery = data => {
return searchApi.get('/search.json', data);
};
/**
* 获取普通banner
* @param {[Object]} data 原始数据
* @return {[Object]} 转换后的数据
*/
const getBannerList = data => {
return data;
};
/**
* 获取带小图的banner
* @param {[Object]} data 原始数据
* @return {[Object]} 转换后的数据
*/
const getDebrisSlide = data => {
const _getDebrisSlide = data => {
let floorData = {
debrisSlider: {
left: [],
... ... @@ -93,7 +83,7 @@ const getDebrisSlide = data => {
* @param {Object} data 原始数据
* @return {Object} 转换后的数据
*/
const getadbannerData = data => {
const _getadbannerData = data => {
return {
adbanner: data
... ... @@ -105,7 +95,7 @@ const getadbannerData = data => {
* @param {Object} srcData 原始数据
* @return {Object} 转换后的数据
*/
const getSlideData = srcData => {
const _getSlideData = srcData => {
const slideData = {
slide: {
list: [],
... ... @@ -114,15 +104,15 @@ const getSlideData = srcData => {
};
if (srcData.big_image) {
slideData.slide.list = getBannerList(srcData.big_image);
slideData.slide.list = srcData.big_image;
}
if (srcData.list) {
slideData.slide.pagination = getBannerList(srcData.list);
slideData.slide.pagination = srcData.list;
}
if (_.isArray(srcData)) {
slideData.slide.list = getBannerList(srcData);
slideData.slide.list = srcData;
}
return slideData;
... ... @@ -134,7 +124,7 @@ const getSlideData = srcData => {
* @param {Object} srcData 原始数据
* @return {Object} 转换后的数据
*/
const getNewReportFloorData = args => {
const _getNewReportFloorData = args => {
const title = args[0].data.text;
let item = args[1].data;
let secondItem = args[2].data;
... ... @@ -168,7 +158,7 @@ const getNewReportFloorData = args => {
floorDatas.push(data);
if (forthItem.template_name === 'single_image') {
adData = getadbannerData(forthItem.data[0]);
adData = _getadbannerData(forthItem.data[0]);
floorDatas.push(adData);
}
... ... @@ -182,7 +172,7 @@ const getNewReportFloorData = args => {
* @param {String} type 需要设置的频道类型
* @return undefined
*/
const setChannelType = (obj, type) => {
const _setChannelType = (obj, type) => {
obj[type + 'Channel'] = true;
};
... ... @@ -192,7 +182,7 @@ const setChannelType = (obj, type) => {
* @param {String} type 频道类型
* @return {Object}
*/
const getPreBrandTopData = (args, type) => {
const _getPreBrandTopData = (args, type) => {
const title = args[0].data.text;
let item = args[1].data;
... ... @@ -206,7 +196,7 @@ const getPreBrandTopData = (args, type) => {
data.preferenceBrands.imgBrand = item;
setChannelType(data.preferenceBrands, type);
_setChannelType(data.preferenceBrands, type);
return data;
};
... ... @@ -217,7 +207,7 @@ const getPreBrandTopData = (args, type) => {
* @param {String} type 频道类型
* @return {Object}
*/
const getHotGoodsFloorData = (args, type) => {
const _getHotGoodsFloorData = (args, type) => {
let item = args[0];
let nextItem = args[1];
... ... @@ -262,7 +252,7 @@ const getHotGoodsFloorData = (args, type) => {
data.recommend.tplrecommend = list;
setChannelType(data.recommend, type);
_setChannelType(data.recommend, type);
return data;
... ... @@ -275,7 +265,7 @@ const getHotGoodsFloorData = (args, type) => {
* @param {String} type 频道类型
* @return {Object}
*/
const getBoysSingleHot = (args, type) => {
const _getBoysSingleHot = (args, type) => {
const len = 10;
const data = {
singlehot: {
... ... @@ -306,11 +296,11 @@ const getBoysSingleHot = (args, type) => {
}
data.singlehot.imgHot = list;
setChannelType(data.singlehot, type);
_setChannelType(data.singlehot, type);
floorDatas.push(data);
if (args[3].template_name === 'single_image') {
adData = getadbannerData(args[3].data[0]);
adData = _getadbannerData(args[3].data[0]);
floorDatas.push(adData);
}
... ... @@ -323,7 +313,7 @@ const getBoysSingleHot = (args, type) => {
* @param {String} type 频道类型
* @return {Object}
*/
const getGirlsSingleHot = args => {
const _getGirlsSingleHot = args => {
let goods = args[2].data;
let skns = '';
... ... @@ -352,11 +342,11 @@ const getGirlsSingleHot = args => {
* @param {String} type 频道类型
* @return {Object}
*/
const getSingleHotFloorData = (args, type) => {
const _getSingleHotFloorData = (args, type) => {
if (type === 'boys') {
return getBoysSingleHot(args, type);
return _getBoysSingleHot(args, type);
} else {
return getGirlsSingleHot(args, type);
return _getGirlsSingleHot(args, type);
}
};
... ... @@ -367,7 +357,7 @@ const getSingleHotFloorData = (args, type) => {
* @param {String} type 频道类型
* @return {Object}
*/
const getSingehotViaResult = (args, queryResult, type) => {
const _getSingehotViaResult = (args, queryResult, type) => {
const data = {
singlehot: {
name: args[0].data.text,
... ... @@ -400,7 +390,7 @@ const getSingehotViaResult = (args, queryResult, type) => {
});
}
data.singlehot.navs = args[1].data;
setChannelType(data.singlehot, type);
_setChannelType(data.singlehot, type);
return data;
};
... ... @@ -413,14 +403,14 @@ const getSingehotViaResult = (args, queryResult, type) => {
* @param {Type} type 人气单品楼层的类型
* @return {Object}
*/
const processFloorDataWithQueryReusult = (rawData, queryResult, title, type) => {
const _processFloorDataWithQueryReusult = (rawData, queryResult, title, type) => {
let data = {};
_.forEach(rawData, (subData, index) => {
const text = subData.data.text && getText(subData.data.text);
const text = subData.data.text && _getText(subData.data.text);
if (text === title) {
data = getSingehotViaResult(rawData.slice(index, index + 4), queryResult, type);
data = _getSingehotViaResult(rawData.slice(index, index + 4), queryResult, type);
}
});
... ... @@ -433,7 +423,7 @@ const processFloorDataWithQueryReusult = (rawData, queryResult, title, type) =>
* @param string data
* return obj
*/
const getNewGoodsFloorData = args => {
const _getNewGoodsFloorData = args => {
const data = {
newArrivls: {
name: args[0].data.text,
... ... @@ -446,59 +436,7 @@ const getNewGoodsFloorData = args => {
return data;
};
/**
* 获取最新上架商品数据
*
* @param string $channel
* @return array
*/
exports.getNewArrival = channel => {
let rel = [],
sortList = sortMap[channel],
params = {
order: 'shelve_time:desc',
status: 1,
sales: 'Y',
attribute_not: 2,
stocknumber: 3,
shelve_time: getShelveTime(20)
};
params.gender = channelMap[channel].gender;
_.forEach(sortList, (item) => {
let data = Object.assign(item, params);
rel.push(httpBuildQuery(data));
});
return Promise.all(rel).then(res => {
let data = [],
result = [];
_.forEach(sortList, (it, index) => {
if (res[index].data.product_list && res[index].data.product_list.length === sortList[index].viewNum) {
data = data.concat(res[index].data.product_list);
}
});
_.forEach(data, (item) => {
result.push(processProduct(item, {
width: 280,
height: 373
}));
});
return result;
});
};
const getCategoryFloorData = args => {
const _getCategoryFloorData = args => {
const data = {
category: {
name: args[0].data.text,
... ... @@ -524,7 +462,7 @@ const getCategoryFloorData = args => {
return data;
};
const getAccordionFloorData = args => {
const _getAccordionFloorData = args => {
let data = {
accordion: {
name: args[0].data.text,
... ... @@ -538,7 +476,11 @@ const getAccordionFloorData = args => {
return data;
};
const requestContent = type => {
const _requestContent = type => {
let data = {
client_type: 'web',
content_code: channelMap[type || 'boys'].code,
... ... @@ -558,23 +500,23 @@ const requestContent = type => {
};
const floorMap = {
slide: getSlideData,
hot: getHotGoodsFloorData,
最新速报: getNewReportFloorData,
人气单品: getSingleHotFloorData,
'GIRL KIDS': getSingleHotFloorData,
'BOY KIDS': getSingleHotFloorData,
优选品牌: getPreBrandTopData,
最新上架: getNewGoodsFloorData,
ad: getadbannerData,
category: getCategoryFloorData,
accordion: getAccordionFloorData,
debrisSlide: getDebrisSlide
slide: _getSlideData,
hot: _getHotGoodsFloorData,
最新速报: _getNewReportFloorData,
人气单品: _getSingleHotFloorData,
'GIRL KIDS': _getSingleHotFloorData,
'BOY KIDS': _getSingleHotFloorData,
优选品牌: _getPreBrandTopData,
最新上架: _getNewGoodsFloorData,
ad: _getadbannerData,
category: _getCategoryFloorData,
accordion: _getAccordionFloorData,
debrisSlide: _getDebrisSlide
};
const processFloorData = (rawData, type) => {
const _processFloorData = (rawData, type) => {
let floorList = [];
let searchPromise = [];
let singlehotFloorIndex = [];
... ... @@ -595,7 +537,7 @@ const processFloorData = (rawData, type) => {
} else if (data.template_intro === '热门品类') { // 处理热门品类
floorData = floorMap.hot.call(null, rawData.slice(index, index + hotCategoryLength), type);
} else if (data.data.text) { // 处理一般楼层
let text = getText(data.data.text);
let text = _getText(data.data.text);
let lastIndex = index + bigFloorLength < rawData.length ?
index + bigFloorLength : index + (rawData.length - index - 1);
... ... @@ -630,7 +572,7 @@ const processFloorData = (rawData, type) => {
singlehotFloorIndex.push(floorList.length);
// 记住楼层标题, 以便后面promise获取数据后插入楼层数据
singlehotFloorTitle.push(getText(data.data.text));
singlehotFloorTitle.push(_getText(data.data.text));
} else if (!_.isNil(floorData)) {
_.isArray(floorData) ?
floorList = floorList.concat(floorData) :
... ... @@ -646,21 +588,71 @@ const processFloorData = (rawData, type) => {
};
};
/**
* 获取最新上架商品数据
*
* @param string $channel
* @return array
*/
const getNewArrival = channel => {
let rel = [],
sortList = sortMap[channel],
params = {
order: 'shelve_time:desc',
status: 1,
sales: 'Y',
attribute_not: 2,
stocknumber: 3,
shelve_time: _getShelveTime(20)
};
params.gender = channelMap[channel].gender;
_.forEach(sortList, (item) => {
let data = Object.assign(item, params);
rel.push(_httpBuildQuery(data));
});
return Promise.all(rel).then(res => {
let data = [],
result = [];
_.forEach(sortList, (it, index) => {
if (res[index].data.product_list && res[index].data.product_list.length === sortList[index].viewNum) {
data = data.concat(res[index].data.product_list);
}
});
_.forEach(data, (item) => {
result.push(processProduct(item, {
width: 280,
height: 373
}));
});
return result;
});
};
/**
* 获取频道页数据
* @param {String} type 传入频道页类型,值可以是: boys, girls, kids, lifestyle
* @return {Object}
*/
exports.getContent = type => {
return Promise.all([headerModel.requestHeaderData(type), requestContent(type)]).then(res => {
const getContent = type => {
return Promise.all([headerModel.requestHeaderData(type), _requestContent(type)]).then(res => {
let headerData = res[0].data || res[0],
contentData = res[1].data ? res[1].data.list : res[1];
let data = {};
const processResult = processFloorData(contentData, type);
const processResult = _processFloorData(contentData, type);
data = headerData;
data.module = 'channel';
... ... @@ -687,7 +679,7 @@ exports.getContent = type => {
_.forEach(res, (data, index) => {
result.floorData.channel
.splice(result.singlehotFloorIndex[index], 0,
processFloorDataWithQueryReusult(
_processFloorDataWithQueryReusult(
result.rawData,
data,
result.singlehotFloorTitle[index],
... ... @@ -705,8 +697,8 @@ exports.getContent = type => {
// 优选品牌楼层floorData-ajax
exports.getbrandFloorDataAjax = type => {
return requestContent(type).then(res => {
const getbrandFloorDataAjax = type => {
return _requestContent(type).then(res => {
let contentData = res.data ? res.data.list : [];
let data = {
... ... @@ -732,3 +724,9 @@ exports.getbrandFloorDataAjax = type => {
return data;
});
};
module.exports = {
getNewArrival: getNewArrival,
getContent: getContent,
getbrandFloorDataAjax: getbrandFloorDataAjax
};
... ...