温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在小程序中实现一个外卖订单界面

发布时间:2021-06-04 16:46:11 阅读:169 作者:Leah 栏目:web开发
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇文章给大家分享的是有关怎么在小程序中实现一个外卖订单界面,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

1.wxml   

<view class="container">
  <view class="index-cont">
   <!-- 左边类型 -->
   <view class="index-left">
    <view wx:for="{{foodsList}}" wx:key="index" class="item {{curId === 'item'+index?'on':''}}" data-id="item{{index}}" bindtap="scrollToViewFn">{{item.name}}</view>
   </view>
   <!-- 右边产品 -->
   <scroll-view class="index-right" scroll-y="{{true}}" scroll-into-view="{{initView}}" scroll-with-animation="true" bindscroll="onPageScroll">
    <view class="boxs">
     <block wx:for="{{foodsList}}" wx:key="index">
      <view class="index-title" id="item{{index}}">{{item.name}}</view>
      <view class="item" wx:for="{{item.list}}" wx:key="ind" wx:for-item="itm" wx:for-index="ind" bindtap="showGoodDetail(itm)">
       <view class="pic"><image src="{{itm.pic}}" mode="aspectFill"></image></view>
       <view class="main">
        <view class="tit">{{itm.title}}</view>
        <view class="desc">{{itm.info}}</view>
        <view class="money">&yen;{{itm.price}}</view>
       </view>
       <view class="box">
        <view wx:if="{{itm.num !== 0}}" class="icon" catchtap="reduceNum(index, ind, itm)"><image src="../../../static/images/reduce-icon.png" alt=""></image></view>
        <input wx:if="{{itm.num !== 0}}" type="text" disabled wx:model="{{itm.num}}"/>
        <view class="icon" catchtap="addNum(index, ind, itm)"><image src="../../../static/images/add-icon.png" alt=""></image></view>
       </view>
      </view>
     </block>
    </view>
   </scroll-view>
  </view>
  <view class="index-cart">
   <view class="left">
    <view class="cart-num" wx:if="{{cartList.length === 0}}">
     <image src="../../../static/images/cart.png"></image>
    </view>
    <view class="cart-num on" wx:else bindtap="showCartMask">
     <image src="../../../static/images/cart.png"></image>
     <text>{{totalNum}}</text>
    </view>
    <view class="cart-money">&yen;{{totalMoney}}</view>
   </view>
   <view class="order-btn" bindtap="submitOrder">去结算</view>
  </view>

  <!--购物车弹窗-->
  <view class="dialog" wx:if="{{isShowCartMask && cartList.length !== 0}}" bindtap="hiddenCartMak()">
   <view class="boxs" catchtap="stopMaopao()">
    <view class="title-block">
     <text>已选商品</text>
     <view class="clear" bindtap="clearCart"><image src="../../../static/images/del.png"></image>清空</view>
    </view>
    <scroll-view class="content" scroll-y="{{true}}" scroll-with-animation="true">
     <block wx:for="{{cartList}}" wx:key="index">
      <view class="item" id="{{item.view}}">
       <view class="tit">{{item.name}}</view>
       <view class="right">
        <text>&yen;{{item.price}}</text>
        <view class="box">
         <view class="icon" bindtap="reduceCart(index, item)"><image src="../../../static/images/reduce-icon.png" alt=""></image></view>
         <input type="text" disabled wx:model="{{item.num}}"/>
         <view class="icon" bindtap="addCart(index, item)"><image src="../../../static/images/add-icon.png" alt=""></image></view>
        </view>
       </view>
      </view>
     </block>
    </scroll-view>
   </view>
  </view>
  <!--商品详情弹窗-->
  <view class="dialog1" wx:if="{{isShowDetail}}">
   <scroll-view class="detbox" scroll-y="{{true}}" scroll-with-animation="true">
    <image class="img" src="{{goodDetail.pic}}" mode="aspectFit"></image>
    <view class="box">
     <view class="tit">{{goodDetail.title}}</view>
     <view class="money">&yen;{{goodDetail.price}}</view>
     <view class="desc">{{goodDetail.info}}</view>
    </view>
    <view class="close" bindtap="hideDetail"><image src="../../../static/images/close_ico.png"></image></view>
   </scroll-view>
  </view>
 </view>

2.script

createPage({
 data: {
  foodsList: [], // 商品数据
  cartList: [], // 购物车数据
  isShowCartMaskfalse,
  totalNum0,
  totalMoney0,
  initView'item0'// 根据此变量的变化,控制左侧选中状态、右侧滑动
  curId'item0',
  isShowDetailfalse,
  goodDetail: {},
  screenWidth0// 手机屏幕宽度
  heightArray: [0// 右侧每一个类型的高度区间数组
 },
 onLoad() {
  this.getGoodsData()
 },
 methods: {
  async getGoodsData() {
   const that = this
   const res = await getGoodsInfo({})
   this.foodsList = res
   wx.getSystemInfo({
    success(ress) => {
     that.screenWidth = ress.windowWidth
    }
   })
   this.getHeightSection()
  },
  // 设置高度区间 所有单位转化为rpx
  getHeightSection() {
   const that = this
   let hg = 0
   for (let index = 0; index < that.foodsList.length - 1; index++) {
    hg += 70 + that.foodsList[index].list.length * 212
    that.heightArray.push(hg)
   }
  },
  // 获取高度区间的下标
  getHeightIndex(arr, hg) {
   const that = this
   arr.forEach((item, index) => {
    if (hg >= item) {
     that.setData({
      curId'item' + index
     })
    }
   })
  },
  // 左边菜单控制右边
  scrollToViewFn(e) {
   this.setData({
    initView: e.target.dataset.id,
    curId: e.target.dataset.id
   })
  },
  // 右边滚动控制左边
  onPageScroll(e) {
   const that = this
   let scrollTop = e.detail.scrollTop * 750 / that.screenWidth
   this.getHeightIndex(that.heightArray, scrollTop)
  },
  // 商品列表的减号点击
  reduceNum(index, ind, item) {
   const that = this
   let val = 'foodsList[' + index + '].list[' + ind + '].num'
   this.setData({
    [val]: item.num - 1
   })
   // 如果商品为0,就把当前商品在购物车清除
   // 如果不为0, 就将当前商品数量减1
   if (that.foodsList[index].list[ind].num === 0) {
    that.removeAarry(that.cartList, item.id)
   } else {
    that.cartList.forEach((itm, i) => {
     if (itm.id === item.id) {
      let value = 'cartList[' + i + '].num'
      that.setData({
       [value]: itm.num - 1
      })
     }
    })
   }
   this.computed()
  },
  // 商品列表的加号点击
  addNum(index, ind, item) {
   const that = this
   let val = 'foodsList[' + index + '].list[' + ind + '].num'
   this.setData({
    [val]: item.num + 1
   })
   // 如果商品为1,就把当前商品加入购物车
   // 否则, 就将当前商品数量加1
   if (that.foodsList[index].list[ind].num === 1) {
    let val = { id: item.idname: item.titleprice: item.pricenum1index: index, ind: ind, pic: item.pic }
    that.cartList.push(val)
   } else {
    that.cartList.forEach((itm, i) => {
     if (itm.id === item.id) {
      let value = 'cartList[' + i + '].num'
      that.setData({
       [value]: itm.num + 1
      })
     }
    })
   }
   this.computed()
  },
  // 购物车的减号点击
  reduceCart(index, item) {
   const that = this
   let val = 'foodsList[' + item.index + '].list[' + item.ind + '].num'
   let val1 = 'cartList[' + index + '].num'
   this.setData({
    [val]: item.num - 1,
    [val1]: item.num - 1
   })
   // 如果商品为0,就把当前商品在购物车清除
   // 如果不为0, 就将当前商品数量减1
   if (that.cartList[index].num === 0) {
    that.removeAarry(that.cartList, item.id)
   }
   this.computed()
  },
  // 购物车的加号点击
  addCart(index, item) {
   const that = this
   let val = 'cartList[' + index + '].num'
   that.setData({
    [val]: item.num + 1
   })
   this.computed()
  },
  // 清空购物车
  clearCart() {
   const that = this
   wx.showModal({
    title'提示',
    content'清空购物车?',
    successfunction (res) {
     if (res.confirm) {
      that.setData({
       cartList: []
      })
      that.foodsList.forEach((item, i) => {
       item.list.forEach((itm, j) => {
        let value = 'foodsList[' + i + '].list[' + j + '].num'
        that.setData({
         [value]: 0
        })
       })
      })
      that.computed()
     }
    }
   })
  },
  // 计算选择商品总价格和总数量
  computed() {
   const that = this
   let num = 0
   let money = 0
   that.cartList.forEach(item => {
    num += item.num
    money += parseFloat(item.price) * item.num
   })
   that.setData({
    totalNum: num,
    totalMoney: money
   })
  },
  // 将数量为0的时候,对应商品在购物车中删除
  removeAarry(arr, id) {
   arr.forEach((item, index) => {
    if (item.id === id) {
     arr.splice(index, 1)
    }
   })
   return arr
  },
  showCartMask() {
   this.isShowCartMask = !this.isShowCartMask
  },
  hiddenCartMak() {
   this.isShowCartMask = false
  },
  stopMaopao() {
  },
  showGoodDetail(item) {
   this.goodDetail = item
   this.isShowDetail = true
  },
  hideDetail() {
   this.isShowDetail = false
  },
  // 订单提交
  submitOrder() {
  }
 }
})

3.css

<style lang='scss'>
@import '../../style/base.scss';
page {
 height100%;
}
.container {
 height100vh;
 background-color#fff;
 box-sizing: border-box;
 overflow: hidden;
  .dialog1{
   width100%;
   height100vh;
   position: fixed;
   top0;
   left0;
   background-colorrgba(0,0,00.5);
   z-index4;
    .detbox{
     position: fixed;
     bottom0;
     left0;
     right0;
     background-color#fff;
     width100%;
     max-height700rpx;
     overflow-y: auto;
     color#333;
     border-radius40rpx 40rpx 0 0;
      .img{
       width100%;
       height375rpx;
       backgroundrgba(0,0,0,0.6);
      }
      .box{
       padding20rpx 30rpx 40rpx;
       box-sizing: border-box;
        .tit{
         font-size28rpx;
         color#333;
         font-weight: bold;
        }
        .money{
         font-size26rpx;
         color#f00;
         margin10rpx 0;
        }
        .desc{
         font-size22rpx;
         color#666;
         line-height32rpx;
        }
      }
      .close{
       width50rpx;
       height50rpx;
       position: absolute;
       right20rpx;
       top20rpx;
       display: flex;
       align-items: center;
       justify-content: center;
        image{
         width40rpx;
         height40rpx;
        }
      }
    }
  }
  .dialog{
   width100%;
   height100vh;
   position: fixed;
   top0;
   left0;
   background-colorrgba(0,0,00.5);
   z-index2;
    .boxs{
     position: fixed;
     bottom80rpx;
     left0;
     right0;
     z-index6;
     background-color#fff;
     width100%;
     max-height600rpx;
     color#333;
      .title-block{
       padding0 30rpx;
       box-sizing: border-box;
       display: flex;
       align-items: center;
       justify-content: space-between;
       height70rpx;
       background#EEF0F1;
        text{
         font-size26rpx;
         color#666;
        }
        .clear{
         font-size22rpx;
         color#888;
         display: flex;
         align-items: center;
          image{
           width24rpx;
           height24rpx;
           margin-right10rpx;
          }
        }
      }
      .content{
       width100%;
       max-height530rpx;
       overflow-y: auto;
       padding-bottom30rpx;
       box-sizing: border-box;
        .item{
         width690rpx;
         height80rpx;
         line-height80rpx;
         margin0 auto;
         position: relative;
         display: flex;
         align-items: center;
         justify-content: space-between;
          &::after{
           position: absolute;
           width100%;
           height1rpx;
           background#f2f2f2;
           content'';
           bottom1rpx;
           left0;
          }
          .tit{
           width400rpx;
           overflow: hidden;
           text-overflow: ellipsis;
           white-space: nowrap;
           font-size28rpx;
           color#333;
          }
          .right{
           display: flex;
           justify-content: flex-start;
           align-items: center;
           height80rpx;
            text{
             font-size26rpx;
             color#f00;
            }
            .box{
             display: flex;
             justify-content: flex-start;
             align-items: center;
             flex-wrap: nowrap;
             margin-left20rpx;
             height80rpx;
              .icon{
               width34rpx;
               height34rpx;
               display: flex;
               align-items: center;
               justify-content: center;
                image{
                 width34rpx;
                 height34rpx;
                }
              }
              input{
               width60rpx;
               height34rpx;
               border: none;
               color#333;
               text-align: center;
               font-size26rpx;
              }
              
            }
          }
        }
      }
    }
  }
  .index-cont{
   heightcalc(100vh - 80rpx);
   display: flex;
   justify-content: space-between;
   .index-left{
    width160rpx;
    height100%;
    background#efefef;
     .item{
      font-size26rpx;
      color#333;
      border-bottom1rpx dashed #666;
      height80rpx;
      line-height80rpx;
      padding0 20rpx;
      box-sizing: border-box;
       &.on{
        background#fff;
       }
     }
   }
   .index-right{
    width590rpx;
    height100%;
     .boxs{
      padding0 30rpx;
      box-sizing: border-box;
      width100%;
     }
     .index-title{
      height70rpx;
      line-height70rpx;
      background#f7f7f7;
      padding-left30rpx;
      font-size26rpx;
      color#666;
      box-sizing: border-box;
     }
     .item{
      padding30rpx 0;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      position: relative;
      height212rpx;
       &::after{
        position: absolute;
        top0rpx;
        left0;
        background#ccc;
        width100%;
        height1rpx;
        content'';
       }
       .pic{
        width150rpx;
        height150rpx;
         image{
          width100%;
          height100%;
         }
       }
       .main{
        width380rpx;
        padding-left30rpx;
        box-sizing: border-box;
         .tit{
          font-size26rpx;
          color#333;
          font-weight: bold;
         }
         .desc{
          font-size22rpx;
          color#999;
          line-height30rpx;
          margin5rpx 0 10rpx;
          min-height65rpx;
         }
         .money{
          font-size28rpx;
          color#f00;
         }
       }
       .box{
        display: flex;
        justify-content: flex-start;
        align-items: center;
        flex-wrap: nowrap;
        margin-left10rpx;
        height34rpx;
        position: absolute;
        right0;
        bottom30rpx;
         .icon{
          width34rpx;
          height34rpx;
          display: flex;
          align-items: center;
          justify-content: center;
           image{
            width34rpx;
            height34rpx;
           }
         }
         input{
          width60rpx;
          height34rpx;
          border: none;
          color#333;
          text-align: center;
          font-size26rpx;
         }
         
       }
     }
   }
  }
  .index-cart{
   width100%;
   height80rpx;
   display: flex;
   align-items: center;
   justify-content: flex-start;
   position: relative;
   z-index3;
    .left{
     width470rpx;
     height100%;
     background#3e3a39;
     display: flex;
     align-items: center;
     justify-content: flex-start;
      .cart-num{
       width100rpx;
       height100rpx;
       background#6E6D6C;
       position: relative;
       padding:25rpx;
       box-sizing: border-box;
       border-radius100%;
       top: -30rpx;
       left22rpx;
        &.on{
         background: $base-color;
        }
        image{
         width50rpx;
         height50rpx;
        }
        text{
         font-size20rpx;
         color#fff;
         display: inline-block;
         padding0 9rpx;
         box-sizing: border-box;
         position: absolute;
         right3rpx;
         top: -3rpx;
         height30rpx;
         line-height30rpx;
         border-radius30rpx;
         background#f00;
        }
      }
      .cart-money{
       color#fff;
       font-size30rpx;
       margin-left50rpx;
      }
    }
    .order-btn{
     width280rpx;
     height100%;
     background: $base-color;
     font-size28rpx;
     color#fff;
     display: flex;
     align-items: center;
     justify-content: center;
    }
  }
}

4.ps

小程序使用mpx为框架;
商品列表数据根据接口获取,测试数据可以根据mock数据测试
实际数据类型是

goodLists: [
  {
    id: 'xx',  
    name: 'xx'// 商品类型
    list: [ // 当前商品类型对应的所有商品
      {
        id: 'xx',
        title: 'xx',
        pic: 'xx',
        price: 'xx',
        detail: 'xx',
        num: '' // num是为了我方便对商品加减操作,让后端加的
      }
    ]
  }
]

以上就是怎么在小程序中实现一个外卖订单界面,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×