shopping_cart.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // 全选按钮状态
  2. var allCheckBtn = true; // 默认为选中
  3. var payTotal = 0; // 支付金额,单位为分
  4. mui.ready(function(){
  5. // 获取购物车项
  6. selectCart();
  7. // 对全选按钮监听
  8. mui('body').on('click','.click-all-btn',function(){
  9. if (allCheckBtn) {
  10. allCheckBtn = false;
  11. $(".all_check").prop('checked', allCheckBtn);
  12. $(".mt-mr-che").prop('checked', allCheckBtn);
  13. $("#pay-total").html("0.0");
  14. $("#go-clearing").html("去结算(0)");
  15. } else {
  16. allCheckBtn = true;
  17. var length = $(".mt-mr-che").length;
  18. //$("#pay-total").html((payTotal / 100).toFixed(2));
  19. $("#go-clearing").html("去结算(" + length + ")");
  20. $(".all_check").prop('checked', allCheckBtn);// 选中
  21. $(".mt-mr-che").prop('checked', allCheckBtn);
  22. reSetTotal();
  23. }
  24. });
  25. // 结算
  26. mui('body').on('tap', '#go-clearing',function() {
  27. var str = $("#go-clearing").html();
  28. mui.alert("有商品暂时缺货,不能购买");
  29. return false;
  30. // if ("去结算(0)" != str) {
  31. //
  32. // // 获取数据
  33. // var input = $(".mui-input-numbox");
  34. // var carts = "";
  35. // for (var i = 0; i < input.length; i++) {
  36. // var id = $(input[i]).attr("id");
  37. // id = id + "btn";
  38. // if ($("#"+id)[0].checked == true) {
  39. // carts += $(input[i]).attr("id").replace("cartIdInput", "-");
  40. // }
  41. // }
  42. // carts = carts.substring(1, carts.length);
  43. // window.location.href='/iamberry/pay/settlement?cartId=' + carts + '&tiemstamp=' + new Date().getTime();
  44. // } else {
  45. // alert("请选择您要结算的产品~");
  46. // }
  47. });
  48. // 其他按钮
  49. mui('body').on('click', '.mt-mr-che', function() {
  50. var input = $(".mui-input-numbox");
  51. var total = 0;
  52. var sum = 0;
  53. var count = 0;
  54. for (var i = 0; i < input.length; i++) {
  55. var id = $(input[i]).attr("id");
  56. id = id + "btn";
  57. if ($("#"+id)[0].checked == true) {
  58. count++;
  59. var price = parseInt($(input[i]).attr("price"));
  60. var num = parseInt($(input[i]).val());
  61. total += price * num;
  62. sum ++;
  63. }
  64. }
  65. //alert($('.mt-mr-che').length + ',' + count);
  66. if ($('.mt-mr-che').length <= count) {
  67. allCheckBtn = true;
  68. $(".all_check").prop('checked', allCheckBtn);
  69. } else {
  70. allCheckBtn = false;
  71. $(".all_check").prop('checked', allCheckBtn);
  72. }
  73. $("#go-clearing").html("去结算(" + sum + ")");
  74. $("#pay-total").html((total / 100).toFixed(2));
  75. });
  76. //左滑显示删除按钮
  77. /* mui('body').on('swipeleft','.shopping-cart-list .mui-table-view-cell',function(){
  78. mui.swipeoutOpen(this);
  79. }); */
  80. //拖拽后显示操作图标,点击操作图标删除元素;
  81. /* mui('body').on('tap', '.btn-del', function(event) {
  82. var btnArray = ['确认', '取消'];
  83. var elem = this;
  84. var li = elem.parentNode.parentNode;
  85. mui.confirm('确认删除该条记录?', '爱贝源-提示', btnArray, function(e) {
  86. if (e.index == 0) {
  87. li.parentNode.removeChild(li);
  88. } else {
  89. setTimeout(function() {
  90. mui.swipeoutClose(li);
  91. }, 0);
  92. }
  93. });
  94. deleteCartItem();
  95. }); */
  96. document.querySelector('.loading-bg').style.display='none';
  97. });
  98. // 获取购物车列表数据
  99. function selectCart() {
  100. mui.ajax('/iamberry/wechat/cart/selectCartByOpenId',{
  101. dataType:'json',//服务器返回json格式数据
  102. type:'post',//HTTP请求类型
  103. timeout:15000,//超时时间设置为15秒;
  104. success:function(dt){
  105. //服务器返回响应
  106. if(dt.status){
  107. if (dt.data.cartList.length <= 0) {
  108. $(".gwc_null").show();
  109. $(".shopping-box").hide();
  110. $('.shoping-tt').hide();
  111. } else {
  112. var noteLi = "";
  113. var sumCount = 0;
  114. var pro_color="";
  115. for (var i = 0; i < dt.data.cartList.length; i++) {
  116. pro_color=switchcolor(dt.data.cartList[i].productColor)
  117. if (dt.data.cartList[i].productStatus != 1) {
  118. noteLi += "<li class='mui-table-view-cell mui-media bg-f'>" +
  119. "<div class='mui-slider-right mui-disabled'><a class='mui-btn mui-btn-red btn-del' onclick='deleteCartItem(this, 1)' lang='" + dt.data.cartList[i].cartProductId + "'>删除</a></div><div class='mui-slider-handle'><img class='mui-media-object mui-pull-left wh-100' src='" + dt.data.cartList[i].productIntroduceImg + "'>" +
  120. "<div class='mui-media-body'>" +
  121. "<div class='wt-space gwc_tit'>" + dt.data.cartList[i].productName + "</div>" +
  122. "<p class='mui-ellipsis delete-cart-style'>产品下架</p>" +
  123. "<p></p>" +
  124. "</div>" +
  125. /* "<span class='mui-icon mui-icon-trash ft-24 del' onclick='deleteCartItem(this, 2)' lang='" + dt.data.cartList[i].cartProductId + "'></span>" + */
  126. "</div></li>";
  127. } else {
  128. sumCount ++; // 有效购物车数量
  129. total = (dt.data.cartList[i].productPrice * dt.data.cartList[i].cartNum / 100).toFixed(2); // 小计
  130. noteLi += "<li class='mui-table-view-cell mui-media bg-f'>" +
  131. "<div class='mui-slider-right mui-disabled'><a class='mui-btn mui-btn-red btn-del' onclick='deleteCartItem(this, 1)' lang='" + dt.data.cartList[i].cartProductId + "'>删除</a></div><div class='mui-slider-handle'><input name='checkbox' type='checkbox' checked='checked' id='cartIdInput" + dt.data.cartList[i].cartId + "btn' class='mui-pull-left mt-mr-che' />" +
  132. "<span class='mui-media-object mui-pull-left wh-100 go_chanpin_details' proid='" + dt.data.cartList[i].cartProductId +"'><img src='" + dt.data.cartList[i].productIntroduceImg + "'></span>" +
  133. "<div class='mui-media-body'>" +
  134. "<div class='wt-space gwc_tit'>" + dt.data.cartList[i].productName + "</div>" +
  135. "<small class='gwc_pp'>颜色:"+pro_color+"</small>" +
  136. "<div class='price'><span class='price_sp'>¥" + ((dt.data.cartList[i].productPrice / 100).toFixed(2)) + "</span><small id='sub-total" + dt.data.cartList[i].cartId + "'>小计:¥" + total + "</small></div>" +
  137. "<p class='mui-ellipsis'>" +
  138. "</p><div class='mui-numbox' data-numbox-min='1' data-numbox-max='999'>" +
  139. "<button class='mui-btn mui-btn-numbox-minus' type='button' onclick='subCartNum(" + dt.data.cartList[i].cartId + ")'>-</button>" +
  140. "<input class='mui-input-numbox' readonly='readonly' price='" + dt.data.cartList[i].productPrice + "' dir='" + dt.data.cartList[i].cartProductId + "' type='tel' lang='" + dt.data.cartList[i].cartNum + "' id='cartIdInput" + dt.data.cartList[i].cartId + "' value='" + dt.data.cartList[i].cartNum + "'>" +
  141. "<button class='mui-btn mui-btn-numbox-plus' onclick='addCartNum(" + dt.data.cartList[i].cartId + ")' type='button'>+</button>" +
  142. "</div>" +
  143. "<p></p>" +
  144. "</div>" +
  145. /* "<span class='mui-icon mui-icon-trash ft-24 del' onclick='deleteCartItem(this, 2)' lang='" + dt.data.cartList[i].cartProductId + "'></span>" + */
  146. "</div></li>";
  147. }
  148. }
  149. payTotal = dt.data.total;
  150. $("#pay-total").html((dt.data.total / 100).toFixed(2));
  151. $("#go-clearing").html("去结算(" + sumCount + ")");
  152. $("#cart-list-ul").append(noteLi);
  153. }
  154. }
  155. },
  156. error:function(xhr,type,errorThrown){
  157. errorfn(xhr);
  158. }
  159. });
  160. }
  161. // 购物项减
  162. function subCartNum(cartId) {
  163. // 当前数量
  164. var nowNum = $("#cartIdInput" + cartId).val();
  165. $.trim(nowNum);
  166. var r = /^[1-9]+[0-9]*$/;
  167. if (!r.test(nowNum)) {
  168. return false;
  169. }
  170. // 转换后的数量
  171. var pId = $("#cartIdInput" + cartId).attr("dir");
  172. var num = parseInt(nowNum);
  173. if (num > 1) {
  174. num = num - 1;
  175. mui.ajax('/iamberry/wechat/cart/deleteCart',{
  176. data:{"productId":pId, "cartNum":1},
  177. dataType:'json',//服务器返回json格式数据
  178. type:'GET',//HTTP请求类型
  179. timeout:15000,//超时时间设置为15秒;
  180. success:function(dt){
  181. if (dt.status == true) {
  182. $("#cartIdInput" + cartId).val(num);
  183. $("#cartIdInput" + cartId).prop("lang", num);
  184. var price = parseInt($("#cartIdInput" + cartId).attr("price"));
  185. // 小计
  186. $("#sub-total" + cartId).html("小计:¥" + (price * num / 100).toFixed(2));
  187. reSetTotal();
  188. } else {
  189. alert(dt.message);
  190. }
  191. },
  192. error:function(xhr,type,errorThrown){}
  193. });
  194. }
  195. }
  196. // 购物车数量加
  197. function addCartNum(cartId) {
  198. // 当前数量
  199. var nowNum = $("#cartIdInput" + cartId).val();
  200. $.trim(nowNum);
  201. var r = /^[1-9]+[0-9]*$/;
  202. if (!r.test(nowNum)) {
  203. return false;
  204. }
  205. // 转换后的数量
  206. var num = parseInt(nowNum);
  207. if (num < 999) {
  208. num = num + 1;
  209. var pId = $("#cartIdInput" + cartId).attr("dir");
  210. mui.ajax('/iamberry/wechat/cart/addCart',{
  211. data:{"productId":pId, "carNum":1},
  212. dataType:'json',//服务器返回json格式数据
  213. type:'GET',//HTTP请求类型
  214. timeout:15000,//超时时间设置为15秒;
  215. success:function(dt){
  216. if (dt.status == true) {
  217. $("#cartIdInput" + cartId).val(num);
  218. $("#cartIdInput" + cartId).prop("lang", num);
  219. var price = parseInt($("#cartIdInput" + cartId).attr("price"));
  220. // 小计
  221. $("#sub-total" + cartId).html("小计:¥" + (price * num / 100).toFixed(2));
  222. reSetTotal();
  223. } else {
  224. alert(dt.message);
  225. }
  226. },
  227. error:function(xhr,type,errorThrown){
  228. errorfn(xhr);
  229. }
  230. });
  231. }
  232. }
  233. // 删除购物车
  234. function deleteCartItem(obj, isParent) {
  235. var btnArray = ['否', '是'];
  236. mui.confirm('您确定要删除吗?', '提示', btnArray, function(e) {
  237. if (e.index == 1) {
  238. mui.ajax('/iamberry/wechat/cart/deleteCartByProductId',{
  239. data:{"productId":$(obj).attr("lang"), "datestamp":new Date().getTime()},
  240. dataType:'json',//服务器返回json格式数据
  241. type:'GET',//HTTP请求类型
  242. timeout:15000,//超时时间设置为15秒;
  243. success:function(dt){
  244. if (dt.status == true) {
  245. if (isParent == 1) {
  246. $(obj).parent().parent().remove();
  247. } else {
  248. $(obj).parent().parent().remove();
  249. }
  250. $(obj).parent().remove();
  251. if ($("#cart-list-ul>li").length <= 0) {
  252. $(".gwc_null").show();
  253. $(".shopping-box").hide();
  254. $('.shoping-tt').hide();
  255. }
  256. var length = $(".mt-mr-che").length;
  257. $("#go-clearing").html("去结算(" + length + ")");
  258. $(".all_check").prop('checked', true);// 选中
  259. $(".mt-mr-che").prop('checked', true);
  260. reSetTotal();
  261. } else {
  262. alert(dt.message);
  263. }
  264. },
  265. error:function(xhr,type,errorThrown){
  266. errorfn(xhr);
  267. }
  268. });
  269. }
  270. })
  271. }
  272. // 从新计算总价
  273. function reSetTotal() {
  274. var input = $(".mui-input-numbox");
  275. var total = 0;
  276. for (var i = 0; i < input.length; i++) {
  277. var id = $(input[i]).attr("id");
  278. id = id + "btn";
  279. if ($("#"+id)[0].checked == true) {
  280. var price = parseInt($(input[i]).attr("price"));
  281. var num = parseInt($(input[i]).val());
  282. total += price * num;
  283. }
  284. }
  285. payTotal = total;
  286. //alert(payTotal)
  287. $("#pay-total").html((total / 100).toFixed(2));
  288. }
  289. mui('body').on('tap', '#cartActivity', function() {
  290. document.querySelector('.loading-bg').style.display = 'block';
  291. location.href = this.getAttribute('url');
  292. });