productList.js 3.35 KB
'use strict'

//获取应用实例
let app = getApp();

const screenHeight = app.globalData.systemInfo.screenHeight;
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;

const DEVICE_WIDTH_RATIO = windowWidth / 320;

let listWidth = Math.ceil(137.5 * DEVICE_WIDTH_RATIO);
let listHeight = Math.ceil(254 * DEVICE_WIDTH_RATIO);

const IMAGE_WIDTH = 145;
const IMAGE_HEIGHT = 193;
const IMAGE_RATIO = IMAGE_HEIGHT / IMAGE_WIDTH;
let listImageTop = 31;
let listImageWidthDefault = listWidth;
let listImageHeightDefault = Math.ceil(listWidth * IMAGE_RATIO);

function parseBrandListData(data, listImageWidth, listImageHeight) {
  let newData = [];
  let imageWidth = listImageWidth ? listImageWidth : listImageWidthDefault;
  let imageHeight = listImageHeight ? listImageHeight : listImageHeightDefault;
  
  data && data.map((item, index) => {
        let salePrice = 0; // 售卖价
        let originPrice = 0; // 原价
        let salePriceStr = ''; // 拼接的售卖价
        let originPriceStr = ''; // 拼接的原价
        let showOriginPrice = true;  // 是否显示原价

        let salePriceFixed2 = ''; 
        let originPriceFixed2 = ''; 

        let isGlobalProduct = item.is_global && item.is_global == 'Y'; // 是否全球购商品
        if (isGlobalProduct) {
            salePrice = parseFloat(item.final_price);
            originPrice = parseFloat(item.orign_price);
            salePriceStr = item.formart_final_price;
            originPriceStr = item.formart_orign_price;
        } else {
            salePrice = parseFloat(item.sales_price);
            originPrice = parseFloat(item.market_price);
            salePriceStr = '¥' + salePrice.toFixed(2);
            originPriceStr = '¥' + originPrice.toFixed(2);

            salePriceFixed2 =  salePrice.toFixed(2);
            originPriceFixed2 = originPrice.toFixed(2);

        }

        if (!originPrice || (salePrice == originPrice)) {
            showOriginPrice = false;
        }

        item.showOriginPrice = showOriginPrice;
        item.salePriceStr = salePriceStr;
        item.originPriceStr = originPriceStr;
        item.salePriceFixed2 = salePriceFixed2;
        item.originPriceFixed2 = originPriceFixed2;

        let default_images = item.default_images.replace(/{width}/g, imageWidth*2).replace(/{height}/g, imageHeight*2).replace('{mode}',2);
        
        let shop_id = item.shop_id;
        let shop_name = item.shop_name;

        let newItem = {
          product_skn: item.product_skn,
          default_images,
          product_name: item.product_name,
          showOriginPrice,
          shop_id,
          shop_name,
          salePriceStr,
          originPriceStr,
          originPriceFixed2,
          salePriceFixed2,
          is_shop_cart_add: item.is_shop_cart_add,//是否应该展示右侧的加入购物车按钮 默认不展示
        };

        newData.push(newItem);
      });

    return newData;
}

function cancelFilterSelectState(filters, exceptKey) {
  let newFilters = {};
  for (let itemKey in filters) {
    if (filters.hasOwnProperty(itemKey)) {
      if (itemKey != exceptKey) {
        let newFilter = filters[itemKey];
        newFilter.selected = false;
        newFilters[itemKey] = newFilter;
      }
    }
  }

  return newFilters;
}


module.exports = {
  parseBrandListData,
  cancelFilterSelectState,
}