123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- // 全选按钮状态
- var allCheckBtn = true; // 默认为选中
- var payTotal = 0; // 支付金额,单位为分
- mui.ready(function(){
- // 获取购物车项
- selectCart();
- // 对全选按钮监听
- mui('body').on('click','.click-all-btn',function(){
- if (allCheckBtn) {
- allCheckBtn = false;
-
- $(".all_check").prop('checked', allCheckBtn);
- $(".mt-mr-che").prop('checked', allCheckBtn);
- $("#pay-total").html("0.0");
- $("#go-clearing").html("去结算(0)");
- } else {
- allCheckBtn = true;
-
- var length = $(".mt-mr-che").length;
- //$("#pay-total").html((payTotal / 100).toFixed(2));
- $("#go-clearing").html("去结算(" + length + ")");
- $(".all_check").prop('checked', allCheckBtn);// 选中
- $(".mt-mr-che").prop('checked', allCheckBtn);
- reSetTotal();
- }
- });
- // 结算
- mui('body').on('tap', '#go-clearing',function() {
- var str = $("#go-clearing").html();
- mui.alert("有商品暂时缺货,不能购买");
- return false;
- // if ("去结算(0)" != str) {
- //
- // // 获取数据
- // var input = $(".mui-input-numbox");
- // var carts = "";
- // for (var i = 0; i < input.length; i++) {
- // var id = $(input[i]).attr("id");
- // id = id + "btn";
- // if ($("#"+id)[0].checked == true) {
- // carts += $(input[i]).attr("id").replace("cartIdInput", "-");
- // }
- // }
- // carts = carts.substring(1, carts.length);
- // window.location.href='/iamberry/pay/settlement?cartId=' + carts + '&tiemstamp=' + new Date().getTime();
- // } else {
- // alert("请选择您要结算的产品~");
- // }
- });
- // 其他按钮
- mui('body').on('click', '.mt-mr-che', function() {
- var input = $(".mui-input-numbox");
- var total = 0;
- var sum = 0;
- var count = 0;
- for (var i = 0; i < input.length; i++) {
- var id = $(input[i]).attr("id");
- id = id + "btn";
- if ($("#"+id)[0].checked == true) {
- count++;
- var price = parseInt($(input[i]).attr("price"));
- var num = parseInt($(input[i]).val());
- total += price * num;
- sum ++;
- }
- }
- //alert($('.mt-mr-che').length + ',' + count);
- if ($('.mt-mr-che').length <= count) {
- allCheckBtn = true;
- $(".all_check").prop('checked', allCheckBtn);
- } else {
- allCheckBtn = false;
- $(".all_check").prop('checked', allCheckBtn);
- }
- $("#go-clearing").html("去结算(" + sum + ")");
- $("#pay-total").html((total / 100).toFixed(2));
- });
- //左滑显示删除按钮
- /* mui('body').on('swipeleft','.shopping-cart-list .mui-table-view-cell',function(){
- mui.swipeoutOpen(this);
- }); */
- //拖拽后显示操作图标,点击操作图标删除元素;
- /* mui('body').on('tap', '.btn-del', function(event) {
- var btnArray = ['确认', '取消'];
- var elem = this;
- var li = elem.parentNode.parentNode;
- mui.confirm('确认删除该条记录?', '爱贝源-提示', btnArray, function(e) {
- if (e.index == 0) {
- li.parentNode.removeChild(li);
- } else {
- setTimeout(function() {
- mui.swipeoutClose(li);
- }, 0);
- }
- });
- deleteCartItem();
- }); */
- document.querySelector('.loading-bg').style.display='none';
- });
- // 获取购物车列表数据
- function selectCart() {
- mui.ajax('/iamberry/wechat/cart/selectCartByOpenId',{
- dataType:'json',//服务器返回json格式数据
- type:'post',//HTTP请求类型
- timeout:15000,//超时时间设置为15秒;
- success:function(dt){
- //服务器返回响应
- if(dt.status){
- if (dt.data.cartList.length <= 0) {
- $(".gwc_null").show();
- $(".shopping-box").hide();
- $('.shoping-tt').hide();
- } else {
- var noteLi = "";
- var sumCount = 0;
- var pro_color="";
- for (var i = 0; i < dt.data.cartList.length; i++) {
- pro_color=switchcolor(dt.data.cartList[i].productColor)
- if (dt.data.cartList[i].productStatus != 1) {
- noteLi += "<li class='mui-table-view-cell mui-media bg-f'>" +
- "<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 + "'>" +
- "<div class='mui-media-body'>" +
- "<div class='wt-space gwc_tit'>" + dt.data.cartList[i].productName + "</div>" +
- "<p class='mui-ellipsis delete-cart-style'>产品下架</p>" +
- "<p></p>" +
- "</div>" +
- /* "<span class='mui-icon mui-icon-trash ft-24 del' onclick='deleteCartItem(this, 2)' lang='" + dt.data.cartList[i].cartProductId + "'></span>" + */
- "</div></li>";
- } else {
- sumCount ++; // 有效购物车数量
- total = (dt.data.cartList[i].productPrice * dt.data.cartList[i].cartNum / 100).toFixed(2); // 小计
- noteLi += "<li class='mui-table-view-cell mui-media bg-f'>" +
- "<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' />" +
- "<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>" +
- "<div class='mui-media-body'>" +
- "<div class='wt-space gwc_tit'>" + dt.data.cartList[i].productName + "</div>" +
- "<small class='gwc_pp'>颜色:"+pro_color+"</small>" +
- "<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>" +
- "<p class='mui-ellipsis'>" +
- "</p><div class='mui-numbox' data-numbox-min='1' data-numbox-max='999'>" +
- "<button class='mui-btn mui-btn-numbox-minus' type='button' onclick='subCartNum(" + dt.data.cartList[i].cartId + ")'>-</button>" +
- "<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 + "'>" +
- "<button class='mui-btn mui-btn-numbox-plus' onclick='addCartNum(" + dt.data.cartList[i].cartId + ")' type='button'>+</button>" +
- "</div>" +
- "<p></p>" +
- "</div>" +
- /* "<span class='mui-icon mui-icon-trash ft-24 del' onclick='deleteCartItem(this, 2)' lang='" + dt.data.cartList[i].cartProductId + "'></span>" + */
- "</div></li>";
- }
- }
- payTotal = dt.data.total;
- $("#pay-total").html((dt.data.total / 100).toFixed(2));
- $("#go-clearing").html("去结算(" + sumCount + ")");
- $("#cart-list-ul").append(noteLi);
- }
- }
- },
- error:function(xhr,type,errorThrown){
- errorfn(xhr);
- }
- });
- }
- // 购物项减
- function subCartNum(cartId) {
- // 当前数量
- var nowNum = $("#cartIdInput" + cartId).val();
- $.trim(nowNum);
- var r = /^[1-9]+[0-9]*$/;
- if (!r.test(nowNum)) {
- return false;
- }
- // 转换后的数量
- var pId = $("#cartIdInput" + cartId).attr("dir");
- var num = parseInt(nowNum);
- if (num > 1) {
- num = num - 1;
- mui.ajax('/iamberry/wechat/cart/deleteCart',{
- data:{"productId":pId, "cartNum":1},
- dataType:'json',//服务器返回json格式数据
- type:'GET',//HTTP请求类型
- timeout:15000,//超时时间设置为15秒;
- success:function(dt){
- if (dt.status == true) {
- $("#cartIdInput" + cartId).val(num);
- $("#cartIdInput" + cartId).prop("lang", num);
- var price = parseInt($("#cartIdInput" + cartId).attr("price"));
- // 小计
- $("#sub-total" + cartId).html("小计:¥" + (price * num / 100).toFixed(2));
- reSetTotal();
- } else {
- alert(dt.message);
- }
- },
- error:function(xhr,type,errorThrown){}
- });
- }
- }
- // 购物车数量加
- function addCartNum(cartId) {
- // 当前数量
- var nowNum = $("#cartIdInput" + cartId).val();
- $.trim(nowNum);
- var r = /^[1-9]+[0-9]*$/;
- if (!r.test(nowNum)) {
- return false;
- }
- // 转换后的数量
- var num = parseInt(nowNum);
- if (num < 999) {
- num = num + 1;
- var pId = $("#cartIdInput" + cartId).attr("dir");
- mui.ajax('/iamberry/wechat/cart/addCart',{
- data:{"productId":pId, "carNum":1},
- dataType:'json',//服务器返回json格式数据
- type:'GET',//HTTP请求类型
- timeout:15000,//超时时间设置为15秒;
- success:function(dt){
- if (dt.status == true) {
- $("#cartIdInput" + cartId).val(num);
- $("#cartIdInput" + cartId).prop("lang", num);
- var price = parseInt($("#cartIdInput" + cartId).attr("price"));
- // 小计
- $("#sub-total" + cartId).html("小计:¥" + (price * num / 100).toFixed(2));
- reSetTotal();
- } else {
- alert(dt.message);
- }
- },
- error:function(xhr,type,errorThrown){
- errorfn(xhr);
- }
- });
- }
- }
- // 删除购物车
- function deleteCartItem(obj, isParent) {
- var btnArray = ['否', '是'];
- mui.confirm('您确定要删除吗?', '提示', btnArray, function(e) {
- if (e.index == 1) {
- mui.ajax('/iamberry/wechat/cart/deleteCartByProductId',{
- data:{"productId":$(obj).attr("lang"), "datestamp":new Date().getTime()},
- dataType:'json',//服务器返回json格式数据
- type:'GET',//HTTP请求类型
- timeout:15000,//超时时间设置为15秒;
- success:function(dt){
- if (dt.status == true) {
- if (isParent == 1) {
- $(obj).parent().parent().remove();
- } else {
- $(obj).parent().parent().remove();
- }
- $(obj).parent().remove();
- if ($("#cart-list-ul>li").length <= 0) {
- $(".gwc_null").show();
- $(".shopping-box").hide();
- $('.shoping-tt').hide();
- }
- var length = $(".mt-mr-che").length;
- $("#go-clearing").html("去结算(" + length + ")");
- $(".all_check").prop('checked', true);// 选中
- $(".mt-mr-che").prop('checked', true);
- reSetTotal();
- } else {
- alert(dt.message);
- }
- },
- error:function(xhr,type,errorThrown){
- errorfn(xhr);
- }
- });
- }
- })
- }
- // 从新计算总价
- function reSetTotal() {
- var input = $(".mui-input-numbox");
- var total = 0;
- for (var i = 0; i < input.length; i++) {
- var id = $(input[i]).attr("id");
- id = id + "btn";
- if ($("#"+id)[0].checked == true) {
- var price = parseInt($(input[i]).attr("price"));
- var num = parseInt($(input[i]).val());
- total += price * num;
- }
- }
- payTotal = total;
- //alert(payTotal)
- $("#pay-total").html((total / 100).toFixed(2));
- }
- mui('body').on('tap', '#cartActivity', function() {
- document.querySelector('.loading-bg').style.display = 'block';
- location.href = this.getAttribute('url');
- });
|