🌱 完善客户端,获取 postMessage

This commit is contained in:
BTMuli
2023-10-18 13:21:56 +08:00
parent 4ab679dcea
commit 751372a4ba
7 changed files with 237 additions and 57 deletions

102
src-tauri/Cargo.lock generated
View File

@@ -12,6 +12,9 @@ dependencies = [
"tauri-build", "tauri-build",
"tauri-plugin-deep-link", "tauri-plugin-deep-link",
"tauri-plugin-sql", "tauri-plugin-sql",
"url",
"webview2-com 0.27.0",
"windows 0.51.1",
] ]
[[package]] [[package]]
@@ -4010,7 +4013,7 @@ dependencies = [
"unicode-segmentation", "unicode-segmentation",
"uuid", "uuid",
"windows 0.39.0", "windows 0.39.0",
"windows-implement", "windows-implement 0.39.0",
"x11-dl", "x11-dl",
] ]
@@ -4093,7 +4096,7 @@ dependencies = [
"url", "url",
"uuid", "uuid",
"webkit2gtk", "webkit2gtk",
"webview2-com", "webview2-com 0.19.1",
"windows 0.39.0", "windows 0.39.0",
] ]
@@ -4203,7 +4206,7 @@ dependencies = [
"thiserror", "thiserror",
"url", "url",
"uuid", "uuid",
"webview2-com", "webview2-com 0.19.1",
"windows 0.39.0", "windows 0.39.0",
] ]
@@ -4222,7 +4225,7 @@ dependencies = [
"tauri-utils", "tauri-utils",
"uuid", "uuid",
"webkit2gtk", "webkit2gtk",
"webview2-com", "webview2-com 0.19.1",
"windows 0.39.0", "windows 0.39.0",
"wry", "wry",
] ]
@@ -4883,10 +4886,24 @@ version = "0.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4a769c9f1a64a8734bde70caafac2b96cada12cd4aefa49196b3a386b8b4178" checksum = "b4a769c9f1a64a8734bde70caafac2b96cada12cd4aefa49196b3a386b8b4178"
dependencies = [ dependencies = [
"webview2-com-macros", "webview2-com-macros 0.6.0",
"webview2-com-sys", "webview2-com-sys 0.19.0",
"windows 0.39.0", "windows 0.39.0",
"windows-implement", "windows-implement 0.39.0",
]
[[package]]
name = "webview2-com"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd15556ff1d1d6bc850dbb362762bae86069773dd30177c90d3bfa917080dc73"
dependencies = [
"webview2-com-macros 0.7.0",
"webview2-com-sys 0.27.0",
"windows 0.51.1",
"windows-core",
"windows-implement 0.51.1",
"windows-interface",
] ]
[[package]] [[package]]
@@ -4900,6 +4917,17 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "webview2-com-macros"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac1345798ecd8122468840bcdf1b95e5dc6d2206c5e4b0eafa078d061f59c9bc"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.37",
]
[[package]] [[package]]
name = "webview2-com-sys" name = "webview2-com-sys"
version = "0.19.0" version = "0.19.0"
@@ -4915,6 +4943,17 @@ dependencies = [
"windows-metadata", "windows-metadata",
] ]
[[package]]
name = "webview2-com-sys"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3775bb005c3170497ec411b36005708b57ad486bfa3d23864c92f5973858ce8d"
dependencies = [
"thiserror",
"windows 0.51.1",
"windows-core",
]
[[package]] [[package]]
name = "whoami" name = "whoami"
version = "1.4.1" version = "1.4.1"
@@ -4971,7 +5010,7 @@ version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1c4bd0a50ac6020f65184721f758dba47bb9fbc2133df715ec74a237b26794a" checksum = "f1c4bd0a50ac6020f65184721f758dba47bb9fbc2133df715ec74a237b26794a"
dependencies = [ dependencies = [
"windows-implement", "windows-implement 0.39.0",
"windows_aarch64_msvc 0.39.0", "windows_aarch64_msvc 0.39.0",
"windows_i686_gnu 0.39.0", "windows_i686_gnu 0.39.0",
"windows_i686_msvc 0.39.0", "windows_i686_msvc 0.39.0",
@@ -4988,6 +5027,18 @@ dependencies = [
"windows-targets 0.48.5", "windows-targets 0.48.5",
] ]
[[package]]
name = "windows"
version = "0.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9"
dependencies = [
"windows-core",
"windows-implement 0.51.1",
"windows-interface",
"windows-targets 0.48.5",
]
[[package]] [[package]]
name = "windows-bindgen" name = "windows-bindgen"
version = "0.39.0" version = "0.39.0"
@@ -4998,6 +5049,15 @@ dependencies = [
"windows-tokens", "windows-tokens",
] ]
[[package]]
name = "windows-core"
version = "0.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64"
dependencies = [
"windows-targets 0.48.5",
]
[[package]] [[package]]
name = "windows-implement" name = "windows-implement"
version = "0.39.0" version = "0.39.0"
@@ -5008,6 +5068,28 @@ dependencies = [
"windows-tokens", "windows-tokens",
] ]
[[package]]
name = "windows-implement"
version = "0.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb2b158efec5af20d8846836622f50a87e6556b9153a42772fa047f773c0e555"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.37",
]
[[package]]
name = "windows-interface"
version = "0.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0546e63e1ce64c04403d2311fa0e3ab5ae3a367bd524b4a38d8d8d18c70cfa76"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.37",
]
[[package]] [[package]]
name = "windows-metadata" name = "windows-metadata"
version = "0.39.0" version = "0.39.0"
@@ -5289,9 +5371,9 @@ dependencies = [
"url", "url",
"webkit2gtk", "webkit2gtk",
"webkit2gtk-sys", "webkit2gtk-sys",
"webview2-com", "webview2-com 0.19.1",
"windows 0.39.0", "windows 0.39.0",
"windows-implement", "windows-implement 0.39.0",
] ]
[[package]] [[package]]

View File

@@ -16,6 +16,9 @@ tauri-build = { version = "1.4", features = [] }
tauri = { version = "1.4", features = ["api-all"] } tauri = { version = "1.4", features = ["api-all"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
webview2-com = "0.27.0"
windows = "0.51.1"
url = "2.4.1"
# sqlite 插件 # sqlite 插件
[dependencies.tauri-plugin-sql] [dependencies.tauri-plugin-sql]

View File

@@ -2,34 +2,45 @@
//! @desc 客户端模块,负责操作米游社客户端 //! @desc 客户端模块,负责操作米游社客户端
//! @since Beta v0.3.3 //! @since Beta v0.3.3
use tauri::{AppHandle, Manager,WindowBuilder}; use tauri::{AppHandle, Manager,WindowBuilder, WindowUrl};
use url::Url;
// 获取米游社客户端入口地址
fn get_mhy_client_url(func: String) -> WindowUrl {
let mut url_res: Url = "https://bbs.mihoyo.com/ys/".parse().unwrap();
if func == "sign_in" {
url_res = "https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html?act_id=e202009291139501".parse().unwrap();
} else if func == "game_record" {
url_res = "https://webstatic.mihoyo.com/app/community-game-records/index.html".parse().unwrap();
}
return WindowUrl::External(url_res);
}
// 操作米游社客户端 // 操作米游社客户端
#[tauri::command] #[tauri::command]
pub async fn operate_mhy(handle: AppHandle, arg: String) { pub async fn create_mhy_client(handle: AppHandle, func: String, arg: String) {
dbg!(&arg);
let has_mhy_client = handle.get_window("mhy_client").is_some(); let has_mhy_client = handle.get_window("mhy_client").is_some();
let mut mhy_client_config = handle.config().tauri.windows.get(1).unwrap().clone();
mhy_client_config.url = get_mhy_client_url(func.clone());
let mhy_client; let mhy_client;
if has_mhy_client { if has_mhy_client {
mhy_client = handle.get_window("mhy_client").unwrap(); mhy_client = handle.get_window("mhy_client").unwrap();
mhy_client.close().unwrap();
} else { } else {
mhy_client = WindowBuilder::from_config( mhy_client = WindowBuilder::from_config(
&handle, &handle,
handle.config().tauri.windows.get(1).unwrap().clone() mhy_client_config,
) )
.build() .build()
.unwrap(); .unwrap();
}
if mhy_client.is_visible().unwrap() == false {
mhy_client.show().unwrap(); mhy_client.show().unwrap();
mhy_client.set_focus().unwrap();
let js_code = r#"
window.MiHoYoJSInterface = {
postMessage: function(arg) { window.__TAURI__.event.emit('post_mhy_client', arg) },
closePage: function() { this.postMessage('{"method":"closePage"}') },
};"#;
mhy_client.eval(&js_code).ok().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

@@ -65,7 +65,7 @@ fn main() {
} }
}) })
.plugin(tauri_plugin_sql::Builder::default().build()) .plugin(tauri_plugin_sql::Builder::default().build())
.invoke_handler(tauri::generate_handler![register_deep_link, init_app, client::operate_mhy]) .invoke_handler(tauri::generate_handler![register_deep_link, init_app, client::create_mhy_client])
.setup(|_app| { .setup(|_app| {
let _window = _app.get_window("TeyvatGuide").unwrap(); let _window = _app.get_window("TeyvatGuide").unwrap();
#[cfg(debug_assertions)] // only include this code on debug builds #[cfg(debug_assertions)] // only include this code on debug builds

View File

@@ -20,25 +20,7 @@
"http": { "http": {
"all": true, "all": true,
"request": true, "request": true,
"scope": [ "scope": ["https://**"]
"https://act-webstatic.mihoyo.com/*",
"https://api-static.mihoyo.com/*",
"https://api-takumi-record.mihoyo.com/*",
"https://api-takumi.mihoyo.com/*",
"https://api.geetest.com/*",
"https://bbs-api-static.miyoushe.com/*",
"https://bbs-api.mihoyo.com/*",
"https://bbs-api.miyoushe.com/*",
"https://bbs.mihoyo.com/*",
"https://enka-api.hut.ao/*",
"https://hk4e-api.mihoyo.com/*",
"https://hk4e-sdk.mihoyo.com/*",
"https://homa.snapgenshin.com/*",
"https://passport-api-v4.mihoyo.com/*",
"https://passport-api.mihoyo.com/*",
"https://passport-api.miyoushe.com/*",
"https://sdk-webstatic.mihoyo.com/*"
]
}, },
"shell": { "shell": {
"all": false, "all": false,
@@ -73,7 +55,19 @@
} }
}, },
"security": { "security": {
"csp": null "dangerousRemoteDomainIpcAccess": [
{
"domain": "api-static.mihoyo.com",
"windows": ["mhy_client"],
"enableTauriAPI": true
},
{
"domain": "webstatic.mihoyo.com",
"windows": ["mhy_client"],
"enableTauriAPI": true
}
],
"csp": "default-src 'self' *; img-src *; script-src *"
}, },
"updater": { "updater": {
"active": false, "active": false,
@@ -100,7 +94,8 @@
"resizable": false, "resizable": false,
"title": "米游社", "title": "米游社",
"label": "mhy_client", "label": "mhy_client",
"url": "https://bbs.mihoyo.com/", "url": "https://api-static.mihoyo.com/",
"userAgent": "Mozilla/5.0 (Linux; Android 12) Mobile miHoYoBBS/2.60.1",
"visible": false, "visible": false,
"width": 360, "width": 360,
"height": 780, "height": 780,

View File

@@ -24,19 +24,16 @@
</div> </div>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { invoke, event } from "@tauri-apps/api";
import { onMounted, ref } from "vue"; import { onMounted, ref } from "vue";
import showGeetest from "../../components/func/geetest"; import showGeetest from "../../components/func/geetest";
import mhyClient from "../../utils/TGClient";
const visible = ref<boolean>(false); const visible = ref<boolean>(false);
onMounted(async () => { onMounted(async () => {
visible.value = false; visible.value = false;
await event.listen("post_mhy_client", (res) => { await mhyClient.run();
console.log(res.payload);
console.log(res.windowLabel);
});
}); });
async function getGC(): Promise<void> { async function getGC(): Promise<void> {
@@ -44,7 +41,7 @@ async function getGC(): Promise<void> {
} }
async function tryNewWindow(): Promise<void> { async function tryNewWindow(): Promise<void> {
await invoke("operate_mhy", { func: "test", arg: "test" }); await mhyClient.open("game_record", "test");
} }
</script> </script>
<style lang="css" scoped> <style lang="css" scoped>

92
src/utils/TGClient.ts Normal file
View File

@@ -0,0 +1,92 @@
/**
* @file utils/TGClient.ts
* @desc 负责米游社客户端的 callback 处理
* @since Beta v0.3.3
*/
import { event, invoke } from "@tauri-apps/api";
import type { Event } from "@tauri-apps/api/event";
// 正常 arg 参数
interface NormalArg {
method: string;
payload: any;
callback: string;
}
/**
* @class TGClient
* @since Beta v0.3.3
* @description 米游社客户端
*/
class TGClient {
/**
* @private 监听实例
* @since Beta v0.3.3
* @type {EventEmitter}
* @memberof TGClient
*/
private listener: any;
/**
* @func run
* @since Beta v0.3.3
* @desc 运行米游社客户端
* @returns {void} - 无返回值
*/
async run(): Promise<void> {
if (this.listener === undefined) {
this.listener = await event.listen("post_mhy_client", (arg: Event<string>) => {
this.handleCallback(arg);
});
} else {
console.log("已经监听了");
}
}
/**
* @func open
* @since Beta v0.3.3
* @desc 打开米游社客户端
* @param {string} func - 方法名
* @param {string} params - 参数
* @returns {void} - 无返回值
*/
async open(func: string, params: string): Promise<void> {
await invoke("create_mhy_client", { func, arg: params });
}
/**
* @func handleCallback
* @since Beta v0.3.3
* @desc 处理米游社客户端的 callback
* @param {Event<string>} arg - 事件参数
* @returns {any} - 返回值
*/
handleCallback(arg: Event<string>): any {
console.log(`[${arg.windowLabel}] ${arg.payload}`);
const { method, payload, callback } = <NormalArg>JSON.parse(arg.payload);
console.log(method, payload, callback);
switch (method) {
case "getStatusBarHeight":
return 0;
case "getCookieInfo":
this.getCookieInfo(callback);
}
}
/**
* @func getCookieInfo
* @since Beta v0.3.3
* @desc 获取米游社客户端的 cookie
* @param {string} callback - 回调函数名
* @returns {void} - 无返回值
*/
getCookieInfo(callback: string): void {
console.log("getCookieInfo");
console.log(callback);
}
}
const mhyClient = new TGClient();
export default mhyClient;