From ce6c2e6cc22251a4de48fefd1c89fa9243976fd9 Mon Sep 17 00:00:00 2001 From: matresnan <1358168412@qq.com> Date: Mon, 18 Aug 2025 15:57:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BB=A4=E7=89=8C?= =?UTF-8?q?=E8=BF=87=E6=9C=9F=E6=97=B6=E9=97=B4=E9=AA=8C=E8=AF=81=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=9F=A5=E8=AF=A2=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在数据库查询逻辑中添加令牌过期时间验证功能,通过比较当前时间与存储的过期时间来判断令牌有效性,并优化查询结果处理方式。 - 交换令牌创建中的过期时间和插入时间字段,并在令牌验证结果中添加状态消息。 --- src/db.rs | 19 ++++++++++++++++--- src/main.rs | 21 +++++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/db.rs b/src/db.rs index 3739dd4..08d2c77 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,3 +1,4 @@ +use chrono::{Local, NaiveDateTime}; use serde::Serialize; use std::{fs, path::Path}; use welds::{connections::sqlite::SqliteClient, prelude::*}; @@ -87,11 +88,23 @@ impl Db { &self, token: &str, ) -> Result> { - let rows = Authorize::where_col(|p| p.token.equal(token)) + let row = Authorize::where_col(|p| p.token.equal(token)) .limit(1) .run(&self.client) - .await?; - Ok(!rows.is_empty()) + .await? + .into_iter() + .next(); + match row { + Some(a) => { + // 判断截止时间 + let expire_time = + NaiveDateTime::parse_from_str(&a.expire, "%Y-%m-%d %H:%M:%S")?; + let now_time = Local::now().naive_local(); + return Ok(now_time < expire_time); + } + // 如果没有找到,直接返回 false + None => return Ok(false), + }; } /// 查询 token 的详细信息,返回 Option 如果存在 diff --git a/src/main.rs b/src/main.rs index 6d4a176..3223181 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,8 +98,8 @@ async fn create_token( project: args.project.clone(), token: token.clone(), device_id: args.device_id.clone(), - expire: str_time, - insert_time: exp_time, + expire: exp_time, + insert_time: str_time, }) .await; @@ -120,9 +120,21 @@ async fn verify_token( Query(args): Query, ) -> (StatusCode, Json) { if state.db.verify_token(&args.token).await.unwrap() { - (StatusCode::OK, Json(VerifyResult { code: 200 })) + ( + StatusCode::OK, + Json(VerifyResult { + code: 200, + msg: "正常".to_owned(), + }), + ) } else { - (StatusCode::OK, Json(VerifyResult { code: 404 })) + ( + StatusCode::OK, + Json(VerifyResult { + code: 404, + msg: "token已过期".to_owned(), + }), + ) } } @@ -184,6 +196,7 @@ struct VerifyToken { #[derive(Serialize)] struct VerifyResult { code: i16, + msg: String, } #[derive(Serialize)]