当前位置:   article > 正文

HarmonyOSAPPS 项目实战_harmonyos shop

harmonyos shop

最重要最紧急的计划: \color{#D02090}{最重要最紧急的计划:} 最重要最紧急的计划:

image.png

进度:4 基于HarmonyOS的音频App项目实战系列课
5 HarmonyOS教育类APP项目实战系列课
进度:0%

重要不紧急的计划: \color{#D02090}{重要不紧急的计划:} 重要不紧急的计划:

学习笔记如下:

目录:image.png
1.简易计算器(ArkTS) (huawei.com)
2.一次开发,多端部署-购物应用(ArkTS) (huawei.com)
3.分布式新闻客户端(ArkTS) (huawei.com)
4 基于HarmonyOS的音频App项目实战系列课
5 HarmonyOS教育类APP项目实战系列课

本地代码:HarmonyOSAPPS
目录 /Users/zhangxiaomeng02/DevEcoStudioProjects/HarmonyOSAPPS/Application/entry/src/main/ets/components/ShopCart.ets

本地代码注释:
打印方法:
Log.info(‘zxmzxm’, " err=" + JSON.stringify(err));

购物车页面:ShopCart;目录 entry/src/main/ets/components/ShopCart.ets
购物车的结算按钮点击事件:settleAccounts
全选按钮的组件 @Builder Settle()
删除购物车数据 ItemDelete中的onClick

订单页面:ConfirmPageOrder
image.png
页面展示 onPageShow
页面布局 ConfirmOrder(),包含订单信息和底部提交订单按钮
提交按钮点击事件:handleConfirmOrder

云数据库操作: LocalDataManager

订单列表页面:OrderListContent
弹框 promptAction.showToast({ message: ${msg}${OrderDetailConstants.SUCCESS} });

订单详情列表:OrderDetailList
image.png

CloudDBService 云数据库封装
具体增删改查看代码:(主要看购物车的云数据库操作)

/*
* Copyright 2022. Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// import * as schema from './app-schema.json';
import { BookInfo } from './BookInfo';
import {
  AGConnectCloudDB,
  CloudDBZoneConfig,
  CloudDBZone,
  CloudDBZoneQuery
} from '@hw-agconnect/database-ohos';
import "@hw-agconnect/core-ohos";
import "@hw-agconnect/auth-ohos";
import { AGCRoutePolicy } from "@hw-agconnect/core-ohos";
import { ShopCartInfo } from './ShopCartInfo';
import { OrderInfo } from './OrderInfo';
import { Log } from '../common/Log';
import { OrderOperationStatus } from '../bean/OrderModel';

export class CloudDBService {
  private static readonly ZONE_NAME = 'XMDB';
  private static cloudDB: AGConnectCloudDB;
  private static cloudDBZone: CloudDBZone;
  private static isInit: boolean;

  public static async init(context: any): Promise<boolean> {
    if (this.isInit) {
      return;
    }
    try {
      this.cloudDB = await AGConnectCloudDB.getInstance({
        context: context, // 在eTS页面中通过全局方法getContext(this)获取当前页面关联的Context
        agcRoutePolicy: AGCRoutePolicy.CHINA, // 数据处理位置,CHINA-中国区,GERMANY-德国,RUSSIA-俄罗斯,SINGAPORE-新加坡
      });
      const schema = {"schemaVersion":7,"permissions":[{"permissions":[{"role":"World","rights":["Read"]},{"role":"Authenticated","rights":["Read","Upsert","Delete"]},{"role":"Creator","rights":["Read","Upsert","Delete"]},{"role":"Administrator","rights":["Read","Upsert","Delete"]}],"objectTypeName":"BookInfo"},{"permissions":[{"role":"World","rights":["Read"]},{"role":"Authenticated","rights":["Read","Upsert","Delete"]},{"role":"Creator","rights":["Read","Upsert","Delete"]},{"role":"Administrator","rights":["Read","Upsert","Delete"]}],"objectTypeName":"ShopCartInfo"},{"permissions":[{"role":"World","rights":["Read"]},{"role":"Authenticated","rights":["Read","Upsert","Delete"]},{"role":"Creator","rights":["Read","Upsert","Delete"]},{"role":"Administrator","rights":["Read","Upsert","Delete"]}],"objectTypeName":"OrderInfo"}],"objectTypes":[{"indexes":[{"indexName":"bookName","indexList":[{"fieldName":"bookName","sortType":"ASC"}]}],"objectTypeName":"BookInfo","fields":[{"isNeedEncrypt":false,"fieldName":"id","notNull":true,"isSensitive":false,"belongPrimaryKey":true,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"bookName","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"author","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"publisher","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"}]},{"indexes":[{"indexName":"name","indexList":[{"fieldName":"name","sortType":"ASC"}]}],"objectTypeName":"ShopCartInfo","fields":[{"isNeedEncrypt":false,"fieldName":"id","notNull":true,"isSensitive":false,"belongPrimaryKey":true,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"name","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"img","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"commodityId","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"description","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"price","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"Double"},{"isNeedEncrypt":false,"fieldName":"count","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"Integer"},{"isNeedEncrypt":false,"fieldName":"selected","notNull":false,"isSensitive":false,"defaultValue":"false","belongPrimaryKey":false,"fieldType":"Boolean"},{"isNeedEncrypt":false,"fieldName":"specifications","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"}]},{"indexes":[{"indexName":"description","indexList":[{"fieldName":"description","sortType":"ASC"}]}],"objectTypeName":"OrderInfo","fields":[{"isNeedEncrypt":false,"fieldName":"uid","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"orderId","notNull":true,"isSensitive":false,"belongPrimaryKey":true,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"image","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"title","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"description","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"payTime","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"orderTime","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"createTime","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"updateTime","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"},{"isNeedEncrypt":false,"fieldName":"price","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"Double"},{"isNeedEncrypt":false,"fieldName":"count","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"Integer"},{"isNeedEncrypt":false,"fieldName":"amount","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"Integer"},{"isNeedEncrypt":false,"fieldName":"status","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"Integer"},{"isNeedEncrypt":false,"fieldName":"commodityId","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"Integer"},{"isNeedEncrypt":false,"fieldName":"specifications","notNull":false,"isSensitive":false,"belongPrimaryKey":false,"fieldType":"String"}]}]};
      this.cloudDB.createObjectType(schema);
      this.openZone(this.ZONE_NAME);
      this.isInit = true;
      Log.info('zxmzxm', 'createObjectType success.');
    } catch (e) {
      Log.info('zxmzxm', 'init failed.')
    }
    return Promise.resolve(this.isInit);
  }

  private static async openZone(zoneName: string): Promise<CloudDBZone> {
    if (this.cloudDBZone) {
      Log.info('zxmzxm', 'zone has been closed.')
      return;
    }
    try {
      const extendParams = { 'x-trace-id': '801d523a1cd1f5'}
      const cloudDBZoneConfig = new CloudDBZoneConfig(zoneName, extendParams);
      this.cloudDBZone = await this.cloudDB.openCloudDBZone(cloudDBZoneConfig);
      Log.info('zxmzxm', '[openZone] open zone success.')
    } catch (e) {
      Log.info('zxmzxm', '[openZone] open zone failed.' + e);
      Log.info('zxmzxm', JSON.stringify(e));
    }
    return this.cloudDBZone;
  }

  public static async closeZone(): Promise<void> {
    try {
      this.cloudDB.closeCloudDBZone(this.cloudDBZone);
      Log.info('zxmzxm', '[closeZone] close zone success.');
      this.cloudDBZone = undefined;
    } catch (e) {
      Log.info('zxmzxm', '[closeZone] close zone failed!' + e);
      Log.info('zxmzxm', JSON.stringify(e));
    }
  }

  public static async upsertRecord(books: BookInfo | Array<BookInfo>): Promise<number> {
    try {
      const upsertNum = await this.cloudDBZone.executeUpsert(books);
      Log.info('zxmzxm', '[upsertRecord] upsertNum=' + upsertNum);
      return upsertNum;
    } catch (e) {
      Log.info('zxmzxm', '[upsertRecord] upsertRecord failed.' + JSON.stringify(e));
      return 0;
    }
  }
  public static async deleteRecord(books: BookInfo | Array<BookInfo>): Promise<number> {
    try {
      const deleteNum = await this.cloudDBZone.executeDelete(books);
      Log.info('zxmzxm', '[deleteRecord] deleteNum=' + deleteNum);
      return deleteNum;
    } catch (e) {
      Log.info('zxmzxm', '[deleteRecord] deleteRecord failed.' + e);
      return 0;
    }
  }

  public static async queryBooks(queryStr?: string): Promise<Array<BookInfo>> {
    try {
      const query = CloudDBZoneQuery.where(BookInfo);
      if (queryStr) {
        query.contains('bookName', queryStr).or().contains('author', queryStr);
      }
      const snapshot = await this.cloudDBZone.executeQuery(query);
      Log.info('zxmzxm', '[queryBooks] query books=' + JSON.stringify(snapshot.getSnapshotObjects()));
      // const message = JSON.stringify(snapshot.getSnapshotObjects());
      // prompt.showToast({
      //     message: message,
      //     duration: 5000,
      // });
      let bookList: Array<BookInfo> = [];
      for (var i = 0; i < snapshot.getSnapshotObjects().length; i++) {
        let bookMap = snapshot.getSnapshotObjects()[i];
        const book = new BookInfo();
        book.setId(bookMap['id']);
        book.setBookName(bookMap['bookName']);
        book.setAuthor(bookMap['author']);
        book.setPublisher(bookMap['publisher']);
        bookList.push(book);
      }
      return bookList;
    } catch (e) {
      Log.info('zxmzxm', '[queryBooks] queryBooks failed.' + JSON.stringify(e));
    }
  }




  // 插入和更新数据(购物车云数据库)
  public static async upsertRecordShopCart(resultTemp: ShopCartInfo, isInsert: boolean): Promise<number> {
    let message = '';
    if (isInsert) {
      message = '插入'
    } else {
      message = '更新'
    }
    try {
      const result:ShopCartInfo = new ShopCartInfo();
      result.id = resultTemp.id;
      result.name = resultTemp.name;
      result.img = resultTemp.img;
      result.commodityId = resultTemp.commodityId;
      result.description = resultTemp.description;
      result.price = resultTemp.price;
      result.count = resultTemp.count
      result.selected = resultTemp.selected;
      result.specifications = JSON.stringify(resultTemp.specifications);

      const upsertNum = await this.cloudDBZone.executeUpsert(result);
      Log.info('zxmzxm', 'upsertRecordShopCart '+ message + '成功 upsertNum=' + upsertNum + ' 数据=' + JSON.stringify(result));
      return upsertNum;
    } catch (e) {
      Log.info('zxmzxm', 'upsertRecordShopCart '+ message + '失败 e=' + JSON.stringify(e));
      return 0;
    }
  }

  // 查询数据
  public static async queryShopCartDB(queryStr?: string): Promise<Array<ShopCartInfo>> {
    try {
      const query = CloudDBZoneQuery.where(ShopCartInfo);
      // if (queryStr) {
      //     query.contains('name', queryStr).or().contains('description', queryStr);
      // }
      const snapshot = await this.cloudDBZone.executeQuery(query);
      const resultArray = snapshot.getSnapshotObjects();
      Log.info('zxmzxm', 'queryShopCartDB 成功 resultArray=' + JSON.stringify(resultArray));

      let shopList: Array<ShopCartInfo> = [];
      for (var i = 0; i < resultArray.length; i++) {
        let shopMap = resultArray[i];
        const shopDB = new ShopCartInfo();
        shopDB.id = shopMap['id'];
        shopDB.name = shopMap['name'];
        shopDB.img = shopMap['img'];
        shopDB.commodityId = shopMap['commodityId'];
        shopDB.description = shopMap['description'];
        shopDB.price = shopMap['price'];
        shopDB.count = shopMap['count'];
        shopDB.selected = shopMap['selected'];
        shopDB.specifications = JSON.parse(shopMap['specifications']);
        shopList.push(shopDB);
      }
      return shopList;
    } catch (e) {
      Log.info('zxmzxm', 'queryShopCartDB 失败 e=' + JSON.stringify(e));
    }
  }
  // 删除数据
  public static async deleteRecordShopCartDB(resultTemp: ShopCartInfo): Promise<number> {
    try {
      const result:ShopCartInfo = new ShopCartInfo();
      result.id = resultTemp.id;
      result.name = resultTemp.name;
      result.img = resultTemp.img;
      result.commodityId = resultTemp.commodityId;
      result.description = resultTemp.description;
      result.price = resultTemp.price;
      result.count = resultTemp.count
      result.selected = resultTemp.selected;
      result.specifications = JSON.stringify(resultTemp.specifications);
      const deleteNum = await this.cloudDBZone.executeDelete(result);
      Log.info('zxmzxm', 'deleteRecordShopCartDB 成功 deleteNum=' + deleteNum + ' 数据=' + JSON.stringify(result));
      return deleteNum;
    } catch (e) {
      Log.info('zxmzxm', 'deleteRecordShopCartDB 失败 e=' + JSON.stringify(e));
      return 0;
    }
  }





  // 插入和更新订单数据 ===============================================================================================
  public static async upsertRecordOrder(resultTemp: OrderInfo): Promise<number> {
    try {
      const result:OrderInfo = new OrderInfo();
      result.commodityId = Number(resultTemp.commodityId);
      result.price = resultTemp.price;
      result.count = resultTemp.count;
      result.specifications = JSON.stringify(resultTemp.specifications);
      result.image = resultTemp.image;
      result.description = resultTemp.description;
      result.title = resultTemp.title;
      result.amount = resultTemp.amount;
      result.createTime = resultTemp.createTime;
      result.orderTime = resultTemp.orderTime;
      result.payTime = resultTemp.payTime;
      result.updateTime = resultTemp.updateTime;
      result.uid = resultTemp.uid;
      result.orderId = resultTemp.orderId;
      result.status = resultTemp.status;
      const upsertNum = await this.cloudDBZone.executeUpsert(result);
      if (result.status == OrderOperationStatus.UN_PAY) {
        Log.info('zxmzxm', 'upsertRecordOrder 添加 upsertNum=' + upsertNum);
      } else if (result.status == OrderOperationStatus.DELIVERED) {
        result.payTime = result.updateTime;
        Log.info('zxmzxm', 'upsertRecordOrder 待收货,已支付 upsertNum=' + upsertNum);
      } else if (result.status == OrderOperationStatus.RECEIPT) {
        Log.info('zxmzxm', 'upsertRecordOrder 待评价 upsertNum=' + upsertNum);
      }
      return upsertNum;
    } catch (e) {
      Log.info('zxmzxm', 'upsertRecordOrder  failed.' + JSON.stringify(e));
      return 0;
    }
  }
  // 查询数据
  public static async queryOrderDB(): Promise<Array<OrderInfo>> {
    try {
      const query = CloudDBZoneQuery.where(OrderInfo);
      const snapshot = await this.cloudDBZone.executeQuery(query);
      const resultArray = snapshot.getSnapshotObjects();
      Log.info('zxmzxm', 'queryOrderDB resultArray=' + JSON.stringify(resultArray));

      let orderList: Array<OrderInfo> = [];
      for (var i = 0; i < resultArray.length; i++) {
        let orderMap = resultArray[i];
        const orderDB = new OrderInfo();
        orderDB.uid = orderMap['uid'];
        orderDB.orderId = orderMap['orderId'];
        orderDB.image = orderMap['image'];
        orderDB.title = orderMap['title'];
        orderDB.description = orderMap['description'];
        orderDB.payTime = orderMap['payTime'];
        orderDB.orderTime = orderMap['orderTime'];
        orderDB.createTime = orderMap['createTime'];
        orderDB.price = orderMap['price'];
        orderDB.count = orderMap['count'];
        orderDB.count = orderMap['amount'];
        orderDB.status = orderMap['status'];
        orderDB.commodityId = orderMap['commodityId'];
        orderDB.specifications = JSON.parse(orderMap['specifications']);
        orderList.push(orderDB);
      }
      return orderList;
    } catch (e) {
      Log.info('zxmzxm', 'queryOrderDB e=' + JSON.stringify(e));
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292

4 基于HarmonyOS的音频App项目实战系列课笔记在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/243842
推荐阅读
相关标签
  

闽ICP备14008679号