shopping-cart.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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">0.0</span></span>元</td>
  123. <td>
  124. <a id="go-clearing" class="mui-btn mui-pull-right add_gwc1 go-clearing">去结算(0)</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. var proStatus='',disabled='';
  162. var zeng = '';
  163. if(this.productStatus==1 && this.colorStatus == 1){
  164. $.each(this.salesUserGiftList, function() {
  165. zeng += this.giftName + '<span class="giftItems" count="'+this.userGiftCount+'">'+(num * this.userGiftCount) + '</span>件 ';
  166. });
  167. if(zeng != '') {
  168. zeng = '<p class="gift">(赠品:' + zeng + ')</p>&nbsp;&nbsp;';
  169. }
  170. }else{
  171. proStatus='<div class="xiajia">已下架</div>';
  172. disabled='disabled="disabled"';
  173. }
  174. 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>';
  175. table.appendChild(li);
  176. });
  177. if($(".shopping-cart-list input[type='checkbox']:not(:disabled)").length>0){//判断购物车列表未下架的产品数量>0
  178. $(".shopping-cart-list input[type='checkbox']:not(:disabled)").prop('checked', true); //选中所有未下架产品
  179. $(".all_check").prop('checked', true);//全选按钮选中
  180. $("#go-clearing").removeClass("add_gwc1").addClass("add_gwc").text('去结算('+$(".shopping-cart-list input[type=checkbox]:not(:disabled):checked").length+')');//点亮结算按钮
  181. }
  182. mui(".mui-numbox").numbox(); //激活加减按钮组合
  183. // $(".shopping-box,.buy-footer1").show();
  184. $(".gwc_null").hide();
  185. calculator();
  186. }
  187. } else {
  188. mui.alert("获取数据失败!");
  189. }
  190. document.querySelector('.loading-bg').style.display = 'none';
  191. }
  192. },
  193. error: function(xhr, type, errorThrown) {
  194. console.log(xhr);
  195. mui.alert("获取数据失败!网络错误");
  196. }
  197. });
  198. });
  199. //拖拽后显示操作图标,点击删除;
  200. mui('body').on('tap', '.btn-del', function(event) {
  201. var btnArray = ['取消', '确定'];
  202. var elem = this;
  203. var li = elem.parentNode.parentNode;
  204. mui.confirm('您确定要删除吗?', '提示', btnArray, function(e) {
  205. if(e.index == 1) {
  206. $.ajax(base_path + '/wechat/cart/deleteCartByProductId?dates=' + new Date().getTime(), {
  207. data: {
  208. "productColorId": elem.getAttribute("name")
  209. },
  210. dataType: 'json',
  211. xhrFields: {
  212. withCredentials: true
  213. },
  214. crossDomain: true,
  215. type: 'get',
  216. timeout: 15000,
  217. success: function(dt) {
  218. if(dt.isRedirect) {
  219. location.href = dt.redirectURL;
  220. } else {
  221. if(dt.status == true) {
  222. li.parentNode.removeChild(li);
  223. if($(".shopping-cart-list>li").length < 1) {
  224. $(".shopping-box,.buy-footer1").hide();
  225. $(".gwc_null,#footer-bar-tab").show();
  226. }
  227. calculator();
  228. mui.toast("删除成功");
  229. } else {
  230. alert(dt.message);
  231. }
  232. }
  233. },
  234. error: function(xhr, type, errorThrown) {
  235. console.log(xhr);
  236. }
  237. });
  238. } else {
  239. //mui.toast("您取消了删除!");
  240. setTimeout(function() {
  241. mui.swipeoutClose(li);
  242. }, 0);
  243. }
  244. });
  245. });
  246. //购物车数量加
  247. $(document).on('tap', '.mui-btn-numbox-plus', function() {
  248. var th = $(this);
  249. var val = mui(th.parent()[0]).numbox().getValue(); //获取当前产品数量
  250. var parameter = {}; //ajax参数json集合
  251. if(typeof(th.attr("colorid")) != "undefined") {
  252. parameter["productColorId"] = th.attr("colorid"); //颜色id
  253. parameter["cartNum"] = 1; //需要加的产品数量,每次都是1
  254. $.ajax(base_path + '/wechat/cart/addCart?dates=' + new Date().getTime(), {
  255. data: parameter,
  256. dataType: 'json',
  257. xhrFields: {
  258. withCredentials: true
  259. },
  260. crossDomain: true,
  261. type: 'get',
  262. timeout: 15000,
  263. success: function(dt) {
  264. if(dt.isRedirect) {
  265. location.href = dt.redirectURL;
  266. } else {
  267. if(dt.status) {
  268. th.parent().attr("cartid", dt.data);
  269. //每次增加数量同时将赠品数量增加
  270. $.each($($(th).parent().parent().parent().find(".giftItems")),function(){
  271. $(this).text(val*parseInt($(this).attr("count")));
  272. });
  273. console.log("增加数量成功!");
  274. } else {
  275. mui.alert("增加数量失败!");
  276. }
  277. }
  278. },
  279. error: function(xhr, type, errorThrown) {
  280. console.log(xhr);
  281. mui.alert("增加数量失败!网络错误");
  282. }
  283. });
  284. } else {
  285. mui.alert("没有获取到产品颜色id");
  286. }
  287. });
  288. //购物车数量减
  289. $(document).on('tap', '.mui-btn-numbox-minus', function() {
  290. var th = $(this);
  291. var val = mui(th.parent()[0]).numbox().getValue(); //获取当前产品数量
  292. var parameter = {}; //ajax参数json集合
  293. if(typeof(th.attr("colorid")) != "undefined") {
  294. parameter["productColorId"] = th.attr("colorid"); //产品颜色id
  295. parameter["cartNum"] = 1; //需要减的产品数量,每次都是1
  296. $.ajax(base_path + '/wechat/cart/deleteCart?dates=' + new Date().getTime(), {
  297. data: parameter,
  298. dataType: 'json',
  299. xhrFields: {
  300. withCredentials: true
  301. },
  302. crossDomain: true,
  303. type: 'get', //减少数量后台限制用get
  304. timeout: 15000,
  305. success: function(dt) {
  306. if(dt.isRedirect) {
  307. location.href = dt.redirectURL;
  308. } else {
  309. if(dt.status) {
  310. //每次减少数量同时将赠品数量减少
  311. $.each($($(th).parent().parent().parent().find(".giftItems")),function(){
  312. $(this).text(val*parseInt($(this).attr("count")));
  313. });
  314. console.log("减少数量数量成功!");
  315. } else {
  316. mui.alert("减少数量数量失败!");
  317. }
  318. }
  319. },
  320. error: function(xhr, type, errorThrown) {
  321. console.log(xhr);
  322. mui.alert("减少数量失败!网络错误");
  323. }
  324. });
  325. } else {
  326. mui.alert("没有获取到产品颜色id");
  327. }
  328. });
  329. //全选/取消全选
  330. $(document).on('click', 'input[type="checkbox"]', function(e) {
  331. if($(this).hasClass("all_check")) {
  332. if($(this).prop("checked") == true) {
  333. console.log("全选")
  334. if($(".shopping-cart-list input[type='checkbox']:not(:disabled)").length>0){
  335. $(".shopping-cart-list input[type='checkbox']:not(:disabled)").prop('checked', true);
  336. $("#go-clearing").removeClass('add_gwc1').addClass('add_gwc').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  337. }
  338. } else {
  339. console.log("取消全选")
  340. $("input[type='checkbox']").prop('checked', false);
  341. $("#go-clearing").removeClass('add_gwc').addClass('add_gwc1').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  342. }
  343. } else {
  344. $(".all_check").prop('checked', false);
  345. if($(".shopping-cart-list input[type='checkbox']:checked").length > 0) {
  346. $("#go-clearing").removeClass('add_gwc1').addClass('add_gwc').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  347. if(($("input[type='checkbox']:checked").length + 1) == $("input[type='checkbox']").length) {
  348. $("input[type='checkbox']").prop('checked', true);
  349. }
  350. } else {
  351. $("#go-clearing").removeClass('add_gwc').addClass('add_gwc1').text("去结算(" + $(".shopping-cart-list input[type='checkbox']:checked").length + ")");
  352. }
  353. }
  354. calculator();
  355. });
  356. //跳转产品列表页面
  357. mui('body').on('tap', '.go_web_shop', function() {
  358. mui.openWindow({
  359. url: 'pro-list.html'
  360. });
  361. });
  362. //结算事件
  363. mui('body').on('tap', '.go-clearing', function() {
  364. if($(".shopping-cart-list input[type='checkbox']:checked").length > 0) {
  365. var cartidList = ''; //购物项id集合
  366. $.each($(".shopping-cart-list input[type='checkbox']:not(:disabled):checked"), function(index) {
  367. var th = $(this).parent().find('.mui-input-numbox');
  368. if(typeof(th.attr("cartid")) != "undefined") {
  369. var a = '';
  370. if(index != 0) {
  371. a = '-' + th.attr("cartid"); //组装购物项id
  372. } else {
  373. a = th.attr("cartid");
  374. }
  375. cartidList += a;
  376. }
  377. });
  378. if(!window.sessionStorage) {
  379. mui.alert("您的浏览器不支持sessionStorage");
  380. } else {
  381. //自定义json数据
  382. var obj = {
  383. text: [cartidList]
  384. };
  385. //json格式化插入 购物项id集合
  386. sessionStorage.setItem('pay_data', JSON.stringify(obj));
  387. location.href = 'clearing.html'; //跳转结算页面
  388. }
  389. } else {
  390. mui.alert('您还没有选中商品', '消息提醒', function() {
  391. console.log("您关闭了警告框");
  392. });
  393. }
  394. });
  395. //监听加减框组合值
  396. mui('body').on('change', '.mui-input-numbox', function() {
  397. calculator();
  398. });
  399. //重新计算
  400. function calculator() {
  401. var count = 0;
  402. var num = 0;
  403. $(".mui-table-view li").each(function() {
  404. if($(this).find("input[type='checkbox']:not(:disabled)").prop('checked') == true) {
  405. num = parseFloat($(this).find(".price_sp").text().replace(/¥/g, "") * $(this).find(".mui-input-numbox").val()).toFixed(2);
  406. count = accAdd(count, num);
  407. }
  408. });
  409. $(".count_price").text(accAdd(count, 0).toFixed(2));
  410. $("#go-clearing").text('去结算('+$(".shopping-cart-list input[type=checkbox]:not(:disabled):checked").length+')');
  411. if($(".shopping-cart-list input[type='checkbox']:not(:disabled):checked").length<1){
  412. $("#go-clearing").removeClass("add_gwc").addClass("add_gwc1");
  413. }else{
  414. $("#go-clearing").removeClass("add_gwc1").addClass("add_gwc");
  415. }
  416. }
  417. </script>
  418. </body>
  419. </html>