共计 723 个字符,预计需要花费 2 分钟才能阅读完成。
router.js
router.get(‘/removeCart’, initMiddleware, controller.default.cart.removeCart)
controller
app/controller/default/cart.js
async removeCart() {
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 || goodsResult.length == 0) {
this.ctx.redirect(‘/cart’);
} else {
var cartList = this.service.cookies.get(‘cartList’);
for (var i = 0; i < cartList.length; i++) {
if (cartList[i]._id == goods_id && cartList[i].color == color) {
cartList.splice(i, 1);
}
}
this.service.cookies.set(‘cartList’, cartList);
this.ctx.redirect(‘/cart’);
}
}
view
<span><a class=”delete” href=”/removeCart?goods_id=<%=cartList[i]._id%>&color=<%=cartList[i].color%>”> 删除 </a></span>
效果
删除前
删除后