'use strict'; require('../app'); const _ = require('lodash'); const camelCase = global.yoho.camelCase; const helpers = global.yoho.helpers; /** * 根据性别来决定 默认图片获取字段 如果是 2、3 * * 则优先从cover2 --》 cover1 -- 》 images_url * 否则优先从cover1 --》 cover2 -- 》 images_url * */ const _procProductImg = (product, gender, yhChannel) => { if (gender === '2,3' || gender === '2' || gender === '3' && yhChannel === '2') { return product.cover2 || product.imagesUrl || product.cover1 || ''; } return product.cover1 || product.imagesUrl || product.cover2 || ''; }; const toArray = (obj) => { if (_.isArray(obj)) { return obj; } let arr = []; _.forEach(obj, (v, k) => { if (_.isObject(v)) { v._key = k; } else { v = { _key: k, _value: v }; } arr.push(v); }); return arr; }; /** * 按照数组中指定字段排序二维数组 * * @param array list 需要排序的数组 * @param string key 字段名称 * @param boolean 有 desc 时候降序排列,默认为false */ const _sortListByField = (list, key, desc) => { let array = toArray(list); array = array.sort((a, b) => { let matchNumber = /([\d]+)/g; // 有键,使用键的值排序 if (a[key] && b[key]) { let numA = +(_.toArray(a[key].match(matchNumber))[0] || 0); // 取第一个出现的数字排序,如果不存在,取0 let numB = +(_.toArray(b[key].match(matchNumber))[0] || 0); return (desc ? numA > numB : numA < numB) ? -1 : 1; } // 无键, 使用本身 let numA = +(_.toArray(a._value.match(matchNumber))[0] || 0); let numB = +(_.toArray(b._value.match(matchNumber))[0] || 0); return numA < numB ? -1 : 1; }); return array; }; /** * 商品搜索商品数据处理 */ exports.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 || product.goodsList.length === 0) { return; } // 如果库存为0,显示已抢完 if (product.storageNum === 0) { product.noStorage = true; } // 市场价和售价一样,则不显示市场价 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); product.defaultImages = product.defaultImages; flag = true; } }); // 如果还未赋值,则取第一个skc产品的默认图片 if (!flag) { product.defaultImages = _procProductImg(product.goodsList[0], product.gender, options.yh_channel); } 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 处理之后的筛选数据 */ exports.processFilter = (list, options) => { const filters = { classify: [] }; const filtersType = { brand: { name: '所有品牌', title: '品牌', dataId: 'id', subsName: 'brandName', firstSub: 0, dataType: 'brand', sortNum: '2' }, color: { name: '所有颜色', title: '颜色', dataId: 'colorId', subsName: 'colorName', firstSub: 0, dataType: 'color', sortNum: '4' }, discount: { name: '所有商品', title: '折扣', dataId: 'key', subsName: 'name', firstSub: '0.1,0.9', dataType: 'p_d', sortNum: '7' }, gender: { name: '所有性别', title: '性别', dataId: 'key', subsName: 'flag', firstSub: '1,2,3', dataType: 'gender', sortNum: '0' }, groupSort: { name: '所有品类', title: '品类', dataId: 'relationParameter', subsName: 'categoryName', firstSub: 0, dataType: 'sort', sortNum: '3' }, priceRange: { name: '所有价格', title: '价格', dataId: 'key', subsName: 'flag', firstSub: 0, dataType: 'price', sortNum: '6' }, size: { name: '所有尺码', title: '尺码', dataId: 'sizeId', subsName: 'sizeName', firstSub: 0, dataType: 'size', sortNum: '5' }, ageLevel: { name: '所有年龄', title: '年龄', dataId: 'id', subsName: 'name', firstSub: 0, dataType: 'ageLevel', sortNum: '1' } }; options = Object.assign({ gender: '1,2,3', // 默认选择的性别,默认1,2,3表示所有 exclude: null // 需要排除的字段 }, options); list = camelCase(list); _.forEach(list, (item, key) => { let classify = { subs: [] }; if (key === 'group_sort' || !filtersType[key]) { return; } if ((options.hideSize && key === 'size') || (options.hideSort && key === 'groupSort')) { return; } classify.dataType = filtersType[key].dataType; classify.name = filtersType[key].name; classify.title = filtersType[key].title; classify.subs.push({ chosed: true, dataId: filtersType[key].firstSub, name: filtersType[key].name }); // 折扣,价格区间,需要排序 if (key === 'discount' || key === 'priceRange') { item = _sortListByField(item, 'name'); } _.forEach(item, (sub, index) => { let subs = {}; if (key === 'discount') { subs.dataId = sub._key; } else if (key === 'priceRange') { subs.dataId = sub._key; } else if (filtersType[key].dataId === 'key') { subs.dataId = index; } else if (filtersType[key].dataId === 'relationParameter') { subs.dataId = sub.relationParameter['sort']; // eslint-disable-line } else { subs.dataId = sub[filtersType[key].dataId]; } if (key === 'priceRange') { subs.name = sub._value; } else if (filtersType[key].subsName === 'flag') { subs.name = sub; } else { subs.name = sub[filtersType[key].subsName]; if (key === 'discount') { subs.name = subs.name + '折商品'; } } classify.subs.push(subs); }); filters.classify[filtersType[key].sortNum] = classify; }); return filters; };