微信小程序图书交易商城云开发实例——书易(七):购物车

微信小程序商城云开发实例——书易

总览博客地址:https://blog.csdn.net/qq_44827933/article/details/130672061
项目github地址:https://github.com/xsfmGenius/BookChange

请添加图片描述

wxml
<view class="title">
  <text>购物车</text>
</view>
<view class='right-container'>
    <block wx:for="{{categoryBook}}" wx:key="index">
      <view class='category-box' bindtap='gotoxiangqing' data-bookname="{{item.bookname}}">
        <view class='category-left'>
          <image src="{{item.bookcover}}"></image>
        </view>
        <view class='category-right'>  
          <text class='bookname'>{{item.bookname}}</text>
          <view class="deleteicon" catchtap="delete" data-bookname="{{item.bookname}}" data-num="{{item.num}}" data-price="{{item.price}}" data-index="{{index}}">
            <image src="../../image/delete.png"></image>
          </view>
          <view class="numcontrol">
            <view catchtap="add" data-bookname="{{item.bookname}}" data-num="{{item.num}}" data-price="{{item.price}}" data-index="{{index}}">
              <image src="../../image/jia.png"></image>
            </view>
            <view class="booknum">
              <text>{{item.num}}</text>
            </view>
            <view catchtap="minus" data-bookname="{{item.bookname}}" data-num="{{item.num}}" data-price="{{item.price}}" data-index="{{index}}">
              <image src="../../image/jian.png"></image>
            </view>
          </view>
          <text class="bookprice">¥{{filters.toFix(item.price)}}</text>
        </view>
      </view>
    </block>
  </view>

<!-- 底边栏 -->
<view class='bottom-container'>
  <view></view>
  <view>
    <text class="bookprice">¥{{filters.toFix(allprice)}}</text>
  </view>
  <view>
    <button class='go' bindtap="buy">立即购买</button>
  </view>
</view>
wxss
.title{
  font-size: 38rpx;
  margin-bottom:5rpx;
  color: #000;
  background-color: #fff;
  letter-spacing:10rpx;
  font-weight:bold;
  padding-left:30rpx;
  padding:10rpx 0 10rpx 40rpx;
  /* letter-spacing:7rpx; */
}

.right-container{
  width:750rpx;
  display: block;
  justify-content: center;
  align-items: center;
  background-color: #fff;
}
.category-box{
  display: flex;
  height:250rpx;
  background:#fff;
}
.category-left{
  display: flex;
  width:30%;
  height:100%;
}
.category-left image{
  width: 90%;
  height: 90%;
  margin: auto;
}
.category-right{
  width: 70%;
  height:100%;
  margin:34rpx 0 0 10rpx;
  flex-flow: column;
  justify-content: space-between;
}
.deleteicon{
  height:50rpx;
  width:50rpx;
  position: relative;
  bottom:40rpx;
  left:430rpx;
}
.deleteicon image{
  width:100%;
  height:100%;
}
.numcontrol{
  display:inline-block;
  height:50rpx;
  width:200rpx;
  margin-left:10rpx;
  /* background-color: #3bcab2; */
  border-radius: 40rpx;
  border-style: solid;
  border-color:rgb(220,220,220);
}
.numcontrol view{
  display:inline-block;
  align-items: center;
  justify-content: center;
  text-align: center; 
  vertical-align:middle;
}
.numcontrol image{
  width:80%;
  height:80%;
  align-items: center;
  justify-content: center;
  text-align: center; 
  vertical-align:middle;
}
.numcontrol view:first-child{
  width:25%;
  height:100%;
  border-right-style: solid;
  border-color:rgb(220,220,220);
}
.numcontrol view:nth-child(2){
  width:45%;
  height:100%;
  text-align: center; 
  vertical-align:bottom;
  /* padding-bottom:20rpx; */
}

.booknum text{
  height:80%;
  width:100%;
  font-size: 38rpx;
  
}
.numcontrol view:nth-child(3){
  width:25%;
  height:100%;
  border-left-style: solid;
  border-color:rgb(220,220,220);
}
.bookname{
  display:block;
  font-size: 30rpx;
  margin:20rpx 40rpx 0 10rpx; 
  color: #000;
}
.bookprice{
  margin-left:120rpx;
  color:#3bcab2;
  font-size:40rpx;
}


/* 底边栏 */
.bottom-container{
  height:90rpx;
  width:750rpx;
  display: flex;
  line-height:80rpx;
  position:fixed;
  bottom:0px;
  left:0px;
  background-color: #fff;
  border-top: solid;
  border-color:rgb(220,220,220);
  border-width: 3rpx;
}
.bottom-container view:first-child{
  height:100%;
  width:20%;
  display:flex;
  align-items:center;
  justify-content: center;
  /* border-right:solid;
  border-color:rgb(220,220,220);
  border-width:1rpx;  */
}
.bottom-container view:nth-child(2){
  height:100%;
  width:50%;
  display:flex;
  align-items:center;
  justify-content: center;

}
.bookprice{
  vertical-align: middle;
  align-items: center;
  justify-content: center;
}
.bottom-container view:nth-child(3){
  display:flex;
  height:100%;
  width:30%;
}
.go{
  width:100%;
  height:100%;
  font-size: 28rpx;
  letter-spacing:2rpx;
  background-color: #45b97c;
  color:#fff;
  border-radius: 0rpx;
  border-style: none;
}
.go:active{
  background-color: #52ac7c;
}
javascript

加载界面时查询cart数据库获取用户购物车数据,并计算价格总和,显示在界面上。

  onLoad(options) {
    // console.log(app.globalData.userInfo)
    db.collection('cart').where({
        username:app.globalData.userInfo.nickName
    }).get()
    .then(res => {
      this.setData({
        categoryBook:res.data
       })
       var all=0;
       var keys = Object.keys(this.data.categoryBook)
       keys.forEach((item)=> {
        //  console.log('ssssssssssssssssssss',item)
        //  console.log("参数名:" + item,"参数值:"+this.data.categoryBook[item])
         all+=this.data.categoryBook[item].num*this.data.categoryBook[item].price
       })
        // console.log("输出",all)
        this.setData({
          allprice:all
        })
        // console.log("输出",this.data.allprice)
      })
      .catch(err => {
        console.log(err)
      })
  }

点击图书跳转至对应详情页

  // 购物车->界面跳转
  gotoxiangqing(e){
    // console.log(e.currentTarget.dataset.bookname)
    wx.navigateTo({
      url: '/pages/xiangqing/xiangqing?name='+e.currentTarget.dataset.bookname,
    })
},

点击删除图标删除数据库cart的数据,并修改界面中图书总价及图书数据。

  // 删除图书
  delete(e){
    // console.log(e)
    var nowprice=this.data.allprice-e.currentTarget.dataset.num*e.currentTarget.dataset.price
    var nowcate= this.data.categoryBook
    nowcate.splice(e.currentTarget.dataset.index,1)
    this.setData({
      categoryBook:nowcate
    })
    // this.data.categoryBook.splice(this.data.allprice-e.currentTarget.dataset.key,1)
    this.setData({
      allprice:nowprice
    })
    db.collection('cart').where({
      username:app.globalData.userInfo.nickName,
      bookname:e.currentTarget.dataset.bookname
    }).remove()
    .then(res => {
    })
    .catch(err => {
      console.log(err)
    })
  },

点击加号,使对应图书数量加一,修改购物车数据及界面数量、总价数据;点击减号,判断当前图书数量是否大于1,大于1则使对应图书数量减一,修改购物车数据及界面数量、总价数据。

  // 图书加一
  add(e){
    // console.log(e)
    var nowprice=this.data.allprice+e.currentTarget.dataset.price
    var nowcate= this.data.categoryBook
    nowcate[e.currentTarget.dataset.index].num++
    var newnum=nowcate[e.currentTarget.dataset.index].num
    this.setData({
      categoryBook:nowcate
    })
    // this.data.categoryBook.splice(this.data.allprice-e.currentTarget.dataset.key,1)
    this.setData({
      allprice:nowprice
    })
    db.collection('cart').where({
      username:app.globalData.userInfo.nickName,
      bookname:e.currentTarget.dataset.bookname
    }).update({
      data:{
        num:newnum
      }
    })
    .then(res => {
    })
    .catch(err => {
      console.log(err)
    })
  },

  // 图书减一
  minus(e){
    if(e.currentTarget.dataset.num>1){
      var nowprice=this.data.allprice-e.currentTarget.dataset.price
      var nowcate= this.data.categoryBook
      nowcate[e.currentTarget.dataset.index].num--
      var newnum=nowcate[e.currentTarget.dataset.index].num
      this.setData({
        categoryBook:nowcate
     })
      // this.data.categoryBook.splice(this.data.allprice-e.currentTarget.dataset.key,1)
      this.setData({
        allprice:nowprice
     })
      db.collection('cart').where({
       username:app.globalData.userInfo.nickName,
       bookname:e.currentTarget.dataset.bookname
     }).update({
        data:{
         num:newnum
       }
      })
      .then(res => {
     })
      .catch(err => {
       console.log(err)
      })
     }
    
  },

点击“立即购买”按钮,将所有该用户购物车数据加入purchase数据库,并删除cart数据库内容,清空界面显示价格及图书数据,提示“购买成功”。

  // 立即购买
  buy(e){
    console.log(this.data.categoryBook)
    var keys = Object.keys(this.data.categoryBook)
    keys.forEach((item)=> {
      db.collection('purchase').add({
        data:{
          bookname:this.data.categoryBook[item].bookname,
          username:app.globalData.userInfo.nickName,
          bookcover:this.data.categoryBook[item].bookcover,
          num:this.data.categoryBook[item].num,
          price:this.data.categoryBook[item].price
        }
      }).then(res => {
      })
      .catch(err => {
        console.log(err)
      })

      db.collection('cart').where({
        username:app.globalData.userInfo.nickName,
        bookname:e.currentTarget.dataset.bookname
      }).remove()
      .then(res => {
      })
      .catch(err => {
        console.log(err)
      })
    })
    wx.showToast({
      title: '购买成功',
    })
    this.setData({
      categoryBook:[],
      allprice:0,
    })
  },
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
技术选型1,前端微信小程序原生框架cssJavaScript2,管理后台云开发Cms内容管理系统web网页3,数据后台小程序云开发云函数云数据库云存储 1,小程序端1-1,首页首页有以下几个功能点点餐菜单浏览排号等位拨打电话顶部轮播图搜索菜品这里点餐分两种1,可以设置直接点餐直接点餐:适合小型饭店,或者奶茶类的快餐店。直接就可以下单,不用识别桌号2,也可设置扫码点餐扫码点餐适合中大型饭店,可以区分桌号,方便管理我后面会教大家如何生成桌号二维码,只需要把对应桌号的二维码贴在餐桌上,用户点击 扫码点餐 识别二维码,即可获取到桌号信息。1-2,菜品浏览页菜品浏览分两种1,不带分类适合菜品少的时候2,带分类菜品多的时候,带分类更方便客户选择不带分类 带分类 1-3,搜索功能我们这里搜索有两个触发方式1,直接点击搜索图标2,点击键盘上的搜索键1-4,搜索结果,支持模糊查询如我这里只搜‘鱼’,那么菜品中所有包含鱼的都可以搜索到 1-5,购物车首先菜品列表页可以直接添加商品到购物车购物车弹起后可以做如下操作1,增删单个菜品2,清空购物车3,删除菜品这些操作都和菜品列表是联动的,也就是菜品列表和购物车里增删个数,都是可以同步的。我会在项目预览章节的视频里做具体演示。1-6,下单页下单页就是确认订单后进行下单支付的。有以下功能1,点餐明细2,价格计算3,桌号地址4,就餐人数5,添加备注6,点击下单1-7,支付页支付页分两种方式1,模拟支付适合前期学习,毕业设计等演示类的场景。2,真实微信支付适合商用,但是使用微信支付必须要有营业执照,所以前期如果只是学习的话,建议使用模拟支付。 1-8,我的订单页我的订单页分以下几个状态1,新下单待上餐2,已上餐待评价3,订单完成4,订单取消1-9,提交评论页我们可以对店家进行评论。 1-10,评价列表页可以查看所有评价和自己的评价 1-11,排号等位可以看出,我们可以选择就餐人数,排大桌或者小桌。我这里已排小桌为例通过上图可以看出1,当前排号情况2,我的排号3,可以重新排号4,到号时会有到号提示后面我会把订阅消息功能加进来,这样到号后会有订阅消息提示。 1-12,个人中心个人中心分登录和未登录两种状态未登录已登录 1-13,微信授权登录小程序 2,后厨端和排号管理端2-1,后厨端主要供后厨的厨师使用1,可以查看当前新下单2,完成后可以操作菜品完成3,可以监听用户新下单4,有新订单时会有语音提示5,厨师登录页语音提示我会在视频课里具体演示厨师登录页 厨师管理页可以查看待制作订单用户新下单后,会有语音提示 2-2,排号管理页同样也有登录页,和上面厨师登录一样,这里重点看下排号管理页管理员可以查看当前排号情况,可以叫号。3,cms管理后台我们这里的可视化网页后台使用的时云开发自带的cms(内容管理)3-1,登录页 3-2,管理后台我们可以在这里 1,添加轮播图,删除轮播图,修改轮播图2,添加菜品,删除菜品,修改菜品,上架下架菜品3,管理订单4,查看评价5,管理后厨和排号管理员6,查看排号数据  比如我查询某个用户的所有订单 查询所有新下单还未上菜的订单 还有更多的功能,我会在视频课里给大家用视频来演示,这样更直观。 4,数据库数据库我们这里用云开发自带的云数据库餐厅管理员查看趋势图

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值