shopping-cart.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">
  6. <meta name="apple-mobile-web-app-capable" content="yes">
  7. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  8. <title>购物车</title>
  9. <link rel="stylesheet" type="text/css" href="css/mui.min.css" />
  10. <link rel="stylesheet" type="text/css" href="css/iconfont.css" />
  11. <link rel="stylesheet" type="text/css" href="css/main.css" />
  12. <script type="text/javascript">
  13. // 屏蔽分享
  14. window.hiddenAllWechatMenu = true;
  15. </script>
  16. </head>
  17. <body>
  18. <!--底部菜单开始-->
  19. <footer class="mui-bar mui-bar-tab" id="footer-bar-tab" style="display: none;">
  20. <a class="mui-tab-item">
  21. <span class="iconfont icon-shouye"></span>
  22. <span class="mui-tab-label">首页</span>
  23. </a>
  24. <a class="mui-tab-item">
  25. <span class="iconfont icon-liebiao"></span>
  26. <span class="mui-tab-label">列表</span>
  27. </a>
  28. <a class="mui-tab-item mui-active">
  29. <span class="iconfont icon-gouwuche"></span>
  30. <span class="mui-tab-label">购物车</span>
  31. </a>
  32. <a class="mui-tab-item">
  33. <span class="iconfont icon-huiyuan"></span>
  34. <span class="mui-tab-label">会员</span>
  35. </a>
  36. </footer>
  37. <!--底部菜单结束-->
  38. <div class="mui-content">
  39. <div class="gwc_null">
  40. <img width="130px" src="images/cart-null.png">
  41. <h4 style="margin: 10px 0 20px 0;">购物车还是空的</h4>
  42. <a href="javascript:;" class="mui-btn my-btn-danger go_web_shop" style="width: 150px;padding: 10px 10px;color: #666;">立即选购</a>
  43. </div>
  44. <div class="shopping-box">
  45. <div class="shoping-tt">
  46. <ul class="mui-table-view my-grid-view-no shopping-cart-list">
  47. <!--购物车列表-->
  48. </ul>
  49. </div>
  50. <div class="buy-footer1">
  51. <table class="tb-clearing">
  52. <tr>
  53. <td class="quanxuan">
  54. <input type="checkbox" name="checkbox" class="all_check" /> 全选
  55. </td>
  56. <td class="mui-text-center">合计:<span class="count_price_box">¥<span class="count_price">0.0</span></span>元</td>
  57. <td>
  58. <a id="go-clearing" class="mui-btn mui-pull-right add_gwc1 go-clearing">结算(0)</a>
  59. </td>
  60. </tr>
  61. </table>
  62. </div>
  63. </div>
  64. </div>
  65. <!-- 加载ing begin-->
  66. <div class="loading covers">
  67. <div class="loading-bj"></div>
  68. <p>正在加载...</p>
  69. </div>
  70. <!-- 加载ing end -->
  71. <script src="js/mui.min.js"></script>
  72. <script src="js/jquery-2.1.1.min.js"></script>
  73. <script src="js/main.js"></script>
  74. <script src="js/wechat-utils-1.0.js"></script>
  75. <script>
  76. window.addEventListener('pageshow', function(e) {
  77. // 通过persisted属性判断是否存在 BF Cache
  78. if(e.persisted) {
  79. location.reload();
  80. }
  81. });
  82. mui.ready(function() {
  83. $.ajax(base_path + '/wechat/cart/selectCartByOpenId?dates=' + new Date().getTime(), {
  84. data: {},
  85. dataType: 'json',
  86. xhrFields: {
  87. withCredentials: true
  88. },
  89. crossDomain: true,
  90. type: 'post',
  91. timeout: 15000,
  92. success: function(dt) {
  93. if(dt.isRedirect) {
  94. location.href = dt.redirectURL;
  95. } else {
  96. if(dt.status) {
  97. console.log(dt);
  98. var table = document.body.querySelector('.shopping-cart-list');
  99. if(dt.data.cartList.length==0){
  100. $(".shopping-box,.buy-footer1").hide();
  101. $(".gwc_null").show();
  102. }else{
  103. mui.each(dt.data.cartList, function(index) {
  104. var li = document.createElement("li");
  105. li.className = 'mui-table-view-cell mui-media';
  106. var num = this.cartNum;
  107. var proStatus='',disabled='';
  108. if(this.productStatus!=1){
  109. proStatus='<div class="xiajia">已售罄</div>';
  110. disabled='disabled="disabled"';
  111. }
  112. li.innerHTML = '<div class="mui-slider-right mui-disabled"><a class="mui-btn mui-btn-red btn-del" name="' + this.cartColorId + '">删除</a></div> <div class="mui-slider-handle"><input name="checkbox" type="checkbox" class="mui-pull-left mt-mr-che" '+disabled+'/><span class="mui-media-object mui-pull-left wh-100"><img src="' + this.productIntroduceImg + '" /></span><div class="mui-media-body"><div class="wt-space gwc_tit">' + this.productName + '</div><small class="gwc_pp">颜色:' + this.productColor + '</small><div class="price"><span class="price_sp">¥' + (this.productDiscount / 100).toFixed(2) + '</span></div><p class="mui-ellipsis"></p><div class="mui-numbox" data-numbox-min="1" data-numbox-max="99"><button class="mui-btn mui-btn-numbox-minus" type="button" colorid='+this.cartColorId+'>-</button><input class="mui-input-numbox" type="tel" value="' + this.cartNum + '" readonly="readonly" disabled="disabled" cartid="'+this.cartId+'"/><button class="mui-btn mui-btn-numbox-plus" type="button" colorid='+this.cartColorId+'>+</button></div></div>'+proStatus+'</div>';
  113. table.appendChild(li);
  114. });
  115. if($(".shopping-cart-list input[type='checkbox']:not(:disabled)").length>0){//判断购物车列表未下架的产品数量>0
  116. $(".shopping-cart-list input[type='checkbox']:not(:disabled)").prop('checked', true); //选中所有未下架产品
  117. $(".all_check").prop('checked', true);//全选按钮选中
  118. $("#go-clearing").removeClass("add_gwc1").addClass("add_gwc").text('去结算('+$(".shopping-cart-list input[type=checkbox]:not(:disabled):checked").length+')');//点亮结算按钮
  119. }
  120. mui(".mui-numbox").numbox(); //激活加减按钮组合
  121. $(".loading").hide().css("opacity", "0");//隐藏(正在加载...)
  122. calculator();
  123. }
  124. } else {
  125. mui.alert("获取数据失败!");
  126. }
  127. }
  128. },
  129. error: function(xhr, type, errorThrown) {
  130. console.log(xhr);
  131. mui.alert("获取数据失败!网络错误");
  132. }
  133. });
  134. // var str="";
  135. // for (i=0;i<3;i++) {
  136. // str+='<li class="mui-table-view-cell mui-media"><div class="mui-slider-right mui-disabled"><a class="mui-btn mui-btn-red btn-del">删除</a></div> <div class="mui-slider-handle"><input name="checkbox" type="checkbox" class="mui-pull-left mt-mr-che" disabled="disabled"/><span class="mui-media-object mui-pull-left wh-100"><img src="images/pro-bai.png" /></span><div class="mui-media-body"><div class="wt-space gwc_tit">柔性声波牙刷</div><small class="gwc_pp">颜色:极光白</small><div class="price"><span class="price_sp">¥2999.00</span></div><p class="mui-ellipsis"></p><div class="mui-numbox" data-numbox-min="1" data-numbox-max="99"><button class="mui-btn mui-btn-numbox-minus" type="button" disabled="">-</button><input class="mui-input-numbox" type="number" value="1" readonly="readonly"><button class="mui-btn mui-btn-numbox-plus" type="button">+</button></div></div><div class="xiajia">已售罄</div></div></li>';
  137. // str+='<li class="mui-table-view-cell mui-media"><div class="mui-slider-right mui-disabled"><a class="mui-btn mui-btn-red btn-del">删除</a></div> <div class="mui-slider-handle"><input name="checkbox" type="checkbox" class="mui-pull-left mt-mr-che" checked/><span class="mui-media-object mui-pull-left wh-100"><img src="images/pro-bai.png" /></span><div class="mui-media-body"><div class="wt-space gwc_tit">柔性声波牙刷</div><small class="gwc_pp">颜色:极光白</small><div class="price"><span class="price_sp">¥2999.00</span></div><p class="mui-ellipsis"></p><div class="mui-numbox" data-numbox-min="1" data-numbox-max="99"><button class="mui-btn mui-btn-numbox-minus" type="button" disabled="">-</button><input class="mui-input-numbox" type="number" value="1" readonly="readonly"><button class="mui-btn mui-btn-numbox-plus" type="button">+</button></div></div></div></li>';
  138. // }
  139. // document.querySelector('.shopping-cart-list').innerHTML=str;
  140. //
  141. // if($(".mui-table-view input[type='checkbox']:checked").length>0){
  142. // $("#go-clearing").removeClass('add_gwc1').addClass('add_gwc').text("结算("+$(".mui-table-view input[type='checkbox']:checked").length+")");
  143. // calculator();
  144. // }else{
  145. //
  146. // }
  147. // mui(".mui-numbox").numbox();
  148. // $(".loading").hide().css("opacity", "0");//隐藏(正在加载...)
  149. });
  150. //拖拽后显示操作图标,点击删除;
  151. mui('body').on('tap', '.btn-del', function(event) {
  152. var btnArray = ['取消', '确定'];
  153. var elem = this;
  154. var li = elem.parentNode.parentNode;
  155. mui.confirm('您确定要删除吗?', '提示', btnArray, function(e) {
  156. if(e.index == 1) {
  157. $.ajax(base_path + '/wechat/cart/deleteCartByProductId?dates=' + new Date().getTime(), {
  158. data: {
  159. "colorId": elem.getAttribute("name")
  160. },
  161. dataType: 'json',
  162. xhrFields: {
  163. withCredentials: true
  164. },
  165. crossDomain: true,
  166. type: 'get',
  167. timeout: 15000,
  168. success: function(dt) {
  169. if(dt.isRedirect) {
  170. location.href = dt.redirectURL;
  171. } else {
  172. if(dt.status == true) {
  173. li.parentNode.removeChild(li);
  174. if($(".shopping-cart-list>li").length < 1) {
  175. $(".shopping-box,.buy-footer1").hide();
  176. $(".gwc_null,#footer-bar-tab").show();
  177. }
  178. calculator();
  179. mui.alert("删除成功!")
  180. } else {
  181. mui.alert("删除失败!");
  182. }
  183. }
  184. },
  185. error: function(xhr, type, errorThrown) {
  186. console.log(xhr);
  187. }
  188. });
  189. } else {
  190. mui.toast("您取消了删除!");
  191. setTimeout(function() {
  192. mui.swipeoutClose(li);
  193. }, 0);
  194. }
  195. });
  196. });
  197. //购物车数量加
  198. $(document).on('tap', '.mui-btn-numbox-plus', function() {
  199. var th = $(this);
  200. var val = mui(th.parent()[0]).numbox().getValue(); //获取当前产品数量
  201. var parameter = {}; //ajax参数json集合
  202. if(typeof(th.attr("colorid")) != "undefined") {
  203. parameter["colorId"] = th.attr("colorid"); //颜色id
  204. parameter["cartNum"] = 1; //需要加的产品数量,每次都是1
  205. $.ajax(base_path + '/wechat/cart/addProductToCart?dates=' + new Date().getTime(), {
  206. data: parameter,
  207. dataType: 'json',
  208. xhrFields: {
  209. withCredentials: true
  210. },
  211. crossDomain: true,
  212. type: 'get',
  213. timeout: 15000,
  214. success: function(dt) {
  215. if(dt.isRedirect) {
  216. location.href = dt.redirectURL;
  217. } else {
  218. if(dt.status) {
  219. th.parent().attr("cartid", dt.data);
  220. console.log("增加数量成功!");
  221. } else {
  222. mui.alert("增加数量失败!");
  223. }
  224. }
  225. },
  226. error: function(xhr, type, errorThrown) {
  227. console.log(xhr);
  228. mui.alert("增加数量失败!网络错误");
  229. }
  230. });
  231. } else {
  232. mui.alert("没有获取到产品颜色id");
  233. }
  234. });
  235. //购物车数量减
  236. $(document).on('tap', '.mui-btn-numbox-minus', function() {
  237. var th = $(this);
  238. var val = mui(th.parent()[0]).numbox().getValue(); //获取当前产品数量
  239. var parameter = {}; //ajax参数json集合
  240. if(typeof(th.attr("colorid")) != "undefined") {
  241. parameter["colorId"] = th.attr("colorid"); //产品颜色id
  242. parameter["cartNum"] = 1; //需要减的产品数量,每次都是1
  243. $.ajax(base_path + '/wechat/cart/deleteCart?dates=' + new Date().getTime(), {
  244. data: parameter,
  245. dataType: 'json',
  246. xhrFields: {
  247. withCredentials: true
  248. },
  249. crossDomain: true,
  250. type: 'get', //减少数量后台限制用get
  251. timeout: 15000,
  252. success: function(dt) {
  253. if(dt.isRedirect) {
  254. location.href = dt.redirectURL;
  255. } else {
  256. if(dt.status) {
  257. console.log("减少数量数量成功!");
  258. } else {
  259. mui.alert("减少数量数量失败!");
  260. }
  261. }
  262. },
  263. error: function(xhr, type, errorThrown) {
  264. console.log(xhr);
  265. mui.alert("减少数量失败!网络错误");
  266. }
  267. });
  268. } else {
  269. mui.alert("没有获取到产品颜色id");
  270. }
  271. });
  272. //全选/取消全选
  273. $(document).on('click', 'input[type="checkbox"]', function(e) {
  274. if($(this).hasClass("all_check")) {
  275. if($(this).prop("checked") == true) {
  276. console.log("全选")
  277. if($(".shopping-cart-list input[type='checkbox']:not(:disabled)").length>0){
  278. $(".shopping-cart-list input[type='checkbox']:not(:disabled)").prop('checked', true);
  279. $("#go-clearing").removeClass('add_gwc1').addClass('add_gwc').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  280. }
  281. } else {
  282. console.log("取消全选")
  283. $("input[type='checkbox']").prop('checked', false);
  284. $("#go-clearing").removeClass('add_gwc').addClass('add_gwc1').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  285. }
  286. } else {
  287. $(".all_check").prop('checked', false);
  288. if($(".shopping-cart-list input[type='checkbox']:checked").length > 0) {
  289. $("#go-clearing").removeClass('add_gwc1').addClass('add_gwc').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  290. if(($("input[type='checkbox']:checked").length + 1) == $("input[type='checkbox']").length) {
  291. $("input[type='checkbox']").prop('checked', true);
  292. }
  293. } else {
  294. $("#go-clearing").removeClass('add_gwc').addClass('add_gwc1').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  295. }
  296. }
  297. calculator();
  298. });
  299. //跳转产品列表页面
  300. mui('body').on('tap', '.go_web_shop', function() {
  301. mui.openWindow({
  302. url: 'pro-list.html'
  303. });
  304. });
  305. //结算事件
  306. mui('body').on('tap', '.go-clearing', function() {
  307. if($(".shopping-cart-list input[type='checkbox']:checked").length > 0) {
  308. var cartidList = ''; //购物项id集合
  309. $.each($(".shopping-cart-list input[type='checkbox']:not(:disabled):checked"), function(index) {
  310. var th = $(this).parent().find('.mui-input-numbox');
  311. if(typeof(th.attr("cartid")) != "undefined") {
  312. var a = '';
  313. if(index != 0) {
  314. a = '-' + th.attr("cartid"); //组装购物项id
  315. } else {
  316. a = th.attr("cartid");
  317. }
  318. cartidList += a;
  319. }
  320. });
  321. if(!window.sessionStorage) {
  322. mui.alert("您的浏览器不支持sessionStorage");
  323. } else {
  324. //自定义json数据
  325. var obj = {
  326. text: [cartidList]
  327. };
  328. //json格式化插入 购物项id集合
  329. sessionStorage.setItem('pay_data', JSON.stringify(obj));
  330. location.href = 'clearing.html'; //跳转结算页面
  331. }
  332. } else {
  333. mui.alert('您还没有选中商品', '消息提醒', function() {
  334. console.log("您关闭了警告框");
  335. });
  336. }
  337. });
  338. //监听加减框组合值
  339. mui('body').on('change', '.mui-input-numbox', function() {
  340. calculator();
  341. });
  342. //重新计算
  343. function calculator() {
  344. var count = 0;
  345. var num = 0;
  346. $(".mui-table-view li").each(function() {
  347. if($(this).find("input[type='checkbox']:not(:disabled)").prop('checked') == true) {
  348. num = parseFloat($(this).find(".price_sp").text().replace(/¥/g, "") * $(this).find(".mui-input-numbox").val()).toFixed(2);
  349. count = accAdd(count, num);
  350. }
  351. });
  352. $(".count_price").text(accAdd(count, 0).toFixed(2));
  353. $("#go-clearing").text('去结算('+$(".shopping-cart-list input[type=checkbox]:not(:disabled):checked").length+')');
  354. if($(".shopping-cart-list input[type='checkbox']:not(:disabled):checked").length<1){
  355. $("#go-clearing").removeClass("add_gwc").addClass("add_gwc1");
  356. }else{
  357. $("#go-clearing").removeClass("add_gwc1").addClass("add_gwc");
  358. }
  359. }
  360. </script>
  361. </body>
  362. </html>