common.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. $(function () {
  2. window.isMobile = !!("ontouchstart" in window);
  3. function AddFavorite(sURL, sTitle) {
  4. if (/firefox/i.test(navigator.userAgent)) {
  5. return false;
  6. } else if (window.external && window.external.addFavorite) {
  7. window.external.addFavorite(sURL, sTitle);
  8. return true;
  9. } else if (window.sidebar && window.sidebar.addPanel) {
  10. window.sidebar.addPanel(sTitle, sURL, "");
  11. return true;
  12. } else {
  13. var touch = (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Command' : 'CTRL');
  14. layer.msg('请使用 ' + touch + ' + D 添加到收藏夹.');
  15. return false;
  16. }
  17. }
  18. var len = function (str) {
  19. if (!str)
  20. return 0;
  21. var length = 0;
  22. for (var i = 0; i < str.length; i++) {
  23. if (str.charCodeAt(i) >= 0x4e00 && str.charCodeAt(i) <= 0x9fa5) {
  24. length += 2;
  25. } else {
  26. length++;
  27. }
  28. }
  29. return length;
  30. };
  31. //new LazyLoad({elements_selector: ".lazy"});
  32. //栏目高亮
  33. var nav = $("header.header .navbar-nav");
  34. if ($("li.active", nav).size() === 0) {
  35. var current = nav.data("current");
  36. var currentNav = $("a[href='" + location.href + "']", nav)[0] || $("a[href='" + location.pathname + "']", nav)[0] || $("li[value='" + current + "'] > a", nav)[0];
  37. currentNav && $(currentNav, nav).parents("li").addClass("active");
  38. }
  39. //移动端菜单点击
  40. $(document).on("click", ".navbar-collapse.collapse.in .navbar-nav .dropdown-submenu > a", function () {
  41. $(this).parents("li.dropdown").addClass("open");
  42. return false;
  43. });
  44. //移动浏览器左右滑动
  45. $(document).on('touchstart', '.carousel', function (event) {
  46. const xClick = event.originalEvent.touches[0].pageX;
  47. $(this).one('touchmove', function (event) {
  48. const xMove = event.originalEvent.touches[0].pageX;
  49. const sensitivityInPx = 5;
  50. if (Math.floor(xClick - xMove) > sensitivityInPx) {
  51. $(this).carousel('next');
  52. } else if (Math.floor(xClick - xMove) < -sensitivityInPx) {
  53. $(this).carousel('prev');
  54. }
  55. });
  56. $(this).on('touchend', function () {
  57. $(this).off('touchmove');
  58. });
  59. });
  60. // 点击收藏
  61. $(".addbookbark").attr("rel", "sidebar").click(function () {
  62. //使用数据库收藏
  63. CMS.api.ajax({
  64. url: $(this).data("action"),
  65. data: {type: $(this).data("type"), aid: $(this).data("aid")}
  66. });
  67. //使用浏览器收藏
  68. //return !AddFavorite(window.location.href, $(this).attr("title"));
  69. });
  70. // 点赞
  71. $(document).on("click", ".btn-like", function () {
  72. var that = this;
  73. var id = $(this).data("id");
  74. var type = $(this).data("type");
  75. if (CMS.api.storage(type + "vote." + id)) {
  76. layer.msg("你已经点过赞了");
  77. return false;
  78. }
  79. CMS.api.ajax({
  80. data: $(this).data()
  81. }, function (data, ret) {
  82. $("span", that).text(type === 'like' ? ret.data.likes : ret.data.dislikes);
  83. CMS.api.storage(type + "vote." + id, true);
  84. return false;
  85. }, function () {
  86. return false;
  87. });
  88. });
  89. // 加载更多
  90. $(document).on("click", ".btn-loadmore", function () {
  91. var that = this;
  92. var page = parseInt($(this).data("page"));
  93. page++;
  94. $(that).prop("disabled", true);
  95. CMS.api.ajax({
  96. url: $(that).attr("href"),
  97. }, function (data, ret) {
  98. $(data).insertBefore($(that).parent());
  99. $(that).remove();
  100. return false;
  101. }, function (data) {
  102. });
  103. return false;
  104. });
  105. //滚动加载更多
  106. $(window).scroll(function () {
  107. var loadmore = $(".btn-loadmore");
  108. if (loadmore.size() > 0 && !loadmore.prop("disabled") && (loadmore.data("autoload") === undefined || loadmore.data("autoload"))) {
  109. if ($(window).scrollTop() - loadmore.height() > loadmore.offset().top - $(window).height()) {
  110. loadmore.trigger("click");
  111. }
  112. }
  113. });
  114. setTimeout(function () {
  115. if ($(window).scrollTop() > 0) {
  116. $(window).trigger("scroll");
  117. }
  118. }, 500);
  119. //评论列表
  120. if ($("#comment-container").size() > 0) {
  121. var ci, si;
  122. $("#commentlist dl dd div,#commentlist dl dd dl dd").on({
  123. mouseenter: function () {
  124. clearTimeout(ci);
  125. var _this = this;
  126. ci = setTimeout(function () {
  127. $(_this).find("small:first").find("a").stop(true, true).fadeIn();
  128. }, 100);
  129. },
  130. mouseleave: function () {
  131. clearTimeout(ci);
  132. $(this).find("small:first").find("a").stop(true, true).fadeOut();
  133. }
  134. });
  135. $(".reply").on("click", function () {
  136. $("#pid").val($(this).data("id"));
  137. $(this).parent().parent().append($("div#postcomment").detach());
  138. $("#postcomment h3 a").show();
  139. $("#commentcontent").focus().val($(this).attr("title"));
  140. });
  141. $("#postcomment h3 a").bind("click", function () {
  142. $("#comment-container").append($("div#postcomment").detach());
  143. $(this).hide();
  144. });
  145. $(".expandall a").on("click", function () {
  146. $(this).parent().parent().find("dl.hide").fadeIn();
  147. $(this).fadeOut();
  148. });
  149. $(document).on("click", "#submit", function () {
  150. var btn = $(this);
  151. var tips = $("#actiontips");
  152. tips.removeClass();
  153. var content = $("#commentcontent").val();
  154. if (len(content) < 3) {
  155. tips.addClass("text-danger").html("评论内容长度不正确!最少3个字符").fadeIn().change();
  156. return false;
  157. }
  158. var form = $("#postform");
  159. btn.attr("disabled", "disabled");
  160. tips.html('正在提交...');
  161. $.ajax({
  162. url: form.prop("action"),
  163. type: 'POST',
  164. data: form.serialize(),
  165. dataType: 'json',
  166. success: function (json) {
  167. btn.removeAttr("disabled");
  168. if (json.code == 1) {
  169. $("#pid").val(0);
  170. tips.addClass("text-success").html(json.msg || "评论成功!").fadeIn(300).change();
  171. $("#commentcontent").val('');
  172. $("#commentcount").text(parseInt($("#commentcount").text()) + 1);
  173. setTimeout(function () {
  174. location.reload();
  175. }, 1000);
  176. } else {
  177. tips.addClass("text-danger").html(json.msg).fadeIn().change();
  178. }
  179. if (json.data && json.data.token) {
  180. $("#postform input[name='__token__']").val(json.data.token);
  181. }
  182. },
  183. error: function () {
  184. btn.removeAttr("disabled");
  185. tips.addClass("text-danger").html("评论失败!请刷新页面重试!").fadeIn();
  186. }
  187. });
  188. return false;
  189. });
  190. $("#commentcontent").on("keydown", function (e) {
  191. if ((e.metaKey || e.ctrlKey) && (e.keyCode == 13 || e.keyCode == 10)) {
  192. $("#submit").trigger('click');
  193. }
  194. });
  195. $("#actiontips").on("change", function () {
  196. clearTimeout(si);
  197. si = setTimeout(function () {
  198. $("#actiontips").fadeOut();
  199. }, 8000);
  200. });
  201. $(document).on("keyup change", "#commentcontent", function () {
  202. var max = 1000;
  203. var c = $(this).val();
  204. var length = len(c);
  205. var t = $("#actiontips");
  206. if (max >= length) {
  207. t.removeClass().show().addClass("loading").html("你还可以输入 <font color=green>" + (Math.floor((max - length) / 2)) + "</font> 字");
  208. $("#submit").removeAttr("disabled");
  209. } else {
  210. t.removeClass().show().addClass("loading").html("你已经超出 <font color=red>" + (Math.ceil((length - max) / 2)) + "</font> 字");
  211. $("#submit").attr("disabled", "disabled");
  212. }
  213. });
  214. }
  215. // 余额支付提示
  216. $(document).on('click', '.btn-balance', function (e) {
  217. var that = this;
  218. layer.confirm("确认支付¥" + $(this).data("price") + "元用于购买?", function () {
  219. CMS.api.ajax({
  220. url: $(that).attr("href")
  221. }, function (data, ret) {
  222. CMS.api.msg(ret.msg, ret.url);
  223. });
  224. });
  225. return false;
  226. });
  227. // 回到顶部
  228. $('#back-to-top').on('click', function (e) {
  229. e.preventDefault();
  230. $('html,body').animate({
  231. scrollTop: 0
  232. }, 700);
  233. });
  234. //如果是PC则移除navbar的dropdown点击事件
  235. if (!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobi/i.test(navigator.userAgent)) {
  236. $("#navbar-collapse [data-toggle='dropdown']").removeAttr("data-toggle");
  237. } else {
  238. $(".navbar-nav ul li:not(.dropdown-submenu):not(.dropdown) a").removeAttr("data-toggle");
  239. }
  240. if (!isMobile) {
  241. var search = $("#searchinput");
  242. var form = search.closest("form");
  243. search.autoComplete({
  244. minChars: 1,
  245. cache: false,
  246. menuClass: 'autocomplete-searchmenu',
  247. header: '',
  248. footer: '',
  249. source: function (term, response) {
  250. try {
  251. xhr.abort();
  252. } catch (e) {
  253. }
  254. xhr = $.getJSON(search.data("suggestion-url"), {q: term}, function (data) {
  255. response(data);
  256. });
  257. },
  258. onSelect: function (e, term, item) {
  259. if (typeof callback === 'function') {
  260. callback.call(elem, term, item);
  261. } else {
  262. form.trigger("submit");
  263. }
  264. }
  265. });
  266. }
  267. // 打赏
  268. $(".btn-donate").popover({
  269. trigger: 'hover',
  270. placement: 'top',
  271. html: true,
  272. content: function () {
  273. return "<img src='" + $(this).data("image") + "' width='250' height='250'/>";
  274. }
  275. });
  276. $(document).on("click", ".btn-paynow", function () {
  277. var paytype = $(this).data("paytype");
  278. var price = $(this).data("price");
  279. var nameArr = {wechat: "微信", alipay: "支付宝", balance: "余额"};
  280. var that = this;
  281. var tips = function () {
  282. layer.confirm("请根据支付状态选择下面的操作按钮", {title: "温馨提示", icon: 0, btn: ["支付成功", "支付失败"]}, function () {
  283. location.reload();
  284. });
  285. };
  286. if (paytype) {
  287. layer.confirm("确认使用" + (typeof nameArr[paytype] !== 'undefined' ? nameArr[paytype] : "未知") + "进行支付?<br>支付金额:¥" + price + "元", {title: "温馨提示", icon: 3, focusBtn: false, btn: ["立即支付", "取消支付"]}, function (index, layero) {
  288. $(".layui-layer-btn0", layero).attr("href", $(that).attr("href")).attr("target", "_blank");
  289. tips();
  290. });
  291. return false;
  292. } else {
  293. tips();
  294. }
  295. });
  296. });