diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index b370e79..3614a16 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -269,7 +269,10 @@ version = "0.1.0" dependencies = [ "anyhow", "notify", - "parking_lot", + "parking_lot 0.12.4", + "reqwest", + "reqwest-middleware", + "reqwest-retry", "serde", "serde_json", "specta", @@ -550,6 +553,16 @@ dependencies = [ "version_check", ] +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "core-foundation" version = "0.10.1" @@ -573,9 +586,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa95a34622365fa5bbf40b20b75dba8dfa8c94c734aea8ac9a5ca38af14316f1" dependencies = [ "bitflags 2.9.1", - "core-foundation", + "core-foundation 0.10.1", "core-graphics-types", - "foreign-types", + "foreign-types 0.5.0", "libc", ] @@ -586,7 +599,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d44a101f213f6c4cdc1853d4b78aef6db6bdfa3468798cc1d9912f4735013eb" dependencies = [ "bitflags 2.9.1", - "core-foundation", + "core-foundation 0.10.1", "libc", ] @@ -980,6 +993,15 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared 0.1.1", +] + [[package]] name = "foreign-types" version = "0.5.0" @@ -987,7 +1009,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" dependencies = [ "foreign-types-macros", - "foreign-types-shared", + "foreign-types-shared 0.3.1", ] [[package]] @@ -1001,6 +1023,12 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "foreign-types-shared" version = "0.3.1" @@ -1035,6 +1063,21 @@ dependencies = [ "new_debug_unreachable", ] +[[package]] +name = "futures" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.31" @@ -1042,6 +1085,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -1109,6 +1153,7 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ + "futures-channel", "futures-core", "futures-io", "futures-macro", @@ -1256,8 +1301,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi 0.11.1+wasi-snapshot-preview1", + "wasm-bindgen", ] [[package]] @@ -1533,6 +1580,22 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-tls" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" +dependencies = [ + "bytes", + "http-body-util", + "hyper", + "hyper-util", + "native-tls", + "tokio", + "tokio-native-tls", + "tower-service", +] + [[package]] name = "hyper-util" version = "0.1.15" @@ -1552,9 +1615,11 @@ dependencies = [ "percent-encoding", "pin-project-lite", "socket2", + "system-configuration", "tokio", "tower-service", "tracing", + "windows-registry", ] [[package]] @@ -1755,6 +1820,18 @@ dependencies = [ "libc", ] +[[package]] +name = "instant" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "io-uring" version = "0.7.8" @@ -2112,6 +2189,23 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "native-tls" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87de3442987e9dbec73158d5c715e7ad9072fda936bb03d19d7fa10e00520f0e" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "ndk" version = "0.9.0" @@ -2488,6 +2582,50 @@ dependencies = [ "pathdiff", ] +[[package]] +name = "openssl" +version = "0.10.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8505734d46c8ab1e19a1dce3aef597ad87dcb4c37e7188231769bd6bd51cebf8" +dependencies = [ + "bitflags 2.9.1", + "cfg-if", + "foreign-types 0.3.2", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.104", +] + +[[package]] +name = "openssl-probe" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" + +[[package]] +name = "openssl-sys" +version = "0.9.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90096e2e47630d78b7d1c20952dc621f957103f8bc2c8359ec81290d75238571" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "option-ext" version = "0.2.0" @@ -2541,6 +2679,17 @@ version = "2.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core 0.8.6", +] + [[package]] name = "parking_lot" version = "0.12.4" @@ -2548,7 +2697,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13" dependencies = [ "lock_api", - "parking_lot_core", + "parking_lot_core 0.9.11", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall 0.2.16", + "smallvec", + "winapi", ] [[package]] @@ -2559,7 +2722,7 @@ checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5" dependencies = [ "cfg-if", "libc", - "redox_syscall", + "redox_syscall 0.5.13", "smallvec", "windows-targets 0.52.6", ] @@ -2994,6 +3157,15 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "redox_syscall" version = "0.5.13" @@ -3077,16 +3249,20 @@ dependencies = [ "http-body", "http-body-util", "hyper", + "hyper-tls", "hyper-util", "js-sys", "log", + "native-tls", "percent-encoding", "pin-project-lite", + "rustls-pki-types", "serde", "serde_json", "serde_urlencoded", "sync_wrapper", "tokio", + "tokio-native-tls", "tokio-util", "tower", "tower-http", @@ -3098,6 +3274,52 @@ dependencies = [ "web-sys", ] +[[package]] +name = "reqwest-middleware" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57f17d28a6e6acfe1733fe24bcd30774d13bffa4b8a22535b4c8c98423088d4e" +dependencies = [ + "anyhow", + "async-trait", + "http", + "reqwest", + "serde", + "thiserror 1.0.69", + "tower-service", +] + +[[package]] +name = "reqwest-retry" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29c73e4195a6bfbcb174b790d9b3407ab90646976c55de58a6515da25d851178" +dependencies = [ + "anyhow", + "async-trait", + "futures", + "getrandom 0.2.16", + "http", + "hyper", + "parking_lot 0.11.2", + "reqwest", + "reqwest-middleware", + "retry-policies", + "thiserror 1.0.69", + "tokio", + "tracing", + "wasm-timer", +] + +[[package]] +name = "retry-policies" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5875471e6cab2871bc150ecb8c727db5113c9338cc3354dc5ee3425b6aa40a1c" +dependencies = [ + "rand 0.8.5", +] + [[package]] name = "rustc-demangle" version = "0.1.25" @@ -3126,6 +3348,15 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "rustls-pki-types" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "229a4a4c221013e7e1f1a043678c5cc39fe5171437c88fb47151a21e6f5b5c79" +dependencies = [ + "zeroize", +] + [[package]] name = "rustversion" version = "1.0.21" @@ -3147,6 +3378,15 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "schannel" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "schemars" version = "0.8.22" @@ -3204,6 +3444,29 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "security-framework" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" +dependencies = [ + "bitflags 2.9.1", + "core-foundation 0.9.4", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49db231d56a190491cb4aeda9527f1ad45345af50b0851622a7adb8c03b01c32" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "selectors" version = "0.24.0" @@ -3465,14 +3728,14 @@ dependencies = [ "bytemuck", "cfg_aliases", "core-graphics", - "foreign-types", + "foreign-types 0.5.0", "js-sys", "log", "objc2 0.5.2", "objc2-foundation 0.2.2", "objc2-quartz-core 0.2.2", "raw-window-handle", - "redox_syscall", + "redox_syscall 0.5.13", "wasm-bindgen", "web-sys", "windows-sys 0.59.0", @@ -3568,7 +3831,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf776ba3fa74f83bf4b63c3dcbbf82173db2632ed8452cb2d891d33f459de70f" dependencies = [ "new_debug_unreachable", - "parking_lot", + "parking_lot 0.12.4", "phf_shared 0.11.3", "precomputed-hash", "serde", @@ -3645,6 +3908,27 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "system-configuration" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" +dependencies = [ + "bitflags 2.9.1", + "core-foundation 0.9.4", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "system-deps" version = "6.2.2" @@ -3665,7 +3949,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49c380ca75a231b87b6c9dd86948f035012e7171d1a7c40a9c2890489a7ffd8a" dependencies = [ "bitflags 2.9.1", - "core-foundation", + "core-foundation 0.10.1", "core-graphics", "crossbeam-channel", "dispatch", @@ -3685,7 +3969,7 @@ dependencies = [ "objc2-app-kit", "objc2-foundation 0.3.1", "once_cell", - "parking_lot", + "parking_lot 0.12.4", "raw-window-handle", "scopeguard", "tao-macros", @@ -4120,7 +4404,7 @@ dependencies = [ "io-uring", "libc", "mio", - "parking_lot", + "parking_lot 0.12.4", "pin-project-lite", "signal-hook-registry", "slab", @@ -4140,6 +4424,16 @@ dependencies = [ "syn 2.0.104", ] +[[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + [[package]] name = "tokio-util" version = "0.7.15" @@ -4502,6 +4796,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version-compare" version = "0.2.0" @@ -4658,6 +4958,21 @@ dependencies = [ "web-sys", ] +[[package]] +name = "wasm-timer" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be0ecb0db480561e9a7642b5d3e4187c128914e58aa84330b9493e3eb68c5e7f" +dependencies = [ + "futures", + "js-sys", + "parking_lot 0.11.2", + "pin-utils", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "web-sys" version = "0.3.77" @@ -4878,6 +5193,17 @@ dependencies = [ "windows-link", ] +[[package]] +name = "windows-registry" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a9ed28765efc97bbc954883f4e6796c33a06546ebafacbabee9696967499e" +dependencies = [ + "windows-link", + "windows-result", + "windows-strings", +] + [[package]] name = "windows-result" version = "0.3.4" @@ -5368,6 +5694,12 @@ dependencies = [ "synstructure", ] +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" + [[package]] name = "zerotrie" version = "0.2.2" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 5fb8847..b293309 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -27,6 +27,10 @@ specta = { version = "=2.0.0-rc.20", features = ["serde_json"] } tauri-specta = { version = "=2.0.0-rc.20", features = ["derive", "typescript"] } specta-typescript = { version = "=0.0.7" } +reqwest = { version = "0.12.22", default-features = false, features = ["default-tls", "system-proxy"] } +reqwest-retry = { version = "0.7.0" } +reqwest-middleware = { version = "0.4.2" } + anyhow = { version = "1.0.98" } parking_lot = { version = "0.12.4", features = ["send_guard"] } tracing = { version = "0.1.41" } diff --git a/src-tauri/src/bili_client.rs b/src-tauri/src/bili_client.rs new file mode 100644 index 0000000..9af539d --- /dev/null +++ b/src-tauri/src/bili_client.rs @@ -0,0 +1,93 @@ +use std::time::Duration; + +use anyhow::{anyhow, Context}; +use parking_lot::RwLock; +use reqwest::StatusCode; +use reqwest_middleware::ClientWithMiddleware; +use reqwest_retry::{policies::ExponentialBackoff, Jitter, RetryTransientMiddleware}; +use serde::{Deserialize, Serialize}; +use tauri::{ + http::{HeaderMap, HeaderValue}, + AppHandle, +}; + +use crate::types::qrcode_data::QrcodeData; + +const USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36"; +const REFERRER: &str = "https://www.bilibili.com/"; + +pub struct BiliClient { + app: AppHandle, + api_client: RwLock, +} + +impl BiliClient { + pub fn new(app: AppHandle) -> Self { + let api_client = create_api_client(&app); + let api_client = RwLock::new(api_client); + + Self { app, api_client } + } + + pub async fn generate_qrcode(&self) -> anyhow::Result { + // 发送生成二维码请求 + let request = self + .api_client + .read() + .get("https://passport.bilibili.com/x/passport-login/web/qrcode/generate"); + let http_resp = request.send().await?; + // 检查http响应状态码 + let status = http_resp.status(); + let body = http_resp.text().await?; + if status != StatusCode::OK { + return Err(anyhow!("预料之外的状态码({status}): {body}")); + } + // 尝试将body解析为BiliResp + let bili_resp: BiliResp = + serde_json::from_str(&body).context(format!("将body解析为BiliResp失败: {body}"))?; + // 检查BiliResp的code字段 + if bili_resp.code != 0 { + return Err(anyhow!("预料之外的code: {bili_resp:?}")); + } + // 检查BiliResp的data是否存在 + let Some(data) = bili_resp.data else { + return Err(anyhow!("BiliResp中不存在data字段: {bili_resp:?}")); + }; + // 尝试将data解析为二维码数据 + let data_str = data.to_string(); + let qrcode_data: QrcodeData = serde_json::from_str(&data_str) + .context(format!("将data解析为QrcodeData失败: {data_str}"))?; + + Ok(qrcode_data) + } +} + +fn create_api_client(_app: &AppHandle) -> ClientWithMiddleware { + let retry_policy = ExponentialBackoff::builder() + .base(1) + .jitter(Jitter::Bounded) + .build_with_total_retry_duration(Duration::from_secs(5)); + + let mut headers = HeaderMap::new(); + headers.insert("user-agent", HeaderValue::from_static(USER_AGENT)); + headers.insert("referer", HeaderValue::from_static(REFERRER)); + + let client = reqwest::ClientBuilder::new() + .timeout(Duration::from_secs(3)) + .default_headers(headers) + .build() + .unwrap(); + + reqwest_middleware::ClientBuilder::new(client) + .with(RetryTransientMiddleware::new_with_policy(retry_policy)) + .build() +} + +#[derive(Default, Debug, Clone, Serialize, Deserialize)] +pub struct BiliResp { + pub code: i64, + #[serde(default, alias = "message")] + pub msg: String, + #[serde(alias = "result")] + pub data: Option, +} diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 88b6978..1fd506b 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -6,6 +6,7 @@ use crate::{ errors::{CommandError, CommandResult}, extensions::AppHandleExt, logger, + types::qrcode_data::QrcodeData, }; #[tauri::command] @@ -55,3 +56,15 @@ pub fn save_config(app: AppHandle, config: Config) -> CommandResult<()> { Ok(()) } + +#[allow(clippy::needless_pass_by_value)] +#[tauri::command(async)] +#[specta::specta] +pub async fn generate_qrcode(app: AppHandle) -> CommandResult { + let bili_client = app.get_bili_client(); + let qrcode_data = bili_client + .generate_qrcode() + .await + .map_err(|err| CommandError::from("生成二维码失败", err))?; + Ok(qrcode_data) +} diff --git a/src-tauri/src/events.rs b/src-tauri/src/events.rs index 3aa35c7..7231d1a 100644 --- a/src-tauri/src/events.rs +++ b/src-tauri/src/events.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use specta::Type; use tauri_specta::Event; -use crate::types::LogLevel; +use crate::types::log_level::LogLevel; #[derive(Debug, Clone, Serialize, Deserialize, Type, Event)] #[serde(rename_all = "camelCase")] diff --git a/src-tauri/src/extensions.rs b/src-tauri/src/extensions.rs index eba86ad..f4b7b6c 100644 --- a/src-tauri/src/extensions.rs +++ b/src-tauri/src/extensions.rs @@ -1,7 +1,7 @@ use parking_lot::RwLock; use tauri::{Manager, State}; -use crate::config::Config; +use crate::{bili_client::BiliClient, config::Config}; pub trait AnyhowErrorToStringChain { /// 将 `anyhow::Error` 转换为chain格式 @@ -26,10 +26,14 @@ impl AnyhowErrorToStringChain for anyhow::Error { pub trait AppHandleExt { fn get_config(&self) -> State>; + fn get_bili_client(&self) -> State; } impl AppHandleExt for tauri::AppHandle { fn get_config(&self) -> State> { self.state::>() } + fn get_bili_client(&self) -> State { + self.state::() + } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index d6479e8..6aa10e1 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,3 +1,4 @@ +mod bili_client; mod commands; mod config; mod errors; @@ -13,7 +14,7 @@ use config::Config; use parking_lot::RwLock; use tauri::{Manager, Wry}; -use crate::events::LogEvent; +use crate::{bili_client::BiliClient, events::LogEvent}; fn generate_context() -> tauri::Context { tauri::generate_context!() @@ -26,6 +27,7 @@ pub fn run() { greet, get_config, save_config, + generate_qrcode, ]) .events(tauri_specta::collect_events![LogEvent]); @@ -51,12 +53,17 @@ pub fn run() { .app_data_dir() .context("获取app_data_dir目录失败")?; - std::fs::create_dir_all(&app_data_dir) - .context(format!("创建app_data_dir目录`{app_data_dir:?}`失败"))?; + std::fs::create_dir_all(&app_data_dir).context(format!( + "创建app_data_dir目录`{:?}`失败", + app_data_dir.display() + ))?; let config = RwLock::new(Config::new(app.handle())?); app.manage(config); + let bili_client = BiliClient::new(app.handle().clone()); + app.manage(bili_client); + logger::init(app.handle())?; Ok(()) diff --git a/src-tauri/src/types/mod.rs b/src-tauri/src/types/mod.rs index 402dadc..e96479a 100644 --- a/src-tauri/src/types/mod.rs +++ b/src-tauri/src/types/mod.rs @@ -1,3 +1,2 @@ -mod log_level; - -pub use log_level::*; +pub mod log_level; +pub mod qrcode_data; diff --git a/src-tauri/src/types/qrcode_data.rs b/src-tauri/src/types/qrcode_data.rs new file mode 100644 index 0000000..08d002a --- /dev/null +++ b/src-tauri/src/types/qrcode_data.rs @@ -0,0 +1,8 @@ +use serde::{Deserialize, Serialize}; +use specta::Type; + +#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize, Type)] +pub struct QrcodeData { + pub url: String, + pub qrcode_key: String, +} diff --git a/src/App.vue b/src/App.vue index ce7037e..8d5ee12 100644 --- a/src/App.vue +++ b/src/App.vue @@ -11,7 +11,7 @@ async function greet() { } async function test() { - const result = await commands.getConfig() + const result = await commands.generateQrcode() console.log(result) } diff --git a/src/bindings.ts b/src/bindings.ts index 6d4d1ae..bf377cf 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -18,6 +18,14 @@ async saveConfig(config: Config) : Promise> { if(e instanceof Error) throw e; else return { status: "error", error: e as any }; } +}, +async generateQrcode() : Promise> { + try { + return { status: "ok", data: await TAURI_INVOKE("generate_qrcode") }; +} catch (e) { + if(e instanceof Error) throw e; + else return { status: "error", error: e as any }; +} } } @@ -41,6 +49,7 @@ export type Config = { downloadDir: string; enableFileLogger: boolean } export type JsonValue = null | boolean | number | string | JsonValue[] | { [key in string]: JsonValue } export type LogEvent = { timestamp: string; level: LogLevel; fields: { [key in string]: JsonValue }; target: string; filename: string; line_number: number } export type LogLevel = "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR" +export type QrcodeData = { url: string; qrcode_key: string } /** tauri-specta globals **/