From 4db1f73a271d0772be7b0d7deba172385d6e8da4 Mon Sep 17 00:00:00 2001 From: matresnan <1358168412@qq.com> Date: Thu, 21 Aug 2025 20:38:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E6=9F=A5=E8=AF=A2=E5=B9=B6=E6=96=B0=E5=A2=9E=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E4=BB=A4=E7=89=8C=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=8F=8A=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化数据库查询逻辑,修复可变引用问题并新增项目令牌查询功能。 - 添加一个新的路由 `/all` 和对应的处理函数 `get_project_token`,用于查询项目令牌信息,并定义相关的请求参数和响应结构体。 --- src/db.rs | 24 ++++++++++++++++-------- src/main.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/db.rs b/src/db.rs index 83ae2fa..4f5b5cf 100644 --- a/src/db.rs +++ b/src/db.rs @@ -19,7 +19,7 @@ pub struct Authorize { #[derive(Debug, WeldsModel)] pub struct Projects { - pub project: String, + pub project: String, } /// 包装类,内部持有 SQLite 连接 @@ -152,14 +152,14 @@ impl Db { token: &str, state: i8, ) -> Result> { - let rows = Authorize::all() + let mut rows = Authorize::all() .where_col(|a| a.token.equal(token)) .run(&self.client) .await?; if rows.is_empty() { return Ok(false); } - for mut row in rows { + for row in &mut rows { row.disable = state; row.save(&self.client).await?; } @@ -202,10 +202,18 @@ impl Db { // 用Projects结构体来收集查询到的结果 let result: Vec = rows.collect_into()?; - let mut projects:Vec = Vec::new(); - for row in result { - projects.push(row.project); - } - Ok(projects) + Ok(result.into_iter().map(|r| r.project).collect()) + } + + pub async fn query_project_token( + &self, + project: &str, + ) -> Result, Box> { + let rows = Authorize::all() + .where_col(|a| a.project.equal(project)) + .run(&self.client) + .await?; + + Ok(rows.into_iter().map(|r| r.into_inner()).collect()) } } diff --git a/src/main.rs b/src/main.rs index 9d1d406..0722e03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,6 +36,7 @@ async fn main() -> Result<(), Box> { .route("/reset", get(update_token_status)) .route("/renewal", get(renewal_token)) .route("/project", get(get_projects)) + .route("/all", get(get_project_token)) .with_state(state); let listener = tokio::net::TcpListener::bind("0.0.0.0:3009").await?; @@ -230,6 +231,21 @@ async fn get_projects( ) } +async fn get_project_token( + State(state): State, + Query(args): Query, +) -> (StatusCode, Json) { + let items = state.db.query_project_token(&args.project).await.unwrap(); + + ( + StatusCode::OK, + Json(ProjectToken { + code: 200, + data: items, + }), + ) +} + fn get_current_datetime() -> String { Local::now().format("%Y-%m-%d %H:%M:%S").to_string() } @@ -284,6 +300,11 @@ struct RenewalToken { days: i64, } +#[derive(Deserialize)] +struct QueryProject { + project: String, +} + #[derive(Serialize)] struct VerifyResult { code: i16, @@ -334,3 +355,9 @@ struct ProjectResponse { code: i32, projects: Vec, } + +#[derive(Serialize)] +struct ProjectToken { + code: i32, + data: Vec, +}