From 4bfba950dd1d81cf5f287f581f805bfe7644d334 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=9C=E6=B5=B7?= Date: Thu, 18 Sep 2025 18:37:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E5=87=86=E5=87=BA=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/components/BillDialog.ets | 23 ++++++-- .../main/ets/entryability/EntryAbility.ets | 56 +++++++++---------- entry/src/main/ets/pages/BillHomePage.ets | 7 ++- entry/src/main/ets/utils/RdbManager.ets | 26 ++++----- hvigor/hvigor-config.json5 | 2 +- oh-package.json5 | 2 +- 6 files changed, 65 insertions(+), 51 deletions(-) diff --git a/entry/src/main/ets/components/BillDialog.ets b/entry/src/main/ets/components/BillDialog.ets index 607f4dd..f8e6366 100644 --- a/entry/src/main/ets/components/BillDialog.ets +++ b/entry/src/main/ets/components/BillDialog.ets @@ -13,6 +13,7 @@ * limitations under the License. */ +import { hilog } from '@kit.PerformanceAnalysisKit'; import { AccountData, AccountItem, EarnList, PayList, TextList } from '../viewmodel/BillViewModel'; import CommonConstants from '../common/CommonConstants'; @@ -203,9 +204,14 @@ export struct BillDialog { .height($r('app.float.component_size_M')) .onClick(() => { if (this.newAccount.typeText === '' || this.curIndex !== this.newAccount.accountType) { - this.getUIContext() - .getPromptAction() - .showToast({ message: CommonConstants.TOAST_TEXT_1, bottom: CommonConstants.PROMPT_BOTTOM }); + try { + this.getUIContext().getPromptAction().showToast({ + message: CommonConstants.TOAST_TEXT_1, + bottom: CommonConstants.PROMPT_BOTTOM + }); + } catch (err) { + hilog.error(0x0000, 'BillDialog', `showToast failed, Code:${err.code},message: ${err.message}`); + } } else { let regex: RegExp = new RegExp('[1-9][0-9]*'); let matchValue: Array | null = this.inputAmount.match(regex); @@ -214,9 +220,14 @@ export struct BillDialog { this.confirm && this.confirm(this.isInsert, this.newAccount); this.controller?.close(); } else { - this.getUIContext() - .getPromptAction() - .showToast({ message: CommonConstants.TOAST_TEXT_2, bottom: CommonConstants.PROMPT_BOTTOM }); + try { + this.getUIContext().getPromptAction().showToast({ + message: CommonConstants.TOAST_TEXT_2, + bottom: CommonConstants.PROMPT_BOTTOM + }); + } catch (err) { + hilog.error(0x0000, 'BillDialog', `showToast failed, Code:${err.code},message: ${err.message}`); + } } } }) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index ee4928e..b8d97f5 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -16,55 +16,57 @@ import { abilityAccessCtrl, AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; - -let uiContext: UIContext | undefined = undefined; +import { BusinessError } from '@kit.BasicServicesKit'; export default class entryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { this.permissions(); - hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); - hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); - hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? ''); + hilog.isLoggable(0x0000, 'EntryAbility', hilog.LogLevel.INFO); + hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onCreate'); + hilog.info(0x0000, 'EntryAbility', '%{public}s', 'want param:' + JSON.stringify(want) ?? ''); + hilog.info(0x0000, 'EntryAbility', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? ''); } onDestroy(): void | Promise { - hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + hilog.isLoggable(0x0000, 'EntryAbility', hilog.LogLevel.INFO); + hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability - hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + hilog.isLoggable(0x0000, 'EntryAbility', hilog.LogLevel.INFO); + hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onWindowStageCreate'); - windowStage.loadContent('pages/BillHomePage', (err, data) => { + windowStage.loadContent('pages/BillHomePage', (err) => { if (err.code) { - hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + hilog.error(0x0000, 'EntryAbility', `Failed to load the content. Code:${err.code},message: ${err.message}`); return; } - hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); - uiContext = windowStage.getMainWindowSync().getUIContext(); - AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); + hilog.info(0x0000, 'EntryAbility', 'Succeeded in loading the content.'); + try { + AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); + } catch (err) { + hilog.error(0x0000, 'EntryAbility', `getMainWindowSync failed. Code:${err.code},message: ${err.message}`); + } }); } onWindowStageDestroy(): void { // Main window is destroyed, release UI related resources - hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + hilog.isLoggable(0x0000, 'EntryAbility', hilog.LogLevel.INFO); + hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { // Ability has brought to foreground - hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + hilog.isLoggable(0x0000, 'EntryAbility', hilog.LogLevel.INFO); + hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onForeground'); } onBackground(): void { // Ability has back to background - hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO); - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + hilog.isLoggable(0x0000, 'EntryAbility', hilog.LogLevel.INFO); + hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onBackground'); } /** @@ -72,12 +74,10 @@ export default class entryAbility extends UIAbility { */ permissions(): void { let atManager = abilityAccessCtrl.createAtManager(); - try { - atManager.requestPermissionsFromUser(this.context, ['ohos.permission.DISTRIBUTED_DATASYNC']).then((data) => { - hilog.info(0x0000, 'hilog', `Data permissions:${data.permissions}`); - }); - } catch (err) { - hilog.info(0x0000, 'hilog', `Catch err: ${err}`); - } + atManager.requestPermissionsFromUser(this.context, ['ohos.permission.DISTRIBUTED_DATASYNC']).then((data) => { + hilog.info(0x0000, 'EntryAbility', `Data permissions:${data.permissions}`); + }).catch((err: BusinessError) => { + hilog.error(0x0000, 'EntryAbility', `request permissions failed, Code:${err.code},message: ${err.message}`); + }); } } \ No newline at end of file diff --git a/entry/src/main/ets/pages/BillHomePage.ets b/entry/src/main/ets/pages/BillHomePage.ets index aaf2446..13a476a 100644 --- a/entry/src/main/ets/pages/BillHomePage.ets +++ b/entry/src/main/ets/pages/BillHomePage.ets @@ -154,7 +154,12 @@ struct BillHomePage { } else { const keyList: Array = Object.keys(TextList); const typeText = keyList.find(key => { - const result = this.context.resourceManager.getStringByNameSync(key); + let result: string = '' + try { + result = this.context.resourceManager.getStringByNameSync(key); + } catch (err) { + hilog.error(0x0000, 'BillHomePage', `getStringByNameSync failed, Code:${err.code},message: ${err.message}`); + } return result === searchValue; }); const result: AccountData[] = await this.rdbManager.query(typeText || '', false); diff --git a/entry/src/main/ets/utils/RdbManager.ets b/entry/src/main/ets/utils/RdbManager.ets index dfa034c..845f051 100644 --- a/entry/src/main/ets/utils/RdbManager.ets +++ b/entry/src/main/ets/utils/RdbManager.ets @@ -38,7 +38,7 @@ export class RdbManager { // Set the created table as a distributed table await this.rdbStore.setDistributedTables([CommonConstants.ACCOUNT_TABLE.tableName]); } catch (error) { - hilog.error(0x0000, 'RdbManager', `have error .Code:${error.code},message: ${error.message}`); + hilog.error(0x0000, 'RdbManager', `have error .Code:${error.code}, message: ${error.message}`); } } @@ -94,7 +94,6 @@ export class RdbManager { // DeviceIds is obtained by calling the getAvailableDeviceListSync method by the DeviceManager if (deviceManager != null) { let devices = deviceManager.getAvailableDeviceListSync(); - let deviceNetworkId: string = deviceManager.getLocalDeviceNetworkId(); hilog.info(0x0000, 'hilog', CommonConstants.RDB_TAG, 'devices size ' + devices.length); for (let i = 0; i < devices.length; i++) { deviceIds[i] = devices[i].networkId as string; @@ -217,7 +216,6 @@ export class RdbManager { if (this.rdbStore) { const deviceIds: string[] = this.getDeviceList(); if (deviceIds.length === 0) { - hilog.error(0x0000, 'hilog', CommonConstants.RDB_TAG, 'no device to sync'); return; } // Construct predicate objects for synchronizing distributed tables @@ -238,9 +236,9 @@ export class RdbManager { `device:${deviceId} sync failed, status:${syncResult}`); } } - } catch (e) { + } catch (err) { hilog.error(0x0000, 'hilog', CommonConstants.RDB_TAG, - 'Push data failed, code: ' + e.code + ', message: ' + e.message); + 'Push data failed, code: ' + err.code + ', message: ' + err.message); } } } @@ -267,9 +265,9 @@ export class RdbManager { hilog.info(0x0000, 'hilog', CommonConstants.RDB_TAG, 'Remote query success, row cout: ' + resultSet.rowCount); hilog.info(0x0000, 'hilog', CommonConstants.RDB_TAG, `ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); - } catch (e) { + } catch (err) { hilog.error(0x0000, 'hilog', CommonConstants.RDB_TAG, - 'Remote query failed, code: ' + e.code + ', message: ' + e.message); + 'Remote query failed, code: ' + err.code + ', message: ' + err.message); } } return list; @@ -323,19 +321,19 @@ export class RdbManager { * @param newList */ updateLocalDataBase(oldList: Array, newList: Array) { - const list: Array = newList.concat(oldList) + const list: Array = newList.concat(oldList); const idList = new Set(list.map((item: AccountData) => item.id)) idList.forEach((id: string) => { - const isFindInOldList = oldList.find((item: AccountData) => id === item.id) - const isFindInNewList = newList.find((item: AccountData) => id === item.id) + const isFindInOldList = oldList.find((item: AccountData) => id === item.id); + const isFindInNewList = newList.find((item: AccountData) => id === item.id); if (!isFindInOldList) { - const item: AccountData = list.find((item: AccountData) => id === item.id) || {} as AccountData - this.insertData(item, false) + const item: AccountData = list.find((item: AccountData) => id === item.id) || {} as AccountData; + this.insertData(item, false); } else if (!isFindInNewList) { - const item: AccountData = list.find((item: AccountData) => id === item.id) || {} as AccountData + const item: AccountData = list.find((item: AccountData) => id === item.id) || {} as AccountData; this.deleteData(item, false); } else { - const item: AccountData = newList.find((item: AccountData) => id === item.id) || {} as AccountData + const item: AccountData = newList.find((item: AccountData) => id === item.id) || {} as AccountData; this.updateData(item, false); } }) diff --git a/hvigor/hvigor-config.json5 b/hvigor/hvigor-config.json5 index 0c81a69..61c774a 100644 --- a/hvigor/hvigor-config.json5 +++ b/hvigor/hvigor-config.json5 @@ -1,5 +1,5 @@ { - "modelVersion": "5.0.5", + "modelVersion": "5.1.1", "dependencies": { }, "execution": { diff --git a/oh-package.json5 b/oh-package.json5 index af893bd..a0b5390 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -1,5 +1,5 @@ { - "modelVersion": "5.0.5", + "modelVersion": "5.1.1", "license": "", "devDependencies": {}, "author": "", -- Gitee From f753373e4e3cd135fa9d6c7c0ca07217fc665c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E4=B8=9C=E6=B5=B7?= Date: Fri, 19 Sep 2025 15:26:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E5=87=86=E5=87=BA=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/entryability/EntryAbility.ets | 8 ++-- entry/src/main/ets/pages/BillHomePage.ets | 8 ++-- entry/src/main/ets/utils/RdbManager.ets | 42 +++++++++---------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index b8d97f5..6321b70 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -33,7 +33,7 @@ export default class entryAbility extends UIAbility { } onWindowStageCreate(windowStage: window.WindowStage): void { - // Main window is created, set main page for this ability + // Main window is created, set main page for this ability. hilog.isLoggable(0x0000, 'EntryAbility', hilog.LogLevel.INFO); hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onWindowStageCreate'); @@ -52,19 +52,19 @@ export default class entryAbility extends UIAbility { } onWindowStageDestroy(): void { - // Main window is destroyed, release UI related resources + // Main window is destroyed, release UI related resources. hilog.isLoggable(0x0000, 'EntryAbility', hilog.LogLevel.INFO); hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onWindowStageDestroy'); } onForeground(): void { - // Ability has brought to foreground + // Ability has brought to foreground. hilog.isLoggable(0x0000, 'EntryAbility', hilog.LogLevel.INFO); hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onForeground'); } onBackground(): void { - // Ability has back to background + // Ability has back to background. hilog.isLoggable(0x0000, 'EntryAbility', hilog.LogLevel.INFO); hilog.info(0x0000, 'EntryAbility', '%{public}s', 'Ability onBackground'); } diff --git a/entry/src/main/ets/pages/BillHomePage.ets b/entry/src/main/ets/pages/BillHomePage.ets index 13a476a..470b4c1 100644 --- a/entry/src/main/ets/pages/BillHomePage.ets +++ b/entry/src/main/ets/pages/BillHomePage.ets @@ -53,7 +53,7 @@ struct BillHomePage { async accept(isInsert: boolean, newAccount: AccountData): Promise { if (isInsert) { - const time = systemDateTime.getTime() + const time = systemDateTime.getTime(); newAccount.id = `id_${time}`; await this.rdbManager.insertData(newAccount); this.accounts.push(newAccount); @@ -62,7 +62,7 @@ struct BillHomePage { await this.rdbManager.updateData(newAccount); const list = this.accounts.map((item: AccountData) => { const result = item.id === newAccount.id ? newAccount : item; - return result + return result; }); this.accounts = list; this.index = -1; @@ -81,7 +81,7 @@ struct BillHomePage { this.context.eventHub.off('dataChange', this.eventFunc); } - // Function for Monitoring Data Changes + // Function for Monitoring Data Changes. eventFunc = (value: string) => { const list: AccountData[] = JSON.parse(value); this.rdbManager.updateLocalDataBase(this.accounts, list) @@ -101,7 +101,7 @@ struct BillHomePage { deleteListItem() { this.deleteList.forEach(async (item: AccountData) => { - const index = this.accounts.findIndex((account: AccountData) => account.id === item.id) + const index = this.accounts.findIndex((account: AccountData) => account.id === item.id); this.accounts.splice(index, 1); await this.rdbManager.deleteData(item); }) diff --git a/entry/src/main/ets/utils/RdbManager.ets b/entry/src/main/ets/utils/RdbManager.ets index 845f051..7fc5339 100644 --- a/entry/src/main/ets/utils/RdbManager.ets +++ b/entry/src/main/ets/utils/RdbManager.ets @@ -20,7 +20,7 @@ import { hilog } from '@kit.PerformanceAnalysisKit'; import CommonConstants from '../common/CommonConstants'; import { AccountData } from '../viewmodel/BillViewModel'; -// Relational data management category +// Relational data management category. export class RdbManager { private rdbStore: relationalStore.RdbStore | null = null; context: common.UIAbilityContext | null = null; @@ -30,24 +30,24 @@ export class RdbManager { this.context = context; } - // Set distributed properties for data tables + // Set distributed properties for data tables. async setDistributedTables(context: Context) { try { this.rdbStore = await relationalStore.getRdbStore(context, CommonConstants.STORE_CONFIG); await this.rdbStore.executeSql(CommonConstants.ACCOUNT_TABLE.sqlCreate); - // Set the created table as a distributed table + // Set the created table as a distributed table. await this.rdbStore.setDistributedTables([CommonConstants.ACCOUNT_TABLE.tableName]); } catch (error) { hilog.error(0x0000, 'RdbManager', `have error .Code:${error.code}, message: ${error.message}`); } } - // Subscribe to data change messages from other devices within the network + // Subscribe to data change messages from other devices within the network. subscribeDataChange() { if (this.rdbStore) { try { - // Call the distributed data subscription interface to register observers for the database - // When data changes occur in a distributed database, a callback will be called + // Call the distributed data subscription interface to register observers for the database. + // When data changes occur in a distributed database, a callback will be called. this.rdbStore.on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, async (devices) => { hilog.info(0x0000, 'hilog', CommonConstants.RDB_TAG, 'dataChange devices ' + devices); for (let i = 0; i < devices.length; i++) { @@ -67,12 +67,12 @@ export class RdbManager { } } - // Get local device ID + // Get local device ID. getLocalDeviceNetworkId() { try { - // create deviceManager + // create deviceManager. const deviceManager = distributedDeviceManager.createDeviceManager('com.example.crossDeviceDataSynchronization'); - // DeviceIds is obtained by calling the getAvailableDeviceListSync method by the DeviceManager + // DeviceIds is obtained by calling the getAvailableDeviceListSync method by the DeviceManager. if (deviceManager != null) { this.localDeviceNetworkId = deviceManager.getLocalDeviceNetworkId(); hilog.info(0x0000, 'hilog', CommonConstants.RDB_TAG, @@ -84,14 +84,14 @@ export class RdbManager { } } - // Query the list of devices within the network + // Query the list of devices within the network. getDeviceList(): string[] { let deviceManager: distributedDeviceManager.DeviceManager; let deviceIds: string[] = []; try { - // create deviceManager + // create deviceManager. deviceManager = distributedDeviceManager.createDeviceManager('com.example.crossDeviceDataSynchronization'); - // DeviceIds is obtained by calling the getAvailableDeviceListSync method by the DeviceManager + // DeviceIds is obtained by calling the getAvailableDeviceListSync method by the DeviceManager. if (deviceManager != null) { let devices = deviceManager.getAvailableDeviceListSync(); hilog.info(0x0000, 'hilog', CommonConstants.RDB_TAG, 'devices size ' + devices.length); @@ -211,21 +211,21 @@ export class RdbManager { return result; } - // Synchronize data + // Synchronize data. async syncData() { if (this.rdbStore) { const deviceIds: string[] = this.getDeviceList(); if (deviceIds.length === 0) { return; } - // Construct predicate objects for synchronizing distributed tables + // Construct predicate objects for synchronizing distributed tables. const predicates = new relationalStore.RdbPredicates(CommonConstants.ACCOUNT_TABLE.tableName); - // Specify the list of devices to synchronize + // Specify the list of devices to synchronize. predicates.inDevices(deviceIds); try { - // Call the interface for synchronizing data to push the current device data changes to other devices in the network + // Call the interface for synchronizing data to push the current device data changes to other devices in the network. const result = await this.rdbStore.sync(relationalStore.SyncMode.SYNC_MODE_PUSH, predicates); - // Obtain synchronization results + // Obtain synchronization results. for (let i = 0; i < result.length; i++) { const deviceId = result[i][0]; const syncResult = result[i][1]; @@ -251,11 +251,11 @@ export class RdbManager { public async remoteQuery(device: string) { let list: AccountData[] = []; if (this.rdbStore) { - // Query the list of devices within the network - // Construct predicate objects for synchronizing distributed tables + // Query the list of devices within the network. + // Construct predicate objects for synchronizing distributed tables. const predicates = new relationalStore.RdbPredicates(CommonConstants.ACCOUNT_TABLE.tableName); try { - // Query the distributed tables on devices within the network + // Query the distributed tables on devices within the network. const resultSet = await this.rdbStore.remoteQuery(device, CommonConstants.ACCOUNT_TABLE.tableName, predicates, CommonConstants.ACCOUNT_TABLE.columns); list = this.formatData(resultSet); @@ -273,7 +273,7 @@ export class RdbManager { return list; } - // Format database data + // Format database data. formatData(resultSet: relationalStore.ResultSet) { let list: AccountData[] = []; let count: number = resultSet.rowCount; -- Gitee