diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index acc560c..9aa805f 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -3,26 +3,29 @@ use tauri::{utils::config::WindowConfig, Manager}; use crate::{ entities::{AppState, EveryLoginData}, - requests::{ - get_address, get_load_user_flow, get_mac_address, get_month_pay, get_refresh_account, - get_user_login_log, simulate_login, simulate_login_via_vpn, unbind_macs, - }, + requests::*, setting::Setting, }; -// 没地方放它了 +#[tauri::command(async)] pub async fn load_user_flow( account: String, - session_id: &str, - via_vpn: bool, + app_state: tauri::State<'_, AppState>, ) -> Result { - get_load_user_flow(&account, session_id, via_vpn) + let via_vpn = *app_state.login_via_vpn.read().unwrap(); + let mut session_id = String::new(); + if via_vpn { + session_id = match app_state.jsessionid.read().unwrap().clone() { + Some(s) => s, + None => return Err("SessionID为空,是否已经登录并单击获取Cookie按钮?".to_string()), + }; + } + get_load_user_flow(&account, &session_id, via_vpn) .await .map_err(|e| format!("Error while loading user flow: {}", e)) .map(|res| res.to_string()) } -// 对 headless browser 进行操作,获取登陆后的 Cookie #[tauri::command(async)] pub async fn get_cookie( app_state: tauri::State<'_, AppState>, @@ -86,7 +89,6 @@ pub async fn get_cookie_vpn( #[tauri::command(async)] pub fn logout( - app: tauri::AppHandle, app_state: tauri::State<'_, AppState>, window: tauri::Webview, ) -> Result { @@ -95,14 +97,6 @@ pub fn logout( } *app_state.jsessionid.write().unwrap() = None; *app_state.login_via_vpn.write().unwrap() = false; // 这之前有个bug一直没人发现,说明没人用我的 app 😭 - Setting::write_setting( - &Setting { - browser_path: app_state.setting.read().unwrap().browser_path.to_owned(), - ..Default::default() - }, - &app, - ) - .map_err(|err| format!("写入配置错误: {}", err))?; window .eval("window.location.reload();") .map_err(|err| format!("刷新网页错误:{}", err))?; @@ -375,10 +369,38 @@ pub async fn manually_check_update(app: tauri::AppHandle) -> Result<(), String> crate::update(app, true) .await .map_err(|err| err.to_string())?; - + if cfg!(target_os = "android") { Err("安卓暂时不支持更新,请到 GitHub 查看是否有更新。".into()) } else { Ok(()) } } + +#[tauri::command(async)] +pub async fn load_ammeter( + app: tauri::AppHandle, + app_state: tauri::State<'_, AppState>, + ammeter_number: u32, +) -> Result { + let kwh = get_ammeter(ammeter_number) + .await + .map_err(|err| err.to_string())?; + match kwh { + Some(kwh) => { + app_state + .setting + .write() + .unwrap() + .set_ammeter_number(ammeter_number); + app_state + .setting + .read() + .unwrap() + .write_setting(&app) + .map_err(|err| err.to_string())?; + Ok(format!("{}", kwh)) + } + None => Err("获取用电量失败,可能是电表号错误".to_string()), + } +} diff --git a/src-tauri/src/entities.rs b/src-tauri/src/entities.rs index 650681c..9acb5e5 100644 --- a/src-tauri/src/entities.rs +++ b/src-tauri/src/entities.rs @@ -83,3 +83,13 @@ pub struct AppState { pub setting: RwLock, pub login_via_vpn: RwLock, } + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct AmmeterData { + #[serde(rename = "ServiceKey")] + pub service_key: String, + #[serde(rename = "message")] + pub message: String, + #[serde(rename = "statusCode")] + pub status_code: String, +} \ No newline at end of file diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 0b61bdf..f4d529a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -44,7 +44,9 @@ pub fn run() { logout, get_cookie_vpn, load_monthly_login_log, - manually_check_update + manually_check_update, + load_ammeter, + load_user_flow ]) .setup(|app| { #[cfg(not(target_os = "android"))] diff --git a/src-tauri/src/requests.rs b/src-tauri/src/requests.rs index 31bff7c..d6525fa 100644 --- a/src-tauri/src/requests.rs +++ b/src-tauri/src/requests.rs @@ -8,7 +8,7 @@ use reqwest::{header::SET_COOKIE, Client}; use scraper::{Html, Selector}; use serde_json::Value; -use crate::entities::{EveryLoginData, MacAddress, MonthPayInfo, MonthlyData, UserLoginLog}; +use crate::entities::{AmmeterData, EveryLoginData, MacAddress, MonthPayInfo, MonthlyData, UserLoginLog}; // Ciallo~(∠・ω< )⌒☆ pub async fn get_load_user_flow(account: &str, session_id: &str, via_vpn: bool) -> Result { @@ -548,6 +548,23 @@ pub async fn get_address() -> Result> { Ok(vec![v4_resp, v6_resp]) } +pub async fn get_ammeter(num: u32) -> Result, Box> { + let response = Client::new() + .post("http://fspapp.ustb.edu.cn/app.GouDian/index.jsp?m=alipay&c=AliPay&a=getDbYe") + .header("Content-Type", "application/x-www-form-urlencoded") + .body(format!("DBNum={}", num)) + .send() + .await?; + let res_text = response.text().await?; + let ammeter_data: AmmeterData = serde_json::from_str(&res_text)?; + let kwh = ammeter_data.service_key.parse::(); + if ammeter_data.status_code != "200".to_string() || kwh.is_err() { + Ok(None) + } else { + Ok(Some(kwh.unwrap())) + } +} + #[cfg(test)] mod tests { // use crate::entities::{GetUserFlowFailed, UserFlow}; diff --git a/src-tauri/src/setting.rs b/src-tauri/src/setting.rs index f7c89a0..ae909bc 100644 --- a/src-tauri/src/setting.rs +++ b/src-tauri/src/setting.rs @@ -12,7 +12,7 @@ use tauri::Manager; pub struct Setting { pub username: Option, pub password: Option, - pub browser_path: Option, + pub ammeter_number: Option, } impl Setting { @@ -48,8 +48,8 @@ impl Setting { self.password = Some(password); } - pub fn set_browser_path(&mut self, path: Option) { - self.browser_path = path; + pub fn set_ammeter_number(&mut self, ammeter_number: u32) { + self.ammeter_number = Some(ammeter_number); } } @@ -66,32 +66,6 @@ fn get_config_path(app: &tauri::AppHandle) -> Result { } None => return Err(anyhow!("There is no such app data dir!")), }; - // dbg!(&path); + dbg!(&path); Ok(path) } - -// #[cfg(test)] -// mod tests { -// use super::*; - -// #[test] -// fn test_write_setting() { -// let mut setting = Setting::default(); -// setting.username = Some("user_name".to_string()); -// setting.password = Some("password".to_string()); -// setting.browser_path = Some("/path/to/browser".to_string()); -// setting.write_setting().unwrap(); -// } - -// #[test] -// fn test_load_setting() { -// let setting = Setting::load_setting().unwrap(); -// println!("{:?}", setting); -// } - -// #[test] -// fn test_get_config_path() { -// let path = get_config_path().unwrap(); -// println!("{:?}", path.to_str()); -// } -// } diff --git a/src/App.vue b/src/App.vue index 6ba0ac4..fc9aaec 100644 --- a/src/App.vue +++ b/src/App.vue @@ -12,6 +12,7 @@ import UserLoginLog from "./pages/UserLoginLog.vue"; import UnbindMacs from "./pages/UnbindMacs.vue"; import SpeedTest from "./pages/SpeedTest.vue"; import MonthlyUserLog from "./pages/MonthlyUserLog.vue"; +import OtherTools from "./pages/OtherTools.vue"; type RouteComponent = DefineComponent<{}, {}, any>; interface Routes { @@ -27,6 +28,7 @@ const routes: Routes = { "/unbindmacs": UnbindMacs, "/speedtest": SpeedTest, "/monthly_user_log": MonthlyUserLog, + "/other_tools": OtherTools, }; // Ref for current path diff --git a/src/components/Menu.vue b/src/components/Menu.vue index 2c4fd63..69e8bff 100644 --- a/src/components/Menu.vue +++ b/src/components/Menu.vue @@ -1,8 +1,12 @@ - + + + + + diff --git a/src/pages/UserInfo.vue b/src/pages/UserInfo.vue index 220ff68..1d0b2e0 100644 --- a/src/pages/UserInfo.vue +++ b/src/pages/UserInfo.vue @@ -26,7 +26,7 @@ interface Data { v6: number; } -interface Flow { +export interface Flow { result: number; data: Data; }