侯体宗的博客
  • 首页
  • Hyperf2.1版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

小程序外卖订单界面的示例代码

微信(小程序)  /  管理员 发布于 2年前   168

1.效果界面




2.涉及功能

*左侧商品类型、右侧商品可以相互控制;
*商品列表加减及购物车商品加减icon消失、显示;
*商品每一次加减,页面视图变化(数量、价格变化、购物车置灰);

3.贴上所有代码

1.wxml   

            {{item.name}}                        {{item.name}}                            {{itm.title}}        {{itm.info}}        ¥{{itm.price}}                                                                                                 {{totalNum}}        ¥{{totalMoney}}      去结算                  已选商品     清空                          {{item.name}}               ¥{{item.price}}                                                                                          {{goodDetail.title}}     ¥{{goodDetail.price}}     {{goodDetail.info}}              

2.script

createPage({ data: {  foodsList: [], // 商品数据  cartList: [], // 购物车数据  isShowCartMask: false,  totalNum: 0,  totalMoney: 0,  initView: 'item0', // 根据此变量的变化,控制左侧选中状态、右侧滑动  curId: 'item0',  isShowDetail: false,  goodDetail: {},  screenWidth: 0, // 手机屏幕宽度  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.id, name: item.title, price: item.price, num: 1, index: 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: '清空购物车?',    success: function (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


										

  • 上一条:
    微信小程序下载工具及调试详解
    下一条:
    微信小程序搭建及解决登录失败问题
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • TP(3/5)
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 微信小程序前端使用七牛云官方SDK上传七牛云代码示例(0个评论)
    • 百度小程序审核未通过,真机审核存在点击返回键退出小程序...解决方式之一tabBar(0个评论)
    • 百度小程序详情页中使用wxapp-rich-text组件解析html标签并展示出来(0个评论)
    • 百度小程序列表中点击跳转详情页流程步骤(0个评论)
    • 百度小程序封装get post请求等全局函数及实现请求后端api数据循环展示功能(0个评论)
    • 近期文章
    • laravel中轻量化的钱包实现方案:Laravel Wallet(0个评论)
    • mysql性能优化之8种常见SQL错误用法(0个评论)
    • Laravel 9.13版本发布(0个评论)
    • beego+GeoLite2免费数据库获取ip地址经纬度等定位归属信息(0个评论)
    • redis安全配置之修改端口、添加密码流程步骤及启动使用(0个评论)
    • PHP + Memcache实现简单的统计当前在线人数功能(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • go语言中使用Signbit()函数判断一个整数是正数或负数(0个评论)
    • 删库跑路之一链家程序员删除公司9TB数据被判7年,望各大码农警之!(0个评论)
    • Laravel角色和权限:拦截器Gates和策略Policies的解释(0个评论)
    • 近期评论
    • 阿凡达123 在

      golang 怎么做热更新中评论 也可以看看这个:https://github.com/edwingeng/hot..
    • 博主 在

      hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 @路过的靓仔:cdn静态资源被墙,已修复..
    • GGGGGGGGG 在

      layui框架常用输入框介绍中评论 写的很好解决问题..
    • 路过的靓仔 在

      hyperf框架常用命令-在centos7中退出命令及在docker容器中退出命令中评论 剩下好多 wait 状态的..
    • 激光豆芽 在

      为什么你不能安逸?国内996为什么没有国外955香?中评论 国内现在无意义的内卷太多了..
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客