publicComment.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*评论 切换*/
  2. $(function () {
  3. let publicHash = window.location.search,
  4. publicHashArr = publicHash.split('='),
  5. piblicid =publicHashArr[1],
  6. publicIds = piblicid.split('&')[0];
  7. $('.tabEvaluate li').click(function () {
  8. let index = $(this).index();
  9. $(this).addClass('active').siblings().removeClass('active');
  10. $('.tabList>div').eq(index).show().siblings().hide();
  11. });
  12. pages();
  13. //评论列表
  14. loadDate();
  15. var thePageNo = 1,
  16. publicData={},
  17. thePageLength = 1,
  18. pageSize = 4;
  19. let hrefs = window.location.href;
  20. if(hrefs.indexOf('achievement')>-1){
  21. publicData.commodityType=1;
  22. }
  23. if(hrefs.indexOf('demand')>-1){
  24. publicData.commodityType=2;
  25. }
  26. function loadDate(pageNo) {
  27. $.ajax({
  28. method: "get",
  29. dataType: "json",
  30. url: globalConfig.context + "/open/unlanded/comment/list",
  31. data: {
  32. pageNo: pageNo || 1,
  33. pageSize: 4,
  34. commodityId: publicIds
  35. },
  36. success: function (data) {
  37. var theArrs = [];
  38. if (data && data.error.length) {
  39. msg(data.error[0].message)
  40. };
  41. if (data.data.totalCommentCount > 0) {
  42. let good = (((data.data.positiveCommentCount) / (data.data.totalCommentCount)) * 100).toFixed(1),
  43. middle = (((data.data.ordinaryCommentCount) / (data.data.totalCommentCount)) * 100).toFixed(1),
  44. bad = (100 - good - middle).toFixed(1);
  45. $('.commentDetails').html(`
  46. 评价 :<span>好评(${good+'%'})</span><span>中评(${middle+'%'})</span><span>差评(${bad+'%'})</span>
  47. `)
  48. } else {
  49. $('.commentDetails').html(`
  50. 评价 :<span>好评(100%)</span><span>中评(0%)</span><span>差评(0%)</span>
  51. `)
  52. }
  53. if (data.data && data.data.comments.list.length) {
  54. let nub = data.data.comments.list.length;
  55. for (let i = 0; i < nub; i++) {
  56. let thisdata = data.data.comments.list[i],
  57. name;
  58. let times = thisdata.createTime ? new Date(thisdata.createTime).toLocaleString() : '',
  59. star = thisdata.star;
  60. if (thisdata.uname.indexOf('游客') > -1) {
  61. name = '匿名'
  62. } else {
  63. name = thisdata.uname
  64. };
  65. theArrs.push(`
  66. <li>
  67. <div class="stasf">
  68. <p>用户名 :<span class="uName">${name}</span></p>
  69. <div class="satisfied">
  70. <span>满意度:</span>
  71. <ol>
  72. <li><span class="glyphicon ${star>=1?'glyphicon-star':'glyphicon-star-empty'} "></span></li>
  73. <li><span class="glyphicon ${star>=2?'glyphicon-star':'glyphicon-star-empty'}"></span></li>
  74. <li><span class="glyphicon ${star>=3?'glyphicon-star':'glyphicon-star-empty'}"></span></li>
  75. <li><span class="glyphicon ${star>=4?'glyphicon-star':'glyphicon-star-empty'}"></span></li>
  76. <li><span class="glyphicon ${star>=5?'glyphicon-star':'glyphicon-star-empty'}"></span></li>
  77. </ol>
  78. </div>
  79. </div>
  80. <div><time>${times}</time></div>
  81. <div class="satisfiedContent">
  82. <p>${(thisdata.content).replace(/<.*?>/g, "")}</p>
  83. </div>
  84. </li>`);
  85. };
  86. };
  87. $('.numberBars').html(data.data.totalCommentCount > 10 ? '10+' : data.data.totalCommentCount)
  88. $('.evaluateList>ul').empty();
  89. $('.evaluateList>ul').append(theArrs.join(''));
  90. if (!(data.data.comments.list.length)) {
  91. $('.evaluateList>ul').html("<div class='noComment'>暂无评论</div>");
  92. $('.pagination_box').css('display', 'none')
  93. } else {
  94. $('.pagination_box').css('display', 'block');
  95. }
  96. thePageLength = data.data.comments.totalCount ? Math.ceil(data.data.comments.totalCount / pageSize) : 1;
  97. $('.totalCount').html(`共${data.data.comments.totalCount}条数据 ${thePageLength}页`)
  98. var pageArr = [],
  99. firstNo = 1,
  100. endNo = 5;
  101. if (thePageNo > 3) {
  102. firstNo = thePageNo - 2;
  103. endNo = Math.min((Number(thePageNo) + 2), thePageLength);
  104. } else {
  105. endNo = Math.min(thePageLength, 5);
  106. };
  107. for (let i = firstNo; i <= endNo; i++) {
  108. if (i == thePageNo) {
  109. pageArr.push(
  110. '<li class="pageNumber active"><a href="#" value=' + i + ' >' + i + '</a></li>'
  111. );
  112. } else {
  113. pageArr.push(
  114. '<li class="pageNumber"><a href="#" value=' + i + ' >' + i + '</a></li>'
  115. );
  116. }
  117. };
  118. $('.pageNumber').remove();
  119. $('.pagePre').after(pageArr.join(''));
  120. if(pageNo=='1'){
  121. $('.pagination ul li').eq(1).addClass('active').siblings().removeClass('active');
  122. }
  123. }
  124. });
  125. }
  126. //f分页
  127. function pages() {
  128. $('.pagination').on('click', 'li', function (e) {
  129. e.preventDefault();
  130. if (this.className === 'pagePre') {
  131. if (thePageNo > 1) {
  132. thePageNo = 1;
  133. loadDate(thePageNo);
  134. }
  135. } else if (this.className === 'pageNext') {
  136. if (thePageNo < thePageLength) {
  137. thePageNo = thePageLength;
  138. loadDate(thePageNo);
  139. }
  140. } else {
  141. var nextPageNo = $(this).children()[0].text;
  142. if (thePageNo != nextPageNo) {
  143. $(this).siblings("li").removeClass("active");
  144. $(this).addClass("active");
  145. thePageNo = nextPageNo;
  146. loadDate(thePageNo);
  147. };
  148. };
  149. });
  150. }
  151. //提示框渐隐函数
  152. function msg(txt) {
  153. if ($('.smg').hasClass('active')) {
  154. return;
  155. }
  156. txt ? $('#msg').val(txt) : '';
  157. $('.smg').addClass('active');
  158. var lit = $('#msg').val(txt);
  159. setTimeout(function () {
  160. $('.smg').removeClass('active');
  161. $('#msg').val('');
  162. }, 2000)
  163. }
  164. //评论星星
  165. star();
  166. function star() {
  167. $('.commentStar .ulStar span').hover(function () {
  168. $(this).addClass('glyphicon-star').removeClass('glyphicon-star-empty').prevAll().addClass('glyphicon-star').removeClass('glyphicon-star-empty');
  169. $(this).nextAll().removeClass('glyphicon-star').addClass('glyphicon-star-empty').stop(true);
  170. })
  171. }
  172. //新建评论
  173. addComment();
  174. function addComment() {
  175. $('.CommentSend .btn').click(function (e) {
  176. e.preventDefault();
  177. let starNum = $('.commentStar .ulStar span.glyphicon-star').length,
  178. textVal = $('#commentArea').val();
  179. if (!starNum) {
  180. msg('请您选择星级!');
  181. return;
  182. }
  183. if (!textVal) {
  184. msg('请您填写评论内容!');
  185. return;
  186. }
  187. $.ajax({
  188. method: 'post',
  189. url: globalConfig.context + "/open/addComment",
  190. dataType: 'json',
  191. data: {
  192. commodityId: publicIds,
  193. content: textVal,
  194. star: starNum,
  195. commodityType:publicData.commodityType
  196. }
  197. }).done(function (data) {
  198. if (data && data.error.length) {
  199. msg(data.error[0].message);
  200. return;
  201. }
  202. msg('评论成功,感谢您的评论。');
  203. $('#commentArea').val('');
  204. $('.ulStar span').removeClass('glyphicon-star').addClass('glyphicon-star-empty');
  205. thePageNo=1;
  206. loadDate(thePageNo);
  207. })
  208. })
  209. }
  210. })