detail.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. function getCookie(name) {
  2. var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
  3. return r ? r[1] : undefined;
  4. }
  5. $(function () {
  6. // 打开登录框
  7. $('.comment_form_logout').click(function () {
  8. $('.login_form_con').show();
  9. })
  10. // 收藏
  11. $(".collection").click(function () {
  12. var news_id = $(".collection").attr("data-newid")
  13. var action = "collect"
  14. var params = {
  15. "news_id": news_id,
  16. "action": action
  17. }
  18. $.ajax({
  19. url: "/news/news_collect",
  20. type: "post",
  21. contentType: "application/json",
  22. headers: {
  23. "X-CSRFToken": getCookie("csrf_token")
  24. },
  25. data: JSON.stringify(params),
  26. success: function (resp) {
  27. if (resp.errno == "0") {
  28. // 收藏成功
  29. // 隐藏收藏成功
  30. $(".collection").hide();
  31. // 显示取消收藏按钮
  32. $(".collected").show();
  33. } else if (resp.errno == "4101") {
  34. $(".login_form_con").show()
  35. } else {
  36. alert(resp.errmsg)
  37. }
  38. }
  39. })
  40. });
  41. // 取消收藏
  42. $(".collected").click(function () {
  43. var news_id = $(".collected").attr("data-newid");
  44. var action = "cancel_collect"
  45. var params = {
  46. "news_id": news_id,
  47. "action": action
  48. };
  49. $.ajax({
  50. url: "/news/news_collect",
  51. type: "post",
  52. contentType: "application/json",
  53. headers: {
  54. "X-CSRFToken": getCookie("csrf_token")
  55. },
  56. data: JSON.stringify(params),
  57. success: function (resp) {
  58. if (resp.errno == "0") {
  59. // 收藏成功
  60. // 显示收藏按钮
  61. $(".collection").show();
  62. // 隐藏取消收藏按钮
  63. $(".collected").hide();
  64. } else if (resp.errno == "4101") {
  65. $(".login_form_con").show();
  66. } else {
  67. alert(resp.errmsg);
  68. }
  69. }
  70. })
  71. });
  72. // 评论提交
  73. $(".comment_form").submit(function (e) {
  74. e.preventDefault();
  75. var news_id = $(this).attr('data-newsid');
  76. var news_comment = $('.comment_input').val();
  77. if (!news_comment) {
  78. alert("请输入评论内容");
  79. return
  80. }
  81. var params = {
  82. "news_id": news_id,
  83. "comment": news_comment
  84. };
  85. $.ajax({
  86. url: "/news/news_comment",
  87. type: "post",
  88. contentType: "application/json",
  89. headers: {
  90. "X-CSRFToken": getCookie("csrf_token")
  91. },
  92. data: JSON.stringify(params),
  93. success: function (resp) {
  94. if (resp.errno == "0") {
  95. var comment = resp.data
  96. // 拼接内容
  97. var comment_html = ''
  98. comment_html += '<div class="comment_list">'
  99. comment_html += '<div class="person_pic fl">'
  100. if (comment.user.avatar_url) {
  101. comment_html += '<img src="' + comment.user.avatar_url + '" alt="用户图标">'
  102. } else {
  103. comment_html += '<img src="../../static/news/images/person01.png" alt="用户图标">'
  104. }
  105. comment_html += '</div>'
  106. comment_html += '<div class="user_name fl">' + comment.user.nick_name + '</div>'
  107. comment_html += '<div class="comment_text fl">'
  108. comment_html += comment.content
  109. comment_html += '</div>'
  110. comment_html += '<div class="comment_time fl">' + comment.create_time + '</div>'
  111. comment_html += '<a href="javascript:;" class="comment_up fr" data-commentid="' + comment.id + '" data-newsid="' + comment.news_id + '">赞</a>'
  112. comment_html += '<a href="javascript:;" class="comment_reply fr">回复</a>'
  113. comment_html += '<form class="reply_form fl" data-commentid="' + comment.id + '" data-newsid="' + news_id + '">'
  114. comment_html += '<textarea class="reply_input"></textarea>'
  115. comment_html += '<input type="button" value="回复" class="reply_sub fr">'
  116. comment_html += '<input type="reset" name="" value="取消" class="reply_cancel fr">'
  117. comment_html += '</form>'
  118. comment_html += '</div>'
  119. //拼接到内容的前面
  120. $(".comment_list_con").prepend(comment_html)
  121. //让comment_sub失去焦点
  122. $(".comment_sub").blur();
  123. //清空输入框内容
  124. $(".comment_input").val("")
  125. updateCommentCount()
  126. } else {
  127. alert(resp.errmsg)
  128. }
  129. }
  130. })
  131. })
  132. $('.comment_list_con').delegate('a,input', 'click', function () {
  133. var sHandler = $(this).prop('class');
  134. if (sHandler.indexOf('comment_reply') >= 0) {
  135. $(this).next().toggle();
  136. }
  137. if (sHandler.indexOf('reply_cancel') >= 0) {
  138. $(this).parent().toggle();
  139. }
  140. if (sHandler.indexOf('comment_up') >= 0) {
  141. var $this = $(this);
  142. var action = "add";
  143. if (sHandler.indexOf('has_comment_up') >= 0) {
  144. // 如果当前该评论已经是点赞状态,再次点击会进行到此代码块内,代表要取消点赞
  145. action = "remove"
  146. }
  147. var comment_id = $(this).attr("data-commentid")
  148. var news_id = $(this).attr("data-newsid")
  149. var params = {
  150. "comment_id": comment_id,
  151. "action": action,
  152. "news_id": news_id
  153. }
  154. $.ajax({
  155. url: "/news/comment_like",
  156. type: "post",
  157. contentType: "application/json",
  158. headers:{
  159. "X-CSRFToken": getCookie("csrf_token")
  160. },
  161. data: JSON.stringify(params),
  162. success: function (resp) {
  163. if(resp.errno == "0"){
  164. var like_count = $this.attr('data-likecount')
  165. if(like_count == undefined){
  166. like_count = 0
  167. }
  168. // 更新点赞按钮图标
  169. if(action == "add"){
  170. like_count = parseInt(like_count) + 1
  171. // 点赞
  172. $this.addClass("has_comment_up")
  173. }else{
  174. like_count = parseInt(like_count) - 1
  175. $this.removeClass("has_comment_up").addClass("comment_up")
  176. }
  177. // 更新点赞数据
  178. $this.attr('data-likecount', like_count)
  179. if (like_count == 0){
  180. $this.html("赞")
  181. }else{
  182. $this.html(like_count)
  183. }
  184. }else if(resp.errno == "4101"){
  185. $('.login_form_con').show();
  186. }else{
  187. alert(resp.errmsg)
  188. }
  189. }
  190. })
  191. }
  192. if (sHandler.indexOf('reply_sub') >= 0) {
  193. // alert('回复评论')
  194. var $this = $(this);
  195. var news_id = $this.parent().attr("data-newsid");
  196. var parent_id = $this.parent().attr("data-commentid");
  197. var comment = $this.prev().val()
  198. if (!comment) {
  199. alert("请输入评论内容")
  200. return
  201. }
  202. var params = {
  203. "news_id": news_id,
  204. "comment": comment,
  205. "parent_id": parent_id
  206. }
  207. $.ajax({
  208. url: "/news/news_comment",
  209. type: "post",
  210. contentType: "application/json",
  211. headers: {
  212. "X-CSRFToken": getCookie("csrf_token")
  213. },
  214. data: JSON.stringify(params),
  215. success: function (resp) {
  216. if (resp.errno == "0") {
  217. var comment = resp.data
  218. // 拼接内容
  219. var comment_html = ""
  220. comment_html += '<div class="comment_list">'
  221. comment_html += '<div class="person_pic fl">'
  222. if (comment.user.avatar_url) {
  223. comment_html += '<img src="' + comment.user.avatar_url + '" alt="用户图标">'
  224. } else {
  225. comment_html += '<img src="../../static/news/images/person01.png" alt="用户图标">'
  226. }
  227. comment_html += '</div>'
  228. comment_html += '<div class="user_name fl">' + comment.user.nick_name + '</div>'
  229. comment_html += '<div class="comment_text fl">'
  230. comment_html += comment.content
  231. comment_html += '</div>'
  232. comment_html += '<div class="reply_text_con fl">'
  233. comment_html += '<div class="user_name2">' + comment.parent.user.nick_name + '</div>'
  234. comment_html += '<div class="reply_text">'
  235. comment_html += comment.parent.content
  236. comment_html += '</div>'
  237. comment_html += '</div>'
  238. comment_html += '<div class="comment_time fl">' + comment.create_time + '</div>'
  239. comment_html += '<a href="javascript:;" class="comment_up fr" data-commentid="' + comment.id + '" data-newsid="' + comment.news_id + '">赞</a>'
  240. comment_html += '<a href="javascript:;" class="comment_reply fr">回复</a>'
  241. comment_html += '<form class="reply_form fl" data-commentid="' + comment.id + '" data-newsid="' + news_id + '">'
  242. comment_html += '<textarea class="reply_input"></textarea>'
  243. comment_html += '<input type="button" value="回复" class="reply_sub fr">'
  244. comment_html += '<input type="reset" name="" value="取消" class="reply_cancel fr">'
  245. comment_html += '</form>'
  246. comment_html += '</div>'
  247. $(".comment_list_con").prepend(comment_html)
  248. // 请空输入框
  249. $this.prev().val('')
  250. // 关闭
  251. $this.parent().hide()
  252. updateCommentCount()
  253. } else {
  254. alert(resp.errmsg)
  255. }
  256. }
  257. })
  258. }
  259. })
  260. // 关注当前新闻作者
  261. $(".focus").click(function () {
  262. })
  263. // 取消关注当前新闻作者
  264. $(".focused").click(function () {
  265. })
  266. })
  267. // 更新评论条数
  268. function updateCommentCount() {
  269. var length = $(".comment_list").length;
  270. $(".comment_count").html(length + "条评论")
  271. }