Authored by 沈志敏

增加参数

@@ -2,28 +2,40 @@ @@ -2,28 +2,40 @@
2 const model = require('../models/individuation'); 2 const model = require('../models/individuation');
3 3
4 exports.productLst = function(req, res, next) { 4 exports.productLst = function(req, res, next) {
5 - let params = {  
6 - uid: req.query.uid,  
7 - udid: req.query.udid,  
8 - newPromotionId: req.query.pid  
9 - }; 5 + let keys = ['sort', 'misort', 'msort', 'gender', 'brand'],
  6 + enumParam = {},
  7 + params = {
  8 + uid: req.query.uid,
  9 + udid: req.query.udid,
  10 + promotion: req.query.pid,
  11 + specified_sort: req.query.enum,
  12 + p_d: req.query.pd,
  13 + limit: req.query.limit || 10
  14 + };
10 15
11 - if (req.query.sort) {  
12 - params.sort = req.query.sort;  
13 - } else if (req.query.brand) {  
14 - params.brand = req.query.brand;  
15 - }  
16 - if (req.query.enum) {  
17 - params.specified_sort = req.query.enum;  
18 - }  
19 - if (req.query.pd) {  
20 - params.p_d = req.query.pd;  
21 - }  
22 - if (req.query.limit) {  
23 - params.limit = req.query.limit; 16 + keys.forEach(function(k) {
  17 + if (req.query[k]) {
  18 + params[k] = req.query[k];
  19 + }
  20 + });
  21 +
  22 + if (params.specified_sort) {
  23 + if (params.brand) {
  24 + enumParam.brands = params.brand.split(',');
  25 + params.limit = enumParam.brands.length;
  26 + } else if (params.sort) {
  27 + enumParam.sorts = params.sort.split(',');
  28 + params.limit = enumParam.sorts.length;
  29 + } else if (params.misort) {
  30 + enumParam.misorts = params.misort.split(',');
  31 + params.limit = enumParam.misorts.length;
  32 + } else if (params.msort) {
  33 + enumParam.msorts = params.msort.split(',');
  34 + params.limit = enumParam.msorts.length;
  35 + }
24 } 36 }
25 37
26 - model.productLst(params).then((result) => { 38 + model.productLst(params, enumParam).then((result) => {
27 res.jsonp(result); 39 res.jsonp(result);
28 }).catch(next); 40 }).catch(next);
29 }; 41 };
1 const api = global.yoho.API; 1 const api = global.yoho.API;
2 2
  3 +let _getProduct = function(o) {
  4 + if (!o) {
  5 + return {};
  6 + }
  7 + return {
  8 + small_sort_id: o.small_sort_id,
  9 + middle_sort_id: o.middle_sort_id,
  10 + max_sort_id: o.max_sort_id,
  11 + brand_id: o.brand_id,
  12 + brand_domain: o.brand_domain,
  13 + brand_name: o.brand_name,
  14 + product_id: o.product_id,
  15 + product_name: o.product_name,
  16 + product_skn: o.product_skn,
  17 + market_price: o.market_price,
  18 + sales_price: o.sales_price,
  19 + cn_alphabet: o.cn_alphabet,
  20 + default_images: o.default_images,
  21 + goods_id: Array.isArray(o.goods_list) && o.goods_list.length ? o.goods_list[0].goods_id : ''
  22 + };
  23 +};
  24 +
3 module.exports = { 25 module.exports = {
4 - productLst: function(params) { 26 + productLst: function(params, enumParam) {
5 return api.get('', Object.assign({ 27 return api.get('', Object.assign({
6 method: 'app.search.newPromotion' 28 method: 'app.search.newPromotion'
7 }, params)).then(res => { 29 }, params)).then(res => {
8 - var data = [], 30 + var data = new Array(Number(params.limit)),
9 lst = (res.data && res.data.product_list) || []; 31 lst = (res.data && res.data.product_list) || [];
10 32
11 - lst.forEach(function(o) {  
12 - data.push({  
13 - brand_domain: o.brand_domain,  
14 - brand_name: o.brand_name,  
15 - product_id: o.product_id,  
16 - product_name: o.product_name,  
17 - product_skn: o.product_skn,  
18 - market_price: o.market_price,  
19 - sales_price: o.sales_price,  
20 - cn_alphabet: o.cn_alphabet,  
21 - default_images: o.default_images,  
22 - goods_id: Array.isArray(o.goods_list) && o.goods_list.length ? o.goods_list[0].goods_id : ''  
23 - });  
24 - }); 33 + for (var i = 0; i < data.length; i++) {
  34 + var o = lst[i] || {};
25 35
  36 + if (params.specified_sort) {
  37 + // 枚举类型
  38 + if (enumParam.brands && Number(enumParam.brands[i]) === Number(o.brand_id)) {
  39 + data[i] = _getProduct(o);
  40 + } else if (enumParam.sorts && Number(enumParam.sorts[i]) === Number(o.small_sort_id)) {
  41 + data[i] = _getProduct(o);
  42 + } else if (enumParam.misorts && Number(enumParam.misorts[i]) === Number(o.middle_sort_id)) {
  43 + data[i] = _getProduct(o);
  44 + } else if (enumParam.msorts && Number(enumParam.msorts[i]) === Number(o.max_sort_id)) {
  45 + data[i] = _getProduct(o);
  46 + } else {
  47 + lst.splice(i, 0, {});
  48 + data[i] = {};
  49 + }
  50 + } else {
  51 + data[i] = _getProduct(o);
  52 + }
  53 + }
26 return data; 54 return data;
27 }); 55 });
28 } 56 }