小程序商品立即購買
用于(yu)小程序(xu)的商品立即購(gou)買操作
使用步驟
第一步:配置接口地址
在 config.js 文件中找到(dao) config 設置
第二步:檢查接口定義
搜索 shop_buy_now 接口是(shi)否已(yi)經存在(zai)定義(yi)(yi)(有的小(xiao)程序(xu)模(mo)板已(yi)經定義(yi)(yi))
如果沒有定義,在 config 底部新增(zeng)以下(xia)代碼:
shopBuyNowUrl: getApiUrl('shop_buy_now'),并復制(zhi)紅(hong)色部分待用。如果已經存在,則直(zhi)接復制(zhi)使(shi)用即可。
第三步:接口調用請求
如果定義名稱不(bu)同,使(shi)用第(di)二步復(fu)制(zhi)的(de)內容替換紅(hong)框(kuang)部分
/**
* 加入購(gou)物車(che)(addCart) && 立即(ji)購(gou)買(buyNow) && 確認規(gui)格選擇(specType)
* 接收參(can)數
* 無(wu)需(xu)接(jie)收參數(shu),從頁面數(shu)據中取值
* 接口傳參(can)
* product_id:購買(mai)的商品ID
* product_num:購買的商(shang)品數量
* spec_value_id:購買的(de)商(shang)品規格ID
*/
onConfirmSubmit() {
let _this = this;
let detail = _this.data.detail;
// 提(ti)交數據
let postData = {
product_id: detail.aid,
product_num: _this.data.product_num,
spec_value_id: detail.spec_value_id ? detail.spec_value_id : 0
};
// 提交類型(xing)
let submitType = _this.data.buttonType;
// 加入購(gou)物車
if ('addCart' == submitType) {
App._requestPost(_this, App.globalData.config.shopAddCartUrl, postData,
function(res) {
_this.setData({
showBottomPopup: !_this.data.showBottomPopup,
cart_total_num: res.data.cart_total_num
});
App.showSuccess('已(yi)添加(jia)至購物車(che)');
},
function(res) {
App.showError(res.msg);
}
);
}
// 立即購買(mai)
else if ('buyNow' == submitType) {
App._requestPost(_this, App.globalData.config.shopBuyNowUrl, postData,
function(res) {
wx.redirectTo({ url: res.url });
},
function(res) {
App.showError(res.msg);
}
);
}
// 確認規格(ge)選擇(ze)
else if ('specType' == submitType) {
_this.setData({
showBottomPopup: !_this.data.showBottomPopup
})
}
},
接口傳值
product_id:購買的商品ID,必傳
product_num:購買的商品數量,必傳
spec_value_id:購買的商品規格ID,選填,無規格傳(chuan)0
文檔最后更新時間:2026-01-13 14:40:50
← 小程序商品加入購物車
小程序商品下單展示 →
未解決你的問題?請到「問答社區」反饋你遇到的問題
