evaluateList.js 4.52 KB
'use strict'
var util = require('/util.js')

//商品详情页晒单评价需预处理的数据
function parseEvaluateListDataDetailWith(data, picWidth, picHeight, icoWidth, icoHeight, imageWidth) {
  let newData = [];
  if (data.length > 2) {
    return newData;
  }
  if (data.length == 1) {
    newData = data;
  } else {
    for (var i = 0; i < data.length; i++) {
      let item = data[i];
      if (i == 0 && item.url) {
        newData.push(item);
        break;
      }
      item.url = '';
      newData.push(item);
    }
  }
  return parseEvaluateListData(newData, picWidth, picHeight, icoWidth, icoHeight, imageWidth);
}

//晒单列表需处理的数据
function parseEvaluateListData(data, picWidth, picHeight, icoWidth, icoHeight, imageWidth) {
  let newData = [];
  data && data.map((item, index) => {
    let headIco = ''; // 头像
    let nickName = ''; // 昵称
    let vipLevelImg = ''; // 等级
    let factory_goods_name = ''; // 颜色
    let size_name = '';  // 尺码

    if (item.userInfo) {
      nickName = getNickName(item.userInfo.nickName, item.anonymous);
      headIco = item.userInfo.headIco ? item.userInfo.headIco.replace(new RegExp('{width}'), icoWidth).replace(new RegExp('{height}'), icoHeight).replace(new RegExp('{mode}'), 2) : '../userCenter/images/mine_default_head.png';
      let vipLevel = item.userInfo.vipLevel;
      if (vipLevel == 1) {
        vipLevelImg = '../userCenter/images/yin-vip@2x.png';
      } else if (vipLevel == 2) {
        vipLevelImg = '../userCenter/images/jin-vip@2x.png';
      } else if (vipLevel == 3) {
        vipLevelImg = '../userCenter/images/baijin-vip@2x.png';
      }
    }

    if (item.goods) {
      size_name = item.goods.size_name ? '尺码: ' + item.goods.size_name : '';
      factory_goods_name = item.goods.factory_goods_name ? '颜色: ' + item.goods.factory_goods_name : '';
    }

    let url = item.url ? item.url.replace(new RegExp('{width}'), picWidth).replace(new RegExp('{height}'), picHeight).replace(new RegExp('{mode}'), 2) : ''; //缩略图
    let src = item.url ? item.url.replace(new RegExp('{width}'), imageWidth).replace(new RegExp('{height}'), imageWidth).replace(new RegExp('{mode}'), 2) : ''; //放大图
    let createTime = util.formatTimeByDefined(item.createTime, 'Y.M.D');
    let satisfiedArr = [];
    if (item.satisfied > 0) {
      for (let i = 0; i < item.satisfied; i++) {
        satisfiedArr.push(i);
      }
    }

    let sizeName = '';
    if (item.size == 'SMALL') {
      sizeName = '偏小';
    } else if (item.size == 'MIDDLE') {
      sizeName = '适中';
    } else if (item.size == 'BIG') {
      sizeName = '偏大';
    }

    let newItem = {
      headIco,
      nickName,
      vipLevelImg,
      weight: item.weight ? '体重:' + item.weight + 'kg' : '',
      height: item.height ? '身高:' + item.height + 'cm' : '',
      satisfied: satisfiedArr,
      size: sizeName,
      content: item.content,
      url,
      src,
      createTime,
      factory_goods_name,
      size_name
    };
    newData.push(newItem);
  });

  return newData;
}

function getNickName(nickName, anoymous){
  if (nickName == null || nickName == '') {
    return '***';
  }
  if (!anoymous) {
    if (getStrLength(nickName) <= 11) {
      return nickName;
    }
    return subString(nickName) + '...';
  } else {
    if (nickName.length <= 3) {
      return '***';
    }
    return nickName.charAt(0).toString() + '***' + nickName.charAt(nickName.length-1).toString();
  }
}

//获取中英文字符串的长度
function getStrLength(value) {
  let valueLength = 0;
  let chinese = "[\u0391-\uFFE5]";
  /* 获取字段值的长度,如果含中文字符,则每个中文字符长度为2,否则为1 */
  for (let i = 0; i < value.length; i++) {
    /* 获取一个字符 */
    let temp = value.substring(i, i + 1);
    /* 判断是否为中文字符 */
    if (temp.match(chinese)) {
      /* 中文字符长度为2 */
      valueLength += 2;
    } else {
      /* 其他字符长度为1 */
      valueLength += 1;
    }
  }
  return valueLength;
}

function subString(str) {
  var newLength = 0;
  var newStr = "";
  var chineseRegex = /[^\x00-\xff]/g;
  var singleChar = "";
  var strLength = str.replace(chineseRegex, "**").length;
  for (var i = 0; i < strLength; i++) {
    singleChar = str.charAt(i).toString();
    newStr += singleChar;
    if (singleChar.match(chineseRegex) != null) {
      newLength += 2;
    } else {
      newLength++;
    }
    if (newLength >= 10) {
      break;
    }
  }
  return newStr;
}

module.exports = {
  parseEvaluateListData,
  parseEvaluateListDataDetailWith
}