《仿盒马》app开发技术分享-- 优惠券逻辑优化(58)

简介: 我们已经实现了优惠券的领取和展示,现在已经趋近于一个完整的电商应用了,但是这时候问题又来了,我们领取完优惠券之后,我们的新用户优惠券模块依然存在,他并没有消失,既然我们是从云数据库中查询的数据,那么我们需要找到一个字段跟他对应起来,来实现新用户领券后关闭这个模块的展示,同时我们在未登录的时候他也要保持隐藏,登录后能实现优惠券的领取。然后在结算的时候得出有几张符合的券能用

技术栈

Appgallery connect

开发准备

我们已经实现了优惠券的领取和展示,现在已经趋近于一个完整的电商应用了,但是这时候问题又来了,我们领取完优惠券之后,我们的新用户优惠券模块依然存在,他并没有消失,既然我们是从云数据库中查询的数据,那么我们需要找到一个字段跟他对应起来,来实现新用户领券后关闭这个模块的展示,同时我们在未登录的时候他也要保持隐藏,登录后能实现优惠券的领取。然后在结算的时候得出有几张符合的券能用

功能分析

因为我们的优惠券展示是用的组件的方式,首次我们没有登录的情况下组件因为并没有被销毁,aboutToAppear已经不会再执行了,所以获取不到user信息,我们需要解决这个问题,保证user信息能及时的获取,在表创建的时候我们预留了一个字段isvip,现在我们用它来判断是否为新用户即可,去到了确认订单页面我们获取到符合最小金额的可用优惠券

代码实现

首先我们在优惠券领取之后,使用回调的方式修改isvip的状态,同时接收登陆后传递的信息,我们传user信息进组件内

 @Link user: User|null
 private  callback?: () => void
  Button('立即领取', {
    type: ButtonType.Normal })
        .width(240)
        .height(40)
        .backgroundColor('#FF0000')
        .fontColor(Color.White)
        .borderRadius(20)
        .margin({
    bottom: 16 })
        .onClick(async ()=>{
   
            try {
   
              for (const item of this.couponList) {
   
                const coupon = new coupon_mall();
                coupon.id=Math.floor(Math.random() * 1000000);
                coupon.user_id=this.user!.user_id
                coupon.coupon_id=item.coupon_id
                coupon.price=Number(item.price)
                coupon.type=0
                coupon.limit_amount=item.limit_amount
                coupon.start_time=this.creatTime()
                coupon.end_time=this.endTime()
                coupon.type_str=item.type
                coupon.txt="全场商品通用"
                const databaseZone = cloudDatabase.zone('default');
                await databaseZone.upsert(coupon); // 等待当前操作完成
              }

              // 所有循环完成后执行
              showToast("优惠券领取成功");
              this.callback!()
            } catch (error) {
   
              hilog.error(0x0000, 'testTag', `插入失败: ${
   error}`);
              showToast("领取优惠券出错");
            }



        })

实现回调

CouponComponent({
   home_activity:this.home_new_people_coupon,couponList:this.couponList,callback: async ()=>{
   
                    let users=new user()
                    users.id=this.user!.id
                    users.user_id=this.user!.user_id
                    users.user_name=this.user!.user_name
                    users.psw=this.user!.psw
                    users.is_vip=false
                    users.user_code=this.user!.user_code
                    users.is_log_off=this.user!.is_vip
                    let num = await databaseZone.upsert(users);
                    StorageUtils.set("user",JSON.stringify(users))

                    if (num>0) {
   

                        let condition3 = new cloudDatabase.DatabaseQuery(user);
                        condition3.equalTo("user_name",this.user?.user_name).and().equalTo("psw",this.user?.psw)
                        let listData3 = await databaseZone.query(condition3);
                        let json3 = JSON.stringify(listData3)
                        let data3:User[]= JSON.parse(json3)
                        this.user=data3[0]

                    }
                  },user:this.user})
                    .visibility(this.user?.is_vip?Visibility.Visible:Visibility.None)

现在我们的显示隐藏需要用isvip来控制了,然后我们接收登陆后的用户信息

 let innerEvent2: emitter.InnerEvent = {
   
      eventId: 2001
    };
    let callback2: Callback<emitter.EventData> = async (eventData: emitter.EventData) => {
   
      console.info(`eventData: ${
   JSON.stringify(eventData)}`);
      const value = await StorageUtils.getAll('user');
      if (value != "") {
   
        this.user = JSON.parse(value)
      }else {
   
        this.userInfo=null
        this.user=null
      }
    }
    emitter.on(innerEvent2, callback2);

实现自后我们来到确认订单页面,先查询出可用的优惠券

let databaseZone = cloudDatabase.zone('default');
    let condition = new cloudDatabase.DatabaseQuery(coupon_mall);
    condition.equalTo("user_id", this.user?.user_id).and().equalTo("type",0)
    let listData = await databaseZone.query(condition);
    let json = JSON.stringify(listData)
    let data: CouponMall[] = JSON.parse(json)
    this.couponList = data

根据结算金额来筛选出有多少张优惠券能够使用

 getCoupon():number{
   
    let eligibleCoupons = this.couponList.filter(coupon =>
    coupon.limit_amount <= this.price()
    ).length;
    return eligibleCoupons
  }

修改ui

    Row(){
   
            Text("优惠券")
              .fontSize(14)
              .fontColor(Color.Black)

            if (this.getCoupon()>0){
   
              Text("可用"+this.getCoupon()+"张")
                .fontSize(14)
                .fontColor(Color.Red)
            }else {
   
              Text("无可用优惠券")
                .fontSize(14)
                .fontColor(Color.Black)
            }

          }
          .padding(10)
          .width('100%')
          .justifyContent(FlexAlign.SpaceBetween)

到这里我们的优惠券逻辑已经开始逐渐完善了

相关文章
|
16天前
|
存储
《仿盒马》app开发技术分享--未完成订单列表展示逻辑优化(61)
上一节我们实现订单与优惠券的联合提交时,我去到订单列表页面查看生成的订单信息,发现现在的订单从信息展示到价格计算全都是有问题的。所以紧急的把对应的问题修改一下。
105 70
|
17天前
|
算法 Unix 程序员
程序员行业的学历门槛与天赋密码:揭开大厂招聘的真相·优雅草卓伊凡
程序员行业的学历门槛与天赋密码:揭开大厂招聘的真相·优雅草卓伊凡
62 3
程序员行业的学历门槛与天赋密码:揭开大厂招聘的真相·优雅草卓伊凡
|
7天前
|
前端开发 C++ 容器
2025高效开发:3个被低估的CSS黑科技
2025高效开发:3个被低估的CSS黑科技
133 95
|
7天前
|
缓存 监控 前端开发
告别卡顿!3大前端性能优化魔法 + CSS容器查询实战
告别卡顿!3大前端性能优化魔法 + CSS容器查询实战
164 95
|
7天前
|
Web App开发 监控 前端开发
2025前端性能优化三连击
2025前端性能优化三连击
210 94
|
7天前
|
Web App开发 前端开发 JavaScript
CSS :has() 选择器:改变游戏规则的父选择器
CSS :has() 选择器:改变游戏规则的父选择器
193 95
|
14天前
|
监控 算法 关系型数据库
分布式事务难题终结:Seata+DRDS全局事务一致性架构设计
在分布式系统中,CAP定理限制了可用性、一致性与分区容错的三者兼得,尤其在网络分区时需做出取舍。为应对这一挑战,最终一致性方案成为常见选择。以电商订单系统为例,微服务化后,原本的本地事务演变为跨数据库的分布式事务,暴露出全局锁失效、事务边界模糊及协议差异等问题。本文深入探讨了基于 Seata 与 DRDS 的分布式事务解决方案,涵盖 AT 模式实践、分片策略优化、典型问题处理、性能调优及高级特性实现,结合实际业务场景提供可落地的技术路径与架构设计原则。通过压测验证,该方案在事务延迟、TPS 及失败率等方面均取得显著优化效果。
116 61
|
23天前
|
SQL 人工智能 Java
阿里云百炼开源面向 Java 开发者的 NL2SQL 智能体框架
Spring-ai-alibaba-nl2sql 是析言 GBI 产品在数据问答领域的一次重要开源尝试,专注于 NL2SQL 场景下的核心能力开放。
407 48
|
21天前
|
人工智能 运维 数据挖掘
一站式智能分析引擎,快速构建企业级数据分析 Agent
本文介绍了一种基于阿里云实时数仓 Hologres 和百炼大模型服务的智能数据分析解决方案。通过 Function AI 提供的 Serverless 平台,企业可快速构建从多源数据接入到业务洞察的端到端流程。方案支持实时数据分析、湖仓直连加速、智能预处理及按需付费模式,大幅降低运维成本并提升效率。同时,文章详细描述了实践部署步骤,包括专有网络配置、Hologres 实例创建、公共数据集导入及应用部署验证等环节,并提供了资源清理指南与参考链接,确保用户能够顺利实施和管理方案。