diff --git a/api/src/controller/account.rs b/api/src/controller/account.rs index 613efd5..dda8455 100644 --- a/api/src/controller/account.rs +++ b/api/src/controller/account.rs @@ -1,19 +1,18 @@ use axum::{Extension, Json}; use domain::{dto::account::{AuthenticateGooleAccountReq, AuthenticateWithPassword, RefreshToken}, vo::account::{LoginAccount, RefreshTokenResult}}; -use library::{cache::account_cache::CacheAccount, resp::response::{ ResData, ResResult}}; -use validator::Validate; +use library::{cache::account_cache::CacheAccount, res::{response::{ ResData, ResResult}, validator}}; pub async fn authenticate_google( Json(req): Json ) -> ResResult> { - req.validate()?; + validator::validate_params(&req, "")?; service::account::authenticate_google(req).await } pub async fn authenticate_with_password( Json(req): Json ) -> ResResult> { - req.validate()?; + validator::validate_params(&req, "")?; service::sys_account::authenticate_with_password(req).await } @@ -22,6 +21,6 @@ pub async fn refresh_token( Json(refresh_token): Json ) -> ResResult> { tracing::debug!("刷新token, {:?}", account); - refresh_token.validate()?; + validator::validate_params(&refresh_token, "")?; service::account::refresh_token(account, refresh_token.token).await } diff --git a/api/src/controller/feedback.rs b/api/src/controller/feedback.rs index d5ada84..ba8b69e 100644 --- a/api/src/controller/feedback.rs +++ b/api/src/controller/feedback.rs @@ -4,8 +4,8 @@ use validator::Validate; use domain::dto::feedback::FeedbackAdd; use domain::dto::pageable::PageParams; use domain::entities::feedback::Feedback; -use library::resp::pageable::Pageable; -use library::resp::response::{ResData, ResResult}; +use library::res::pageable::Pageable; +use library::res::response::{ResData, ResResult}; /// 添加反馈信息 pub async fn add_feedback( diff --git a/library/src/lib.rs b/library/src/lib.rs index 6e954fd..c1bed03 100644 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -1,7 +1,7 @@ extern crate self as library; pub mod core; -pub mod resp; +pub mod res; pub mod middleware; pub mod token; pub mod social; diff --git a/library/src/middleware/req_log.rs b/library/src/middleware/req_log.rs index 72ea8d3..f29e2f1 100644 --- a/library/src/middleware/req_log.rs +++ b/library/src/middleware/req_log.rs @@ -1,4 +1,4 @@ -use crate::resp::response::ResErr; +use crate::res::response::ResErr; use axum::body::Body; use axum::extract::Request; use axum::http::header::CONTENT_TYPE; diff --git a/library/src/res/mod.rs b/library/src/res/mod.rs new file mode 100644 index 0000000..7d49cb5 --- /dev/null +++ b/library/src/res/mod.rs @@ -0,0 +1,4 @@ +pub mod response; +pub mod status; +pub mod pageable; +pub mod validator; \ No newline at end of file diff --git a/library/src/resp/pageable.rs b/library/src/res/pageable.rs similarity index 100% rename from library/src/resp/pageable.rs rename to library/src/res/pageable.rs diff --git a/library/src/resp/response.rs b/library/src/res/response.rs similarity index 87% rename from library/src/resp/response.rs rename to library/src/res/response.rs index c59f066..a3b6d23 100644 --- a/library/src/resp/response.rs +++ b/library/src/res/response.rs @@ -24,7 +24,7 @@ where } } -impl ResData { +impl ResData { pub fn none() -> Self { ResData(None) } @@ -45,7 +45,7 @@ pub enum ResErr { ErrService(Option), ErrSocial(Option), ErrAccountDisable(Option), - ErrSqlx(Option) + ErrSqlx(Option), } use ResErr::*; @@ -189,42 +189,68 @@ impl Default for ResErr { } impl ResErr { - pub fn error(code: i32, msg: T) -> Self where T: Into { + pub fn error(code: i32, msg: T) -> Self + where + T: Into, + { Error(code, msg.into()) } - pub fn params(msg: T) -> Self where T: Into { + pub fn params(msg: T) -> Self + where + T: Into, + { ErrParams(Some(msg.into())) } - pub fn auth(msg: T) -> Self where T: Into { + pub fn auth(msg: T) -> Self + where + T: Into, + { ErrAuth(Some(msg.into())) } - pub fn perm(msg: T) -> Self where T: Into { + pub fn perm(msg: T) -> Self + where + T: Into, + { ErrPerm(Some(msg.into())) } - pub fn not_found(msg: T) -> Self where T: Into { + pub fn not_found(msg: T) -> Self + where + T: Into, + { ErrNotFound(Some(msg.into())) } - pub fn system(msg: T) -> Self where T: Into { + pub fn system(msg: T) -> Self + where + T: Into, + { ErrSystem(Some(msg.into())) } - pub fn data(msg: T) -> Self where T: Into { + pub fn data(msg: T) -> Self + where + T: Into, + { ErrData(Some(msg.into())) } - pub fn service(msg: T) -> Self where T: Into { + pub fn service(msg: T) -> Self + where + T: Into, + { ErrService(Some(msg.into())) } - pub fn social(msg: T) -> Self where T: Into { + pub fn social(msg: T) -> Self + where + T: Into, + { ErrSocial(Some(msg.into())) } } - pub type ResResult = std::result::Result; diff --git a/library/src/resp/status.rs b/library/src/res/status.rs similarity index 100% rename from library/src/resp/status.rs rename to library/src/res/status.rs diff --git a/library/src/res/validator.rs b/library/src/res/validator.rs new file mode 100644 index 0000000..c05d492 --- /dev/null +++ b/library/src/res/validator.rs @@ -0,0 +1,44 @@ +use std::borrow::Cow; + +use validator::Validate; + +use super::response::{ResData, ResErr, ResResult}; + +pub fn validate_params(params: &impl Validate, _local: &str) -> ResResult> { + params.validate().map_err(|e| { + let mut errors = vec![]; + for (_, err) in e.errors().iter() { + match err { + validator::ValidationErrorsKind::Struct(err) => { + err.field_errors().iter().for_each(|(_field, errs)| { + errs.iter().for_each(|e| { + if let Some(Cow::Owned(msg)) = e.message.clone() { + errors.push(msg); + } + }); + }); + }, + validator::ValidationErrorsKind::List(err) => { + for (_, err) in err.iter() { + err.field_errors().iter().for_each(|(_field, errs)| { + errs.iter().for_each(|e| { + if let Some(Cow::Owned(msg)) = e.message.clone() { + errors.push(msg); + } + }); + }); + } + }, + validator::ValidationErrorsKind::Field(err) => { + err.iter().for_each(|e| { + if let Some(Cow::Owned(msg)) = e.message.clone() { + errors.push(msg); + } + }); + }, + }; + } + ResErr::params(errors.join(";")) + })?; + Result::Ok(ResData::none()) +} \ No newline at end of file diff --git a/library/src/resp/mod.rs b/library/src/resp/mod.rs deleted file mode 100644 index 7c6929d..0000000 --- a/library/src/resp/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod response; -pub mod status; -pub mod pageable; \ No newline at end of file diff --git a/library/src/social/apple.rs b/library/src/social/apple.rs index 46f5478..eaec8d3 100644 --- a/library/src/social/apple.rs +++ b/library/src/social/apple.rs @@ -8,7 +8,7 @@ use reqwest::Client; use serde::Deserialize; use serde_json::Value; -use crate::resp::response::ResErr; +use crate::res::response::ResErr; use super::SocialResult; diff --git a/library/src/social/google.rs b/library/src/social/google.rs index 29b184f..eb80048 100644 --- a/library/src/social/google.rs +++ b/library/src/social/google.rs @@ -8,7 +8,7 @@ use reqwest::Client; use serde::Deserialize; use serde_json::Value; -use crate::resp::response::ResErr; +use crate::res::response::ResErr; use super::SocialResult; diff --git a/service/src/account.rs b/service/src/account.rs index 6ad658c..b07751b 100644 --- a/service/src/account.rs +++ b/service/src/account.rs @@ -3,8 +3,8 @@ use domain::dto::account::AuthenticateGooleAccountReq; use domain::entities::account::Account; use domain::vo::account::{LoginAccount, RefreshTokenResult}; use library::cache::account_cache::{CacheAccount, LOGIN_CACHE}; -use library::resp::response::ResErr::ErrPerm; -use library::resp::response::{ResData, ResErr, ResResult}; +use library::res::response::ResErr::ErrPerm; +use library::res::response::{ResData, ResErr, ResResult}; use library::social::google::GOOGLE_SOCIAL; use library::token::{generate_refresh_token, generate_token}; use library::{db, token}; diff --git a/service/src/feedback.rs b/service/src/feedback.rs index 26243b9..706f68b 100644 --- a/service/src/feedback.rs +++ b/service/src/feedback.rs @@ -1,8 +1,8 @@ use domain::dto::feedback::FeedbackAdd; use domain::entities::feedback::Feedback; use library::db; -use library::resp::pageable::Pageable; -use library::resp::response::{ResData, ResResult}; +use library::res::pageable::Pageable; +use library::res::response::{ResData, ResResult}; /// 获取反馈信息列表 pub async fn get_feedback_list_by_page(page: i64, page_size: i64) -> ResResult>> { @@ -37,7 +37,7 @@ pub async fn add_feedback(req: FeedbackAdd) -> ResResult> { } Err(err) => { tracing::error!(error = ?err, "添加反馈信息失败"); - return Err(library::resp::response::ResErr::ErrSystem(None)); + return Err(library::res::response::ResErr::ErrSystem(None)); } } diff --git a/service/src/sys_account.rs b/service/src/sys_account.rs index 16f9e47..52cc54e 100644 --- a/service/src/sys_account.rs +++ b/service/src/sys_account.rs @@ -4,7 +4,7 @@ use domain::{ entities::account::{Account, Role}, vo::account::LoginAccount, }; use library::{ - cache::account_cache::{CacheAccount, LOGIN_CACHE}, db, resp::response::{ResErr, ResData, ResResult}, token::{generate_refresh_token, generate_token} + cache::account_cache::{CacheAccount, LOGIN_CACHE}, db, res::response::{ResErr, ResData, ResResult}, token::{generate_refresh_token, generate_token} };