chore: get token after qr login

This commit is contained in:
2026-01-08 00:56:31 +08:00
parent b15088c332
commit 0ccc425a19
3 changed files with 19 additions and 8 deletions

View File

@@ -1,10 +1,16 @@
use std::backtrace::Backtrace;
use nyquest::{AsyncClient, Body, Request, header::USER_AGENT};
use nyquest::{
AsyncClient, Body, Request,
header::{SET_COOKIE, USER_AGENT},
};
mod model;
use model::{GetResponse, GetUserId};
use model::GetUserId;
use serde::Serialize;
use spdlog::debug;
pub use model::GetResponse;
pub struct QRCode<'a> {
pub qrcode_content: &'a str,
@@ -40,7 +46,7 @@ pub enum QRLoginError {
}
impl QRCode<'_> {
pub async fn login(self, client: &AsyncClient) -> Result<i64, QRLoginError> {
pub async fn login(self, client: &AsyncClient) -> Result<GetResponse, QRLoginError> {
let qr_code = &self.qrcode_content.as_bytes()[self.qrcode_content.len() - 64..];
let qr_code = String::from_utf8_lossy(qr_code);
@@ -49,12 +55,14 @@ impl QRCode<'_> {
.with_header(USER_AGENT, "WC_AIME_LIB");
let resp = client.request(req).await?;
let cookie = resp.get_header(SET_COOKIE)?;
let resp: GetResponse = resp.json().await?;
let user_id = resp.user_id;
debug!("Set-Cookie: {cookie:?}");
match resp.error_id {
0 => return Ok(user_id),
0 => return Ok(resp),
2 => Err(QRLoginError::QRCodeExpired10),
1 => Err(QRLoginError::QRCodeExpired30),
50 => Err(QRLoginError::BadSingature),

View File

@@ -15,7 +15,7 @@ pub struct GetUserId {
pub timestamp: String,
}
#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
#[derive(Default, Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetResponse {
pub key: String,
@@ -24,6 +24,7 @@ pub struct GetResponse {
pub error_id: i64,
#[serde(rename = "userID")]
pub user_id: i64,
pub token: String,
}
impl GetUserId {