comment.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. $(document).ready(function() {
  2. setTimeout(() => {
  3. $('.quanpinghei').hide();
  4. }, 400);
  5. $('.logo').on('click', function () {
  6. window.location.href = "https://www.bibidd.com";
  7. })
  8. var url = window.location.search;
  9. var theRequest = new Object();
  10. if (url.indexOf("?") != -1) {
  11. var str = url.substr(1);
  12. strs = str.split("&");
  13. for (var i = 0; i < strs.length; i++) {
  14. theRequest[strs[i].split("=")[0]] = unescape(
  15. decodeURIComponent(strs[i].split("=")[1])
  16. );
  17. }
  18. }
  19. var videoid = theRequest.videoid;
  20. var uid = localStorage.getItem("uid");
  21. if (uid) {
  22. $("#register").text("My Collection");
  23. $("#login").text("Log out");
  24. $("#login").css("backgroundColor", "#D6565A");
  25. $('.userName').text('');
  26. $('.userImg').css('opacity','0');
  27. } else {
  28. $("#register").text("Sign up");
  29. $("#login").text("Login");
  30. $("#login").css("backgroundColor", "#CE5C2A");
  31. $('.userName').text('Please login');
  32. }
  33. // 注册
  34. $('#register').on('click', function () {
  35. $(location).attr('href', 'register.html');
  36. $('.loginBox').hide();
  37. })
  38. // 登录/退出登录
  39. $('#login').on('click', function () {
  40. $(location).attr('href', 'blogin.html');
  41. $('.loginBox').hide();
  42. })
  43. $('.guanbi').on('click', function () {
  44. $('.loginBox').hide();
  45. })
  46. var commentsNum = 0;
  47. // 获取评论数据
  48. function commentData() {
  49. $.ajax({
  50. type: 'post',
  51. url: 'https://www.bibidd.com/bibidd/Mediafive/comments_list',
  52. dataType: "json",
  53. data: {
  54. type: 'video',
  55. cid: videoid
  56. },
  57. success: function (res) {
  58. let commentData = res.data;
  59. commentsNum = commentData.length;
  60. $('.commentTitle').text('Video comments ( ' + commentsNum + ' )');
  61. let commentListHtml = '';
  62. $.each(commentData, function (index, data) {
  63. commentListHtml += '<div class="commentContent">';
  64. commentListHtml += ' <div class="commentUser">';
  65. commentListHtml += ' <img class="commentUserImg" src="/Public/bibidd/images/user.png" alt="">';
  66. commentListHtml += ' <p class="commentTime">' + judgeDate(Number(data.time) * 1000) + '</p>';
  67. commentListHtml += ' </div>';
  68. commentListHtml += ' <div class="commentText">' + data.content + '</div>';
  69. commentListHtml += '</div>';
  70. })
  71. $('.commentList').html(commentListHtml);
  72. }
  73. })
  74. }
  75. commentData();
  76. // 评论
  77. var commentStatus = true;
  78. $('.sendBtn').on('click', function () {
  79. let commentInputVal = $('#commentInput').val();
  80. if (uid) {
  81. if (commentInputVal && uid) {
  82. if (commentStatus) {
  83. // 添加评论内容
  84. let commentListHtml = '';
  85. commentListHtml += '<div class="commentContent">';
  86. commentListHtml += ' <div class="commentUser">';
  87. commentListHtml += ' <img class="commentUserImg" src="/Public/bibidd/images/user.png" alt="">';
  88. commentListHtml += ' <p class="commentTime">just</p>';
  89. commentListHtml += ' </div>';
  90. commentListHtml += ' <div class="commentText">' + commentInputVal + '</div>';
  91. commentListHtml += '</div>';
  92. $('.commentList').append(commentListHtml);
  93. $('#commentInput').val('');
  94. commentStatus = false;
  95. commentsNum += 1;
  96. $('.commentTitle').text('Video comments ( ' + commentsNum + ' )');
  97. // 添加评论到数据库中
  98. $.ajax({
  99. type: 'post',
  100. url: 'https://www.bibidd.com/bibidd/Mediafive/add_comments',
  101. dataType: "json",
  102. data: {
  103. type: 'video',
  104. cid: theRequest.videoid,
  105. uid: uid,
  106. username: '',
  107. content: commentInputVal
  108. },
  109. success: function (res) {
  110. },
  111. complete: function () {
  112. commentStatus = true;
  113. }
  114. })
  115. }
  116. }
  117. } else if (commentInputVal && !uid) {
  118. $('.loginBox').show();
  119. }
  120. })
  121. // 时间戳转换
  122. function formatDateTime(inputTime) {
  123. var timeStr;
  124. var date = new Date(inputTime);
  125. var y = date.getFullYear();
  126. var m = date.getMonth() + 1;
  127. m = m < 10 ? "0" + m : m;
  128. var d = date.getDate();
  129. d = d < 10 ? "0" + d : d;
  130. var h = date.getHours();
  131. h = h < 10 ? "0" + h : h;
  132. var minute = date.getMinutes();
  133. var second = date.getSeconds();
  134. minute = minute < 10 ? "0" + minute : minute;
  135. second = second < 10 ? "0" + second : second;
  136. let date1 = new Date(inputTime).getTime(); //开始时间
  137. let date2 = new Date().getTime(); //结束时间
  138. let date3 = Math.floor(date2 - date1);
  139. //计算出相差天数
  140. let days = Math.floor(date3 / (24 * 3600 * 1000));
  141. //计算出小时数
  142. let leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  143. let hours = Math.floor(leave1 / (3600 * 1000));
  144. //计算相差分钟数
  145. let leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  146. let minutes = Math.floor(leave2 / (60 * 1000));
  147. //计算相差秒数
  148. let leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  149. let seconds = Math.round(leave3 / 1000);
  150. if (days === 0 && hours === 0 && minutes <= 5) {
  151. // 超过一分钟的显示具体时间,精确到分钟,如:15分钟前。
  152. // timeStr = minutes < 10 ? "0" + minutes + '分钟前' : minutes + '分钟前';
  153. timeStr = "just";
  154. } else if (days === 0 && hours < 1 && minutes >= 5) {
  155. // 超过一分钟的显示具体时间,精确到分钟,如:15分钟前。
  156. // timeStr = hours < 10 ? "0" + hours + '小时前' : hours + '小时前';
  157. timeStr = "Within 1 hour";
  158. } else if (days === 0 && hours < 24 && hours >= 1) {
  159. // 超过一分钟的显示具体时间,精确到分钟,如:15分钟前。
  160. // timeStr = hours < 10 ? "0" + hours + '小时前' : hours + '小时前';
  161. timeStr = h + ":" + minute;
  162. }
  163. return timeStr;
  164. }
  165. Date.prototype.format = function (fmt) {
  166. var o = {
  167. "M+": this.getMonth() + 1, //月份
  168. "d+": this.getDate(), //日
  169. "h+": this.getHours(), //小时
  170. "m+": this.getMinutes(), //分
  171. "s+": this.getSeconds(), //秒
  172. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  173. "S": this.getMilliseconds() //毫秒
  174. };
  175. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  176. for (var k in o)
  177. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  178. return fmt;
  179. };
  180. function judgeDate(timestamp) { // 判断是否是今天还是昨天,在项目中用于说说时的时间显示
  181. //昨天的时间
  182. var day1 = new Date();
  183. day1.setDate(day1.getDate() - 1);
  184. var yesterday = day1.format("yyyy-MM-dd");
  185. //今天的时间
  186. var day2 = new Date();
  187. day2.setTime(day2.getTime());
  188. var today = day2.format("yyyy-MM-dd");
  189. if (timestamp) {
  190. var date = new Date(timestamp);
  191. } else {
  192. var date = new Date();
  193. }
  194. Y = date.getFullYear(),
  195. m = date.getMonth() + 1,
  196. d = date.getDate(),
  197. H = date.getHours(),
  198. i = date.getMinutes();
  199. if (m < 10) {
  200. m = '0' + m;
  201. }
  202. if (d < 10) {
  203. d = '0' + d;
  204. }
  205. if (H < 10) {
  206. H = '0' + H;
  207. }
  208. if (i < 10) {
  209. i = '0' + i;
  210. }
  211. var t = Y + '-' + m + '-' + d + ' ' + H + ':' + i;
  212. if (t.split(" ")[0] == today) {
  213. // return "今天"+t.split(" ")[1];
  214. return formatDateTime(timestamp);
  215. return H + ':' + i;
  216. } else if (t.split(" ")[0] == yesterday) {
  217. return "yesterday ";
  218. } else {
  219. return m + '-' + d;
  220. }
  221. }
  222. function tip(text) {
  223. $('#tip').show();
  224. $('.tiptext').text(text);
  225. setTimeout(() => {
  226. $('#tip').hide();
  227. }, 1000);
  228. }
  229. function videoTime(time) {
  230. let timeStr = "";
  231. let h =
  232. Math.floor(time / 60 / 60) < 10
  233. ? "0" + Math.floor(time / 60 / 60)
  234. : Math.floor(time / 60 / 60);
  235. let m =
  236. Math.floor(Math.floor(time / 60) % 60) < 10
  237. ? "0" + Math.floor(Math.floor(time / 60) % 60)
  238. : Math.floor(Math.floor(time / 60) % 60);
  239. let s =
  240. Math.floor(time % 60) < 10
  241. ? "0" + Math.floor(time % 60)
  242. : Math.floor(time % 60);
  243. timeStr = h + ":" + m + ":" + s;
  244. return timeStr;
  245. }
  246. $('.back').on('click', function () {
  247. window.location.href = "https://www.bibidd.com";
  248. })
  249. })