《仿盒马》app开发技术分享-- 购物车基础功能实现(13)

鸿蒙小林
• 阅读 2

技术栈

Appgallery connect

开发准备

上一节我们实现了加入购物车和购物车列表的简单展示。对一个电商类的应用来说,这很显然是不够的,我们的购物车内容应该更加的丰富,他需要用户能自主的去选择想要结算的商品,删除一些不需要的商品,或者取消掉一些本次不结算的商品,同时根据选择的不同,我们需要把相对应的价格和选择的数量等信息传递给用户,帮助用户节省更多的时间。

功能分析

1.商品选中 在表中我们定义了多个参数来帮助我们更好的去实现功能,这里我们需要用到这些参数来进行条件判断,当商品选中和未选中时候我们需要修改数据源中isNeedPay的状态的,然后渲染列表,以这样的方式去实现。

2.商品加减 商品的新增和减少我们也需要针对数据源进行操作,增减我们会控制事先预留的buyAmount字段进行加减,同时要对数据做好判断,不要出现减为负数的情况,同时增减都要刷新列表对应条目的状态

3.商品删除 商品删除我们使用ListItem的swipeAction 去实现,我们只需要定义好删除的ui样式,把相关逻辑写到点击事件中即可

4.价格计算 计算选中商品的价格和数量,并且在选中和增减的事件里调用,只要状态变化价格就要有对应的变化。

代码实现

import { CartProductList } from "../entity/CartProductList" import { cloudDatabase } from "@kit.CloudFoundationKit"; import { cart_product_list } from "../clouddb/cart_product_list"; import { hilog } from "@kit.PerformanceAnalysisKit"; import showToast from "../utils/ToastUtils";

let databaseZone = cloudDatabase.zone('default');

@Preview @Component export struct CartList {

@State productList:CartProductList[]=[]

@State flag:boolean=false

@State checkCount:number=0 @State allPrice:number=0 @State allOriginalPrice:number=0

private scroller: ListScroller = new ListScroller(); async aboutToAppear(): Promise {

let condition = new cloudDatabase.DatabaseQuery(cart_product_list);
let listData = await databaseZone.query(condition);
let json = JSON.stringify(listData)
this.productList= JSON.parse(json)
hilog.info(0x0000, 'testTag', `Succeeded in upserting data, result: ${listData}`);
this.getCountPrice()
this.flag=true

} @Builder itemEnd(item:CartProductList) { Row() { Button('删除').margin(4) .onClick(()=>{ let index = this.productList.indexOf(item); this.productList.splice(index, 1); }) }.padding(10).justifyContent(FlexAlign.SpaceEvenly) } build() { Column(){ if (this.flag){ List({scroller:this.scroller}){ ForEach(this.productList,(item:CartProductList,index:number)=>{ ListItem(){ Row(){ Image(item.isNeedPay?$r('app.media.cart_check'):$r('app.media.cart_no_check')) .height(20) .width(20) .objectFit(ImageFit.Contain) .onClick(()=>{ if (item.isNeedPay) { item.isNeedPay=false this.productList[index] = new CartProductList(item.id, item.productId, item.productSpecId, item.productName, item.productSpecName, item.buyAmount, item.isNeedPay,item.activityType,item.productImgAddress, item.productOriginalPrice, item.productPrice, item.couponPrice) }else { item.isNeedPay=true this.productList[index] = new CartProductList(item.id, item.productId, item.productSpecId, item.productName, item.productSpecName, item.buyAmount, item.isNeedPay,item.activityType,item.productImgAddress, item.productOriginalPrice, item.productPrice, item.couponPrice) this.getCountPrice() }

               })
             Image(item.productImgAddress)
               .height(120)
               .width(120)
               .margin({left:10})
             Column(){
               Text(item.productName)
                 .fontColor(Color.Black)
                 .fontSize(16)

               Text(item.productSpecName)
                 .fontColor(Color.Grey)
                 .fontSize(14)

               Text(){
                 Span("¥ ")
                   .fontSize(14)
                   .fontColor(Color.Red)
                 Span(item.productPrice+"")
                   .fontSize(22)
                   .fontColor(Color.Red)
               }

               Text("¥"+item.productOriginalPrice+"")
                 .fontColor('#999')
                 .decoration({
                   type: TextDecorationType.LineThrough,
                   color: Color.Gray
                 })
                 .fontSize(16)
                 .margin({left:10})
             }

             Row(){
               Text(" - ")
                 .textAlign(TextAlign.Center)
                 .border({width:0.5,color:Color.Gray})
                 .fontSize(14)
                 .height(20)
                 .padding({left:7,right:7})
                 .fontColor(Color.Black)
                 .onClick(()=>{
                   if (item.buyAmount==1) {
                     showToast("已经是最小数量了")
                   }else {
                     item.buyAmount--
                     this.productList[index] = new CartProductList(item.id, item.productId, item.productSpecId, item.productName, item.productSpecName,
                       item.buyAmount, item.isNeedPay,item.activityType,item.productImgAddress,
                       item.productOriginalPrice, item.productPrice, item.couponPrice)
                     this.getCountPrice()

                   }
                 })
                 .borderRadius({topLeft:5,bottomLeft:5})

               Text(item.buyAmount+"")
                 .textAlign(TextAlign.Center)
                 .fontColor(Color.Black)
                 .fontSize(14)
                 .height(20)
                 .padding({left:20,right:20})
                 .border({width:0.5,color:Color.Gray})


               Text(" + ")
                 .textAlign(TextAlign.Center)
                 .fontColor(Color.Black)
                 .fontSize(14)
                 .height(20)
                 .padding({left:7,right:7})
                 .onClick(()=>{
                   item.buyAmount++
                   this.productList[index] = new CartProductList(item.id, item.productId, item.productSpecId, item.productName, item.productSpecName,
                     item.buyAmount, item.isNeedPay,item.activityType,item.productImgAddress,
                     item.productOriginalPrice, item.productPrice, item.couponPrice)
                   this.getCountPrice()
                 })
                 .border({width:0.5,color:Color.Gray})
                 .borderRadius({topRight:5,bottomRight:5})

             }
             .justifyContent(FlexAlign.SpaceBetween)
         }
         .padding(10)
         .alignItems(VerticalAlign.Center)
        }
        .swipeAction({
          end: {
            builder: () => { this.itemEnd(item) },
            actionAreaDistance: 56
          }
        })

      })

    }

    .height('auto')
    .layoutWeight(1)
  }
  Row(){

    Text("合计:")
      .fontColor(Color.Black)

    Text(){
      Span("¥ ")
        .fontSize(14)
        .fontColor(Color.Red)
      Span(this.allPrice.toFixed(2))
        .fontSize(22)
        .fontColor(Color.Red)
    }

    Blank()


    Text("去结算"+"("+this.checkCount+")")
      .fontColor(Color.White)
      .padding(10)
      .borderRadius(20)
      .backgroundColor('#ccfa1818')
  }
  .justifyContent(FlexAlign.SpaceBetween)
  .padding(10)
}

}

getCountPrice(){ this.allPrice=0 this.allOriginalPrice=0 this.checkCount=0 for (let i = 0; i < this.productList.length; i++) { if (this.productList[i].isNeedPay) { this.checkCount+=this.productList[i].buyAmount this.allPrice+=this.productList[i].productPricethis.productList[i].buyAmount this.allOriginalPrice+=this.productList[i].productOriginalPricethis.productList[i].buyAmount }

}

}

}

到这里列表的展示价格的计算都已经实现了

点赞
收藏
评论区
推荐文章
CuterCorley CuterCorley
4年前
Django+Vue开发生鲜电商平台之10.购物车、订单管理和支付功能
@toc很多机遇是外界赋予的,这方面我们自己觉得很幸运,所以更加不能浪费这个机会,应该想得更多。而不能说你现在得到的是自然的,别人打不赢你,我们从来都会很担心,不会觉得自己很强。——马化腾Github和Gitee代码同步更新:;。一、购物车功能实现1.加入购物车功能实现购物车需要实现在商品详情页面将该商品加入购物车后,右上角同步
鸿蒙小林 鸿蒙小林
7小时前
《仿盒马》app开发技术分享-- 商品详情页(10)
技术栈Appgalleryconnect开发准备上一节我们实现了自定义标题栏和商品详情的数据接收,我们已经拿到了想要的数据,这一节我们要丰富商品详情页的内容。商品详情页面我们需要展示的是商品的各个属性参数、商品的图片、商品规格、活动详情等功能分析商品详情页
鸿蒙小林 鸿蒙小林
7小时前
《仿盒马》app开发技术分享-- 加入购物车&加购列表展示(12)
技术栈Appgalleryconnect开发准备上一节我们实现了商品详情页面的规格选择弹窗,这在任何购物类应用中都是最常用的功能之一。当然了,作为一个购物类的应用,我们仅仅展示是用处不大的,我们还需要有添加的动作。这一节我们就来实现添加到购车里并且在购物车
鸿蒙小林 鸿蒙小林
7小时前
《仿盒马》app开发技术分享-- 购物车功能完善(14)
技术栈Appgalleryconnect开发准备上一节我们实现了购物车商品列表的状态切换,已添加商品数量的增减,已添加商品滑动删除,已添加商品在选中情况下的价格计算。这一节我们在这些功能的基础上实现云端记录,因为我们现在只有数据的查询是从云端获取的,其他的
鸿蒙小林 鸿蒙小林
7小时前
《仿盒马》app开发技术分享-- 分类模块顶部导航列表(15)
技术栈Appgalleryconnect开发准备上一节我们实现了购物车商品列表的大部分功能,实现了商品的添加、删除、增减、价格计算等业务,并且都跟云端进行通信。现在我们继续对项目进行改造,这一节我们要改造的内容是分类页,这个页面我们在之前的非端云一体化项目
鸿蒙小林 鸿蒙小林
48分钟前
《仿盒马》app开发技术分享-- 确认订单页(数据展示)(29)
技术栈Appgalleryconnect开发准备上一节我们实现了地址的添加,那么有了地址之后我们接下来的重点就可以放到订单生成上了,我们在购物车页面,点击结算会跳转到一个订单确认页面,在这个页面我们需要有地址选择、加购列表展示、价格计算、优惠计算、商品数量
鸿蒙小林 鸿蒙小林
48分钟前
《仿盒马》app开发技术分享-- 购物车业务逻辑完善(34)
技术栈Appgalleryconnect开发准备之前我们已经实现了购物车相关的内容,实现了购物车数据列表的展示,但是我们结算订单之后我们的购物车列表并没有刷新,而且底部的状态栏并没有明显的数据展示来提醒用户,而且当我们在商品详情页添加新商品,底部也没有同步
鸿蒙小林 鸿蒙小林
48分钟前
《仿盒马》app开发技术分享-- 购物车逻辑优化(39)
技术栈Appgalleryconnect开发准备我们的app主要购物功能已经开发的相对来说比较完善了,接下来就针对各个功能的逻辑性进行迭代和修改,让我们的程序更加的健壮,减少一些逻辑上的bug功能分析在之前的开发中我们的购物车功能已经实现,但是在后来的使用
鸿蒙小林 鸿蒙小林
48分钟前
《仿盒马》app开发技术分享-- 旧物回收页(静态)(40)
技术栈Appgalleryconnect开发准备上一节我们进行了购物车业务逻辑的优化,使我们的程序变得更加健壮,这一节我们将要开始电商业务以外的内容,旧物回收,这是一个全新的业务模块,我们将要在这里实现对应的,回收金,积分,回收业务相关内容功能分析要想实现
鸿蒙小林 鸿蒙小林
50分钟前
《仿盒马》app开发技术分享-- 兑换页地址商品展示(71)
技术栈Appgalleryconnect开发准备上一节我们实现了商品兑换的校验功能,这能很好的帮助用户节省更多的时间,同时也能减小服务器的开销,同时我们的业务逻辑也会更加的完善功能也更加的丰富了,这一节我们实现校验通过后的内容,实现地址的选择和兑换商品信息