article.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. $(function () {
  2. $("#toc").show();
  3. var toc = $("#toc").tocify({
  4. selectors: ".ibox-content h3,.ibox-content h4,.ibox-content h5"
  5. }).data("toc-tocify");
  6. $(".tocify>.close").on("click", function(e) {
  7. $(this).parent().hide();
  8. });
  9. SyntaxHighlighter.all();
  10. SyntaxHighlighter.defaults['toolbar'] = false;
  11. layui.use('layedit', function () {
  12. layui.layedit.build('layedit', {
  13. tool: ["strong", "italic", 'link', "unlink", "face"],
  14. height: 100
  15. });
  16. });
  17. $("#code-token").on("submit", function(e) {
  18. e.preventDefault();
  19. $.post("/post/CheckViewToken", $(this).serialize(), function(data) {
  20. if (data.Success) {
  21. window.location.reload();
  22. } else {
  23. window.notie.alert({
  24. type: 3,
  25. text: data.Message,
  26. time: 4
  27. });
  28. }
  29. });
  30. });
  31. $(".getcode").on("click", function(e) {
  32. e.preventDefault();
  33. $.post("/post/getviewtoken",
  34. {
  35. __RequestVerificationToken:$("[name=__RequestVerificationToken]").val(),
  36. email:$("#email3").val()
  37. }, function(data) {
  38. if (data.Success) {
  39. window.notie.alert({
  40. type: 1,
  41. text: "验证码发送成功,请注意查收邮件,若未收到,请检查你的邮箱地址或邮件垃圾箱!",
  42. time: 4
  43. });
  44. window.localStorage.setItem("email",$("#email3").val());
  45. $(".getcode").attr('disabled',true);
  46. var count=0;
  47. var timer=setInterval(function() {
  48. count++;
  49. $(".getcode").text('重新发送('+(120-count)+')');
  50. if (count>120) {
  51. clearInterval(timer);
  52. $(".getcode").attr('disabled', false);
  53. $(".getcode").text('重新发送');
  54. }
  55. },1000);
  56. } else {
  57. window.notie.alert({
  58. type: 3,
  59. text: data.Message,
  60. time: 4
  61. });
  62. }
  63. });
  64. });
  65. var user = JSON.parse(localStorage.getItem("user"));
  66. if (user) {
  67. $("[name='NickName']").val(user.NickName||$("[name='NickName']").val());
  68. $("[name='Email']").val(user.Email||$("[name='Email']").val());
  69. $("[name='QQorWechat']").val(user.QQorWechat);
  70. }
  71. bindReplyBtn();//绑定回复按钮事件
  72. bindVote();//绑定文章投票按钮
  73. getcomments();//获取评论
  74. commentVoteBind(); //评论投票
  75. $("#OperatingSystem").val(platform.os.toString());
  76. $("#Browser").val(platform.name + " " + platform.version);
  77. //异步提交评论表单
  78. $("#comment").on("submit", function(e) {
  79. e.preventDefault();
  80. layui.layedit.sync(1);
  81. if ($("#name").val().trim().length <= 1 || $("#name").val().trim().length > 36) {
  82. window.notie.alert({
  83. type: 3,
  84. text: '再怎么你也应该留个合理的名字吧,非主流的我可不喜欢!',
  85. time: 4
  86. });
  87. return;
  88. }
  89. if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($("#email").val().trim())) {
  90. window.notie.alert({
  91. type: 3,
  92. text: '请输入正确的邮箱格式!',
  93. time: 4
  94. });
  95. return;
  96. }
  97. if($("#email").val().indexOf("163")>1||$("#email").val().indexOf("126")>1) {
  98. var _this=this;
  99. swal({
  100. title: '邮箱确认',
  101. text: "检测到您输入的邮箱是网易邮箱,本站的邮件服务器可能会因为您的反垃圾设置而无法将邮件正常发送到您的邮箱,建议使用您的其他邮箱,或者检查反垃圾设置后,再点击确定按钮继续!",
  102. type: 'warning',
  103. showCancelButton: true,
  104. confirmButtonColor: '#3085d6',
  105. cancelButtonColor: '#d33',
  106. confirmButtonText: '确定',
  107. cancelButtonText: '换个邮箱',
  108. confirmButtonClass: 'btn btn-success btn-lg',
  109. cancelButtonClass: 'btn btn-danger btn-lg',
  110. buttonsStyling: false
  111. }).then(function(isConfirm) {
  112. if (isConfirm === true) {
  113. submitComment(_this);
  114. }
  115. });
  116. return;
  117. }
  118. submitComment(this);
  119. });
  120. //表单取消按钮
  121. $(".btn-cancel").click(function() {
  122. $(':input', '#reply-form').not(':button,:submit,:reset,:hidden').val('').removeAttr('checked')
  123. .removeAttr('checked'); //评论成功清空表单
  124. layer.closeAll();
  125. setTimeout(function() {
  126. $("#reply").css("display", "none");
  127. }, 500);
  128. });
  129. //回复表单的提交
  130. $("#reply-form").on("submit", function(e) {
  131. e.preventDefault();
  132. var formData = $(this).serializeObject();
  133. layui.layedit.sync(window.currentEditor);
  134. loading();
  135. if (formData["NickName"].trim().length <= 0 ||formData["NickName"].trim().length > 36) {
  136. window.notie.alert({
  137. type: 3,
  138. text: "亲,能留个正常点的名字不!",
  139. time: 4
  140. });
  141. loadingDone();
  142. return;
  143. }
  144. if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(formData["Email"].trim())) {
  145. window.notie.alert({
  146. type: 3,
  147. text: "请输入正确的邮箱格式!",
  148. time: 4
  149. });
  150. loadingDone();
  151. return;
  152. }
  153. localStorage.setItem("user", JSON.stringify(formData));
  154. $.post("/comment/put", $(this).serialize(), (data) => {
  155. loadingDone();
  156. if (data.Success) {
  157. window.notie.alert({
  158. type: 1,
  159. text: data.Message,
  160. time: 4
  161. });
  162. layer.closeAll();
  163. setTimeout(function () {
  164. getcomments();
  165. $("[id^='LAY_layedit']").contents().find('body').html('');
  166. $("#reply").css("display", "none");
  167. }, 500);
  168. } else {
  169. window.notie.alert({
  170. type: 3,
  171. text: data.Message,
  172. time: 4
  173. });
  174. }
  175. });
  176. });
  177. $("#donate").on("click", function (e) {
  178. $.post("/system/getsetting", { name: "Donate" }, function (data) {
  179. swal({
  180. title: "支付宝扫一扫付款捐赠!",
  181. html:"<a href='/donate'>更多方式</a>",
  182. showCancelButton: true,
  183. confirmButtonColor: "#DD6B55",
  184. confirmButtonText: "确定",
  185. cancelButtonText: "取消",
  186. showLoaderOnConfirm: true,
  187. imageUrl: data.Data.Value,
  188. imageWidth: 400,
  189. animation: true,
  190. allowOutsideClick: false
  191. }).then(function() {
  192. }, function() {
  193. swal("您的捐赠将会支持本站做的更好!", null, "error");
  194. });
  195. });
  196. });
  197. });
  198. /**
  199. * 提交评论
  200. * @returns {}
  201. */
  202. function submitComment(_this) {
  203. loading();
  204. localStorage.setItem("user", JSON.stringify($(_this).serializeObject()));
  205. $.post("/comment/put", $(_this).serialize(), (data) => {
  206. loadingDone();
  207. if (data.Success) {
  208. window.notie.alert({
  209. type: 1,
  210. text: data.Message,
  211. time: 4
  212. });
  213. setTimeout(function() {
  214. getcomments();
  215. $("[id^='LAY_layedit']").contents().find('body').html('');
  216. },100);
  217. } else {
  218. window.notie.alert({
  219. type: 3,
  220. text: data.Message,
  221. time: 4
  222. });
  223. }
  224. });
  225. }
  226. //评论回复按钮事件
  227. function bindReplyBtn() {
  228. $(".msg-list article .panel-body a").on("click", function(e) {
  229. e.preventDefault();
  230. loadingDone();
  231. var user = JSON.parse(localStorage.getItem("user"));
  232. if (user) {
  233. $("[name='NickName']").val(user.NickName||$("[name='NickName']").val());
  234. $("[name='Email']").val(user.Email||$("[name='Email']").val());
  235. $("[name='QQorWechat']").val(user.QQorWechat);
  236. }
  237. var href = $(this).attr("href");
  238. var uid = href.substring(href.indexOf("uid") + 4);
  239. $("#uid").val(uid);
  240. $("#OperatingSystem2").val(platform.os.toString());
  241. $("#Browser2").val(platform.name + " " + platform.version);
  242. layui.use("layer", function() {
  243. var layer = layui.layer;
  244. layer.open({
  245. type: 1,
  246. zIndex: 20,
  247. title: '回复评论',
  248. area: (window.screen.width > 540 ? 540 : window.screen.width) + 'px',// '340px'], //宽高
  249. content: $("#reply"),
  250. end: function() {
  251. $("#reply").css("display", "none");
  252. }
  253. });
  254. });
  255. $(".layui-layer").insertBefore($(".layui-layer-shade"));
  256. window.currentEditor = layui.layedit.build('layedit2', {
  257. tool: ["strong", "italic", 'link', "unlink", "face"],
  258. height: 100
  259. });
  260. });
  261. }
  262. //绑定评论投票
  263. function commentVoteBind() {
  264. $(".cmvote").on("click", function(e) {
  265. $.post("/comment/CommentVote", {
  266. id: $(this).data("id")
  267. }, (data) => {
  268. if (data) {
  269. if (data.Success) {
  270. console.log($(this).children("span.count"));
  271. $(this).children("span.count").text(parseInt($(this).children("span.count").text())+1);
  272. $(this).addClass("disabled");
  273. this.disabled = true;
  274. window.notie.alert({
  275. type: 1,
  276. text: data.Message,
  277. time: 4
  278. });
  279. } else {
  280. window.notie.alert({
  281. type: 3,
  282. text: data.Message,
  283. time: 4
  284. });
  285. }
  286. }
  287. });
  288. });
  289. }
  290. function bindVote() {
  291. $("#voteup").on("click", function(e) {
  292. $.post("/post/voteup", {
  293. id: $("#postId").val()
  294. }, (data) => {
  295. if (data) {
  296. if (data.Success) {
  297. $(this).children()[1].innerText = parseInt($(this).children()[1].innerText) + 1;
  298. $(this).addClass("disabled");
  299. this.disabled = true;
  300. window.notie.alert({
  301. type: 1,
  302. text: data.Message,
  303. time: 4
  304. });
  305. } else {
  306. window.notie.alert({
  307. type: 3,
  308. text: data.Message,
  309. time: 4
  310. });
  311. }
  312. }
  313. });
  314. });
  315. $("#votedown").on("click", function(e) {
  316. $.post("/post/votedown", {
  317. id: $("#postId").val()
  318. }, (data) => {
  319. if (data) {
  320. if (data.Success) {
  321. $(this).children()[1].innerText = parseInt($(this).children()[1].innerText) + 1;
  322. $(this).addClass("disabled");
  323. this.disabled = true;
  324. window.notie.alert({
  325. type: 1,
  326. text: data.Message,
  327. time: 4
  328. });
  329. } else {
  330. window.notie.alert({
  331. type: 3,
  332. text: data.Message,
  333. time: 4
  334. });
  335. }
  336. }
  337. });
  338. });
  339. }
  340. //递归加载评论
  341. //加载父楼层
  342. function loadParentComments(data) {
  343. loading();
  344. var html = '';
  345. if (data) {
  346. var rows = Enumerable.From(data.rows).Where(c => c.ParentId === 0).ToArray();
  347. var page = data.page;
  348. var size = data.size;
  349. var maxPage = Math.ceil(data.total / size);
  350. page = page > maxPage ? maxPage : page;
  351. page = page < 1 ? 1 : page;
  352. var startfloor = data.parentTotal - (page - 1) * size;
  353. for (let i = 0; i < rows.length; i++) {
  354. html += `<li class="msg-list media animated fadeInRight" id='${rows[i].Id}'>
  355. <div class="media-body">
  356. <article class="panel panel-info">
  357. <header class="panel-heading">${startfloor}# ${rows[i].IsMaster ? `<i class="icon icon-user"></i>` : ""}${rows[i].NickName}${rows[i].IsMaster ? `(管理员)` : ""}
  358. <span class="pull-right" style="font-size: 10px;">${rows[i].CommentDate}<span class="hidden-sm hidden-xs"> | ${GetOperatingSystem(rows[i].OperatingSystem) + " | " + GetBrowser(rows[i].Browser)}</span></span>
  359. </header>
  360. <div class="panel-body">
  361. ${rows[i].Content}
  362. <span class="cmvote label label-info" data-id="${rows[i].Id}"><i class="icon-thumbsup"></i>(<span>${rows[i].VoteCount}</span>)</span>
  363. <a class="label label-info" href="?uid=${rows[i].Id}"><i class="icon-comment"></i></a>
  364. ${loadComments(data.rows, Enumerable.From(data.rows).Where(c => c.ParentId === rows[i].Id).OrderBy(c => c.CommentDate).ToArray(), startfloor--)}
  365. </div>
  366. </article>
  367. </div>
  368. </li>`;
  369. }
  370. }
  371. loadingDone();
  372. return html;
  373. }
  374. //加载子楼层
  375. function loadComments(data, comments, root, depth = 0) {
  376. var colors = ["info", "success", "primary", "warning", "danger"];
  377. var floor = 1;
  378. depth++;
  379. var html = '';
  380. Enumerable.From(comments).ForEach((item, index) => {
  381. var color = colors[depth%5];
  382. html += `<article id="${item.Id}" class="panel panel-${color}">
  383. <div class="panel-heading">
  384. ${depth}-${floor++}# ${item.IsMaster ?`<i class="icon icon-user"></i>`:""}${item.NickName}${item.IsMaster ?`(管理员)`:""}
  385. <span class="pull-right" style="font-size: 10px;">${item.CommentDate}<span class="hidden-sm hidden-xs"> | ${GetOperatingSystem(item.OperatingSystem) + " | " + GetBrowser(item.Browser)}</span></span>
  386. </div>
  387. <div class="panel-body">
  388. ${item.Content}
  389. <span class="cmvote label label-${color}" data-id="${item.Id}"><i class="icon-thumbsup"></i>(<span>${item.VoteCount}</span>)</span>
  390. <a class="label label-${color}" href="?uid=${item.Id}"><i class="icon-comment"></i></a>
  391. ${loadComments(data, Enumerable.From(data).Where(c => c.ParentId === item.Id).OrderBy(c => c.CommentDate), root, depth)}
  392. </div>
  393. </article>`;
  394. });
  395. return html;
  396. }