🌱 初步构建米游社客户端子窗口

This commit is contained in:
BTMuli
2023-10-18 01:24:19 +08:00
parent c41b89acf7
commit 4ab679dcea
4 changed files with 62 additions and 2 deletions

35
src-tauri/src/client.rs Normal file
View File

@@ -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();
}

View File

@@ -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

View File

@@ -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
}
]
}

View File

@@ -17,9 +17,14 @@
<div class="btn-list">
<v-btn class="test-btn" @click="getGC">获取极验</v-btn>
</div>
<h1>窗口建立测试</h1>
<div class="btn-list">
<v-btn class="test-btn" @click="tryNewWindow">创建新窗口</v-btn>
</div>
</div>
</template>
<script lang="ts" setup>
import { invoke, event } from "@tauri-apps/api";
import { onMounted, ref } from "vue";
import showGeetest from "../../components/func/geetest";
@@ -28,11 +33,19 @@ const visible = ref<boolean>(false);
onMounted(async () => {
visible.value = false;
await event.listen("post_mhy_client", (res) => {
console.log(res.payload);
console.log(res.windowLabel);
});
});
async function getGC(): Promise<void> {
await showGeetest();
}
async function tryNewWindow(): Promise<void> {
await invoke("operate_mhy", { func: "test", arg: "test" });
}
</script>
<style lang="css" scoped>
.btn-list {