egg(95下)–egg之购物车之异步改变数量

31次阅读

共计 3515 个字符,预计需要花费 9 分钟才能阅读完成。

router.js
router.get(‘/incCart’, initMiddleware, controller.default.cart.incCart);

router.get(‘/decCart’, initMiddleware, controller.default.cart.decCart);
controller
app/controller/default/cart.js
增加
async incCart() {

var goods_id = this.ctx.request.query.goods_id;
var color = this.ctx.request.query.color;

var goodsResult = await this.ctx.model.Goods.find({“_id”: goods_id});
if (goodsResult.length == 0) {
this.ctx.body = {
“success”: false,
msg: ‘ 修改数量失败 ’
}
} else {

var cartList = this.service.cookies.get(‘cartList’);
var currentNum = 0; // 当前数量
var allPrice = 0; // 总价格
for (var i = 0; i < cartList.length; i++) {
if (cartList[i]._id == goods_id && cartList[i].color == color) {
cartList[i].num += 1;
currentNum = cartList[i].num;

}
if (cartList[i].checked) {
allPrice += cartList[i].price * cartList[i].num;
}
}
this.service.cookies.set(‘cartList’, cartList);

this.ctx.body = {
“success”: true,
num: currentNum,
allPrice: allPrice
}
}

}
减少
async decCart() {

var goods_id = this.ctx.request.query.goods_id;

var color = this.ctx.request.query.color;

var goodsResult = await this.ctx.model.Goods.find({“_id”: goods_id});
if (goodsResult.length == 0) {
this.ctx.body = {
“success”: false,
msg: ‘ 修改数量失败 ’
}
} else {

var cartList = this.service.cookies.get(‘cartList’);
var currentNum = 0; // 当前数量
var allPrice = 0; // 总价格
for (var i = 0; i < cartList.length; i++) {
if (cartList[i]._id == goods_id && cartList[i].color == color) {
if (cartList[i].num > 1) {
cartList[i].num -= 1;
}
currentNum = cartList[i].num;

}

if (cartList[i].checked) {
allPrice += cartList[i].price * cartList[i].num;
}
}
this.service.cookies.set(‘cartList’, cartList);

this.ctx.body = {
“success”: true,
num: currentNum,
allPrice: allPrice
}
}

}
view
app/view/default/cart.html
<div class=”cart_number”>
<div class=”input_left decCart” goods_id=”<%=cartList[i]._id%>” color=”<%=cartList[i].color%>”>-</div>

<div class=”input_center”>
<input id=”num” name=”num” readonly=”readonly” type=”text” value=”<%=cartList[i].num%>” />
</div>
<div class=”input_right incCart” goods_id=”<%=cartList[i]._id%>” color=”<%=cartList[i].color%>”>+</div>
</div>
前端 js
app/public/default/js/cart.js
(function($) {

var app = {
init: function() {
this.initCheckBox();

this.changeCartNum();
},
initCheckBox() {
$(“#checkAll”).click(function() {
if (this.checked) {
$(“:checkbox”).prop(“checked”, true);
} else {
$(“:checkbox”).prop(“checked”, false);
}
});

$(“.cart_list input:checkbox”).click(function() {
var chknum = $(“.cart_list input:checkbox”).size(); //checkbox 总个数

var chk = 0; //checkbox checked=true 总个数
$(“.cart_list input:checkbox”).each(function() {
if ($(this).prop(“checked”) == true) {
chk++;
}
});
if (chknum == chk) {// 全选
$(“#checkAll”).prop(“checked”, true);
} else {// 不全选
$(“#checkAll”).prop(“checked”, false);
}
});
},
changeCartNum() {

$(‘.decCart’).click(function() {

var goods_id = $(this).attr(‘goods_id’);

var color = $(this).attr(‘color’);

// alert(color);

$.get(‘/decCart?goods_id=’ + goods_id + ‘&color=’ + color, function(response) {
console.log(response);

if (response.success) {

$(“#allPrice”).html(response.allPrice + ‘ 元 ’);

$(this).siblings(‘.input_center’).find(‘input’).val(response.num);

var price = parseFloat($(this).parent().parent().siblings(‘.price’).html());

$(this).parent().parent().siblings(‘.totalPrice’).html(response.num * price + “ 元 ”);
}
}.bind(this)); // 注意 this 指向

})

$(‘.incCart’).click(function() {

var goods_id = $(this).attr(‘goods_id’);

var color = $(this).attr(‘color’);

$.get(‘/incCart?goods_id=’ + goods_id + ‘&color=’ + color, function(response) {

if (response.success) {

$(“#allPrice”).html(response.allPrice + ‘ 元 ’);

$(this).siblings(‘.input_center’).find(‘input’).val(response.num);

var price = parseFloat($(this).parent().parent().siblings(‘.price’).html());

$(this).parent().parent().siblings(‘.totalPrice’).html(response.num * price + ‘ 元 ’);
}
}.bind(this));

})

}

}

$(function() {
app.init();
})

})($)
效果

正文完
 0