refactor: login-logout action
This commit is contained in:
@@ -18,17 +18,18 @@ use sdgb_api::{
|
||||
methods::APIMethod,
|
||||
model::{
|
||||
GetUserDataApi, GetUserDataApiResp, GetUserPreviewApi, GetUserPreviewApiResp, Ping,
|
||||
PingResp, UserLoginApi, UserLoginApiResp, UserLogoutApi, UserLogoutApiResp,
|
||||
PingResp, UserLogoutApi, UserLogoutApiResp,
|
||||
},
|
||||
},
|
||||
};
|
||||
use spdlog::{error, info, warn};
|
||||
|
||||
use crate::commands::Cli;
|
||||
use crate::{commands::Cli, utils::login_action};
|
||||
|
||||
#[cfg(feature = "cache")]
|
||||
mod cache;
|
||||
mod commands;
|
||||
mod utils;
|
||||
|
||||
static EARLY_QUIT: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
@@ -202,48 +203,7 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
|
||||
serde_json::to_writer_pretty(output, &players)?;
|
||||
}
|
||||
commands::Commands::Userdata { user_id } => {
|
||||
let login = UserLoginApi::new(user_id);
|
||||
let date_time = login.date_time;
|
||||
let Ok(login_resp): Result<UserLoginApiResp, _> =
|
||||
Sdgb1_50::request(&client, APIMethod::UserLoginApi, user_id, login).await
|
||||
else {
|
||||
let logout_resp: UserLogoutApiResp = Sdgb1_50::request(
|
||||
&client,
|
||||
APIMethod::UserLogoutApi,
|
||||
user_id,
|
||||
UserLogoutApi {
|
||||
user_id,
|
||||
date_time,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
info!("logout: {logout_resp:?}");
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
match login_resp.return_code {
|
||||
1 => info!("login succeed"),
|
||||
100 => {
|
||||
error!("user already logged");
|
||||
return Ok(());
|
||||
}
|
||||
102 => {
|
||||
error!("QRCode expired");
|
||||
return Ok(());
|
||||
}
|
||||
103 => {
|
||||
error!("Unregistered userId");
|
||||
return Ok(());
|
||||
}
|
||||
e @ _ => {
|
||||
error!("unknown login error: {e}");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
match Sdgb1_50::request::<_, GetUserDataApiResp>(
|
||||
let action = async |_| match Sdgb1_50::request::<_, GetUserDataApiResp>(
|
||||
&client,
|
||||
APIMethod::GetUserDataApi,
|
||||
user_id,
|
||||
@@ -257,21 +217,8 @@ async fn main() -> Result<(), Box<dyn snafu::Error>> {
|
||||
Err(e) => {
|
||||
error!("failed to get userdata: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
let logout_resp: UserLogoutApiResp = Sdgb1_50::request(
|
||||
&client,
|
||||
APIMethod::UserLogoutApi,
|
||||
user_id,
|
||||
UserLogoutApi {
|
||||
user_id,
|
||||
date_time,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
info!("logout: {logout_resp:?}");
|
||||
};
|
||||
login_action(&client, user_id, action).await?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user