《仿盒马》app开发技术分享-- 领取优惠券(56)

鸿蒙小林
• 阅读 3

技术栈

Appgallery connect

开发准备

在之前的功能开发中,我们有些功能只有展示的能力并没有与云端产生任何的交互,后续经过我们的迭代,更多的能力有了交互能力,这一节我们就要开始着手给那些静态展示的模块添加业务逻辑,我们现在要实现的是首页的新人优惠券的领取

功能分析

新人优惠券我们在创建的时候给他赋予了一些字段,分别对应了优惠券的id,面额,最小可用金额等,那我们既然需要跟用户进行绑定,还是需要新建一个优惠券的表,把优惠券已有的数据填充进去,并且添加上userid,方便我们按用户查

代码实现

首先我们创建对应的优惠券表,进行字段的定义

{
  "objectTypeName": "coupon_mall",
  "fields": [
    {"fieldName": "id", "fieldType": "Integer", "notNull": true, "belongPrimaryKey": true},
    {"fieldName": "user_id", "fieldType": "Integer"},
    {"fieldName": "coupon_id", "fieldType": "Integer", "notNull": true, "defaultValue": 0},
    {"fieldName": "price", "fieldType": "Double"},
    {"fieldName": "type", "fieldType": "Integer"},
    {"fieldName": "limit_amount", "fieldType": "Double"},
    {"fieldName": "start_time", "fieldType": "String"},
    {"fieldName": "end_time", "fieldType": "String"},
    {"fieldName": "type_str", "fieldType": "String"},
    {"fieldName": "txt", "fieldType": "String"}

  ],
  "indexes": [
    {"indexName": "field1Index", "indexList": [{"fieldName":"id","sortType":"ASC"}]}
  ],
  "permissions": [
    {"role": "World", "rights": ["Read", "Upsert", "Delete"]},
    {"role": "Authenticated", "rights": ["Read", "Upsert", "Delete"]},
    {"role": "Creator", "rights": ["Read", "Upsert", "Delete"]},
    {"role": "Administrator", "rights": ["Read", "Upsert", "Delete"]}
  ]
}

添加完我们需要的字段后,我们生成对应的实体和db类

import { cloudDatabase } from '@kit.CloudFoundationKit';

class coupon_mall extends cloudDatabase.DatabaseObject {
  public id: number;
  public user_id: number;
  public coupon_id = 0;
  public price: number;
  public type: number;
  public limit_amount: number;
  public start_time: string;
  public end_time: string;
  public type_str: string;
  public txt: string;

  public naturalbase_ClassName(): string {
    return 'coupon_mall';
  }
}

export { coupon_mall };



/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
 * Generated by the CloudDB ObjectType compiler. DO NOT EDIT!
 */

class CouponMall {
    id: number;
    user_id: number;
    coupon_id: number = 0;
    price: number;
    type: number;
    limit_amount: number;
    start_time: string;
    end_time: string;
    type_str: string;
    txt: string;

    constructor() {
    }


    setId(id: number): void {
        this.id = id;
    }

    getId(): number  {
        return this.id;
    }

    setUser_id(user_id: number): void {
        this.user_id = user_id;
    }

    getUser_id(): number  {
        return this.user_id;
    }

    setCoupon_id(coupon_id: number): void {
        this.coupon_id = coupon_id;
    }

    getCoupon_id(): number  {
        return this.coupon_id;
    }

    setPrice(price: number): void {
        this.price = price;
    }

    getPrice(): number  {
        return this.price;
    }

    setType(type: number): void {
        this.type = type;
    }

    getType(): number  {
        return this.type;
    }

    setLimit_amount(limit_amount: number): void {
        this.limit_amount = limit_amount;
    }

    getLimit_amount(): number  {
        return this.limit_amount;
    }

    setStart_time(start_time: string): void {
        this.start_time = start_time;
    }

    getStart_time(): string  {
        return this.start_time;
    }

    setEnd_time(end_time: string): void {
        this.end_time = end_time;
    }

    getEnd_time(): string  {
        return this.end_time;
    }

    setType_str(type_str: string): void {
        this.type_str = type_str;
    }

    getType_str(): string  {
        return this.type_str;
    }

    setTxt(txt: string): void {
        this.txt = txt;
    }

    getTxt(): string  {
        return this.txt;
    }

}

export { CouponMall };

都生成之后我们在首页的新人优惠券模块,立即领取按钮添加对应的逻辑,因为券有多张,所以我们需要循环获取,并且上传到云数据库

```css import { coupon_mall } from "../clouddb/coupon_mall" import { couponInfo } from "../entity/couponInfo" import { homeNewPeopleCoupon } from "../entity/homeNewPeopleCoupon" import { cloudDatabase } from "@kit.CloudFoundationKit" import { hilog } from "@kit.PerformanceAnalysisKit" import showToast from "../utils/ToastUtils" import { StorageUtils } from "../utils/StorageUtils" import { User } from "../entity/User"

@Component @Preview export struct CouponComponent { @Link home_activity:homeNewPeopleCoupon|null @Link couponList:couponInfo[] @State user: User|null=null

async aboutToAppear(): Promise { const value = await StorageUtils.getAll('user'); if (value!='') { this.user=JSON.parse(value) } }

build() { Column() { Row() { Text(this.home_activity?.title) .fontSize(20) .fontColor('#FF0000')

    Text(this.home_activity?.msg)
      .fontSize(14)
      .fontColor('#888888')
      .margin({left:10})
  }
  .width('100%')
  .padding(16)

  List({ space: 10 }) {
    ForEach(this.couponList, (item:couponInfo) => {
      ListItem() {
        Column() {
          Text(item.price)
            .fontSize(22)
            .fontColor('#FF4444')
            .margin({ bottom: 8 })

          Text(item.type)
            .fontSize(12)
            .fontColor('#FF4444')
        }
        .padding(10)
        .backgroundColor("#ffffff")
        .borderRadius(8)
      }
    })
  }
  .margin({left:50})
  .listDirection(Axis.Horizontal)
  .width('100%')
  .height(80)

  Button('立即领取', { type: ButtonType.Normal })
    .width(240)
    .height(40)
    .backgroundColor('#FF0000')
    .fontColor(Color.White)
    .borderRadius(20)
    .margin({ bottom: 16 })
    .onClick(async ()=>{
      for (let i = 0; i < this.couponList.length; i++) {
        let coupon=new coupon_mall()
        coupon.id=Math.floor(Math.random() * 1000000);
        coupon.user_id=this.user!.user_id
        coupon.coupon_id=this.couponList[i].coupon_id
        coupon.price=Number(this.couponList[i].price)
        coupon.type=0
        coupon.limit_amount=this.couponList[i].limit_amount
        coupon.start_time=this.creatTime()
        coupon.end_time=this.endTime()
        coupon.type_str=this.couponList[i].type
        coupon.txt="全场商品通用"
        let databaseZone = cloudDatabase.zone('default');
        let num = await databaseZone.upsert(coupon);
        hilog.info(0x0000, 'testTag', `Succeeded in upserting data, result: ${num}`);
        if (num>0) {
          showToast("优惠券领取成功")
        }
      }
    })
}
.backgroundColor("#fffce2be")
.width('95%')
.margin({top:10})
.borderRadius(20)

}

creatTime(): string { const now = new Date();

const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;

}

endTime(): string { const now = new Date();

const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');

return `${year}-${month}-${day+7} ${hours}:${minutes}:${seconds}`;

} }

点赞
收藏
评论区
推荐文章
鸿蒙小林 鸿蒙小林
6小时前
《仿盒马》app开发技术分享-- 地图选点(27)
技术栈Appgalleryconnect开发准备上一节我们实现了地图的简单展示,这一节我们要实现的内容是,根据展示的地图,实现当前定位功能,当前位置的poi地址功能,以及列表的展示,给地图添加标记,展示自己的当前定位功能分析要想实现这些功能,首先我们需要在
鸿蒙小林 鸿蒙小林
6小时前
《仿盒马》app开发技术分享-- 回收金查询页面(48)
技术栈Appgalleryconnect开发准备上一节我们实现了查看当前账号下的预收益,以及当下收益,并且展示了已完成订单的列表,现在我们可以针对收益来做更多的内容了,在之前的开发中我们在个人中心页面实现了一个静态的金额展示,后续我们将会在这里展示当前账号
鸿蒙小林 鸿蒙小林
6小时前
《仿盒马》app开发技术分享-- 回收金提现记录查询(54)
技术栈Appgalleryconnect开发准备上一节我们实现了回收金提现的功能,并且成功展示了当前账户的支出列表,但是我们的提现相关的记录并没有很好的给用户做出展示,用户只知道当前账户提现扣款,并不知道回收金的去向,这一节我们就要实现回收金记录的查询添加
鸿蒙小林 鸿蒙小林
6小时前
《仿盒马》app开发技术分享-- 回收金提现安全锁校验(55)
技术栈Appgalleryconnect开发准备上一节我们实现了回收金提现记录的展示功能,我们回收金相关的内容更加的丰富了,在之前的业务逻辑中我们添加了一个设置安全锁的功能,虽然我们成功设置了安全锁,也把对应的表信息提交到云端,但是我们并没有在提现的流程中
鸿蒙小林 鸿蒙小林
6小时前
《仿盒马》app开发技术分享-- 优惠券展示页(57)
技术栈Appgalleryconnect开发准备上一节我们实现了优惠券的领取功能,并且在云端已经成功查询出优惠券信息,那么我们需要来实现一个优惠券展示的页面来向用户展示当前账号下的优惠券信息,辅助用户更好的去购买需要的商品,因为优惠券会有多种状态,在展示时
鸿蒙小林 鸿蒙小林
6小时前
《仿盒马》app开发技术分享-- 优惠券逻辑优化(58)
技术栈Appgalleryconnect开发准备我们已经实现了优惠券的领取和展示,现在已经趋近于一个完整的电商应用了,但是这时候问题又来了,我们领取完优惠券之后,我们的新用户优惠券模块依然存在,他并没有消失,既然我们是从云数据库中查询的数据,那么我们需要找
鸿蒙小林 鸿蒙小林
6小时前
《仿盒马》app开发技术分享-- 订单结合优惠券结算(60)
技术栈Appgalleryconnect开发准备上一节我们已经实现了优惠券的选择,并且成功的把券后的价格也展示给用户,不能使用的优惠券我们也用友好的方式告知用户,这一节我们来实现优惠券内容的下一步,优惠券内容结合订单进行结算提交功能分析因为我们之前的订单列
鸿蒙小林 鸿蒙小林
6小时前
《仿盒马》app开发技术分享-- 个人中心页优化(62)
技术栈Appgalleryconnect开发准备上一节我们实现了订单逻辑的优化,现在我们的app功能更加的完善了,并且随着我们的迭代逻辑疏漏越来越少,现在我们继续进行优化,在之前的业务逻辑中我们的个人中心页面展示了用户的余额以及积分商城入口,这里我们要展示
鸿蒙小林 鸿蒙小林
6小时前
《仿盒马》app开发技术分享-- 兑换商品数据插入(67)
技术栈Appgalleryconnect开发准备上一节我们实现了积分列表的展示,我们可以更直观的查看当前用户积分的收支情况,但是现在我们只有积分收入并没有消费的地方,所以现在我们开始着手积分兑换相关的内容。这一节我们来实现积分兑换商品的内容功能分析首先我们
鸿蒙小林 鸿蒙小林
4小时前
《伴时匣》app开发技术分享--表单提交准备(4)
技术栈Appgalleryconnect开发准备上一节我们实现了用户登录功能,现在我们进入首页,可以开始准备着手发布我们的日期计划了,在这之前我们先实现信息表的创建。在首页实现一个标题栏,一个悬浮的按钮。功能分析我们的信息表要展示的内容很多,首先是我们的事