const util = require('../utils/util.js');
const api = require('../config/api.js');
/**
* * @param {number} spuId
* @param {number} pageNum
* @param {number} pageSize
* @param {number} commentsLevel
* @param {boolean} hasImage
*/
let recordsLength = 0;
export function getGoodsAllComments(params) {
let commentList = {
pageNum: 1,
pageSize: 10,
totalCount: '47',
pageList: [],
};
console.log("来查询评价信息params", params)
const {
hasImage,
spuId,
commentLevel
} = params.queryParameter;
const {
pageNum,
pageSize
} = params;
// 发起远程 网络请求查询每一件商品的评价
let commentTemplate = {
spuId: '1722045',
skuId: '1697694',
specInfo: '很不错',
commentContent: '收到货了,第一时间试了一下,很漂亮特别喜欢,大爱大爱,颜色也很好看。棒棒!',
// commentImageUrls: null,
commentScore: 1,
uid: '88881048075',
userName: 'Dean',
userHeadUrl: 'https://cdn-we-retail.ym.tencent.com/tsr/avatar/avatar1.png',
isAnonymity: false,
commentTime: '1592224320000',
isAutoComment: false,
sellerReply: '亲,你好,我们会联系商家和厂商给您一个满意的答复请一定妥善保管好发票',
goodsDetailInfo: '颜色:纯净白 尺码:S码',
commentResources: []
}
return new Promise(function (resolve, reject) {
util.request(api.CommentList, {
userId: wx.getStorageSync('userId'),
prodId: Number(spuId) + 1,
pageNum: pageNum,
pageSize: pageSize
}, 'GET', '').then(res => {
if (res.code == 200) {
let records = res.data.records;
console.log("评价records", records)
// 总评论数
recordsLength = records.length
console.log("recordsLength", records.length)
records.forEach((comment) => {
console.log("每一个comment", comment)
// 商品ID
commentTemplate.spuId = comment.productId
// 评价ID
// commentTemplate. =comment.evaluateId
// 评价内容
commentTemplate.commentContent = comment.evaluationcontent
// 评价时间
commentTemplate.commentTime = comment.evaluationtime
// 评价星级
commentTemplate.commentScore = comment.stars
// 评价用户头像
commentTemplate.userHeadUrl = comment.useravatar
// 评价用户昵称
commentTemplate.userName = comment.username
// 评价商品尺码颜色
commentTemplate.goodsDetailInfo = comment.prodSpec
// 商家回复信息
commentTemplate.sellerReply = comment.sellerReply
// 用户ID
commentTemplate.uid = comment.userId
let aa = commentTemplate
console.log("----commentTemplate.", aa)
console.log("----comment", comment)
// 如果包含评论图片或视频
let imgs = comment.evaluateimgurl;
let video = comment.evaluatevediour;
if (imgs != null || video != null) {
if (video != null) {
let picOrVideo = {
src: '',
type: '',
}
picOrVideo.type = 'video'
picOrVideo.src = api.RESOURCE_COMMON_PATH + video
console.log("push。。")
commentTemplate.commentResources.push(picOrVideo)
} else {
let picOrVideo = {
src: '',
type: '',
}
picOrVideo.type = 'image'
picOrVideo.src = api.RESOURCE_COMMON_PATH + imgs
console.log("push。。")
commentTemplate.commentResources.push(picOrVideo)
}
}
// push到列表里
commentList.pageList.push(commentTemplate)
// console.log("---commentTemplate",commentTemplate)
// commentTemplate.commentResources=[]
// 判断是否有好评、中评、差评、
if (commentLevel) {
if (commentLevel == 3) {
// 好评
commentList.pageList = commentList.pageList.filter(
(comments) => comments.commentScore > 3,
);
} else if (commentLevel == 2) {
// 中评
commentList.pageList = commentList.pageList.filter(
(comments) => comments.commentScore == 3,
);
} else {
// 差
commentList.pageList = commentList.pageList.filter(
(comments) => comments.commentScore < 3,
);
}
}
})
console.log("评论res", res.data.records)
console.log("commentList-----", commentList)
resolve(commentList)
}
});
}).then((res) => {
//同步返回
console.log("操作后的返回评价", commentList)
return commentList;
})
}
部分输入结果
尤其是输出这段预览数组和展开不一致(预览是正常的数据 展开的数据就变成了下一条数据的内容)
数据展示效果也是异常
后台返回两个评价 两个数组一样 而且图片是两个评价的图片合一起的(正常应该是小人图片是第一个评价的图片 小狗是第二个的),网上我查了一下好像是异步 里for循环的问题 请问如何解决? 谢谢啦
你提到的异步for循环的问题是在foreach内部使用异步函数会出的一系列问题,并不是你代码中的情况
util.request(api.CommentList 前面加上return 试下