微信小程序实现购物车功能,包含完整小程序代码和运行效果截图

//获取应用实例

const app = getApp()

Page({

data: {

remark: ‘’,

goodsInfo: {},

receiveName: ‘’,

receiveMobile: ‘’,

receiveAddress: ‘’,

orderRemarks: ‘’,

couponUsed:“没有优惠劵”,

couponAmount: 0,//优惠金额

couponInfoListMap:[],

userCouponInfo:null,

goodsId: “”,

openId: “”,

goodsNum: 0,

goodsPrice: 0.0,//商品总金额

goodsLogisticsPrice: 0.0,

goodsTotalPrice: 0,//支付金额

currentColorIndex: 0,

currentColorId: “”,

currentColorText: “”,

currentLogisticsIndex: 0,

currentLogisticsType: 0,

currentLogisticsText: “”,

addCart: 0,

cartIds: “”,

goodsList: [],

curGoodsNums:{}

},

onLoad: function(opt) {

app.isLogin((data)=>{

let cartIdsValue = ‘’;

if(opt!=undefined){

cartIdsValue=opt.cartIds;

}

app.$post(app.API_ShowMyCart, {}, (res) => {

let cartIdsTmp = “”

for (let goods of res.data) {

//console.log(“goods.id=” + goods.id);

if (cartIdsTmp != “”){

cartIdsTmp = cartIdsTmp+“,”

}

cartIdsTmp = cartIdsTmp + goods.id

}

cartIdsValue = cartIdsTmp

//console.log(“cartIds=” + cartIdsValue);

this.GetGoodsInfo(cartIdsValue)

});

},‘1’)

},

onShow: function(opt){

this.onLoad();

},

selectLogistics(e) {

let index = e.currentTarget.dataset.index

let currentLogisticsText = e.currentTarget.dataset.logisticsname

let logisticsType = e.currentTarget.dataset.logisticstype

this.setData({

currentLogisticsIndex: index,

currentLogisticsType: logisticsType,

currentLogisticsText: currentLogisticsText

})

if (logisticsType != 2) {

this.setData({

goodsLogisticsPrice: 0

})

this.countTotalPrice()

} else {

this.getLogisticsFee()

}

},

getLogisticsFee() {

let orderParam = {

logisticsType: this.data.currentLogisticsType,

receiveAddress: this.data.receiveAddress

}

app.$post(app.API_getLogisticsFees, orderParam, (res) => {

if (res.statusCode == 0) {

let dataStatus = res.data.status

if (dataStatus == 2

  • 13
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序
微信小程序实现购物车功能代码主要分为两部分:前端和后端。前端代码实现用户界面的展示和交互,后端代码实现数据存储和处理。 前端代码: 1. 在页面的 .json 文件中添加购物车数据和购物车总价的变量 ``` { "data": { "cartList": [], // 购物车数据 "cartTotalPrice": 0 // 购物车总价 } } ``` 2. 在页面的 .wxml 文件中渲染购物车数据 ``` <view wx:for="{{cartList}}" wx:key="index"> <view>{{item.name}}</view> <view>{{item.price}}</view> <view>{{item.count}}</view> <view>{{item.totalPrice}}</view> </view> ``` 3. 绑定加入购物车事件,将商品数据添加到购物车数据中,并计算购物车总价 ``` onAddCart: function(event) { const item = event.currentTarget.dataset.item; let cartList = this.data.cartList; let index = cartList.findIndex(i => i.id === item.id); if (index === -1) { cartList.push({ id: item.id, name: item.name, price: item.price, count: 1, totalPrice: item.price }); } else { let count = cartList[index].count + 1; let totalPrice = count * item.price; cartList[index] = { ...cartList[index], count, totalPrice }; } let cartTotalPrice = cartList.reduce((total, i) => total + i.totalPrice, 0); this.setData({ cartList, cartTotalPrice }); } ``` 4. 绑定移除购物车事件,将商品数据从购物车数据中删除,并计算购物车总价 ``` onRemoveCart: function(event) { const item = event.currentTarget.dataset.item; let cartList = this.data.cartList; let index = cartList.findIndex(i => i.id === item.id); if (index !== -1) { cartList.splice(index, 1); } let cartTotalPrice = cartList.reduce((total, i) => total + i.totalPrice, 0); this.setData({ cartList, cartTotalPrice }); } ``` 后端代码: 1. 创建一个云数据库集合 cart,用于存储购物车数据 2. 在云函数中编写添加购物车数据的函数 ``` const cloud = require('wx-server-sdk') cloud.init() const db = cloud.database() exports.main = async (event, context) => { const { id, name, price } = event try { const res = await db.collection('cart').where({ id }).get() if (res.data.length > 0) { const count = res.data[0].count + 1 const totalPrice = count * price await db.collection('cart').doc(res.data[0]._id).update({ data: { count, totalPrice } }) } else { const count = 1 const totalPrice = price await db.collection('cart').add({ data: { id, name, price, count, totalPrice } }) } return { success: true } } catch (err) { return { success: false, errMsg: err.message } } } ``` 3. 在云函数中编写获取购物车数据的函数 ``` const cloud = require('wx-server-sdk') cloud.init() const db = cloud.database() exports.main = async (event, context) => { try { const res = await db.collection('cart').get() return { success: true, data: res.data } } catch (err) { return { success: false, errMsg: err.message } } } ``` 4. 在页面的 .js 文件中调用云函数,实现前端与后端的数据交互 ``` onAddCart: function(event) { const item = event.currentTarget.dataset.item; wx.cloud.callFunction({ name: 'add-to-cart', data: item, success: res => { console.log(res); this.getCartList(); }, fail: err => { console.log(err); } }); }, getCartList: function() { wx.cloud.callFunction({ name: 'get-cart-list', success: res => { console.log(res); let cartList = res.result.data; let cartTotalPrice = cartList.reduce((total, i) => total + i.totalPrice, 0); this.setData({ cartList, cartTotalPrice }); }, fail: err => { console.log(err); } }); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值