common.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. layer.config({focusBtn: false});
  33. //栏目高亮
  34. var nav = $("header.header .navbar-nav");
  35. if ($("li.active", nav).length === 0) {
  36. var current = nav.data("current");
  37. var currentNav = $("a[href='" + location.href + "']", nav)[0] || $("a[href='" + location.pathname + "']", nav)[0] || $("li[value='" + current + "'] > a", nav)[0];
  38. currentNav && $(currentNav, nav).parents("li").addClass("active");
  39. }
  40. $(".collapse.navbar-collapse.active").on("mousedown click", ".dropdown-menu .dropdown-submenu a[data-toggle='dropdown']", function (e) {
  41. e.stopPropagation();
  42. });
  43. $("header.header .navbar-right").insertBefore($("header.header .navbar-right").prev());
  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. var container = $(this).data("container");
  94. container = container ? $(container) : $(".article-list,.product-list");
  95. var loadmoreText = $(this).text();
  96. $(that).text("正在加载").prop("disabled", true);
  97. CMS.api.ajax({
  98. url: $(that).attr("href"),
  99. }, function (data, ret) {
  100. if (data) {
  101. $(data).appendTo(container);
  102. page++;
  103. $(that).attr("href", $(that).data("url").replace("__page__", page)).data("page", page);
  104. $(that).text(loadmoreText).prop("disabled", false);
  105. } else {
  106. $(that).replaceWith('<div class="loadmore loadmore-line loadmore-nodata"><span class="loadmore-tips">暂无更多数据</span></div>');
  107. }
  108. return false;
  109. }, function (data) {
  110. $(that).text(loadmoreText).prop("disabled", false);
  111. });
  112. return false;
  113. });
  114. //滚动加载更多
  115. $(window).scroll(function () {
  116. var loadmore = $(".btn-loadmore");
  117. if (loadmore.length > 0 && !loadmore.prop("disabled") && (loadmore.data("autoload") === undefined || loadmore.data("autoload"))) {
  118. if ($(window).scrollTop() - loadmore.height() > loadmore.offset().top - $(window).height()) {
  119. loadmore.trigger("click");
  120. }
  121. }
  122. });
  123. setTimeout(function () {
  124. if ($(window).scrollTop() > 0) {
  125. $(window).trigger("scroll");
  126. }
  127. }, 500);
  128. //评论列表
  129. if ($("#comment-container").length > 0) {
  130. var ci, si;
  131. $("#commentlist dl dd div,#commentlist dl dd dl dd").on({
  132. mouseenter: function () {
  133. clearTimeout(ci);
  134. var _this = this;
  135. ci = setTimeout(function () {
  136. $(_this).find("small:first").find("a").stop(true, true).css('display', 'inline-block');
  137. }, 100);
  138. },
  139. mouseleave: function () {
  140. clearTimeout(ci);
  141. $(this).find("small:first").find("a").stop(true, true).fadeOut();
  142. }
  143. });
  144. $(".reply").on("click", function () {
  145. $("#pid").val($(this).data("id"));
  146. $(this).parent().parent().append($("div#postcomment").detach());
  147. $("#postcomment h3 a").show();
  148. $("#commentcontent").focus().val($(this).attr("title"));
  149. });
  150. $("#postcomment h3 a").bind("click", function () {
  151. $("#comment-container").append($("div#postcomment").detach());
  152. $(this).hide();
  153. });
  154. $(".expandall a").on("click", function () {
  155. $(this).parent().parent().find("dl.hide").fadeIn();
  156. $(this).fadeOut();
  157. });
  158. $(document).on("click", "#submit", function () {
  159. var btn = $(this);
  160. var tips = $("#actiontips");
  161. tips.removeClass();
  162. var content = $("#commentcontent").val();
  163. if (len(content) < 3) {
  164. tips.addClass("text-danger").html("评论内容长度不正确!最少3个字符").fadeIn().change();
  165. return false;
  166. }
  167. if (btn.prop("disabled")) {
  168. return false;
  169. }
  170. var form = $("#postform");
  171. btn.attr("disabled", "disabled");
  172. tips.html('正在提交...');
  173. $.ajax({
  174. url: form.prop("action"),
  175. type: 'POST',
  176. data: form.serialize(),
  177. dataType: 'json',
  178. success: function (json) {
  179. btn.removeAttr("disabled");
  180. if (json.code == 1) {
  181. $("#pid").val(0);
  182. tips.addClass("text-success").html(json.msg || "评论成功!").fadeIn(300).change();
  183. $("#commentcontent").val('');
  184. $("#commentcount").text(parseInt($("#commentcount").text()) + 1);
  185. setTimeout(function () {
  186. location.reload();
  187. }, 1500);
  188. } else {
  189. tips.addClass("text-danger").html(json.msg).fadeIn();
  190. }
  191. if (json.data && json.data.token) {
  192. $("#postform input[name='__token__']").val(json.data.token);
  193. }
  194. },
  195. error: function () {
  196. btn.removeAttr("disabled");
  197. tips.addClass("text-danger").html("评论失败!请刷新页面重试!").fadeIn();
  198. }
  199. });
  200. return false;
  201. });
  202. $("#commentcontent").on("keydown", function (e) {
  203. if ((e.metaKey || e.ctrlKey) && (e.keyCode == 13 || e.keyCode == 10)) {
  204. $("#submit").trigger('click');
  205. return false;
  206. }
  207. });
  208. $("#actiontips").on("change", function () {
  209. clearTimeout(si);
  210. si = setTimeout(function () {
  211. $("#actiontips").fadeOut();
  212. }, 8000);
  213. });
  214. $(document).on("keyup change", "#commentcontent", function (e) {
  215. if (e.metaKey || e.ctrlKey || [13, 10, 18, 91].indexOf(e.keyCode) > -1) {
  216. return false;
  217. }
  218. var max = 1000;
  219. var c = $(this).val();
  220. var length = len(c);
  221. var t = $("#actiontips");
  222. if (max >= length) {
  223. t.removeClass().show().addClass("loading").html("你还可以输入 <font color=green>" + (Math.floor((max - length) / 2)) + "</font> 字");
  224. $("#submit").removeAttr("disabled");
  225. } else {
  226. t.removeClass().show().addClass("loading").html("你已经超出 <font color=red>" + (Math.ceil((length - max) / 2)) + "</font> 字");
  227. $("#submit").attr("disabled", "disabled");
  228. }
  229. });
  230. }
  231. // 余额支付提示
  232. $(document).on('click', '.btn-balance', function (e) {
  233. var that = this;
  234. layer.confirm("确认支付¥" + $(this).data("price") + "元用于购买?", function () {
  235. CMS.api.ajax({
  236. url: $(that).attr("href")
  237. }, function (data, ret) {
  238. CMS.api.msg(ret.msg, ret.url);
  239. }, function (data, ret) {
  240. if (ret.msg && ret.msg.indexOf("登录") > -1) {
  241. layer.alert(ret.msg, {btn: ["去登录", "暂不登录"]}, function () {
  242. location.href = ret.url;
  243. });
  244. return false;
  245. }
  246. });
  247. });
  248. return false;
  249. });
  250. // 回到顶部
  251. $('#back-to-top').on('click', function (e) {
  252. e.preventDefault();
  253. $('html,body').animate({
  254. scrollTop: 0
  255. }, 700);
  256. });
  257. //如果是PC则移除navbar的dropdown点击事件
  258. if (!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobi/i.test(navigator.userAgent)) {
  259. $("#navbar-collapse [data-toggle='dropdown']").removeAttr("data-toggle");
  260. } else {
  261. $(".navbar-nav ul li:not(.dropdown-submenu):not(.dropdown) a").removeAttr("data-toggle");
  262. }
  263. if (!isMobile) {
  264. var search = $("#searchinput");
  265. var form = search.closest("form");
  266. search.autoComplete({
  267. minChars: 1,
  268. cache: false,
  269. menuClass: 'autocomplete-searchmenu',
  270. header: '',
  271. footer: '',
  272. source: function (term, response) {
  273. try {
  274. xhr.abort();
  275. } catch (e) {
  276. }
  277. xhr = $.getJSON(search.data("suggestion-url"), {q: term}, function (data) {
  278. response(data);
  279. });
  280. },
  281. onSelect: function (e, term, item) {
  282. if (typeof callback === 'function') {
  283. callback.call(elem, term, item);
  284. } else {
  285. form.trigger("submit");
  286. }
  287. }
  288. });
  289. }
  290. // 手机端左右滑动切换菜单栏
  291. if (isMobile && 'ontouchstart' in document.documentElement) {
  292. var startX, startY, moveEndX, moveEndY, relativeX, relativeY, element;
  293. element = $('#navbar-collapse');
  294. $("body").on("touchstart", function (e) {
  295. startX = e.originalEvent.changedTouches[0].pageX;
  296. startY = e.originalEvent.changedTouches[0].pageY;
  297. });
  298. $("body").on("touchend", function (e) {
  299. moveEndX = e.originalEvent.changedTouches[0].pageX;
  300. moveEndY = e.originalEvent.changedTouches[0].pageY;
  301. relativeX = moveEndX - startX;
  302. relativeY = moveEndY - startY;
  303. //右滑
  304. if (relativeX > 45) {
  305. if ((Math.abs(relativeX) - Math.abs(relativeY)) > 50 && !element.hasClass("active") && startX < ($(window).width() / 4)) {
  306. $(".sidebar-toggle").trigger("click");
  307. }
  308. }
  309. //左滑
  310. else if (relativeX < -45) {
  311. if ((Math.abs(relativeX) - Math.abs(relativeY)) > 50 && element.hasClass("active")) {
  312. $(".sidebar-toggle").trigger("click");
  313. }
  314. }
  315. });
  316. }
  317. // 打赏
  318. $(".btn-donate").popover({
  319. trigger: 'hover',
  320. placement: 'top',
  321. html: true,
  322. content: function () {
  323. return "<img src='" + $(this).data("image") + "' width='250' height='250'/>";
  324. }
  325. });
  326. $(document).on("click", ".btn-paynow", function () {
  327. var paytype = $(this).data("paytype");
  328. var price = $(this).data("price");
  329. var nameArr = {wechat: "微信", alipay: "支付宝", balance: "余额"};
  330. var that = this;
  331. var tips = function () {
  332. layer.confirm("请根据支付状态选择下面的操作按钮", {title: "温馨提示", icon: 0, btn: ["支付成功", "支付失败"]}, function () {
  333. location.reload();
  334. });
  335. };
  336. if (paytype) {
  337. layer.confirm("确认使用" + (typeof nameArr[paytype] !== 'undefined' ? nameArr[paytype] : "未知") + "进行支付?<br>支付金额:¥" + price + "元", {title: "温馨提示", icon: 3, focusBtn: false, btn: ["立即支付", "取消支付"]}, function (index, layero) {
  338. $(".layui-layer-btn0", layero).attr("href", $(that).attr("href")).attr("target", "_blank");
  339. tips();
  340. });
  341. return false;
  342. } else {
  343. tips();
  344. }
  345. });
  346. //点击切换
  347. $(document).on("click", ".sidebar-toggle", function () {
  348. var collapse = $("#navbar-collapse");
  349. if (collapse.hasClass("active")) {
  350. $(".navbar-collapse-bg").remove();
  351. } else {
  352. $("<div />").addClass("navbar-collapse-bg").insertAfter(collapse).on("click", function () {
  353. $(".sidebar-toggle").trigger("click");
  354. });
  355. }
  356. collapse.toggleClass("active");
  357. $(this).toggleClass("active");
  358. });
  359. //内容中的图片点击事件
  360. $(document).on("click", ".article-text img", function () {
  361. if ($(this).closest("a").length > 0) {
  362. return;
  363. }
  364. var that = this;
  365. var data = [];
  366. var index = 0;
  367. $(".article-text img").each(function (i, j) {
  368. if (that == this) {
  369. index = i;
  370. }
  371. data.push({
  372. "src": $(this).attr("src") //原图地址
  373. });
  374. });
  375. layer.photos({
  376. photos: {
  377. "start": index, "data": data
  378. },
  379. // scrollbar: true,
  380. // full: true,
  381. // closeBtn: 1
  382. });
  383. return false;
  384. });
  385. //分享参数配置
  386. var shareConfig = {
  387. title: $("meta[property='og:title']").attr("content") || document.title,
  388. description: $("meta[property='og:description']").attr("content") || $("meta[name='description']").attr("content") || "",
  389. url: $("meta[property='og:url']").attr("content") || location.href,
  390. image: $("meta[property='og:image']").attr("content") || ""
  391. };
  392. //微信公众号内分享
  393. if (typeof wx != 'undefined') {
  394. shareConfig.url = location.href;
  395. CMS.api.ajax({
  396. url: "/addons/cms/ajax/share",
  397. data: {url: shareConfig.url},
  398. loading: false
  399. }, function (data, ret) {
  400. try {
  401. wx.config({
  402. appId: data.appId,
  403. timestamp: data.timestamp,
  404. nonceStr: data.nonceStr,
  405. signature: data.signature,
  406. jsApiList: [
  407. 'checkJsApi',
  408. 'updateAppMessageShareData',
  409. 'updateTimelineShareData',
  410. ]
  411. });
  412. var shareData = {
  413. title: shareConfig.title,
  414. desc: shareConfig.description,
  415. link: shareConfig.url,
  416. imgUrl: shareConfig.image,
  417. success: function () {
  418. layer.closeAll();
  419. },
  420. cancel: function () {
  421. layer.closeAll();
  422. }
  423. };
  424. wx.ready(function () {
  425. wx.updateAppMessageShareData(shareData);
  426. wx.updateTimelineShareData(shareData);
  427. });
  428. } catch (e) {
  429. console.log(e);
  430. }
  431. return false;
  432. }, function () {
  433. return false;
  434. }
  435. );
  436. $(".social-share").on("click", ".icon-wechat", function () {
  437. layer.msg("请点击右上角的●●●进行分享");
  438. return false;
  439. }).find(".wechat-qrcode").remove();
  440. }
  441. });