diff --git a/README.md b/README.md index c3cb99f..48f28df 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,14 @@ - [x] 测速功能:可以选择不同测速点,贝壳内网,以及其他教育网 ipv6 测速点进行测速(不消耗校园网流量),同时显示当前公网 IP 地址 - [x] 关于页面:一些作者的废话以及我的捐赠打钱二维码,还有一些小 Tips - [x] 查别人流量,和查电表 +- [x] 通过 Onedrive 进行同步配置文件 ## TODO - [x] 通过校园网 VPN 转换校园网内网相关链接,在校外也可以使用这个 APP - [x] 迁移框架版本至 Tauri 2 - [x] 更新提醒 -- [ ] 前端页面美化 +- [x] 前端页面美化 - [ ] 前端加入图表展示,更加直观 - [ ] 密码输入错误超过 3 次之后显示验证码(懒得搞,输错3次密码自己等半小时。) - [ ] 给应用签名(苹果要钱,不签了) diff --git a/src-tauri/src/onedrive.rs b/src-tauri/src/onedrive.rs index 48be8b2..6bd7012 100644 --- a/src-tauri/src/onedrive.rs +++ b/src-tauri/src/onedrive.rs @@ -6,7 +6,7 @@ use sha2::{Digest, Sha256}; use tauri::{Manager, WebviewWindow}; use tauri_plugin_dialog::DialogExt; -use crate::entities::AppState; +use crate::{entities::AppState, setting::Setting}; #[tauri::command(async)] pub async fn open_microsoft_login(app_handle: tauri::AppHandle) -> Result<(), String> { @@ -110,7 +110,7 @@ pub async fn code_to_access_token( match ans { true => upload_setting_to_onedrive(&app_handle, token_response).await, - false => download_setting_to_onedrive().await, + false => download_setting_to_onedrive(&app_handle, token_response).await, } let _ = window.close(); @@ -145,7 +145,51 @@ async fn upload_setting_to_onedrive(app_handle: &tauri::AppHandle, token_respons } } -async fn download_setting_to_onedrive() {} +async fn download_setting_to_onedrive( + app_handle: &tauri::AppHandle, + token_response: TokenResponse, +) { + let response = Client::new() + .get("https://graph.microsoft.com/v1.0/drive/special/approot:/setting.txt:/content") + .bearer_auth(token_response.access_token.unwrap()) + .send() + .await; + if let Err(e) = response { + app_handle + .dialog() + .message("下载失败!可能由于网络问题") + .blocking_show(); + println!("Error downloading setting: {}", e); + } else { + let text = response.unwrap().text().await.unwrap(); + let text = match URL_SAFE.decode(text.as_bytes()) { + Ok(text) => text, + Err(e) => { + app_handle + .dialog() + .message(format!("解码base64错误:{e:?}")) + .blocking_show(); + return; + } + }; + let setting: Setting = match serde_json::from_slice(&text) { + Ok(s) => s, + Err(e) => { + app_handle + .dialog() + .message(format!("配置文件格式不正确!{e:?}")) + .blocking_show(); + return; + } + }; + // dbg!(&setting); + let state = app_handle.state::(); + *state.setting.write().unwrap() = setting; + let _ = state.setting.write().unwrap().write_setting(app_handle); + app_handle.dialog().message("下载成功!").blocking_show(); + // dbg!(response.unwrap().text().await); + } +} fn generate_random_string(length: usize) -> String { const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";