From aa1ace0caf98056e2c79b34d9065c49338be5d9c Mon Sep 17 00:00:00 2001 From: SWangHash <88996709+SWangHash@users.noreply.github.com> Date: Sat, 9 May 2026 10:45:23 +0800 Subject: [PATCH] Fix the issue of url not opening --- src/apps/desktop/src/api/ohos/browser.rs | 26 +++++++++++++++++++ src/apps/desktop/src/api/ohos/mod.rs | 3 ++- src/apps/desktop/src/api/system_api.rs | 13 +++++++++- src/apps/desktop/src/lib.rs | 1 + src/apps/desktop/src/theme.rs | 2 +- .../main/ets/entryability/EntryAbility.ets | 15 +++++++++++ .../api/service-api/SystemAPI.ts | 13 +++++++--- 7 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 src/apps/desktop/src/api/ohos/browser.rs diff --git a/src/apps/desktop/src/api/ohos/browser.rs b/src/apps/desktop/src/api/ohos/browser.rs new file mode 100644 index 000000000..879b4b4d9 --- /dev/null +++ b/src/apps/desktop/src/api/ohos/browser.rs @@ -0,0 +1,26 @@ +use bitfun_core::util::JS_THREADSAFE_FUNCTION; +use log::{info,error}; +#[tauri::command] +pub async fn open_browser(url: String) -> Result<(), String> { + let function = { + let lock = JS_THREADSAFE_FUNCTION.read(); + lock.get("open_browser").cloned() + }; + let Some(function) = function else { + return Err("The Arkts has not register the function".to_owned()); + }; + let res = function.call_async(Ok(url)).await; + match res { + Ok(res) => match res.await{ + Ok(_) => { + info!("open_browser successfully"); + Ok(()) + }, + Err(err) => { + error!("open_browser failed: {}", err); + Err(err.to_string()) + } + }, + Err(err) => Err(err.to_string()), + } +} \ No newline at end of file diff --git a/src/apps/desktop/src/api/ohos/mod.rs b/src/apps/desktop/src/api/ohos/mod.rs index 62094c044..6eedc89d3 100644 --- a/src/apps/desktop/src/api/ohos/mod.rs +++ b/src/apps/desktop/src/api/ohos/mod.rs @@ -1,2 +1,3 @@ pub mod ohos_file_system; -pub mod window; \ No newline at end of file +pub mod window; +pub mod browser; \ No newline at end of file diff --git a/src/apps/desktop/src/api/system_api.rs b/src/apps/desktop/src/api/system_api.rs index 6281b606f..cff1d61dc 100644 --- a/src/apps/desktop/src/api/system_api.rs +++ b/src/apps/desktop/src/api/system_api.rs @@ -120,7 +120,18 @@ pub async fn run_system_command( success: result.success, }) } - +#[tauri::command] +pub async fn open_external_ohos(url: String) -> Result<(), String> { + #[cfg(target_env = "ohos")] + { + use crate::api::ohos::browser::open_browser; + open_browser(url).await + } + #[cfg(not(target_env = "ohos"))] + { + Err("open_external is only supported on ohos".to_string()) + } +} #[tauri::command] pub async fn set_macos_edit_menu_mode( state: State<'_, AppState>, diff --git a/src/apps/desktop/src/lib.rs b/src/apps/desktop/src/lib.rs index a31d0be9b..b3ae8b6ca 100644 --- a/src/apps/desktop/src/lib.rs +++ b/src/apps/desktop/src/lib.rs @@ -742,6 +742,7 @@ pub async fn _run() { api::terminal_api::terminal_shutdown_all, api::terminal_api::terminal_get_history, get_system_info, + open_external_ohos, send_system_notification, check_command_exists, check_commands_exist, diff --git a/src/apps/desktop/src/theme.rs b/src/apps/desktop/src/theme.rs index f7351b433..1b61ff40f 100644 --- a/src/apps/desktop/src/theme.rs +++ b/src/apps/desktop/src/theme.rs @@ -375,7 +375,7 @@ fn resize_agent_companion_window( #[tauri::command] pub async fn show_agent_companion_desktop_pet(app: tauri::AppHandle) -> Result<(), String> { - Err("Failed to create Agent companion window".to_string()); + Err("Failed to create Agent companion window".to_string()) } #[tauri::command] diff --git a/src/apps/vcoder/entry/src/main/ets/entryability/EntryAbility.ets b/src/apps/vcoder/entry/src/main/ets/entryability/EntryAbility.ets index 5d4c850fb..606b5c592 100644 --- a/src/apps/vcoder/entry/src/main/ets/entryability/EntryAbility.ets +++ b/src/apps/vcoder/entry/src/main/ets/entryability/EntryAbility.ets @@ -77,6 +77,21 @@ export default class EntryAbility extends RustAbility { return 'false'; } }); + RustModule.registerArktsFunction('open_browser', async (err: Error, url: string): Promise => { + try { + let want : Want = { + "action": "ohos.want.action.viewData", + "entities": ["entity.system.browsable"], + "uri": url + } + await this.context.startAbility(want); + hilog.info(201, 'vnext', `open url succeed: ${url}`); + return `start ability succeed with ${url}`; + } catch (error) { + hilog.info(201, 'vnext', `fail to open ${url} ${error.code}`); + return `fail to open ${url} ${error.code}`; + } + }); RustModule.registerArktsFunction('window_is_minimized', async (err: Error, arg: string): Promise => { let state = windowStage.getMainWindowSync().getWindowStatus(); if (state == window.WindowStatusType.MINIMIZE) { diff --git a/src/web-ui/src/infrastructure/api/service-api/SystemAPI.ts b/src/web-ui/src/infrastructure/api/service-api/SystemAPI.ts index 3736164c6..f04ecaae4 100644 --- a/src/web-ui/src/infrastructure/api/service-api/SystemAPI.ts +++ b/src/web-ui/src/infrastructure/api/service-api/SystemAPI.ts @@ -46,10 +46,17 @@ export class SystemAPI { async openExternal(url: string): Promise { try { - await openUrl(url); + // 尝试调用 open_external_ohos (OHOS平台会成功) + await api.invoke('open_external_ohos', {url}); } catch (error) { - log.error('Failed to open external URL', { url, error }); - throw new Error(`Failed to open external URL: ${error}`); + // 非 OHOS 平台会收到错误,然后回退到 openUrl + log.warn('open_external_ohos failed, falling back to openUrl', { url, error }); + try { + await openUrl(url); + } catch (error) { + log.error('Failed to open external URL', { url, error }); + throw new Error(`Failed to open external URL: ${error}`); + } } }