...
|
...
|
@@ -16,40 +16,334 @@ const resourcesProcess = require(`${utils}/resources-process`); |
|
|
const _ = require('lodash');
|
|
|
const api = new API();
|
|
|
const serviceAPI = new ServiceAPI();
|
|
|
const aMinute = 60000;
|
|
|
const anHour = 3600000;
|
|
|
const aDay = anHour * 24;
|
|
|
|
|
|
const timeFormat = {
|
|
|
d: '剩{d}天',
|
|
|
h: '剩{h}小时',
|
|
|
m: '剩{m}分钟',
|
|
|
s: '剩{s}秒',
|
|
|
dh: '剩{d}天{h}小时',
|
|
|
dhms: '剩{d}天{h}小时{m}分钟{s}秒',
|
|
|
hms: '剩{h}小时{m}分钟{s}秒',
|
|
|
ms: '剩{m}分钟{s}秒'
|
|
|
};
|
|
|
|
|
|
const typeCont = {
|
|
|
price: ['s_p_desc', 's_p_asc'],
|
|
|
discount: ['p_d_desc', 'p_d_asc'],
|
|
|
sales: ['s_n_desc', 's_n_asc'],
|
|
|
newest: ['s_t_desc', 's_t_asc']
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 折扣专场专题列表过期时间处理
|
|
|
*/
|
|
|
const processTime = (time) => {
|
|
|
let data = {};
|
|
|
let type = '';
|
|
|
|
|
|
if (time < anHour) {
|
|
|
data.warnColor = true;
|
|
|
data.time = '低于1小时';
|
|
|
} else {
|
|
|
if (time > aDay) {
|
|
|
type = 'dh';
|
|
|
} else {
|
|
|
type = 'h';
|
|
|
}
|
|
|
|
|
|
data.time = helpers.dateDiffFormat(timeFormat[type], time, 'ms');
|
|
|
}
|
|
|
|
|
|
return data;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 折扣专场专题详情过期时间处理
|
|
|
*/
|
|
|
const processTimes = (time) => {
|
|
|
let data = {};
|
|
|
let type = '';
|
|
|
|
|
|
if (time > aDay) {
|
|
|
type = 'dhms';
|
|
|
} else {
|
|
|
if (time > anHour) {
|
|
|
type = 'hms';
|
|
|
} else {
|
|
|
if (time > aMinute) {
|
|
|
type = 'ms';
|
|
|
} else {
|
|
|
type = 's';
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
data.time = helpers.dateDiffFormat(timeFormat[type], time, 'ms');
|
|
|
|
|
|
return data;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 折扣专场列表数据处理
|
|
|
*/
|
|
|
const processSpecial = (list) => {
|
|
|
const formatData = [];
|
|
|
let flag = true;
|
|
|
|
|
|
list = list || [];
|
|
|
list = camelCase(list);
|
|
|
|
|
|
_.forEach(list, (data) => {
|
|
|
data.specialUrl = `/product/specialDetail?id=${data.id}`;
|
|
|
if (list.length === 1) {
|
|
|
flag = false;
|
|
|
}
|
|
|
|
|
|
if (data.leftTime < anHour) {
|
|
|
data.warnColor = true;
|
|
|
data.leftTime = '低于1小时';
|
|
|
_.forEach(list, (data) => {
|
|
|
if (flag === true) {
|
|
|
data.specialUrl = `/product/specialDetail?id=${data.id}`;
|
|
|
_.merge(data, processTime(data.leftTime));
|
|
|
} else {
|
|
|
if (data.leftTime > aDay) {
|
|
|
data.leftTime = helpers.dateDiffFormat('剩{d}天{h}小时', data.leftTime, 'ms');
|
|
|
} else {
|
|
|
data.leftTime = helpers.dateDiffFormat('剩{h}小时', data.leftTime, 'ms');
|
|
|
}
|
|
|
_.merge(data, processTimes(data.leftTime));
|
|
|
}
|
|
|
|
|
|
formatData.push(data);
|
|
|
});
|
|
|
|
|
|
return formatData;
|
|
|
};
|
|
|
|
|
|
exports.getSaleResources = () => {
|
|
|
/**
|
|
|
* 折扣专场接口调用
|
|
|
*/
|
|
|
const special = (params) => {
|
|
|
params = params || {};
|
|
|
|
|
|
return api.get('', sign.apiSign(Object.assign({
|
|
|
method: 'app.activity.get',
|
|
|
sort: 2,
|
|
|
plateform: 2
|
|
|
}, params)));
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 根据性别来决定 默认图片获取字段 如果是 2、3
|
|
|
*
|
|
|
* 则优先从cover2 --》 cover1 -- 》 images_url
|
|
|
* 否则优先从cover1 --》 cover2 -- 》 images_url
|
|
|
*
|
|
|
*/
|
|
|
const procProductImg = (product, gender) => {
|
|
|
if (gender === '2,3') {
|
|
|
return product.cover2 || product.cover1 || product.imagesUrl || '';
|
|
|
}
|
|
|
|
|
|
return product.cover1 || product.cover2 || product.imagesUrl || '';
|
|
|
};
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 商品搜索商品数据处理
|
|
|
*/
|
|
|
const processProductList = (list, options) => {
|
|
|
const pruductList = [];
|
|
|
|
|
|
options = Object.assign({
|
|
|
showTags: true,
|
|
|
showNew: true,
|
|
|
showSale: true,
|
|
|
width: 290,
|
|
|
height: 388,
|
|
|
isApp: false,
|
|
|
showPoint: true,
|
|
|
gender: '2,3'
|
|
|
}, options);
|
|
|
list = camelCase(list);
|
|
|
|
|
|
_.forEach(list, (product) => {
|
|
|
// 商品信息有问题,则不显示
|
|
|
if (!product.productId || !product.goodsList.length) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 市场价和售价一样,则不显示市场价
|
|
|
if (product.marketPrice === product.salesPrice) {
|
|
|
product.marketPrice = false;
|
|
|
}
|
|
|
|
|
|
// 判别默认的商品是否将默认的图片URL赋值到skn
|
|
|
let flag = false;
|
|
|
|
|
|
// 如果设置了默认图片,就取默认的图片
|
|
|
_.forEach(product.goodsList, (goods) => {
|
|
|
if (flag) {
|
|
|
return;
|
|
|
}
|
|
|
if (goods.isDefault === 'Y') {
|
|
|
product.defaultImages = procProductImg(goods);
|
|
|
flag = true;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
// 如果还未赋值,则取第一个skc产品的默认图片
|
|
|
if (!flag) {
|
|
|
product.defaultImages = procProductImg(product.goodsList[0]);
|
|
|
}
|
|
|
|
|
|
// product = Object.assign(product, {
|
|
|
// id: product.productSkn,
|
|
|
// thumb: product.defaultImages
|
|
|
// });
|
|
|
|
|
|
if (options.showPoint) {
|
|
|
product.price += '.00';
|
|
|
product.salePrice += '.00';
|
|
|
}
|
|
|
|
|
|
product.isSoonSoldOut = product.isSoonSoldOut === 'Y';
|
|
|
product.url = helpers.urlFormat(`/product/pro_${product.productId}_${product.goodsList[0].goodsId}/${product.cnAlphabet}.html`); // eslint-disable-line
|
|
|
|
|
|
// APP访问需要加附加的参数
|
|
|
// 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护
|
|
|
if (options.isApp) {
|
|
|
product.url += `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":'${product.productId}'}}`; // eslint-disable-line
|
|
|
}
|
|
|
|
|
|
if (options.showTags) {
|
|
|
product.tags = {};
|
|
|
|
|
|
product.tags.isNew = options.showNew && product.isNew === 'Y'; // 新品
|
|
|
product.tags.isDiscount = options.showSale && product.isDiscount === 'Y'; // 在售
|
|
|
product.tags.isLimited = product.isLimited === 'Y'; // 限量
|
|
|
product.tags.isYohood = product.isYohood === 'Y'; // YOHOOD
|
|
|
product.tags.midYear = product.midYear === 'Y'; // 年中
|
|
|
product.tags.yearEnd = product.yearEnd === 'Y'; // 年末
|
|
|
product.tags.isAdvance = product.isAdvance === 'Y'; // 再到着
|
|
|
|
|
|
// 打折与即将售完组合显示打折
|
|
|
if (product.isSoonSoldOut && product.tags.isDiscount) {
|
|
|
product.tags.isNew = false;
|
|
|
} else if (product.tags.isDiscount &&
|
|
|
(product.tags.isNew || product.tags.isLimited || product.tags.isYohood || product.tags.isAdvance)) {
|
|
|
// 打折与其它组合则隐藏打折
|
|
|
product.tags.isDiscount = false;
|
|
|
} else if (product.tags.isYohood && product.tags.isNew) {
|
|
|
// YOHOOD和新品组合显示YOHOOD
|
|
|
product.tags.isNew = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
pruductList.push(product);
|
|
|
});
|
|
|
return pruductList;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 处理筛选数据
|
|
|
* @param list
|
|
|
* @param string | options
|
|
|
* @return array 处理之后的筛选数据
|
|
|
*/
|
|
|
const processFilter = (list, options) => {
|
|
|
const filters = {
|
|
|
classify: {}
|
|
|
};
|
|
|
|
|
|
options = Object.assign({
|
|
|
gender: '1,2,3', // 默认选择的性别,默认1,2,3表示所有
|
|
|
exclude: null // 需要排除的字段
|
|
|
}, options);
|
|
|
list = camelCase(list);
|
|
|
|
|
|
_.forEach(list, (item, key) => {
|
|
|
console.log(key);
|
|
|
});
|
|
|
return filters;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 断码区分类数据处理
|
|
|
*/
|
|
|
const processBreakingSort = (list) => {
|
|
|
const formatData = {};
|
|
|
const sort = [];
|
|
|
const sub = [];
|
|
|
|
|
|
list = list || [];
|
|
|
list = camelCase(list);
|
|
|
|
|
|
_.forEach(list, (data, index) => {
|
|
|
sort.push({
|
|
|
sortName: data.sortName,
|
|
|
sortId: data.sortId
|
|
|
});
|
|
|
data.sub.key = index;
|
|
|
sub.push(data.sub);
|
|
|
});
|
|
|
|
|
|
formatData.sortData = sort;
|
|
|
formatData.sub = sub;
|
|
|
return formatData;
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 商品搜索接口请求
|
|
|
*/
|
|
|
const searchSales = (params) => {
|
|
|
params = Object.assign({
|
|
|
limit: '20'
|
|
|
}, params);
|
|
|
|
|
|
if (typeCont[params.type]) {
|
|
|
params.order = typeCont[params.type][params.order];
|
|
|
}
|
|
|
return api.get('', sign.apiSign(Object.assign({
|
|
|
method: 'app.search.sales'
|
|
|
}, params)), true);
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 获取筛选数据
|
|
|
*/
|
|
|
exports.getFilterData = (params) => {
|
|
|
return searchSales(params).then((result) => {
|
|
|
if (result && result.code === 200) {
|
|
|
return processFilter(result.data.filter || []);
|
|
|
} else {
|
|
|
logger.error('SALE 商品搜索返回 code 不是 200');
|
|
|
return [];
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 获取商品数据
|
|
|
*/
|
|
|
exports.getSearchData = (params) => {
|
|
|
return searchSales(params).then((result) => {
|
|
|
if (result && result.code === 200) {
|
|
|
return processProductList(result.data.product_list || []);
|
|
|
} else {
|
|
|
logger.error('SALE 商品搜索返回 code 不是 200');
|
|
|
return {};
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 获取资源位数据
|
|
|
*/
|
|
|
const getResources = (page) => {
|
|
|
const contentCode = {
|
|
|
sale: '7c2b77093421efa8ae9302c91460db73',
|
|
|
breakCode: '7c2b77093421efa8ae9302c91460db73',
|
|
|
vip: '7c2b77093421efa8ae9302c91460db73'
|
|
|
};
|
|
|
|
|
|
return serviceAPI.get('operations/api/v5/resource/get', sign.apiSign({
|
|
|
content_code: '7c2b77093421efa8ae9302c91460db73'
|
|
|
content_code: contentCode[page]
|
|
|
})).then((result) => {
|
|
|
if (result && result.code === 200) {
|
|
|
return resourcesProcess(result.data);
|
...
|
...
|
@@ -61,15 +355,51 @@ exports.getSaleResources = () => { |
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 获取首页数据
|
|
|
* 获取断码区分类数据
|
|
|
*/
|
|
|
const getBreakingSort = (yhChannel) => {
|
|
|
return api.get('', sign.apiSign({
|
|
|
method: 'app.sale.getBreakingSort',
|
|
|
yh_channel: yhChannel || '1'
|
|
|
})).then((result) => {
|
|
|
if (result && result.code === 200) {
|
|
|
return processBreakingSort(result.data);
|
|
|
} else {
|
|
|
logger.error('断码区分类接口返回 code 不是 200');
|
|
|
return {};
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 获取sale首页数据
|
|
|
*/
|
|
|
exports.getSaleData = () => {
|
|
|
return getResources('sale');
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 获取会员享数据
|
|
|
* @param {[object]} params
|
|
|
* @return {[object]}
|
|
|
*/
|
|
|
exports.getSaleData = (params) => {
|
|
|
exports.getVipData = () => {
|
|
|
return getResources('vip');
|
|
|
};
|
|
|
|
|
|
return api.get('', sign.apiSign(Object.assign({
|
|
|
method: 'app.search.sales'
|
|
|
}, params)));
|
|
|
/**
|
|
|
* 获取断码区数据
|
|
|
* @param {[object]} params
|
|
|
* @return {[object]}
|
|
|
*/
|
|
|
exports.getBreakCodeData = (params) => {
|
|
|
params = params || {};
|
|
|
return Promise.all([getResources('breakCode'), getBreakingSort(params.yhChannel)]).then((result) => {
|
|
|
return {
|
|
|
content: result[0],
|
|
|
nav: result[1]
|
|
|
};
|
|
|
});
|
|
|
};
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -80,16 +410,13 @@ exports.getSaleData = (params) => { |
|
|
exports.getSpecialData = (params) => {
|
|
|
const specialData = {};
|
|
|
|
|
|
return api.get('', sign.apiSign(Object.assign({
|
|
|
method: 'app.activity.get',
|
|
|
sort: 2,
|
|
|
plateform: 2
|
|
|
}, params))).then((result) => {
|
|
|
return special(params).then((result) => {
|
|
|
if (result && result.code === 200) {
|
|
|
specialData.data = processSpecial(result.data);
|
|
|
return specialData;
|
|
|
} else {
|
|
|
return Promise.reject('error');
|
|
|
logger.error('折扣专场专题列表返回 code 不是 200');
|
|
|
return {};
|
|
|
}
|
|
|
});
|
|
|
};
|
...
|
...
|
@@ -100,35 +427,26 @@ exports.getSpecialData = (params) => { |
|
|
* @param {[object]} params
|
|
|
* @return {[object]}
|
|
|
*/
|
|
|
exports.getSpecialDetailData = (params) => {
|
|
|
return api.get('', sign.apiSign(Object.assign({
|
|
|
method: 'app.activity.get',
|
|
|
sort: 2,
|
|
|
plateform: 2
|
|
|
}, params))).then((result) => {
|
|
|
if (result && result.code === 200) {
|
|
|
return camelCase(result.data);
|
|
|
} else {
|
|
|
return Promise.reject('error');
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
exports.getSpecialDetailData = (id) => {
|
|
|
let res = {};
|
|
|
let param = {
|
|
|
id: id
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 获取断码区数据
|
|
|
* @param {[object]} params
|
|
|
* @return {[object]}
|
|
|
*/
|
|
|
exports.getBreakCodeData = (params) => {
|
|
|
return api.get('', sign.apiSign(Object.assign({
|
|
|
method: 'app.activity.get',
|
|
|
sort: 2,
|
|
|
plateform: 2
|
|
|
}, params))).then((result) => {
|
|
|
return special(param).then((result) => {
|
|
|
if (result && result.code === 200) {
|
|
|
return camelCase(result.data);
|
|
|
res = processSpecial(result.data);
|
|
|
return {
|
|
|
title: res[0].title,
|
|
|
activity: {
|
|
|
coverUrl: res[0].coverUrl,
|
|
|
time: res[0].time,
|
|
|
leftTime: res[0].leftTime
|
|
|
}
|
|
|
};
|
|
|
} else {
|
|
|
return Promise.reject('error');
|
|
|
logger.error('折扣专场专题详情返回 code 不是 200');
|
|
|
return {};
|
|
|
}
|
|
|
});
|
|
|
}; |
...
|
...
|
|