// 全选按钮状态
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 += "
" +
"
" +
"
" +
"
" + dt.data.cartList[i].productName + "
" +
"
产品下架
" +
"
" +
"
" +
/* "
" + */
"
";
} else {
sumCount ++; // 有效购物车数量
total = (dt.data.cartList[i].productPrice * dt.data.cartList[i].cartNum / 100).toFixed(2); // 小计
noteLi += "" +
"" +
"

" +
"
" +
"
" + dt.data.cartList[i].productName + "
" +
"
颜色:"+pro_color+"" +
"
¥" + ((dt.data.cartList[i].productPrice / 100).toFixed(2)) + "小计:¥" + total + "
" +
"
" +
"
" +
"" +
"" +
"" +
"
" +
"
" +
"
" +
/* "
" + */
"
";
}
}
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');
});