ITPub博客

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

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

原创 HTML5 作者:鸿蒙小林 时间:2025-06-27 21:17:30 0 删除 编辑


## 技术栈


Appgallery connect


## 开发准备

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


## 功能分析

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


## 代码实现

首先创建对应的表


```css

{

  "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"]}

  ]

}

```

实体类的创建


```css


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类


```css

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

```

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


```css

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%')

  }

}

```

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


```css

 case "积分兑换":

          router.pushUrl({url:'pages/points/PointsProductPage'})

          break;

```

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


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70046889/viewspace-3089314/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2025-06-26

  • 博文量
    90
  • 访问量
    67456