♻️ 感觉差不多了,剩下的就靠测试了 #92

This commit is contained in:
目棃
2024-07-03 22:33:00 +08:00
parent 8a2c7d13c6
commit f4de7552e1
28 changed files with 249 additions and 399 deletions

View File

@@ -43,16 +43,11 @@ fn create_utils_menu(app: AppHandle) -> Submenu<Wry> {
}
// 创建米游社客户端菜单
pub fn create_mhy_menu(func: String, app: AppHandle) -> Menu<Wry> {
pub fn create_mhy_menu(app: AppHandle) -> Menu<Wry> {
let top_menu = MenuItemBuilder::with_id("top", "置顶").build(&app).unwrap();
let cancel_top_menu = MenuItemBuilder::with_id("cancel_top", "取消置顶").build(&app).unwrap();
let sign_in_menu = MenuItemBuilder::with_id("sign_in", "用户登录").build(&app).unwrap();
let open_post_menu = MenuItemBuilder::with_id("open_post", "打开帖子").build(&app).unwrap();
let utils_menu = create_utils_menu(app.clone());
// 如果是登录
if func == "config_sign_in" {
return MenuBuilder::new(&app).item(&sign_in_menu).build().expect("failed to create mhy_menu");
}
return MenuBuilder::new(&app)
.item(&top_menu)
.item(&cancel_top_menu)
@@ -67,7 +62,6 @@ pub fn handle_menu_event(window: &Window, event: MenuEvent) {
match event.id.as_ref() {
"top" => handle_menu_top(window),
"cancel_top" => handle_menu_cancel_top(window),
"sign_in" => handle_menu_sign_in(window),
"open_post" => handle_menu_open_post(window),
"retry" => handle_menu_retry(window),
"mock_touch" => handle_menu_mock_touch(window),
@@ -93,29 +87,6 @@ fn handle_menu_cancel_top(app_handle: &Window) {
}
}
// 处理用户登录菜单
fn handle_menu_sign_in(app_handle: &Window) {
let window = app_handle.get_webview_window("mhy_client");
let execute_js = r#"
javascript:(async function(){
// 首先检测是不是 user.mihoyo.com
const url = new URL(window.location.href);
if(url.hostname !== "user.mihoyo.com"){
alert("当前页面不是米游社登录页面");
return;
}
const ck = document.cookie;
const arg = {
method: 'teyvat_sign_in',
payload: ck,
}
await window.__TAURI__.event.emit('post_mhy_client',JSON.stringify(arg));
})()"#;
if window.is_some() {
window.unwrap().eval(&execute_js).ok().unwrap();
}
}
// 处理打开帖子菜单
fn handle_menu_open_post(app_handle: &Window) {
let window = app_handle.get_webview_window("mhy_client");

View File

@@ -3,42 +3,37 @@
//! @since Beta v0.5.0
mod menu;
use tauri::async_runtime::block_on;
use tauri::{AppHandle, Manager, WebviewWindowBuilder, WindowEvent};
use tauri::{AppHandle, Manager, WebviewWindowBuilder};
use tauri_utils::config::WebviewUrl;
#[tauri::command]
pub async fn create_mhy_client(handle: AppHandle, func: String, url: String) {
let mut mhy_window_config = handle.config().app.windows.get(1).unwrap().clone();
mhy_window_config.visible = true;
let mut option = handle.config().app.windows.get(1).unwrap().clone();
let url_parse;
if url != "" {
mhy_window_config.url = WebviewUrl::External(url.parse().unwrap());
url_parse = WebviewUrl::External(url.parse().unwrap());
} else {
mhy_window_config.url = menu::get_mhy_client_url(func.clone());
url_parse = menu::get_mhy_client_url(func.clone());
}
if func == "birthday"
|| func == "web_act"
|| url.starts_with("https://webstatic.mihoyo.com/ys/event/e20220303-birthday/index.html")
{
mhy_window_config.width = 1280.0;
mhy_window_config.height = 720.0;
option.width = 1280.0;
option.height = 720.0;
}
let window_get = handle.get_webview_window("mhy_client");
if window_get.is_some() {
let mhy_client = window_get.unwrap();
mhy_client.on_window_event(move |e| {
if let WindowEvent::Destroyed = e {
let future = create_mhy_client(handle.clone(), func.clone(), url.clone());
block_on(future);
}
});
mhy_client.close().unwrap();
let window_find = handle.get_webview_window("mhy_client");
if window_find.is_some() {
window_find.unwrap().destroy().unwrap();
return;
}
WebviewWindowBuilder::from_config(&handle, &mhy_window_config)
.expect("failed to create mhy_client")
.menu(menu::create_mhy_menu(func.clone(), handle.clone()))
WebviewWindowBuilder::new(&handle, "mhy_client", url_parse)
.inner_size(option.width, option.height)
.title(option.title)
.center()
.user_agent(option.user_agent.unwrap().as_str())
.menu(menu::create_mhy_menu(handle.clone()))
.on_menu_event(move |app, event| menu::handle_menu_event(app, event))
.build()
.expect("failed to add menu")
.on_menu_event(move |app, event| menu::handle_menu_event(app, event));
.unwrap();
}

View File

@@ -38,8 +38,7 @@ pub async fn create_window(
) {
let window_find = app_handle.get_webview_window(&label);
if window_find.is_some() {
let window = window_find.unwrap();
window.destroy().unwrap();
window_find.unwrap().destroy().unwrap();
return;
}
let url_parse = WebviewUrl::App(url.parse().unwrap());
@@ -55,10 +54,10 @@ pub async fn create_window(
// 执行 js
#[tauri::command]
pub async fn execute_js(app_handle: tauri::AppHandle, label: String, js: String) {
pub async fn execute_js(app_handle: AppHandle, label: String, js: String) {
let window = app_handle.get_webview_window(&label);
if window.is_some() {
window.unwrap().eval(&js).ok().unwrap();
window.unwrap().eval(&js).unwrap();
}
}