shopping-cart.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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" src="//res.wx.qq.com/open/js/jweixin-1.1.0.js"></script>
  13. <script>
  14. //屏蔽分享
  15. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  16. WeixinJSBridge.call('hideOptionMenu');
  17. });
  18. </script>
  19. </head>
  20. <body>
  21. <div class="loading-bg">
  22. <div class="mui-loading">
  23. <div class="mui-spinner">
  24. </div>
  25. &nbsp;&nbsp;加载中...
  26. </div>
  27. </div>
  28. <div class="mui-content">
  29. <div class="gwc_null">
  30. <img width="130px" src="images/cart-null.png">
  31. <h4 style="margin: 10px 0 20px 0;">购物车还是空的</h4>
  32. <a href="javascript:;" class="mui-btn my-btn-danger go_web_shop" style="width: 150px;padding: 10px 10px;color: #666;">立即选购</a>
  33. </div>
  34. <div class="shopping-box">
  35. <div class="shoping-tt">
  36. <ul class="mui-table-view my-grid-view-no shopping-cart-list">
  37. <!--<li class="mui-table-view-cell mui-media bg-f">
  38. <a href="javascript:;">
  39. <input name="checkbox" type="checkbox" class="mui-pull-left mt-mr-che" />
  40. <img class="mui-media-object mui-pull-left wh-100 bd-all-1" src="images/image-7.jpg">
  41. <div class="mui-media-body">
  42. <div class="wt-space gwc_tit">水时代净水机-Watero-N1</div>
  43. <small class="gwc_pp">水时代</small>
  44. <div class="price"><span class="price_sp">¥55.02</span><small>小计:¥1999.00</small></div>
  45. <p class="mui-ellipsis">
  46. </p><div class="mui-numbox" data-numbox-min="1" data-numbox-max="999">
  47. <button class="mui-btn mui-btn-numbox-minus" type="button" disabled="">-</button>
  48. <input class="mui-input-numbox" type="number" value="1">
  49. <button class="mui-btn mui-btn-numbox-plus" type="button">+</button>
  50. </div>
  51. <p></p>
  52. </div>
  53. </a>
  54. <span class="mui-icon mui-icon-trash ft-24 del"></span>
  55. <span class="jifen"><img src="images/corner_icon.png"> </span>
  56. </li>
  57. <li class="mui-table-view-cell mui-media bg-f">
  58. <a href="javascript:;">
  59. <input name="checkbox" type="checkbox" class="mui-pull-left mt-mr-che" />
  60. <img class="mui-media-object mui-pull-left wh-100 bd-all-1" src="images/image-7.jpg">
  61. <div class="mui-media-body">
  62. <div class="wt-space gwc_tit">水时代净水机-Watero-N1</div>
  63. <small class="gwc_pp">水时代</small>
  64. <div class="price"><span class="price_sp">¥55.02</span><small>小计:¥1999.00</small></div>
  65. <p class="mui-ellipsis">
  66. </p><div class="mui-numbox" data-numbox-min="1" data-numbox-max="999">
  67. <button class="mui-btn mui-btn-numbox-minus" type="button" disabled="">-</button>
  68. <input class="mui-input-numbox" type="number" value="1">
  69. <button class="mui-btn mui-btn-numbox-plus" type="button">+</button>
  70. </div>
  71. <p></p>
  72. </div>
  73. </a>
  74. <span class="mui-icon mui-icon-trash ft-24 del"></span>
  75. <span class="jifen"><img src="images/corner_icon.png"> </span>
  76. </li>
  77. <li class="mui-table-view-cell mui-media bg-f">
  78. <a href="javascript:;">
  79. <input name="checkbox" type="checkbox" class="mui-pull-left mt-mr-che" />
  80. <img class="mui-media-object mui-pull-left wh-100 bd-all-1" src="images/image-7.jpg">
  81. <div class="mui-media-body">
  82. <div class="wt-space gwc_tit">水时代净水机-Watero-N1</div>
  83. <small class="gwc_pp">水时代</small>
  84. <div class="price"><span class="price_sp">¥55.02</span><small>小计:¥1999.00</small></div>
  85. <p class="mui-ellipsis">
  86. </p><div class="mui-numbox" data-numbox-min="1" data-numbox-max="999">
  87. <button class="mui-btn mui-btn-numbox-minus" type="button" disabled="">-</button>
  88. <input class="mui-input-numbox" type="number" value="1">
  89. <button class="mui-btn mui-btn-numbox-plus" type="button">+</button>
  90. </div>
  91. <p></p>
  92. </div>
  93. </a>
  94. <span class="mui-icon mui-icon-trash ft-24 del"></span>
  95. </li>-->
  96. </ul>
  97. </div>
  98. <div class="buy-footer1">
  99. <table class="tb-clearing">
  100. <tr>
  101. <td class="quanxuan">
  102. <input type="checkbox" name="checkbox" class="all_check" /> 全选
  103. </td>
  104. <td class="mui-text-center">合计:<span class="count_price_box">¥<span class="count_price">0.0</span></span>元</td>
  105. <td>
  106. <a id="go-clearing" class="mui-btn mui-pull-right add_gwc1 go-clearing">去结算(0)</a>
  107. </td>
  108. </tr>
  109. </table>
  110. </div>
  111. </div>
  112. </div>
  113. <script src="js/mui.min.js"></script>
  114. <script src="js/jquery-2.1.1.min.js"></script>
  115. <script src="js/main.js"></script>
  116. <script>
  117. mui.ready(function() {
  118. $.ajax(base_path + '/wechat/cart/selectCartByOpenId?dates=' + new Date().getTime(), {
  119. data: {},
  120. dataType: 'json',
  121. xhrFields: {
  122. withCredentials: true
  123. },
  124. crossDomain: true,
  125. type: 'post',
  126. timeout: 15000,
  127. success: function(dt) {
  128. if(dt.isRedirect) {
  129. location.href = dt.redirectURL;
  130. } else {
  131. if(dt.status) {
  132. console.log(dt);
  133. var table = document.body.querySelector('.shopping-cart-list');
  134. if(dt.data.cartList.length==0){
  135. $(".shopping-box,.buy-footer1").hide();
  136. $(".gwc_null").show();
  137. $("#footer-bar-tab").show();
  138. }else{
  139. mui.each(dt.data.cartList, function(index) {
  140. var li = document.createElement("li");
  141. li.className = 'mui-table-view-cell mui-media';
  142. var num = this.cartNum;
  143. var proStatus='',disabled='';
  144. var zeng = '';
  145. if(this.productStatus==1 && this.colorStatus == 1){
  146. $.each(this.salesUserGiftList, function() {
  147. zeng += this.giftName + '<span class="giftItems" count="'+this.userGiftCount+'">'+(num * this.userGiftCount) + '</span>件 ';
  148. });
  149. if(zeng != '') {
  150. zeng = '<p class="gift">(赠品:' + zeng + ')</p>&nbsp;&nbsp;';
  151. }
  152. }else{
  153. proStatus='<div class="xiajia">已下架</div>';
  154. disabled='disabled="disabled"';
  155. }
  156. 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 style="background-color: #eff6f6;" src="' + this.productIntroduceImg + '" /></span><div class="mui-media-body"><div class="wt-space gwc_tit">' + this.productName + '</div><p class="gwc_pp">颜色:' + this.productColor + '</p><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>' + zeng +proStatus+ '</div>';
  157. table.appendChild(li);
  158. });
  159. if($(".shopping-cart-list input[type='checkbox']:not(:disabled)").length>0){//判断购物车列表未下架的产品数量>0
  160. $(".shopping-cart-list input[type='checkbox']:not(:disabled)").prop('checked', true); //选中所有未下架产品
  161. $(".all_check").prop('checked', true);//全选按钮选中
  162. $("#go-clearing").removeClass("add_gwc1").addClass("add_gwc").text('去结算('+$(".shopping-cart-list input[type=checkbox]:not(:disabled):checked").length+')');//点亮结算按钮
  163. }
  164. mui(".mui-numbox").numbox(); //激活加减按钮组合
  165. // $(".shopping-box,.buy-footer1").show();
  166. $(".gwc_null").hide();
  167. calculator();
  168. }
  169. } else {
  170. mui.alert("获取数据失败!");
  171. }
  172. document.querySelector('.loading-bg').style.display = 'none';
  173. }
  174. },
  175. error: function(xhr, type, errorThrown) {
  176. console.log(xhr);
  177. mui.alert("获取数据失败!网络错误");
  178. }
  179. });
  180. });
  181. //拖拽后显示操作图标,点击删除;
  182. mui('body').on('tap', '.btn-del', function(event) {
  183. var btnArray = ['取消', '确定'];
  184. var elem = this;
  185. var li = elem.parentNode.parentNode;
  186. mui.confirm('您确定要删除吗?', '提示', btnArray, function(e) {
  187. if(e.index == 1) {
  188. $.ajax(base_path + '/wechat/cart/deleteCartByProductId?dates=' + new Date().getTime(), {
  189. data: {
  190. "productColorId": elem.getAttribute("name")
  191. },
  192. dataType: 'json',
  193. xhrFields: {
  194. withCredentials: true
  195. },
  196. crossDomain: true,
  197. type: 'get',
  198. timeout: 15000,
  199. success: function(dt) {
  200. if(dt.isRedirect) {
  201. location.href = dt.redirectURL;
  202. } else {
  203. if(dt.status == true) {
  204. li.parentNode.removeChild(li);
  205. if($(".shopping-cart-list>li").length < 1) {
  206. $(".shopping-box,.buy-footer1").hide();
  207. $(".gwc_null,#footer-bar-tab").show();
  208. }
  209. calculator();
  210. mui.toast("删除成功");
  211. } else {
  212. alert(dt.message);
  213. }
  214. }
  215. },
  216. error: function(xhr, type, errorThrown) {
  217. console.log(xhr);
  218. }
  219. });
  220. } else {
  221. //mui.toast("您取消了删除!");
  222. setTimeout(function() {
  223. mui.swipeoutClose(li);
  224. }, 0);
  225. }
  226. });
  227. });
  228. //购物车数量加
  229. $(document).on('tap', '.mui-btn-numbox-plus', function() {
  230. var th = $(this);
  231. var val = mui(th.parent()[0]).numbox().getValue(); //获取当前产品数量
  232. var parameter = {}; //ajax参数json集合
  233. if(typeof(th.attr("colorid")) != "undefined") {
  234. parameter["productColorId"] = th.attr("colorid"); //颜色id
  235. parameter["cartNum"] = 1; //需要加的产品数量,每次都是1
  236. $.ajax(base_path + '/wechat/cart/addCart?dates=' + new Date().getTime(), {
  237. data: parameter,
  238. dataType: 'json',
  239. xhrFields: {
  240. withCredentials: true
  241. },
  242. crossDomain: true,
  243. type: 'get',
  244. timeout: 15000,
  245. success: function(dt) {
  246. if(dt.isRedirect) {
  247. location.href = dt.redirectURL;
  248. } else {
  249. if(dt.status) {
  250. th.parent().attr("cartid", dt.data);
  251. //每次增加数量同时将赠品数量增加
  252. $.each($($(th).parent().parent().parent().find(".giftItems")),function(){
  253. $(this).text(val*parseInt($(this).attr("count")));
  254. });
  255. console.log("增加数量成功!");
  256. } else {
  257. mui.alert("增加数量失败!");
  258. }
  259. }
  260. },
  261. error: function(xhr, type, errorThrown) {
  262. console.log(xhr);
  263. mui.alert("增加数量失败!网络错误");
  264. }
  265. });
  266. } else {
  267. mui.alert("没有获取到产品颜色id");
  268. }
  269. });
  270. //购物车数量减
  271. $(document).on('tap', '.mui-btn-numbox-minus', function() {
  272. var th = $(this);
  273. var val = mui(th.parent()[0]).numbox().getValue(); //获取当前产品数量
  274. var parameter = {}; //ajax参数json集合
  275. if(typeof(th.attr("colorid")) != "undefined") {
  276. parameter["productColorId"] = th.attr("colorid"); //产品颜色id
  277. parameter["cartNum"] = 1; //需要减的产品数量,每次都是1
  278. $.ajax(base_path + '/wechat/cart/deleteCart?dates=' + new Date().getTime(), {
  279. data: parameter,
  280. dataType: 'json',
  281. xhrFields: {
  282. withCredentials: true
  283. },
  284. crossDomain: true,
  285. type: 'get', //减少数量后台限制用get
  286. timeout: 15000,
  287. success: function(dt) {
  288. if(dt.isRedirect) {
  289. location.href = dt.redirectURL;
  290. } else {
  291. if(dt.status) {
  292. //每次减少数量同时将赠品数量减少
  293. $.each($($(th).parent().parent().parent().find(".giftItems")),function(){
  294. $(this).text(val*parseInt($(this).attr("count")));
  295. });
  296. console.log("减少数量数量成功!");
  297. } else {
  298. mui.alert("减少数量数量失败!");
  299. }
  300. }
  301. },
  302. error: function(xhr, type, errorThrown) {
  303. console.log(xhr);
  304. mui.alert("减少数量失败!网络错误");
  305. }
  306. });
  307. } else {
  308. mui.alert("没有获取到产品颜色id");
  309. }
  310. });
  311. //全选/取消全选
  312. $(document).on('click', 'input[type="checkbox"]', function(e) {
  313. if($(this).hasClass("all_check")) {
  314. if($(this).prop("checked") == true) {
  315. console.log("全选")
  316. if($(".shopping-cart-list input[type='checkbox']:not(:disabled)").length>0){
  317. $(".shopping-cart-list input[type='checkbox']:not(:disabled)").prop('checked', true);
  318. $("#go-clearing").removeClass('add_gwc1').addClass('add_gwc').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  319. }
  320. } else {
  321. console.log("取消全选")
  322. $("input[type='checkbox']").prop('checked', false);
  323. $("#go-clearing").removeClass('add_gwc').addClass('add_gwc1').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  324. }
  325. } else {
  326. $(".all_check").prop('checked', false);
  327. if($(".shopping-cart-list input[type='checkbox']:checked").length > 0) {
  328. $("#go-clearing").removeClass('add_gwc1').addClass('add_gwc').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  329. if(($("input[type='checkbox']:checked").length + 1) == $("input[type='checkbox']").length) {
  330. $("input[type='checkbox']").prop('checked', true);
  331. }
  332. } else {
  333. $("#go-clearing").removeClass('add_gwc').addClass('add_gwc1').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  334. }
  335. }
  336. calculator();
  337. });
  338. //跳转产品列表页面
  339. mui('body').on('tap', '.go_web_shop', function() {
  340. mui.openWindow({
  341. url: 'pro-list.html'
  342. });
  343. });
  344. //结算事件
  345. mui('body').on('tap', '.go-clearing', function() {
  346. if($(".shopping-cart-list input[type='checkbox']:checked").length > 0) {
  347. var cartidList = ''; //购物项id集合
  348. $.each($(".shopping-cart-list input[type='checkbox']:not(:disabled):checked"), function(index) {
  349. var th = $(this).parent().find('.mui-input-numbox');
  350. if(typeof(th.attr("cartid")) != "undefined") {
  351. var a = '';
  352. if(index != 0) {
  353. a = '-' + th.attr("cartid"); //组装购物项id
  354. } else {
  355. a = th.attr("cartid");
  356. }
  357. cartidList += a;
  358. }
  359. });
  360. if(!window.sessionStorage) {
  361. mui.alert("您的浏览器不支持sessionStorage");
  362. } else {
  363. //自定义json数据
  364. var obj = {
  365. text: [cartidList]
  366. };
  367. //json格式化插入 购物项id集合
  368. sessionStorage.setItem('pay_data', JSON.stringify(obj));
  369. location.href = 'clearing.html'; //跳转结算页面
  370. }
  371. } else {
  372. mui.alert('您还没有选中商品', '消息提醒', function() {
  373. console.log("您关闭了警告框");
  374. });
  375. }
  376. });
  377. //监听加减框组合值
  378. mui('body').on('change', '.mui-input-numbox', function() {
  379. calculator();
  380. });
  381. //重新计算
  382. function calculator() {
  383. var count = 0;
  384. var num = 0;
  385. $(".mui-table-view li").each(function() {
  386. if($(this).find("input[type='checkbox']:not(:disabled)").prop('checked') == true) {
  387. num = parseFloat($(this).find(".price_sp").text().replace(/¥/g, "") * $(this).find(".mui-input-numbox").val()).toFixed(2);
  388. count = accAdd(count, num);
  389. }
  390. });
  391. $(".count_price").text(accAdd(count, 0).toFixed(2));
  392. $("#go-clearing").text('去结算('+$(".shopping-cart-list input[type=checkbox]:not(:disabled):checked").length+')');
  393. if($(".shopping-cart-list input[type='checkbox']:not(:disabled):checked").length<1){
  394. $("#go-clearing").removeClass("add_gwc").addClass("add_gwc1");
  395. }else{
  396. $("#go-clearing").removeClass("add_gwc1").addClass("add_gwc");
  397. }
  398. }
  399. </script>
  400. </body>
  401. </html>