common.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. $(function () {
  2. window.isMobile = !!("ontouchstart" in window);
  3. //new LazyLoad({elements_selector: ".lazy"});
  4. if (!isMobile) {
  5. // 搜索框
  6. $("input[name='search']").on("focus", function () {
  7. $(this).closest(".form-search").addClass("focused");
  8. }).on("blur", function (e) {
  9. var that = this;
  10. setTimeout(function () {
  11. $(that).closest(".form-search").removeClass("focused");
  12. }, 500);
  13. });
  14. }
  15. // 点击收藏
  16. $(".addbookbark").attr("rel", "sidebar").click(function () {
  17. var url = location.href;
  18. var title = $(this).attr("title") || document.title;
  19. if (/firefox/i.test(navigator.userAgent)) {
  20. } else if (window.external && window.external.addFavorite) {
  21. window.external.addFavorite(url, title);
  22. } else if (window.sidebar && window.sidebar.addPanel) {
  23. window.sidebar.addPanel(title, url, "");
  24. } else {
  25. var touch = (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Command' : 'CTRL');
  26. layer.msg('请使用 ' + touch + ' + D 添加到收藏夹.');
  27. }
  28. return false;
  29. });
  30. // 点击收藏
  31. $(document).on('click', '.btn-collect', function (e) {
  32. var that = this;
  33. SHOP.api.ajax({
  34. url: "/addons/shop/ajax/collect",
  35. data: {goods_id: $(this).data("id")}
  36. }, function (data, ret) {
  37. $("span", that).text("已收藏");
  38. layer.msg(ret.msg);
  39. return false;
  40. });
  41. });
  42. if (typeof wx !== 'undefined') {
  43. //分享参数配置
  44. var shareConfig = {
  45. title: $("meta[property='og:title']").attr("content") || document.title,
  46. description: $("meta[property='og:description']").attr("content") || "",
  47. url: $("meta[property='og:url']").attr("content") || location.href,
  48. image: $("meta[property='og:image']").attr("content") || ""
  49. };
  50. //微信公众号内分享
  51. shareConfig.url = location.href;
  52. SHOP.api.ajax({
  53. url: "/addons/shop/ajax/share",
  54. data: {url: shareConfig.url},
  55. loading: false
  56. }, function (data, ret) {
  57. try {
  58. wx.config({
  59. appId: data.appId,
  60. timestamp: data.timestamp,
  61. nonceStr: data.nonceStr,
  62. signature: data.signature,
  63. jsApiList: [
  64. 'checkJsApi',
  65. 'updateAppMessageShareData',
  66. 'updateTimelineShareData',
  67. ]
  68. });
  69. var shareData = {
  70. title: shareConfig.title,
  71. desc: shareConfig.description,
  72. link: shareConfig.url,
  73. imgUrl: shareConfig.image,
  74. success: function () {
  75. layer.closeAll();
  76. },
  77. cancel: function () {
  78. layer.closeAll();
  79. }
  80. };
  81. wx.ready(function () {
  82. wx.updateAppMessageShareData(shareData);
  83. wx.updateTimelineShareData(shareData);
  84. });
  85. } catch (e) {
  86. console.log(e);
  87. }
  88. return false;
  89. }, function () {
  90. return false;
  91. }
  92. );
  93. }
  94. // 点击分享
  95. $(document).on('click', '.btn-share', function (e) {
  96. var that = this;
  97. var data = $(that).data();
  98. if (typeof wx != 'undefined') {
  99. layer.msg("请点击右上角的●●●进行分享");
  100. } else {
  101. layer.open({
  102. title: '分享',
  103. content: '<div class="social-share text-center mt-2 mb-1"><div class="share-qrcode"></div><p class="small">请使用微信扫一扫进行分享</p></div>',
  104. btn: false,
  105. success: function (layero) {
  106. $(".share-qrcode").qrcode({text: $(that).data("url") || location.href});
  107. $('.social-share', layero).data(data).share({sites: 'qzone,qq,weibo,douban'});
  108. }
  109. });
  110. }
  111. return false;
  112. });
  113. //领取优惠券
  114. $(document).on('click', '.btn-coupon', function () {
  115. let id = $(this).data('name');
  116. SHOP.api.ajax({
  117. url: "/addons/shop/coupon/drawCoupon",
  118. data: {id: id}
  119. }, function (data, ret) {
  120. setTimeout(function () {
  121. window.location.reload();
  122. }, 1500);
  123. }, function (data, ret) {
  124. if (ret.msg.indexOf("请登录") > -1) {
  125. layer.alert("请登录后再进行操作", {
  126. title: "温馨提示", icon: 0, btn: ["去登录"]
  127. }, function () {
  128. location.href = ret.url;
  129. });
  130. return false;
  131. }
  132. });
  133. });
  134. // 倒计时
  135. $('[data-countdown]').each(function () {
  136. var that = this;
  137. var $this = $(this), finalDate = parseInt($(this).data('countdown'));
  138. if (finalDate > 0) {
  139. finalDate = isNaN(finalDate) ? finalDate : new Date().getTime() + finalDate * 1000;
  140. var format = $(that).data("format");
  141. $this.on('finish.countdown', function () {
  142. if (typeof $this.data("finish") == 'function') {
  143. $this.data("finish").call($this);
  144. } else {
  145. location.reload();
  146. }
  147. }).countdown(finalDate, function (event) {
  148. $this.html(event.strftime(format) || event.strftime('%D天%H时%M分%S秒'));
  149. });
  150. }
  151. });
  152. var backtotop = $('#back-to-top');
  153. $(window).scroll(function () {
  154. if ($(window).scrollTop() >= 200) {
  155. backtotop.fadeIn();
  156. } else {
  157. backtotop.fadeOut();
  158. }
  159. });
  160. $(window).trigger("scroll");
  161. // 回到顶部
  162. backtotop.on('click', function (e) {
  163. e.preventDefault();
  164. $('html,body').animate({
  165. scrollTop: 0
  166. }, 700);
  167. });
  168. // 如果是PC则移除navbar的dropdown点击事件
  169. if (!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobi/i.test(navigator.userAgent)) {
  170. $(".nav-bar [data-toggle='dropdown']").removeAttr("data-toggle");
  171. } else {
  172. $(".navbar-nav ul li:not(.dropdown-submenu):not(.dropdown) a").removeAttr("data-toggle");
  173. }
  174. $.fn.typeahead.Constructor.prototype.click = function (e) {
  175. };
  176. if (!isMobile) {
  177. // 搜索自动完成
  178. $("#searchinput").typeahead({
  179. onSelect: function (item) {
  180. location.href = item.value.url;
  181. },
  182. grepper: function (data) {
  183. return data;
  184. },
  185. render: function (items) {
  186. var that = this;
  187. items = $(items).map(function (i, item) {
  188. var i = $(that.options.item);
  189. i.data("value", item);
  190. i.find('a').attr('href', item.url);
  191. i.find('a').html('<h5>' + item.title + '</h5>');
  192. return i[0];
  193. });
  194. items.first().addClass('active');
  195. that.$menu.css("width", "250px");
  196. that.$menu.html(items);
  197. return that;
  198. },
  199. alignWidth: false,
  200. ajax: {
  201. url: $("#searchinput").data("typeahead-url"),
  202. valueField: "url",
  203. method: "post",
  204. dataType: "JSON",
  205. preDispatch: function (query) {
  206. return {
  207. search: query
  208. };
  209. },
  210. preProcess: function (data) {
  211. return data;
  212. }
  213. }
  214. });
  215. }
  216. // 点击支付
  217. $(document).on("click", ".btn-paynow", function () {
  218. layer.confirm("请根据支付状态选择下面的操作按钮", {title: "温馨提示", icon: 0, btn: ["支付成功", "支付失败"]}, function () {
  219. location.reload();
  220. });
  221. });
  222. // 导航事件
  223. var st;
  224. $(document).on("mouseenter", "li.category,.nav-item-topmenu", function () {
  225. clearTimeout(st);
  226. $(".top-menu").addClass("open");
  227. });
  228. $(document).on("mouseleave", "li.category,.nav-item-topmenu", function () {
  229. st = setTimeout(function () {
  230. if (!$(".top-menu").hasClass("opened")) {
  231. $(".top-menu").removeClass("open");
  232. }
  233. }, 150);
  234. });
  235. //顶部菜单
  236. $(document).on("mouseenter mouseleave", ".top-menu", function (e) {
  237. if (e.type === 'mouseenter') {
  238. $(this).addClass("opened");
  239. } else {
  240. $(this).removeClass("open opened");
  241. }
  242. });
  243. // 顶部浮动导航
  244. var headerAction = $(".header-menu");
  245. if (headerAction.length > 0 && $(window).width() > 1200) {
  246. var actionOffset = headerAction.offset().top;
  247. var inNav = false;
  248. var hasBanner = headerAction.hasClass("hasbanner");
  249. var check_action = function () {
  250. if ($(window).scrollTop() >= actionOffset) {
  251. headerAction.addClass("fixed");
  252. if (hasBanner) {
  253. headerAction.removeClass("hasbanner");
  254. }
  255. inNav = true;
  256. } else {
  257. headerAction.removeClass("fixed");
  258. if (hasBanner) {
  259. headerAction.addClass("hasbanner");
  260. }
  261. inNav = false;
  262. }
  263. };
  264. $(window).scroll(function () {
  265. check_action();
  266. });
  267. check_action();
  268. }
  269. if (headerAction.length > 0) {
  270. $("a[data-toggle='dropdown']", headerAction).on("click", function () {
  271. $(this).next("ul").find("li:first-child a.btn-download").trigger("click");
  272. });
  273. }
  274. });