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

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

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

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
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 小程序开发之跳转微信直播示例api(0个评论)
    • 在uni_app中开发小程序之常用功能示例代码汇总(0个评论)
    • 小程序开发之多端框架:taro(0个评论)
    • 微信小程序前端使用七牛云官方SDK上传七牛云代码示例(0个评论)
    • 百度小程序审核未通过,真机审核存在点击返回键退出小程序...解决方式之一tabBar(0个评论)
    • 近期文章
    • 如何优雅处理async await错误推荐:await-to-js库(0个评论)
    • lodash工具库(0个评论)
    • 在Laravel项目中使用中间件方式统计用户在线时长功能代码示例(0个评论)
    • 在Laravel中构建业务流程模型(0个评论)
    • windows系统中安装FFMpeg及在phpstudy环境php7.3 + php-ffmpeg扩展的使用流程步骤(0个评论)
    • 在go语言中对浮点的数组、切片(slice)进行正向排序和反向排序(0个评论)
    • 在go语言中对整数数组、切片(slice)进行排序和反向排序(0个评论)
    • 在go语言中对字符串数组、切片(slice)进行排序和反向排序(0个评论)
    • 最新国内免注册ChatGPT体验站_ChatGPT镜像站访问链接地址2023/3/28持续更新(0个评论)
    • 在Laravel项目中的实现无密码认证之:发送邮箱链接授权(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2017-10
    • 2018-01
    • 2020-03
    • 2021-06
    • 2021-10
    • 2022-03
    • 2023-02
    Top

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

    侯体宗的博客