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

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

技术栈

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}`;

}
}

相关文章
|
3月前
《仿盒马》app开发技术分享-- 商品兑换校验(70)
上一节我们实现了可兑换商品的详情,我们能够查看到商品更多的信息,这一节我们来实现商品兑换相关的功能,在进行商品兑换之前,我们在兑换详情页面,点击立即兑换按钮之后我们需要跳转到兑换详情页,但是用户的积分可能达不到我们当前商品的兑换标准,这时候如果我们进入了下个页面,在用户点击确认的时候去校验,就让用户多操作了一步,这样的操作体验非常的不友好,所以我们在兑换前进行校验,通过校验后我们在确认页实现地址添加相关的内容
87 4
|
3月前
|
数据库
《仿盒马》app开发技术分享-- 兑换商品取消订单&取消列表展示(77)
上一节我们实现了兑换订单待发货列表的展示逻辑,成功的在列表中展示出来,我们在订单条目中新增了两个按钮,确认揽收与取消订单,这一节我们要实现的功能是订单的取消,以及订单取消后取消列表的展示
83 1
|
3月前
|
存储
《仿盒马》app开发技术分享-- 兑换订单提交(73)
上一节我们实现了兑换提交前的准备页面,向用户展示了兑换相关的所有信息,这一节我们就可以实现兑换订单的提交了
83 1
|
3月前
《仿盒马》app开发技术分享-- 注销账号恢复(85)
上一节我们实现了欢迎页的逻辑,并且在欢迎页面实现了对账号状态的提示,但是如果我们的用户之前因为一些原因注销了账号,但现在又想用回我们的应用怎么办?我们这一节就要在注销账号的提示弹窗处,实现一个账号恢复功能,使我们的用户可以继续使用我们的应用
89 0
|
3月前
《仿盒马》app开发技术分享-- 逻辑优化第三弹(83)
现在我们的app功能已经趋近完善,bug和缺失的细节也越来越少了,我们继续对app进行优化,首先是我们的积分页面,我们只实现了全部的积分展示内容,对收入和支出的积分明细并没有进行展示,这里我们要实现一下,然后就是我们的优惠券,我们已过期的优惠券并没有修改状态为已过期。
80 0
|
数据库
《仿盒马》app开发技术分享-- 逻辑优化第二弹(82)
这一节我们继续对我们已有的业务逻辑进行优化,在积分兑换完商品后我们回到积分展示页面发现积分的数量并没有减少,而是重新进入才会发生变化,上一节我们实现商城订单的确认揽收之后继续在待收货页面实现确认揽收按钮的业务逻辑。
79 0
|
3月前
《仿盒马》app开发技术分享-- 逻辑优化第一弹(81)
随着上一节我们兑换商品订单相关逻辑的实现,我们的app功能已经更加的完善了,接下来我们开始对整个app缺失的小功能以及对已有的功能bug进行优化和逻辑的新增,这一节我们新增的功能是,商城订单的揽收 功能,兑换订单的取消后积分退回功能,如果不实现积分退回,就会出现用户兑换后取消订单,但是积分已经消耗的情况
76 0
|
3月前
《仿盒马》app开发技术分享-- 兑换商品收货确认&已完成列表展示(79)
上一节我们实现了兑换商品订单的确认揽收功能,实现了tabs切换时的数据刷新,实现了待收货订单的列表展示。这一节我们要实现确认收货功能,并且实现待收货的列表展示功能
79 0
|
3月前
《仿盒马》app开发技术分享-- 兑换商品确认揽收&待收货列表展示(78)
上一节我们实现了订单取消功能,实现了tabs切换时的数据刷新,实现了已取消订单的列表展示。这一节我们要实现揽收功能,并且实现待收货的列表展示功能
76 0
|
存储
《仿盒马》app开发技术分享-- 待发货兑换订单列表(76)
上一节我们实现了兑换订单展示页面的框架,这一节我们要进行兑换订单的展示,在兑换订单提交后,默认的状态是待发货状态,我们用列表的方式展示出来
74 0