From 66bae62e97603a4bd0589a466a974101ababd3c5 Mon Sep 17 00:00:00 2001 From: lewis Date: Sun, 14 Sep 2025 11:18:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20IDE=E5=91=8A=E8=AD=A6=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/Index.ets | 23 +++++-- entry/src/main/ets/utils/CustomFunction.ets | 65 ++++++++++++------- .../src/main/ets/utils/WebDownloadManager.ets | 51 +++++++++++---- 3 files changed, 99 insertions(+), 40 deletions(-) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index be21325..4143539 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -16,6 +16,7 @@ import { webview } from '@kit.ArkWeb'; import { common } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BusinessError } from '@kit.BasicServicesKit'; import { AlertDialog, ComponentContent, promptAction, window } from '@kit.ArkUI'; import { Constants } from '../common/Constants'; import { WebDownloadManager } from '../utils/WebDownloadManager'; @@ -71,17 +72,28 @@ struct Index { }) }); private openDownloadDialog = () => { - this.ctx.getPromptAction().openCustomDialog(this.contentNode, this.options); + this.ctx.getPromptAction().openCustomDialog(this.contentNode, this.options).catch((error: BusinessError) => { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); + }); }; private closeDownloadDialog = () => { - this.ctx.getPromptAction().closeCustomDialog(this.contentNode); + this.ctx.getPromptAction().closeCustomDialog(this.contentNode).catch((error: BusinessError) => { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); + }); }; private changeIsShow = () => { this.isShow = !this.isShow; }; aboutToAppear(): void { - window.getLastWindow(this.context).then((windowClass) => this.windowClass = windowClass); + window.getLastWindow(this.context) + .then((windowClass) => this.windowClass = windowClass) + .catch((error: BusinessError) => { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); + }); this.manager.registerController(Constants.INDEX_WEB_CONTROLLER, this.webController); AppStorage.setOrCreate>('contentNode', this.contentNode); hilog.info(0x000, Constants.TAG, 'language: ' + this.language); @@ -94,7 +106,10 @@ struct Index { * @returns void - This function does not return any value. */ changeOrientation(orientation: window.Orientation) { - this.windowClass?.setPreferredOrientation(orientation); + this.windowClass?.setPreferredOrientation(orientation).catch((error: BusinessError) => { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); + }); } onBackPress(): boolean | void { diff --git a/entry/src/main/ets/utils/CustomFunction.ets b/entry/src/main/ets/utils/CustomFunction.ets index 6a1a759..3e5f8ef 100644 --- a/entry/src/main/ets/utils/CustomFunction.ets +++ b/entry/src/main/ets/utils/CustomFunction.ets @@ -14,12 +14,14 @@ */ import { common, Want } from '@kit.AbilityKit'; -import { pasteboard } from '@kit.BasicServicesKit'; +import { BusinessError, pasteboard } from '@kit.BasicServicesKit'; import { fileIo as fs, picker } from '@kit.CoreFileKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; import { Constants } from '../common/Constants'; import { WebDownloadManager } from './WebDownloadManager'; -import { hilog } from '@kit.PerformanceAnalysisKit'; + const uiContext: UIContext | undefined = AppStorage.get('uiContext'); + /** * Copies a URI to the system clipboard. * @@ -33,9 +35,14 @@ export function copyLink(uri: string) { hilog.error(0x000, Constants.TAG, JSON.stringify(err)); return; } - uiContext?.getPromptAction().showToast({ - message: $r('app.string.copy_success') - }) + try { + uiContext?.getPromptAction().showToast({ + message: $r('app.string.copy_success') + }) + } catch (error) { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); + } }) } @@ -45,7 +52,10 @@ export function openVideoInBrowser(uri: string, context: common.UIAbilityContext entities: ['entity.system.browsable'], uri }; - context.startAbility(want); + context.startAbility(want).catch((error: BusinessError) => { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); + }); } export async function saveVideoToGallery( @@ -58,27 +68,32 @@ export async function saveVideoToGallery( documentSaveOptions.newFileNames = ['video.mp4']; let documentPicker = new picker.DocumentViewPicker(context); documentPicker.save(documentSaveOptions).then(async (uris: Array) => { - if (uris.length === 0) { - uiContext?.getPromptAction().showToast({ message: $r('app.string.user_cancelled_authorization') }) - return; - } - openDownloadDialog(); // open dialog - const path = await webDownloadManager.start(Constants.INDEX_WEB_CONTROLLER, context.filesDir, uri); - closeDownloadDialog(); // close dialog - webDownloadManager.clearProgress(); - uiContext?.getPromptAction().showToast({ message: $r('app.string.download_success') }) - const targetFile = fs.openSync(uris[0], fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); - const sourceFile = fs.openSync(path, fs.OpenMode.READ_ONLY); - let buf = new ArrayBuffer(1024); - while (true) { - const len = fs.readSync(sourceFile.fd, buf); - if (0 === len) { - break; + try { + if (uris.length === 0) { + uiContext?.getPromptAction().showToast({ message: $r('app.string.user_cancelled_authorization') }) + return; + } + openDownloadDialog(); // open dialog + const path = await webDownloadManager.start(Constants.INDEX_WEB_CONTROLLER, context.filesDir, uri); + closeDownloadDialog(); // close dialog + webDownloadManager.clearProgress(); + uiContext?.getPromptAction().showToast({ message: $r('app.string.download_success') }) + const targetFile = fs.openSync(uris[0], fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + const sourceFile = fs.openSync(path, fs.OpenMode.READ_ONLY); + let buf = new ArrayBuffer(1024); + while (true) { + const len = fs.readSync(sourceFile.fd, buf); + if (0 === len) { + break; + } + fs.writeSync(targetFile.fd, buf); } - fs.writeSync(targetFile.fd, buf); + fs.closeSync(sourceFile); + fs.closeSync(targetFile); + } catch (error) { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); } - fs.closeSync(sourceFile); - fs.closeSync(targetFile); }).finally(() => { webDownloadManager.deleteFile(); }) diff --git a/entry/src/main/ets/utils/WebDownloadManager.ets b/entry/src/main/ets/utils/WebDownloadManager.ets index 8e1797a..b769fe0 100644 --- a/entry/src/main/ets/utils/WebDownloadManager.ets +++ b/entry/src/main/ets/utils/WebDownloadManager.ets @@ -16,6 +16,7 @@ import { webview } from '@kit.ArkWeb'; import { fileIo } from '@kit.CoreFileKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BusinessError } from '@kit.BasicServicesKit'; import { Constants } from '../common/Constants'; interface ControllerManager { @@ -86,32 +87,52 @@ implements ControllerManager { this.taskStatus = webDownloadItem.getState(); resolve(this.sourcePath); }) - // Associate the download delegate with the webview controller. - controller?.setDownloadDelegate(this.delegate); - controller?.startDownload(url); + try { + // Associate the download delegate with the webview controller. + controller?.setDownloadDelegate(this.delegate); + controller?.startDownload(url); + } catch (error) { + hilog.error(0x0000, 'testTag', 'getDefaultDisplaySync failed, code = %{public}d, message = %{public}s', + error.code, error.message); + } }); } public pause() { const state = this.currentTask.getState(); if (state === webview.WebDownloadState.IN_PROGRESS) { - this.currentTask.pause(); + try { + this.currentTask.pause(); + } catch (error) { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); + } } } public unpause() { const state = this.currentTask.getState(); if (state === webview.WebDownloadState.PAUSED) { - this.currentTask.resume(); + try { + this.currentTask.resume(); + } catch (error) { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); + } } } // Provides an external interface for resuming downloads. public resume() { - const state = this.currentTask.getState(); - if (state === webview.WebDownloadState.CANCELED) { - webview.WebDownloadManager.setDownloadDelegate(this.delegate); - webview.WebDownloadManager.resumeDownload(webview.WebDownloadItem.deserialize(this.failedData)); + try { + const state = this.currentTask.getState(); + if (state === webview.WebDownloadState.CANCELED) { + webview.WebDownloadManager.setDownloadDelegate(this.delegate); + webview.WebDownloadManager.resumeDownload(webview.WebDownloadItem.deserialize(this.failedData)); + } + } catch (error) { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); } } @@ -131,8 +152,16 @@ implements ControllerManager { } public deleteFile() { - if (fileIo.accessSync(this.sourcePath)) { - fileIo.unlink(this.sourcePath); + try { + if (fileIo.accessSync(this.sourcePath)) { + fileIo.unlink(this.sourcePath).catch((error: BusinessError) => { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); + }); + } + } catch (error) { + hilog.error(0x0000, 'testTag', 'Execution failed, code = %{public}d, message = %{public}s', + error.code, error.message); } } -- Gitee