evaluateList.js
4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'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) : '/pages/userCenter/images/mine_default_head.png';
let vipLevel = item.userInfo.vipLevel;
if (vipLevel == 1) {
vipLevelImg = '/pages/userCenter/images/yin-vip@2x.png';
} else if (vipLevel == 2) {
vipLevelImg = '/pages/userCenter/images/jin-vip@2x.png';
} else if (vipLevel == 3) {
vipLevelImg = '/pages/userCenter/images/baijin-vip@2x.png';
}
}
if (item.goods) {
let title = item.goods.sku_title ? item.goods.sku_title : '尺码'
size_name = item.goods.size_name ? title+': ' + 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,
reply: item.reply,
replyTitle: item.replyTitle,
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
}