小程序購物車操作
小程序購物車操作
用(yong)于(yu)小程序(xu)購物車操作(數量(liang)遞增、數量(liang)遞減、選(xuan)中(zhong)單個(ge)、取消(xiao)(xiao)單個(ge)、選(xuan)中(zhong)全(quan)(quan)部、取消(xiao)(xiao)全(quan)(quan)部、刪(shan)除)
使用步驟
第一步:配置接口地址
在 config.js 文件中找到 config 設置
第二步:檢查接口定義
搜索 shop_cart_action 接(jie)口是否已(yi)(yi)經存在定(ding)義(yi)(有的(de)小程序模(mo)板已(yi)(yi)經定(ding)義(yi))
如果沒有定義,在 config 底部(bu)新(xin)增以下代碼:
shopCartActionUrl: getApiUrl('shop_cart_action'),并復制紅色部分(fen)待用(yong)。如(ru)果已經存在,則直接(jie)復制使用(yong)即可。
第三步:接口調用請求
如果定義名(ming)稱不同,使用第二步復(fu)制的內容(rong)替換紅(hong)框部分(fen)
/**
* 遞增指定(ding)的商品數(shu)量
* 接收參(can)數
* e 點擊對(dui)象參(can)數(shu)
* 接口傳(chuan)參
* action: 操作(zuo)方法(數量遞增(add))
* product_id: 操作(zuo)商品ID
* spec_value_id: 操作商品規格(ge)ID
*/
onAddCount(e) {
let _this = this,
index = e.currentTarget.dataset.index,
cart_list = _this.data.cart_list,
cartListData = _this.data.cart_list[index];
cartListData['product_num']++;
cart_list[index] = cartListData;
// 后端(duan)同步更新
wx.showLoading({title: '加載(zai)中(zhong)', mask: true});
let postData = {
action: 'add',
product_id: cartListData.product_id,
spec_value_id: cartListData.spec_value_id
};
App._requestApi(_this, App.globalData.config.shopCartActionUrl, postData,
function (result) {
_this.updateTotalPrice(cart_list);
wx.hideLoading();
}
);
},
/**
* 遞減指定的(de)商品數量
* 接收(shou)參數
* e 點(dian)擊對(dui)象參數
* 接口(kou)傳參(can)
* action: 操作(zuo)方(fang)法(數量遞減(less))
* product_id: 操作商品(pin)ID
* spec_value_id: 操作商品規格ID
*/
onSubCount(e) {
let _this = this,
index = e.currentTarget.dataset.index,
cart_list = _this.data.cart_list,
cartListData = _this.data.cart_list[index];
if (1 >= cartListData['product_num']) return false;
cartListData['product_num']--;
cart_list[index] = cartListData;
// 后端同步(bu)更新
wx.showLoading({title: '加載中', mask: true});
let postData = {
action: 'less',
product_id: cartListData.product_id,
spec_value_id: cartListData.spec_value_id
};
App._requestApi(_this, App.globalData.config.shopCartActionUrl, postData,
function (result) {
_this.updateTotalPrice(cart_list);
wx.hideLoading();
}
);
},
/**
* 選(xuan)中單個(ge)or取消單個(ge)
* 接口傳參
* action: 操(cao)作方(fang)法(selected)
* selected: 操(cao)作商品選中狀態
* product_id: 操作商品ID
* spec_value_id: 操作商品規格ID
*/
onChecked(e) {
let _this = this,
index = e.currentTarget.dataset.index,
cart_list = _this.data.cart_list,
cartListData = _this.data.cart_list[index];
wx.showLoading({title: '加載中', mask: true});
let postData = {
action: 'selected',
selected: cartListData.selected,
product_id: cartListData.product_id,
spec_value_id: cartListData.spec_value_id
};
App._requestApi(_this, App.globalData.config.shopCartActionUrl, postData,
function (result) {
cartListData['selected'] = cartListData['selected'] == 1 ? 0 : 1;
cart_list[index] = cartListData;
_this.updateTotalPrice(cart_list);
wx.hideLoading();
}
);
},
/**
* 選中全部(bu)(bu)or取(qu)消(xiao)全部(bu)(bu)
* 接口(kou)傳參
* action: 操作方法(all_selected)
* all_selected: 全部(bu)商品選中(zhong) or 全部(bu)商品取(qu)消
*/
onCheckedAll(e) {
let _this = this,
cart_list = _this.data.cart_list;
wx.showLoading({title: '加(jia)載(zai)中(zhong)', mask: true});
let postData = {
action: 'all_selected',
all_selected: !_this.data.checkedAll ? 1 : 0
};
App._requestApi(_this, App.globalData.config.shopCartActionUrl, postData,
function (result) {
cart_list.forEach(item => {
item.selected = !_this.data.checkedAll ? 1 : 0
});
_this.setData({ cart_list, checkedAll: !_this.data.checkedAll ? 1 : 0 });
_this.updateTotalPrice(cart_list);
wx.hideLoading();
}
);
},
/**
* 購物車商(shang)品刪除
* 接口傳參
* action: 操作方法(del)
* cart_id: 購物車商(shang)品(pin)ID
*/
onDelete(e) {
let _this = this;
let cart_ids = [e.currentTarget.dataset.aid];
wx.showLoading({title: '加載(zai)中', mask: true});
let postData = {
action: 'del',
cart_id: cart_ids
};
App._requestPost(_this, App.globalData.config.shopCartActionUrl, postData,
function (result) {
_this.getCartList();
App.showSuccess('刪除成(cheng)功');
wx.hideLoading();
},
function (result) {
App.showError(result.msg);
}
);
},
/**
* 更新(xin)購物車已(yi)選商品總價格
*/
updateTotalPrice(cart_list) {
let _this = this;
let cart_list_length = 0;
let cartTotalPrice = 0;
if (cart_list.length > 0) {
cart_list.forEach(item => {
if (1 == item.selected) {
cart_list_length++;
cartTotalPrice += (Number(item.users_price) * Number(item.product_num));
}
});
} else {
let cart_list = [];
}
_this.setData({
cart_list,
cartTotalPrice: cartTotalPrice.toFixed(2),
checkedAll: cart_list_length == cart_list.length
});
},
接口傳值
action:操作方法,必傳
add - 數量遞增
less - 數量遞減
selected - 選中單(dan)個/取消(xiao)單(dan)個
all_selected - 選中全部(bu)/取消全部(bu)
del - 刪除
product_id:商品ID,遞增/遞減/選中時必傳
spec_value_id:商品規格ID,遞增/遞減/選中時必傳
selected:商品選中狀態,選中單個時必傳
all_selected:全部選中狀態(1選中/0取消),選中全部時必傳
cart_id:購物車商品ID,刪除時必傳
文檔最后更新時間:2026-01-13 15:28:43
未解決你的問題?請到「問答社區」反饋你遇到的問題
