shopping-cart.html 17 KB

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