《仿盒马》app开发技术分享-- 旧物回收页(提交云端)(42)


## 技术栈


Appgallery connect

## 开发准备

上一节我们已经实现了地址,留言,取件时间的选择,以及静态的预估重量。现在我们需要把预估重量创建出来,从云端去获取,以应对后续的其他业务逻辑,以及回收订单的创建



## 功能分析

预估重量列表的实现首先需要创建对应的表和数据源,然后在页面打开的时候从云端查询出对应的数据,展示到我们创建的静态列表中,然后我们创建订单,把页面中的数据放置到创建的订单创建表中


## 代码实现

首先我们创建对应的表


```c

{

  "objectTypeName": "weight_info",

  "fields": [

    {"fieldName": "id", "fieldType": "Integer", "notNull": true, "belongPrimaryKey": true},

    {"fieldName": "weight_id", "fieldType": "Integer"},

    {"fieldName": "weight", "fieldType": "String"},

    {"fieldName": "txt", "fieldType": "String"},

    {"fieldName": "integral", "fieldType": "Double"},

    {"fieldName": "money", "fieldType": "Double"}

  ],

  "indexes": [

    {"indexName": "field1IndexId", "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类


```c

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


class weight_info extends cloudDatabase.DatabaseObject {

  public id: number;

  public weight_id: number;

  public weight: string;

  public txt: string;

  public integral: number;

  public money: number;


  public naturalbase_ClassName(): string {

    return 'weight_info';

  }

}


export { weight_info };





class WeightInfo {

    id: number;

    weight_id: number;

    weight: string;

    txt: string;

    integral: number;

    money: number;


    constructor() {

    }


    getFieldTypeMap():  Map {

        let fieldTypeMap = new Map();

        fieldTypeMap.set('id', 'Integer');

        fieldTypeMap.set('weight_id', 'Integer');

        fieldTypeMap.set('weight', 'String');

        fieldTypeMap.set('txt', 'String');

        fieldTypeMap.set('integral', 'Double');

        fieldTypeMap.set('money', 'Double');

        return fieldTypeMap;

    }


    getClassName(): string {

        return 'weight_info';

    }


    getPrimaryKeyList(): string[] {

        let primaryKeyList: string[] = [];

        primaryKeyList.push('id');

        return primaryKeyList;

    }


    getIndexList(): string[] {

        let indexList: string[] = [];

        indexList.push('id');

        return indexList;

    }


    getEncryptedFieldList(): string[] {

        let encryptedFieldList: string[] = [];

        return encryptedFieldList;

    }


    setId(id: number): void {

        this.id = id;

    }


    getId(): number  {

        return this.id;

    }


    setWeight_id(weight_id: number): void {

        this.weight_id = weight_id;

    }


    getWeight_id(): number  {

        return this.weight_id;

    }


    setWeight(weight: string): void {

        this.weight = weight;

    }


    getWeight(): string  {

        return this.weight;

    }


    setTxt(txt: string): void {

        this.txt = txt;

    }


    getTxt(): string  {

        return this.txt;

    }


    setIntegral(integral: number): void {

        this.integral = integral;

    }


    getIntegral(): number  {

        return this.integral;

    }


    setMoney(money: number): void {

        this.money = money;

    }


    getMoney(): number  {

        return this.money;

    }


    static parseFrom(inputObject: any): WeightInfo {

        let result = new WeightInfo();

        if (!inputObject) {

            return result;

        }

        if (inputObject.id) {

            result.id = inputObject.id;

        }

        if (inputObject.weight_id) {

            result.weight_id = inputObject.weight_id;

        }

        if (inputObject.weight) {

            result.weight = inputObject.weight;

        }

        if (inputObject.txt) {

            result.txt = inputObject.txt;

        }

        if (inputObject.integral) {

            result.integral = inputObject.integral;

        }

        if (inputObject.money) {

            result.money = inputObject.money;

        }

        return result;

    }

}


export { WeightInfo };

```

在页面打开后获取云端的数据


```c

 @State weightList:WeightInfo[]=[]

  @State weightInfo:WeightInfo|null=null

async aboutToAppear(): Promise {

    const value = await StorageUtils.getAll('user');

    if (value != "") {

      this.user = JSON.parse(value)

    }

    let condition = new cloudDatabase.DatabaseQuery(weight_info);

    let listData = await databaseZone.query(condition);

    let json = JSON.stringify(listData)

    let weightInfo:WeightInfo[]= JSON.parse(json)

    this.weightList=weightInfo

    hilog.info(0x0000, 'testTag', `Succeeded in querying data, result: ${weightInfo}`);

    this.flag=true

  }

```

添加对应的数据到提交信息表


```c

Text("预约呼叫")

              .width('95%')

              .height(45)

              .fontSize(16)

              .fontWeight(FontWeight.Bold)

              .borderRadius(5)

              .backgroundColor("#fff84f4f")

              .textAlign(TextAlign.Center)

              .margin({top:15})

              .onClick(async ()=>{

                let recyclePushInfo=new recycle_info()

                recyclePushInfo.id=Math.floor(Math.random() * 1000000);

                recyclePushInfo.user_id=this.user!.user_id

                recyclePushInfo.nike_name=this.addressInfo!.nikeName

                recyclePushInfo.phone=this.addressInfo!.phone

                recyclePushInfo.address=this.addressInfo!.address

                recyclePushInfo.day=this.formatCurrent()

                recyclePushInfo.start_time=this.formatCurrentDate()

                recyclePushInfo.end_time=this.formatCurrentEndDate()

                recyclePushInfo.weight_id=String(this.weightInfo!.weight_id)

                if (this.remark!='') {

                  recyclePushInfo.msg=this.remark

                }

                recyclePushInfo.create_time=this.formatCurrentCreatTime()

                recyclePushInfo.express_code=this.generate16DigitRandom()

                recyclePushInfo.express_people="骑士阿三"

                recyclePushInfo.express_company="中国邮政(默认)"

                recyclePushInfo.order_type=0

                recyclePushInfo.logistics_id=10

                let num = await databaseZone.upsert(recyclePushInfo);

                hilog.info(0x0000, 'testTag', `Succeeded in upserting data, result: ${num}`);

                if (num>0) {

                  showToast("下单成功")

                }

              })

```

到这里我们就实现了提交云端数据的功能


请使用浏览器的分享功能分享到微信等