mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-12 09:18:14 +08:00
🌱 完善窗口处理,代码格式化
This commit is contained in:
@@ -9,7 +9,9 @@ use url::Url;
|
|||||||
fn get_mhy_client_url(func: String) -> WindowUrl {
|
fn get_mhy_client_url(func: String) -> WindowUrl {
|
||||||
let mut url_res: Url = "https://bbs.mihoyo.com/ys/".parse().unwrap();
|
let mut url_res: Url = "https://bbs.mihoyo.com/ys/".parse().unwrap();
|
||||||
if func == "sign_in" {
|
if func == "sign_in" {
|
||||||
url_res = "https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html?act_id=e202009291139501".parse().unwrap();
|
url_res = "https://webstatic.mihoyo.com/bbs/event/signin-ys/index.html?act_id=e202009291139501"
|
||||||
|
.parse()
|
||||||
|
.unwrap();
|
||||||
} else if func == "game_record" {
|
} else if func == "game_record" {
|
||||||
url_res = "https://webstatic.mihoyo.com/app/community-game-records/index.html".parse().unwrap();
|
url_res = "https://webstatic.mihoyo.com/app/community-game-records/index.html".parse().unwrap();
|
||||||
}
|
}
|
||||||
@@ -18,29 +20,19 @@ fn get_mhy_client_url(func: String) -> WindowUrl {
|
|||||||
|
|
||||||
// 操作米游社客户端
|
// 操作米游社客户端
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub async fn create_mhy_client(handle: AppHandle, func: String, arg: String) {
|
pub async fn create_mhy_client(handle: AppHandle, func: String) {
|
||||||
dbg!(&arg);
|
let mut mhy_window_config = handle.config().tauri.windows.get(1).unwrap().clone();
|
||||||
|
mhy_window_config.url = get_mhy_client_url(func.clone());
|
||||||
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;
|
|
||||||
if has_mhy_client {
|
if has_mhy_client {
|
||||||
mhy_client = handle.get_window("mhy_client").unwrap();
|
return;
|
||||||
mhy_client.close().unwrap();
|
}
|
||||||
} else {
|
let js_bridge = r#"
|
||||||
mhy_client = WindowBuilder::from_config(
|
|
||||||
&handle,
|
|
||||||
mhy_client_config,
|
|
||||||
)
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
mhy_client.show().unwrap();
|
|
||||||
mhy_client.set_focus().unwrap();
|
|
||||||
let js_code = r#"
|
|
||||||
window.MiHoYoJSInterface = {
|
window.MiHoYoJSInterface = {
|
||||||
postMessage: function(arg) { window.__TAURI__.event.emit('post_mhy_client', arg) },
|
postMessage: function(arg) { window.__TAURI__.event.emit('post_mhy_client', arg) },
|
||||||
closePage: function() { this.postMessage('{"method":"closePage"}') },
|
closePage: function() { this.postMessage('{"method":"closePage"}') },
|
||||||
};"#;
|
};
|
||||||
mhy_client.eval(&js_code).ok().unwrap();
|
"#;
|
||||||
}
|
let mhy_client = WindowBuilder::from_config(&handle, mhy_window_config).build();
|
||||||
|
mhy_client.unwrap().eval(&js_bridge).ok().unwrap();
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
|
//! @file src/main.rs
|
||||||
|
//! @desc 主模块,用于启动应用
|
||||||
|
//! @since Beta v0.3.3
|
||||||
|
|
||||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||||
|
|
||||||
@@ -30,19 +34,23 @@ async fn register_deep_link(app_handle: tauri::AppHandle) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
tauri_plugin_deep_link::register(
|
tauri_plugin_deep_link::register("teyvatguide", move |request| {
|
||||||
"teyvatguide",
|
|
||||||
move |request| {
|
|
||||||
dbg!(&request);
|
dbg!(&request);
|
||||||
app_handle.emit_all("active_deep_link", request).unwrap();
|
app_handle.emit_all("active_deep_link", request).unwrap();
|
||||||
},
|
})
|
||||||
)
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
unsafe {
|
unsafe {
|
||||||
DEEP_LINK_REGISTERED = true;
|
DEEP_LINK_REGISTERED = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 执行 js
|
||||||
|
#[tauri::command]
|
||||||
|
async fn execute_js(app_handle: tauri::AppHandle, label: String, js: String) {
|
||||||
|
let window = app_handle.get_window(&label).unwrap();
|
||||||
|
window.eval(&js).ok().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tauri_plugin_deep_link::prepare("teyvatguide");
|
tauri_plugin_deep_link::prepare("teyvatguide");
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
@@ -60,12 +68,17 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
window.close().unwrap();
|
window.close().unwrap();
|
||||||
},
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.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::create_mhy_client])
|
.invoke_handler(tauri::generate_handler![
|
||||||
|
register_deep_link,
|
||||||
|
init_app,
|
||||||
|
execute_js,
|
||||||
|
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
|
||||||
|
|||||||
@@ -104,14 +104,13 @@ onMounted(async () => {
|
|||||||
|
|
||||||
.hta-tuf-box {
|
.hta-tuf-box {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
overflow: hidden auto;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
max-height: 100%;
|
max-height: 100%;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
overflow-x: hidden;
|
|
||||||
overflow-y: auto;
|
|
||||||
row-gap: 10px;
|
row-gap: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ async function getGC(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function tryNewWindow(): Promise<void> {
|
async function tryNewWindow(): Promise<void> {
|
||||||
await mhyClient.open("game_record", "test");
|
await mhyClient.open("game_record");
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<style lang="css" scoped>
|
<style lang="css" scoped>
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
/**
|
/**
|
||||||
* @file utils/TGClient.ts
|
* @file utils/TGClient.ts
|
||||||
* @desc 负责米游社客户端的 callback 处理
|
* @desc 负责米游社客户端的 callback 处理
|
||||||
* @since Beta v0.3.3
|
* @since Beta v0.3.4
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { event, invoke } from "@tauri-apps/api";
|
import { event, invoke } from "@tauri-apps/api";
|
||||||
import type { Event } from "@tauri-apps/api/event";
|
import type { Event } from "@tauri-apps/api/event";
|
||||||
|
import { WebviewWindow } from "@tauri-apps/api/window";
|
||||||
|
|
||||||
|
import TGSqlite from "../plugins/Sqlite";
|
||||||
|
|
||||||
// 正常 arg 参数
|
// 正常 arg 参数
|
||||||
interface NormalArg {
|
interface NormalArg {
|
||||||
@@ -14,9 +17,14 @@ interface NormalArg {
|
|||||||
callback: string;
|
callback: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// invoke 参数
|
||||||
|
interface InvokeArg {
|
||||||
|
func: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class TGClient
|
* @class TGClient
|
||||||
* @since Beta v0.3.3
|
* @since Beta v0.3.4
|
||||||
* @description 米游社客户端
|
* @description 米游社客户端
|
||||||
*/
|
*/
|
||||||
class TGClient {
|
class TGClient {
|
||||||
@@ -27,53 +35,106 @@ class TGClient {
|
|||||||
* @memberof TGClient
|
* @memberof TGClient
|
||||||
*/
|
*/
|
||||||
private listener: any;
|
private listener: any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private 窗口实例
|
||||||
|
* @since Beta v0.3.4
|
||||||
|
* @type {WebviewWindow}
|
||||||
|
* @memberof TGClient
|
||||||
|
*/
|
||||||
|
private window: WebviewWindow | null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @since Beta v0.3.4
|
||||||
|
* @description 构造函数
|
||||||
|
* @memberof TGClient
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
try {
|
||||||
|
this.window = WebviewWindow.getByLabel("mhy_client");
|
||||||
|
} catch (error) {
|
||||||
|
this.window = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @func run
|
* @func run
|
||||||
* @since Beta v0.3.3
|
* @since Beta v0.3.4
|
||||||
* @desc 运行米游社客户端
|
* @desc 运行米游社客户端
|
||||||
* @returns {void} - 无返回值
|
* @returns {void} - 无返回值
|
||||||
*/
|
*/
|
||||||
async run(): Promise<void> {
|
async run(): Promise<void> {
|
||||||
if (this.listener === undefined) {
|
if (this.listener === undefined) {
|
||||||
this.listener = await event.listen("post_mhy_client", (arg: Event<string>) => {
|
this.listener = await event.listen("post_mhy_client", async (arg: Event<string>) => {
|
||||||
this.handleCallback(arg);
|
await this.handleCallback(arg);
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
console.log("已经监听了");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @func open
|
* @func open
|
||||||
* @since Beta v0.3.3
|
* @since Beta v0.3.4
|
||||||
* @desc 打开米游社客户端
|
* @desc 打开米游社客户端
|
||||||
* @param {string} func - 方法名
|
* @param {string} func - 方法名
|
||||||
* @param {string} params - 参数
|
|
||||||
* @returns {void} - 无返回值
|
* @returns {void} - 无返回值
|
||||||
*/
|
*/
|
||||||
async open(func: string, params: string): Promise<void> {
|
async open(func: string): Promise<void> {
|
||||||
await invoke("create_mhy_client", { func, arg: params });
|
if (this.window !== null) {
|
||||||
|
await this.window.close();
|
||||||
|
}
|
||||||
|
await invoke<InvokeArg>("create_mhy_client", { func });
|
||||||
|
this.window = WebviewWindow.getByLabel("mhy_client");
|
||||||
|
await this.window?.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @func handleCallback
|
* @func handleCallback
|
||||||
* @since Beta v0.3.3
|
* @since Beta v0.3.4
|
||||||
* @desc 处理米游社客户端的 callback
|
* @desc 处理米游社客户端的 callback
|
||||||
* @param {Event<string>} arg - 事件参数
|
* @param {Event<string>} arg - 事件参数
|
||||||
* @returns {any} - 返回值
|
* @returns {any} - 返回值
|
||||||
*/
|
*/
|
||||||
handleCallback(arg: Event<string>): any {
|
async handleCallback(arg: Event<string>): Promise<any> {
|
||||||
console.log(`[${arg.windowLabel}] ${arg.payload}`);
|
console.log(`[${arg.windowLabel}] ${arg.payload}`);
|
||||||
const { method, payload, callback } = <NormalArg>JSON.parse(arg.payload);
|
const { method, payload, callback } = <NormalArg>JSON.parse(arg.payload);
|
||||||
console.log(method, payload, callback);
|
console.log(method, payload, callback);
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case "getStatusBarHeight":
|
case "getStatusBarHeight":
|
||||||
return 0;
|
this.getStatusBarHeight(callback);
|
||||||
|
break;
|
||||||
case "getCookieInfo":
|
case "getCookieInfo":
|
||||||
this.getCookieInfo(callback);
|
await this.getCookieInfo(callback);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @func callback
|
||||||
|
* @since Beta v0.3.4
|
||||||
|
* @desc 回调函数
|
||||||
|
* @param {string} callback - 回调函数名
|
||||||
|
* @param {string} payload - 回调函数参数
|
||||||
|
* @returns {void} - 无返回值
|
||||||
|
*/
|
||||||
|
async callback(callback: string, payload?: string): Promise<void> {
|
||||||
|
const js = `javascript:mhyWebBridge("${callback}", ${payload ?? ""})`;
|
||||||
|
await invoke("create_mhy_client");
|
||||||
|
await invoke("execute_js", { label: "mhy_client", js });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @func getStatusBarHeight
|
||||||
|
* @since Beta v0.3.4
|
||||||
|
* @desc 获取状态栏高度
|
||||||
|
* @param {string} callback - 回调函数名
|
||||||
|
* @returns {void} - 无返回值
|
||||||
|
*/
|
||||||
|
getStatusBarHeight(callback: string): void {
|
||||||
|
console.log("getStatusBarHeight");
|
||||||
|
console.log(callback);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @func getCookieInfo
|
* @func getCookieInfo
|
||||||
* @since Beta v0.3.3
|
* @since Beta v0.3.3
|
||||||
@@ -81,9 +142,10 @@ class TGClient {
|
|||||||
* @param {string} callback - 回调函数名
|
* @param {string} callback - 回调函数名
|
||||||
* @returns {void} - 无返回值
|
* @returns {void} - 无返回值
|
||||||
*/
|
*/
|
||||||
getCookieInfo(callback: string): void {
|
async getCookieInfo(callback: string): Promise<void> {
|
||||||
console.log("getCookieInfo");
|
console.log("getCookieInfo");
|
||||||
console.log(callback);
|
const cookie = await TGSqlite.getCookie();
|
||||||
|
await this.callback(callback, JSON.stringify(cookie));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,9 @@
|
|||||||
".yml",
|
".yml",
|
||||||
"package.json",
|
"package.json",
|
||||||
"src/**/*.d.ts",
|
"src/**/*.d.ts",
|
||||||
"src/data/**/*.json",
|
|
||||||
"src/**/*.ts",
|
"src/**/*.ts",
|
||||||
"src/**/*.vue",
|
"src/**/*.vue",
|
||||||
|
"src/data/**/*.json",
|
||||||
"tsconfig.json",
|
"tsconfig.json",
|
||||||
"vite.config.ts"
|
"vite.config.ts"
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user