/*评论 切换*/
$(function () {
let publicHash = window.location.search,
publicHashArr = publicHash.split('='),
piblicid =publicHashArr[1],
publicIds = piblicid.split('&')[0];
$('.tabEvaluate li').click(function () {
let index = $(this).index();
$(this).addClass('active').siblings().removeClass('active');
$('.tabList>div').eq(index).show().siblings().hide();
});
pages();
//评论列表
loadDate();
var thePageNo = 1,
publicData={},
thePageLength = 1,
pageSize = 4;
let hrefs = window.location.href;
if(hrefs.indexOf('achievement')>-1){
publicData.commodityType=1;
}
if(hrefs.indexOf('demand')>-1){
publicData.commodityType=2;
}
function loadDate(pageNo) {
$.ajax({
method: "get",
dataType: "json",
url: globalConfig.context + "/open/unlanded/comment/list",
data: {
pageNo: pageNo || 1,
pageSize: 4,
commodityId: publicIds
},
success: function (data) {
var theArrs = [];
if (data && data.error.length) {
msg(data.error[0].message)
};
if (data.data.totalCommentCount > 0) {
let good = (((data.data.positiveCommentCount) / (data.data.totalCommentCount)) * 100).toFixed(1),
middle = (((data.data.ordinaryCommentCount) / (data.data.totalCommentCount)) * 100).toFixed(1),
bad = (100 - good - middle).toFixed(1);
$('.commentDetails').html(`
评价 :好评(${good+'%'})中评(${middle+'%'})差评(${bad+'%'})
`)
} else {
$('.commentDetails').html(`
评价 :好评(100%)中评(0%)差评(0%)
`)
}
if (data.data && data.data.comments.list.length) {
let nub = data.data.comments.list.length;
for (let i = 0; i < nub; i++) {
let thisdata = data.data.comments.list[i],
name;
let times = thisdata.createTime ? new Date(thisdata.createTime).toLocaleString() : '',
star = thisdata.star;
if (thisdata.uname.indexOf('游客') > -1) {
name = '匿名'
} else {
name = thisdata.uname
};
theArrs.push(`
`);
};
};
$('.numberBars').html(data.data.totalCommentCount > 10 ? '10+' : data.data.totalCommentCount)
$('.evaluateList>ul').empty();
$('.evaluateList>ul').append(theArrs.join(''));
if (!(data.data.comments.list.length)) {
$('.evaluateList>ul').html("");
$('.pagination_box').css('display', 'none')
} else {
$('.pagination_box').css('display', 'block');
}
thePageLength = data.data.comments.totalCount ? Math.ceil(data.data.comments.totalCount / pageSize) : 1;
$('.totalCount').html(`共${data.data.comments.totalCount}条数据 ${thePageLength}页`)
var pageArr = [],
thePageNo=pageNo||1,
firstNo = 1,
endNo = 5;
if (thePageNo > 3) {
firstNo = thePageNo - 2;
endNo = Math.min((Number(thePageNo) + 2), thePageLength);
} else {
endNo = Math.min(thePageLength, 5);
};
for (let i = firstNo; i <= endNo; i++) {
if (i == thePageNo) {
pageArr.push(
'' + i + ''
);
} else {
pageArr.push(
'' + i + ''
);
}
};
$('.pageNumber').remove();
$('.pagePre').after(pageArr.join(''));
if(pageNo=='1'){
$('.pagination ul li').eq(1).addClass('active').siblings().removeClass('active');
}
}
});
}
//f分页
function pages() {
$('.pagination').on('click', 'li', function (e) {
e.preventDefault();
if (this.className === 'pagePre') {
if (thePageNo > 1) {
thePageNo = 1;
loadDate(thePageNo);
}
} else if (this.className === 'pageNext') {
if (thePageNo < thePageLength) {
thePageNo = thePageLength;
loadDate(thePageNo);
}
} else {
var nextPageNo = $(this).children()[0].text;
if (thePageNo != nextPageNo) {
$(this).siblings("li").removeClass("active");
$(this).addClass("active");
thePageNo = nextPageNo;
loadDate(thePageNo);
};
};
});
}
//提示框渐隐函数
function msg(txt) {
if ($('.smg').hasClass('active')) {
return;
}
txt ? $('#msg').val(txt) : '';
$('.smg').addClass('active');
var lit = $('#msg').val(txt);
setTimeout(function () {
$('.smg').removeClass('active');
$('#msg').val('');
}, 2000)
}
//评论星星
star();
function star() {
$('.commentStar .ulStar span').hover(function () {
$(this).addClass('glyphicon-star').removeClass('glyphicon-star-empty').prevAll().addClass('glyphicon-star').removeClass('glyphicon-star-empty');
$(this).nextAll().removeClass('glyphicon-star').addClass('glyphicon-star-empty').stop(true);
})
}
//新建评论
addComment();
function addComment() {
$('.CommentSend .btn').click(function (e) {
e.preventDefault();
let starNum = $('.commentStar .ulStar span.glyphicon-star').length,
textVal = $('#commentArea').val();
if (!starNum) {
msg('请您选择星级!');
return;
}
if (!textVal) {
msg('请您填写评论内容!');
return;
}
$.ajax({
method: 'post',
url: globalConfig.context + "/open/addComment",
dataType: 'json',
data: {
commodityId: publicIds,
content: textVal,
star: starNum,
commodityType:publicData.commodityType
}
}).done(function (data) {
if (data && data.error.length) {
msg(data.error[0].message);
return;
}
msg('评论成功,感谢您的评论。');
$('#commentArea').val('');
$('.ulStar span').removeClass('glyphicon-star').addClass('glyphicon-star-empty');
loadDate(1);
})
})
}
})