赞
踩
一 最重要最紧急的计划: \color{#D02090}{最重要最紧急的计划:} 最重要最紧急的计划:
进度:4 基于HarmonyOS的音频App项目实战系列课
5 HarmonyOS教育类APP项目实战系列课
进度:0%
目录:
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
页面展示 onPageShow
页面布局 ConfirmOrder(),包含订单信息和底部提交订单按钮
提交按钮点击事件:handleConfirmOrder
云数据库操作: LocalDataManager
订单列表页面:OrderListContent
弹框 promptAction.showToast({ message:${msg}${OrderDetailConstants.SUCCESS}
});
订单详情列表:OrderDetailList
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)); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。