《仿盒马》app开发技术分享-- 兑换商品数据插入(67)

鸿蒙小林
• 阅读 5

## 技术栈

Appgallery connect

开发准备

上一节我们实现了积分列表的展示,我们可以更直观的查看当前用户积分的收支情况,但是现在我们只有积分收入并没有消费的地方,所以现在我们开始着手积分兑换相关的内容。这一节我们来实现积分兑换商品的内容

功能分析

首先我们需要创建对应的积分商品表、实体类、db类,现在我们的表是空的,在进行数据展示之前我们先要添加数据,我们创建一个空页面,在生命周期方法中先添加数据,方便我们等会儿使用

代码实现

首先创建对应的表

{
  "objectTypeName": "points_product",
  "fields": [
    {"fieldName": "id", "fieldType": "Integer", "notNull": true, "belongPrimaryKey": true},
    {"fieldName": "url", "fieldType": "String"},
    {"fieldName": "name", "fieldType": "Text"},
    {"fieldName": "points", "fieldType": "Double"},
    {"fieldName": "amount", "fieldType": "Integer"},
    {"fieldName": "text_message", "fieldType": "String"},
    {"fieldName": "parameter", "fieldType": "String"},
    {"fieldName": "delivery_time", "fieldType": "String"},
    {"fieldName": "sales_volume", "fieldType": "Integer"},
    {"fieldName": "spec_str", "fieldType": "String"},
    {"fieldName": "max_loop_amount", "fieldType": "Integer"}
  ],
  "indexes": [
    {"indexName": "field1Index", "indexList": [{"fieldName":"id","sortType":"ASC"}]}
  ],
  "permissions": [
    {"role": "World", "rights": ["Read"]},
    {"role": "Authenticated", "rights": ["Read", "Upsert"]},
    {"role": "Creator", "rights": ["Read", "Upsert", "Delete"]},
    {"role": "Administrator", "rights": ["Read", "Upsert", "Delete"]}
  ]
}

实体类的创建


class PointsProduct {
    id: number;
    url: string;
    name: string;
    points: number;
    amount: number;
    text_message: string;
    parameter: string;
    delivery_time: string;
    sales_volume: number;
    spec_str: string;
    max_loop_amount: number;

    constructor() {
    }


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

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

    setUrl(url: string): void {
        this.url = url;
    }

    getUrl(): string  {
        return this.url;
    }

    setName(name: string): void {
        this.name = name;
    }

    getName(): string  {
        return this.name;
    }

    setPoints(points: number): void {
        this.points = points;
    }

    getPoints(): number  {
        return this.points;
    }

    setAmount(amount: number): void {
        this.amount = amount;
    }

    getAmount(): number  {
        return this.amount;
    }

    setText_message(text_message: string): void {
        this.text_message = text_message;
    }

    getText_message(): string  {
        return this.text_message;
    }

    setParameter(parameter: string): void {
        this.parameter = parameter;
    }

    getParameter(): string  {
        return this.parameter;
    }

    setDelivery_time(delivery_time: string): void {
        this.delivery_time = delivery_time;
    }

    getDelivery_time(): string  {
        return this.delivery_time;
    }

    setSales_volume(sales_volume: number): void {
        this.sales_volume = sales_volume;
    }

    getSales_volume(): number  {
        return this.sales_volume;
    }

    setSpec_str(spec_str: string): void {
        this.spec_str = spec_str;
    }

    getSpec_str(): string  {
        return this.spec_str;
    }

    setMax_loop_amount(max_loop_amount: number): void {
        this.max_loop_amount = max_loop_amount;
    }

    getMax_loop_amount(): number  {
        return this.max_loop_amount;
    }

}

export { PointsProduct };

db类

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

class points_product extends cloudDatabase.DatabaseObject {
  public id: number;
  public url: string;
  public name: string;
  public points: number;
  public amount: number;
  public text_message: string;
  public parameter: string;
  public delivery_time: string;
  public sales_volume: number;
  public spec_str: string;
  public max_loop_amount: number;

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

export { points_product };

都创建完成之后我们新建一个页面,在页面加载时新增数据

import { points_product } from '../../clouddb/points_product'
import { cloudDatabase } from '@kit.CloudFoundationKit';
import showToast from '../../utils/ToastUtils';

@Entry
@Component
struct PointsProductPage {
  async aboutToAppear(): Promise<void> {
    let databaseZone = cloudDatabase.zone('default');
    let product=new points_product()
    product.id=Math.floor(Math.random() * 1000000)
    product.url="在线图片链接"
    product.name="拉布布"
    product.points=300000
    product.amount=1
    product.text_message="典藏版拉布布"
    product.parameter="常温存放"
    product.delivery_time="兑换后立马发货"
    product.sales_volume=9999
    product.spec_str="典藏款"
    product.max_loop_amount=1
    let points_nums =  await databaseZone.upsert(product);
    if (points_nums>0) {
      showToast("添加成功")
    }
  }


  build() {
    RelativeContainer() {

    }
    .height('100%')
    .width('100%')
  }
}

我们新建的页面,通过积分兑换按钮进入

 case "积分兑换":
          router.pushUrl({url:'pages/points/PointsProductPage'})
          break;

到这里我们就实现了数据的插入

点赞
收藏
评论区
推荐文章
鸿蒙小林 鸿蒙小林
12小时前
《仿盒马》app开发技术分享-- 插入积分信息(65)
技术栈Appgalleryconnect开发准备上一节我们实现了积分页面的大概框架,那么现在我们开始进行数据的填充和查询,积分相关的内容现在之后当回收订单结算之后才会进行积分数据的添加,那我们想查询出对应的积分数据,还需要新增一张积分信息表功能分析现在积分
鸿蒙小林 鸿蒙小林
12小时前
《仿盒马》app开发技术分享-- 兑换列表展示(68)
技术栈Appgalleryconnect开发准备上一节我们创建了积分相关的商品表,我们现在可以针对积分进行更多的操作了,我们首先添加了对应的数据到我们的云数据库中,这一节我们就要把我们存储的数据查询出来展示给用户功能分析首先我们需要在进入页面后进行数据查询
鸿蒙小林 鸿蒙小林
12小时前
《仿盒马》app开发技术分享-- 兑换商品详情(69)
技术栈Appgalleryconnect开发准备上一节我们实现了兑换商品列表的展示,用户可以在回收之后通过积分页面进入兑换列表页查看当前能够兑换的商品了,我们距离一个完整的app又更近了一步,现在我们要实现的就是当用户点击列表条目的时候能够查看数据详情。功
鸿蒙小林 鸿蒙小林
12小时前
《仿盒马》app开发技术分享-- 商品兑换校验(70)
技术栈Appgalleryconnect开发准备上一节我们实现了可兑换商品的详情,我们能够查看到商品更多的信息,这一节我们来实现商品兑换相关的功能,在进行商品兑换之前,我们在兑换详情页面,点击立即兑换按钮之后我们需要跳转到兑换详情页,但是用户的积分可能达不
鸿蒙小林 鸿蒙小林
12小时前
《仿盒马》app开发技术分享-- 兑换提交准备(72)
技术栈Appgalleryconnect开发准备上一节我们实现了地址的选择,商品数据的展示,我们页面中需要的提交的内容还有所欠缺,我们还需要新增一些展示兑换细节的组件,同时在提交之前还需要实现备注功能,我们还要在页面中展示一些积分相关的内容,告知用户积分的
鸿蒙小林 鸿蒙小林
12小时前
《仿盒马》app开发技术分享-- 兑换订单提交(73)
技术栈Appgalleryconnect开发准备上一节我们实现了兑换提交前的准备页面,向用户展示了兑换相关的所有信息,这一节我们就可以实现兑换订单的提交了功能分析订单提交我们需要创建对应的兑换商品订单提交信息表,我们需要把地址,商品信息,积分,备注,订单状
鸿蒙小林 鸿蒙小林
12小时前
《仿盒马》app开发技术分享-- 订单提交逻辑完善(74)
技术栈Appgalleryconnect开发准备上一节我们实现了兑换订单的提交功能,并且成功的把数据提交到云端,但是我们的业务逻辑并没有完全实现,我们只是把数据提交到了云端,但我们的积分还存在,我们回到积分数据查看的页面也没有消费积分的记录,这一节我们要实
鸿蒙小林 鸿蒙小林
12小时前
《仿盒马》app开发技术分享-- 兑换订单列表框架(75)
技术栈Appgalleryconnect开发准备上一节我们针对订单兑换的业务逻辑进行了完善,成功的在兑换物品之后修改了用户信息的修改,新增了积分消费的记录。这一节我们实现订单创建之后进入的列表展示页框架。功能分析兑换商品的订单列表框架我们选择使用tabs,
鸿蒙小林 鸿蒙小林
12小时前
《仿盒马》app开发技术分享-- 逻辑优化第二弹(82)
技术栈Appgalleryconnect开发准备这一节我们继续对我们已有的业务逻辑进行优化,在积分兑换完商品后我们回到积分展示页面发现积分的数量并没有减少,而是重新进入才会发生变化,上一节我们实现商城订单的确认揽收之后继续在待收货页面实现确认揽收按钮的业务
鸿蒙小林 鸿蒙小林
12小时前
《仿盒马》app开发技术分享-- 逻辑优化第三弹(83)
技术栈Appgalleryconnect开发准备现在我们的app功能已经趋近完善,bug和缺失的细节也越来越少了,我们继续对app进行优化,首先是我们的积分页面,我们只实现了全部的积分展示内容,对收入和支出的积分明细并没有进行展示,这里我们要实现一下,然后