diff --git a/src-tauri/src/client.rs b/src-tauri/src/client.rs new file mode 100644 index 00000000..3b0fd0df --- /dev/null +++ b/src-tauri/src/client.rs @@ -0,0 +1,35 @@ +//! @file src/client.rs +//! @desc 客户端模块,负责操作米游社客户端 +//! @since Beta v0.3.3 + +use tauri::{AppHandle, Manager,WindowBuilder}; + +// 操作米游社客户端 +#[tauri::command] +pub async fn operate_mhy(handle: AppHandle, arg: String) { + let has_mhy_client = handle.get_window("mhy_client").is_some(); + let mhy_client; + if has_mhy_client { + mhy_client = handle.get_window("mhy_client").unwrap(); + } else { + mhy_client = WindowBuilder::from_config( + &handle, + handle.config().tauri.windows.get(1).unwrap().clone() + ) + .build() + .unwrap(); + } + if mhy_client.is_visible().unwrap() == false { + mhy_client.show().unwrap(); + } + mhy_client.set_focus().unwrap(); + let js_code = r#" + window.MiHoYoJSInterface = { + postMessage: function(arg) { chrome.webview.postMessage(arg);console.log(arg) }, + closePage: function() { this.postMessage('{"method":"closePage"}') }, + };"#; + mhy_client.eval(&js_code).ok().unwrap(); + let js_eval = format!("window.MiHoYoJSInterface.postMessage('{}')", arg); + handle.emit_all("post_mhy_client", arg).unwrap(); + mhy_client.eval(&js_eval).ok().unwrap(); +} \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index b6e76dd7..8e9f03a4 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -2,6 +2,7 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] use tauri::Manager; +mod client; // 放一个常数,用来判断应用是否初始化 static mut APP_INITIALIZED: bool = false; @@ -52,7 +53,7 @@ fn main() { let window = event.window().clone(); if window.label() == "TeyvatGuide" { // 子窗口 label 的数组 - const SUB_WINDOW_LABELS: [&str; 2] = ["Sub_window", "Dev_JSON"]; + const SUB_WINDOW_LABELS: [&str; 3] = ["Sub_window", "Dev_JSON", "mhy_client"]; for label in SUB_WINDOW_LABELS.iter() { let sub = window.get_window(label).unwrap(); sub.close().unwrap(); @@ -64,7 +65,7 @@ fn main() { } }) .plugin(tauri_plugin_sql::Builder::default().build()) - .invoke_handler(tauri::generate_handler![register_deep_link, init_app]) + .invoke_handler(tauri::generate_handler![register_deep_link, init_app, client::operate_mhy]) .setup(|_app| { let _window = _app.get_window("TeyvatGuide").unwrap(); #[cfg(debug_assertions)] // only include this code on debug builds diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 5b445b33..8c2b35d3 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -94,6 +94,17 @@ "height": 900, "center": true, "transparent": true + }, + { + "fullscreen": false, + "resizable": false, + "title": "米游社", + "label": "mhy_client", + "url": "https://bbs.mihoyo.com/", + "visible": false, + "width": 360, + "height": 780, + "center": true } ] } diff --git a/src/pages/common/Test.vue b/src/pages/common/Test.vue index bc8574eb..1a02f225 100644 --- a/src/pages/common/Test.vue +++ b/src/pages/common/Test.vue @@ -17,9 +17,14 @@
获取极验
+

窗口建立测试

+
+ 创建新窗口 +