Authored by ccbikai

商品处理改成组件

@@ -13,6 +13,7 @@ const logger = require(`${library}/logger`); @@ -13,6 +13,7 @@ const logger = require(`${library}/logger`);
13 const helpers = require(`${library}/helpers`); 13 const helpers = require(`${library}/helpers`);
14 const camelCase = require(`${library}/camel-case`); 14 const camelCase = require(`${library}/camel-case`);
15 const resourcesProcess = require(`${utils}/resources-process`); 15 const resourcesProcess = require(`${utils}/resources-process`);
  16 +const productProcess = require(`${utils}/product-process`);
16 const _ = require('lodash'); 17 const _ = require('lodash');
17 const api = new API(); 18 const api = new API();
18 const serviceAPI = new ServiceAPI(); 19 const serviceAPI = new ServiceAPI();
@@ -130,248 +131,6 @@ const special = (params) => { @@ -130,248 +131,6 @@ const special = (params) => {
130 }; 131 };
131 132
132 /** 133 /**
133 - * 根据性别来决定 默认图片获取字段 如果是 2、3  
134 - *  
135 - * 则优先从cover2 --》 cover1 -- 》 images_url  
136 - * 否则优先从cover1 --》 cover2 -- 》 images_url  
137 - *  
138 - */  
139 -const procProductImg = (product, gender) => {  
140 - if (gender === '2,3') {  
141 - return product.cover2 || product.cover1 || product.imagesUrl || '';  
142 - }  
143 -  
144 - return product.cover1 || product.cover2 || product.imagesUrl || '';  
145 -};  
146 -  
147 -  
148 -/**  
149 - * 商品搜索商品数据处理  
150 - */  
151 -const processProductList = (list, options) => {  
152 - const pruductList = [];  
153 -  
154 - options = Object.assign({  
155 - showTags: true,  
156 - showNew: true,  
157 - showSale: true,  
158 - width: 290,  
159 - height: 388,  
160 - isApp: false,  
161 - showPoint: true,  
162 - gender: '2,3'  
163 - }, options);  
164 - list = camelCase(list);  
165 -  
166 - _.forEach(list, (product) => {  
167 - // 商品信息有问题,则不显示  
168 - if (!product.productId || !product.goodsList.length) {  
169 - return;  
170 - }  
171 -  
172 - // 市场价和售价一样,则不显示市场价  
173 - if (product.marketPrice === product.salesPrice) {  
174 - product.marketPrice = false;  
175 - }  
176 -  
177 - // 判别默认的商品是否将默认的图片URL赋值到skn  
178 - let flag = false;  
179 -  
180 - // 如果设置了默认图片,就取默认的图片  
181 - _.forEach(product.goodsList, (goods) => {  
182 - if (flag) {  
183 - return;  
184 - }  
185 - if (goods.isDefault === 'Y') {  
186 - product.defaultImages = procProductImg(goods);  
187 - flag = true;  
188 - }  
189 - });  
190 -  
191 - // 如果还未赋值,则取第一个skc产品的默认图片  
192 - if (!flag) {  
193 - product.defaultImages = procProductImg(product.goodsList[0]);  
194 - }  
195 -  
196 - // product = Object.assign(product, {  
197 - // id: product.productSkn,  
198 - // thumb: product.defaultImages  
199 - // });  
200 -  
201 - if (options.showPoint) {  
202 - product.price += '.00';  
203 - product.salePrice += '.00';  
204 - }  
205 -  
206 - product.isSoonSoldOut = product.isSoonSoldOut === 'Y';  
207 - product.url = helpers.urlFormat(`/product/pro_${product.productId}_${product.goodsList[0].goodsId}/${product.cnAlphabet}.html`); // eslint-disable-line  
208 -  
209 - // APP访问需要加附加的参数  
210 - // 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护  
211 - if (options.isApp) {  
212 - product.url += `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":'${product.productId}'}}`; // eslint-disable-line  
213 - }  
214 -  
215 - if (options.showTags) {  
216 - product.tags = {};  
217 -  
218 - product.tags.isNew = options.showNew && product.isNew === 'Y'; // 新品  
219 - product.tags.isDiscount = options.showSale && product.isDiscount === 'Y'; // 在售  
220 - product.tags.isLimited = product.isLimited === 'Y'; // 限量  
221 - product.tags.isYohood = product.isYohood === 'Y'; // YOHOOD  
222 - product.tags.midYear = product.midYear === 'Y'; // 年中  
223 - product.tags.yearEnd = product.yearEnd === 'Y'; // 年末  
224 - product.tags.isAdvance = product.isAdvance === 'Y'; // 再到着  
225 -  
226 - // 打折与即将售完组合显示打折  
227 - if (product.isSoonSoldOut && product.tags.isDiscount) {  
228 - product.tags.isNew = false;  
229 - } else if (product.tags.isDiscount &&  
230 - (product.tags.isNew || product.tags.isLimited || product.tags.isYohood || product.tags.isAdvance)) {  
231 - // 打折与其它组合则隐藏打折  
232 - product.tags.isDiscount = false;  
233 - } else if (product.tags.isYohood && product.tags.isNew) {  
234 - // YOHOOD和新品组合显示YOHOOD  
235 - product.tags.isNew = false;  
236 - }  
237 - }  
238 -  
239 - pruductList.push(product);  
240 - });  
241 - return pruductList;  
242 -};  
243 -  
244 -/**  
245 - * 处理筛选数据  
246 - * @param list  
247 - * @param string | options  
248 - * @return array 处理之后的筛选数据  
249 - */  
250 -const processFilter = (list, options) => {  
251 - const filters = {  
252 - classify: []  
253 - };  
254 -  
255 - const filtersType = {  
256 - brand: {  
257 - name: '所有品牌',  
258 - title: '品牌',  
259 - dataId: 'id',  
260 - subsName: 'brandName',  
261 - firstSub: 0,  
262 - dataType: 'brand'  
263 - },  
264 - color: {  
265 - name: '所有颜色',  
266 - title: '颜色',  
267 - dataId: 'colorId',  
268 - subsName: 'colorName',  
269 - firstSub: 0,  
270 - dataType: 'color'  
271 - },  
272 - discount: {  
273 - name: '所有商品',  
274 - title: '折扣',  
275 - dataId: 'key',  
276 - subsName: 'name',  
277 - firstSub: '0.1,0.9',  
278 - dataType: 'discount'  
279 - },  
280 - gender: {  
281 - name: '所有性别',  
282 - title: '性别',  
283 - dataId: 'key',  
284 - subsName: 'flag',  
285 - firstSub: '1,2,3',  
286 - dataType: 'gender'  
287 - },  
288 - groupSort: {  
289 - name: '所有品类',  
290 - title: '品类',  
291 - dataId: 'relationParameter',  
292 - subsName: 'categoryName',  
293 - firstSub: 0,  
294 - dataType: 'sort'  
295 - },  
296 - priceRange: {  
297 - name: '所有价格',  
298 - title: '价格',  
299 - dataId: 'key',  
300 - subsName: 'flag',  
301 - firstSub: 0,  
302 - dataType: 'price'  
303 - },  
304 - size: {  
305 - name: '所有尺码',  
306 - title: '尺码',  
307 - dataId: 'sizeId',  
308 - subsName: 'sizeName',  
309 - firstSub: 0,  
310 - dataType: 'size'  
311 - }  
312 - };  
313 -  
314 - options = Object.assign({  
315 - gender: '1,2,3', // 默认选择的性别,默认1,2,3表示所有  
316 - exclude: null // 需要排除的字段  
317 - }, options);  
318 - list = camelCase(list);  
319 -  
320 - _.forEach(list, (item, key) => {  
321 - let classify = {  
322 - subs: []  
323 - };  
324 -  
325 - if (key === 'group_sort') {  
326 - return;  
327 - }  
328 -  
329 - console.log(options, key);  
330 -  
331 - if ((options.hideSize && key === 'size') || (options.hideSort && key === 'groupSort')) {  
332 - return;  
333 - }  
334 -  
335 - classify.dataType = filtersType[key].dataType;  
336 - classify.name = filtersType[key].name;  
337 - classify.title = filtersType[key].title;  
338 -  
339 - classify.subs.push({  
340 - chosed: true,  
341 - dataId: filtersType[key].firstSub,  
342 - name: filtersType[key].name  
343 - });  
344 -  
345 - _.forEach(item, (sub, index) => {  
346 - let subs = {};  
347 -  
348 - if (filtersType[key].dataId === 'key') {  
349 - subs.dataId = index;  
350 - } else if (filtersType[key].dataId === 'relationParameter') {  
351 - subs.dataId = sub.relationParameter['sort']; // eslint-disable-line  
352 - } else {  
353 - subs.dataId = sub[filtersType[key].dataId];  
354 - }  
355 -  
356 - if (filtersType[key].subsName === 'flag') {  
357 - subs.name = sub;  
358 - } else {  
359 - subs.name = sub[filtersType[key].subsName];  
360 -  
361 - if (key === 'discount') {  
362 - subs.name = subs.name + '折商品';  
363 - }  
364 - }  
365 - classify.subs.push(subs);  
366 - });  
367 -  
368 - filters.classify.push(classify);  
369 - });  
370 -  
371 - return filters;  
372 -};  
373 -  
374 -/**  
375 * 断码区分类数据处理 134 * 断码区分类数据处理
376 */ 135 */
377 const processBreakingSort = (list) => { 136 const processBreakingSort = (list) => {
@@ -419,8 +178,7 @@ const searchSales = (params) => { @@ -419,8 +178,7 @@ const searchSales = (params) => {
419 exports.getFilterData = (params) => { 178 exports.getFilterData = (params) => {
420 return searchSales(params).then((result) => { 179 return searchSales(params).then((result) => {
421 if (result && result.code === 200) { 180 if (result && result.code === 200) {
422 - console.log(params);  
423 - return processFilter(result.data.filter || [], { 181 + return productProcess.processFilter(result.data.filter || [], {
424 hideSize: params.saleType === '1', 182 hideSize: params.saleType === '1',
425 hideSort: params.saleType === '1' 183 hideSort: params.saleType === '1'
426 }); 184 });
@@ -437,7 +195,7 @@ exports.getFilterData = (params) => { @@ -437,7 +195,7 @@ exports.getFilterData = (params) => {
437 exports.getSearchData = (params) => { 195 exports.getSearchData = (params) => {
438 return searchSales(params).then((result) => { 196 return searchSales(params).then((result) => {
439 if (result && result.code === 200) { 197 if (result && result.code === 200) {
440 - return processProductList(result.data.product_list || []); 198 + return productProcess.processProductList(result.data.product_list || []);
441 } else { 199 } else {
442 logger.error('SALE 商品搜索返回 code 不是 200'); 200 logger.error('SALE 商品搜索返回 code 不是 200');
443 return {}; 201 return {};
1 -{{#if .}} {{!-- 剔除值为false的项 --}} 1 +{{#if this}} {{!-- 剔除值为false的项 --}}
2 <div class="good-info" data-id="{{productSkn}}" data-bp-id="guang_goodList_{{productName}}_false"> 2 <div class="good-info" data-id="{{productSkn}}" data-bp-id="guang_goodList_{{productName}}_false">
3 <div class="tag-container clearfix"> 3 <div class="tag-container clearfix">
4 {{# tags}} 4 {{# tags}}
@@ -12,7 +12,7 @@ @@ -12,7 +12,7 @@
12 <p class="good-tag sale-tag">SALE</p> 12 <p class="good-tag sale-tag">SALE</p>
13 {{/ isDiscount}} 13 {{/ isDiscount}}
14 {{# isYohoood}} 14 {{# isYohoood}}
15 - <p class="good-tag running-man-tag">跑男同款</p> 15 + <p class="good-tag running-man-tag">跑男同款</p>
16 {{/ isYohoood}} 16 {{/ isYohoood}}
17 {{# isLimited}} 17 {{# isLimited}}
18 <p class="good-tag limit-tag">限量商品</p> 18 <p class="good-tag limit-tag">限量商品</p>
@@ -47,4 +47,4 @@ @@ -47,4 +47,4 @@
47 {{/if}} 47 {{/if}}
48 </div> 48 </div>
49 </div> 49 </div>
50 -{{/if}}  
  50 +{{/if}}
  1 +'use strict';
  2 +const _ = require('lodash');
  3 +const camelCase = require('../library/camel-case');
  4 +const helpers = require('../library/helpers');
  5 +
  6 +/**
  7 + * 根据性别来决定 默认图片获取字段 如果是 2、3
  8 + *
  9 + * 则优先从cover2 --》 cover1 -- 》 images_url
  10 + * 否则优先从cover1 --》 cover2 -- 》 images_url
  11 + *
  12 + */
  13 +const procProductImg = (product, gender) => {
  14 + if (gender === '2,3') {
  15 + return product.cover2 || product.cover1 || product.imagesUrl || '';
  16 + }
  17 +
  18 + return product.cover1 || product.cover2 || product.imagesUrl || '';
  19 +};
  20 +
  21 +
  22 +/**
  23 + * 商品搜索商品数据处理
  24 + */
  25 +exports.processProductList = (list, options) => {
  26 + const pruductList = [];
  27 +
  28 + options = Object.assign({
  29 + showTags: true,
  30 + showNew: true,
  31 + showSale: true,
  32 + width: 290,
  33 + height: 388,
  34 + isApp: false,
  35 + showPoint: true,
  36 + gender: '2,3'
  37 + }, options);
  38 + list = camelCase(list);
  39 +
  40 + _.forEach(list, (product) => {
  41 + // 商品信息有问题,则不显示
  42 + if (!product.productId || !product.goodsList.length) {
  43 + return;
  44 + }
  45 +
  46 + // 市场价和售价一样,则不显示市场价
  47 + if (product.marketPrice === product.salesPrice) {
  48 + product.marketPrice = false;
  49 + }
  50 +
  51 + // 判别默认的商品是否将默认的图片URL赋值到skn
  52 + let flag = false;
  53 +
  54 + // 如果设置了默认图片,就取默认的图片
  55 + _.forEach(product.goodsList, (goods) => {
  56 + if (flag) {
  57 + return;
  58 + }
  59 + if (goods.isDefault === 'Y') {
  60 + product.defaultImages = procProductImg(goods);
  61 + flag = true;
  62 + }
  63 + });
  64 +
  65 + // 如果还未赋值,则取第一个skc产品的默认图片
  66 + if (!flag) {
  67 + product.defaultImages = procProductImg(product.goodsList[0]);
  68 + }
  69 +
  70 + // product = Object.assign(product, {
  71 + // id: product.productSkn,
  72 + // thumb: product.defaultImages
  73 + // });
  74 +
  75 + if (options.showPoint) {
  76 + product.price += '.00';
  77 + product.salePrice += '.00';
  78 + }
  79 +
  80 + product.isSoonSoldOut = product.isSoonSoldOut === 'Y';
  81 + product.url = helpers.urlFormat(`/product/pro_${product.productId}_${product.goodsList[0].goodsId}/${product.cnAlphabet}.html`); // eslint-disable-line
  82 +
  83 + // APP访问需要加附加的参数
  84 + // 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护
  85 + if (options.isApp) {
  86 + product.url += `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":'${product.productId}'}}`; // eslint-disable-line
  87 + }
  88 +
  89 + if (options.showTags) {
  90 + product.tags = {};
  91 +
  92 + product.tags.isNew = options.showNew && product.isNew === 'Y'; // 新品
  93 + product.tags.isDiscount = options.showSale && product.isDiscount === 'Y'; // 在售
  94 + product.tags.isLimited = product.isLimited === 'Y'; // 限量
  95 + product.tags.isYohood = product.isYohood === 'Y'; // YOHOOD
  96 + product.tags.midYear = product.midYear === 'Y'; // 年中
  97 + product.tags.yearEnd = product.yearEnd === 'Y'; // 年末
  98 + product.tags.isAdvance = product.isAdvance === 'Y'; // 再到着
  99 +
  100 + // 打折与即将售完组合显示打折
  101 + if (product.isSoonSoldOut && product.tags.isDiscount) {
  102 + product.tags.isNew = false;
  103 + } else if (product.tags.isDiscount &&
  104 + (product.tags.isNew || product.tags.isLimited || product.tags.isYohood || product.tags.isAdvance)) {
  105 + // 打折与其它组合则隐藏打折
  106 + product.tags.isDiscount = false;
  107 + } else if (product.tags.isYohood && product.tags.isNew) {
  108 + // YOHOOD和新品组合显示YOHOOD
  109 + product.tags.isNew = false;
  110 + }
  111 + }
  112 +
  113 + pruductList.push(product);
  114 + });
  115 + return pruductList;
  116 +};
  117 +
  118 +
  119 +/**
  120 + * 处理筛选数据
  121 + * @param list
  122 + * @param string | options
  123 + * @return array 处理之后的筛选数据
  124 + */
  125 +exports.processFilter = (list, options) => {
  126 + const filters = {
  127 + classify: []
  128 + };
  129 +
  130 + const filtersType = {
  131 + brand: {
  132 + name: '所有品牌',
  133 + title: '品牌',
  134 + dataId: 'id',
  135 + subsName: 'brandName',
  136 + firstSub: 0,
  137 + dataType: 'brand'
  138 + },
  139 + color: {
  140 + name: '所有颜色',
  141 + title: '颜色',
  142 + dataId: 'colorId',
  143 + subsName: 'colorName',
  144 + firstSub: 0,
  145 + dataType: 'color'
  146 + },
  147 + discount: {
  148 + name: '所有商品',
  149 + title: '折扣',
  150 + dataId: 'key',
  151 + subsName: 'name',
  152 + firstSub: '0.1,0.9',
  153 + dataType: 'discount'
  154 + },
  155 + gender: {
  156 + name: '所有性别',
  157 + title: '性别',
  158 + dataId: 'key',
  159 + subsName: 'flag',
  160 + firstSub: '1,2,3',
  161 + dataType: 'gender'
  162 + },
  163 + groupSort: {
  164 + name: '所有品类',
  165 + title: '品类',
  166 + dataId: 'relationParameter',
  167 + subsName: 'categoryName',
  168 + firstSub: 0,
  169 + dataType: 'sort'
  170 + },
  171 + priceRange: {
  172 + name: '所有价格',
  173 + title: '价格',
  174 + dataId: 'key',
  175 + subsName: 'flag',
  176 + firstSub: 0,
  177 + dataType: 'price'
  178 + },
  179 + size: {
  180 + name: '所有尺码',
  181 + title: '尺码',
  182 + dataId: 'sizeId',
  183 + subsName: 'sizeName',
  184 + firstSub: 0,
  185 + dataType: 'size'
  186 + }
  187 + };
  188 +
  189 + options = Object.assign({
  190 + gender: '1,2,3', // 默认选择的性别,默认1,2,3表示所有
  191 + exclude: null // 需要排除的字段
  192 + }, options);
  193 + list = camelCase(list);
  194 +
  195 + _.forEach(list, (item, key) => {
  196 + let classify = {
  197 + subs: []
  198 + };
  199 +
  200 + if (key === 'group_sort') {
  201 + return;
  202 + }
  203 +
  204 + if ((options.hideSize && key === 'size') || (options.hideSort && key === 'groupSort')) {
  205 + return;
  206 + }
  207 +
  208 + classify.dataType = filtersType[key].dataType;
  209 + classify.name = filtersType[key].name;
  210 + classify.title = filtersType[key].title;
  211 +
  212 + classify.subs.push({
  213 + chosed: true,
  214 + dataId: filtersType[key].firstSub,
  215 + name: filtersType[key].name
  216 + });
  217 +
  218 + _.forEach(item, (sub, index) => {
  219 + let subs = {};
  220 +
  221 + if (filtersType[key].dataId === 'key') {
  222 + subs.dataId = index;
  223 + } else if (filtersType[key].dataId === 'relationParameter') {
  224 + subs.dataId = sub.relationParameter['sort']; // eslint-disable-line
  225 + } else {
  226 + subs.dataId = sub[filtersType[key].dataId];
  227 + }
  228 +
  229 + if (filtersType[key].subsName === 'flag') {
  230 + subs.name = sub;
  231 + } else {
  232 + subs.name = sub[filtersType[key].subsName];
  233 +
  234 + if (key === 'discount') {
  235 + subs.name = subs.name + '折商品';
  236 + }
  237 + }
  238 + classify.subs.push(subs);
  239 + });
  240 +
  241 + filters.classify.push(classify);
  242 + });
  243 +
  244 + return filters;
  245 +};