shopping-cart.html 16 KB

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