//UI界面
<
block
wx:for
=
'{{item.rateArray}}'
wx:key
wx:for-item
=
"i"
>
<
image
wx:if
=
'{{ i==0}}'
src
=
'../../assets/xing4.png'></
image
>
<
image
wx:if
=
'{{ i==1}}'
src
=
'../../assets/xing1.png'></
image
>
<
image
wx:if
=
'{{ i==2}}'
src
=
'../../assets/xing2.png'></
image
>
<
image
wx:if
=
'{{ i==3}}'
src
=
'../../assets/xing3.png'></
image
>
<
image
wx:if
=
'{{ i==4}}'
src
=
'../../assets/xing0.png'></
image
>
</
block
>
//使用方式
res.result[i].rateArray = app.convertToStarsArray(res.result[i].totalScore)
// 星星數據處理函數
convertToStarsArray:
function
(stars) {
var
lightNum = stars /
10
;
var
darkNum = stars %
10
;
var
blackNum =
4
- lightNum;
var
array = [];
for
(
var
i =
1
; i <=
5
; i++) {
if
(i <= lightNum) {
array.push(
0
);
}
}
if
(darkNum <
5
&& darkNum !==
0
) {
array.push(
1
);
}
else
if
(darkNum ===
5
) {
array.push(
2
);
}
else
if
(darkNum >
5
) {
array.push(
3
);
}
else
if
(darkNum ===
0
&& lightNum !==
5
) {
array.push(
4
);
}
if
(lightNum !==
5
) {
for
(
var
i =
0
; i < blackNum; i++) {
array.push(
4
);
}
}
this
.setData({
array: array
});
},