移除冗余数据返回转换
This commit is contained in:
parent
76ae54cd9b
commit
185c9eaea1
@ -1,4 +1,3 @@
|
||||
pub mod response;
|
||||
pub mod status;
|
||||
pub mod pageable;
|
||||
pub mod validator;
|
@ -1,4 +1,4 @@
|
||||
use std::{error::Error as StdError, fmt::Display};
|
||||
use std::{error::Error as StdError, fmt::Display, sync::Arc};
|
||||
|
||||
use axum::{
|
||||
response::{IntoResponse, Response},
|
||||
@ -7,7 +7,17 @@ use axum::{
|
||||
use http::StatusCode;
|
||||
use serde::Serialize;
|
||||
|
||||
use super::status::Status;
|
||||
#[derive(Serialize)]
|
||||
pub struct Reply<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
pub code: i32,
|
||||
pub err: bool,
|
||||
pub msg: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub data: Option<T>,
|
||||
}
|
||||
|
||||
pub struct ResData<T>(pub Option<T>)
|
||||
where
|
||||
@ -19,9 +29,14 @@ where
|
||||
{
|
||||
fn into_response(self) -> Response {
|
||||
let ResData(data) = self;
|
||||
let status = Status::OK(data);
|
||||
let reply = Reply {
|
||||
code: 0,
|
||||
err: false,
|
||||
msg: String::from("OK"),
|
||||
data,
|
||||
};
|
||||
|
||||
Json(status.to_reply()).into_response()
|
||||
Json(reply).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,71 +67,77 @@ use ResErr::*;
|
||||
impl IntoResponse for ResErr {
|
||||
fn into_response(self) -> Response {
|
||||
let status = match self {
|
||||
Error(code, msg) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, msg),
|
||||
Error(code, msg) => (StatusCode::INTERNAL_SERVER_ERROR, code, msg),
|
||||
ErrParams(msg) => {
|
||||
let code = 10000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("参数错误")),
|
||||
Some(v) => (StatusCode::BAD_REQUEST, code, v),
|
||||
None => (StatusCode::BAD_REQUEST, code, String::from("参数错误")),
|
||||
}
|
||||
}
|
||||
ErrAuth(msg) => {
|
||||
let code = 20000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(StatusCode::UNAUTHORIZED, code, v),
|
||||
None => Status::<()>::Err(StatusCode::UNAUTHORIZED, code, String::from("未授权,请先登录")),
|
||||
Some(v) => (StatusCode::UNAUTHORIZED, code, v),
|
||||
None => (StatusCode::UNAUTHORIZED, code, String::from("未授权,请先登录")),
|
||||
}
|
||||
}
|
||||
ErrPerm(msg) => {
|
||||
let code = 30000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(StatusCode::FORBIDDEN, code, v),
|
||||
None => Status::<()>::Err(StatusCode::FORBIDDEN, code, String::from("权限不足")),
|
||||
Some(v) => (StatusCode::FORBIDDEN, code, v),
|
||||
None => (StatusCode::FORBIDDEN, code, String::from("权限不足")),
|
||||
}
|
||||
}
|
||||
ErrNotFound(msg) => {
|
||||
let code = 40000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(StatusCode::NOT_FOUND, code, v),
|
||||
None => Status::<()>::Err(StatusCode::NOT_FOUND, code, String::from("数据不存在")),
|
||||
Some(v) => (StatusCode::NOT_FOUND, code, v),
|
||||
None => (StatusCode::NOT_FOUND, code, String::from("数据不存在")),
|
||||
}
|
||||
}
|
||||
ErrService(msg) => {
|
||||
let code = 50000;
|
||||
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("内部服务器错误,请稍后重试")),
|
||||
Some(v) => (StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => (StatusCode::INTERNAL_SERVER_ERROR, code, String::from("内部服务器错误,请稍后重试")),
|
||||
}
|
||||
}
|
||||
ErrSocial(msg) => {
|
||||
let code = 80000;
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("社交业务异常")),
|
||||
Some(v) => (StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => (StatusCode::INTERNAL_SERVER_ERROR, code, String::from("社交业务异常")),
|
||||
}
|
||||
}
|
||||
ErrAccountDisable(msg) => {
|
||||
let code = 90000;
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(StatusCode::FORBIDDEN, code, v),
|
||||
None => Status::<()>::Err(StatusCode::FORBIDDEN, code, String::from("账户已禁用")),
|
||||
Some(v) => (StatusCode::FORBIDDEN, code, v),
|
||||
None => (StatusCode::FORBIDDEN, code, String::from("账户已禁用")),
|
||||
}
|
||||
}
|
||||
ErrSqlx(msg) => {
|
||||
let code = 90000;
|
||||
match msg {
|
||||
Some(v) => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => Status::<()>::Err(StatusCode::INTERNAL_SERVER_ERROR, code, String::from("业务错误")),
|
||||
Some(v) => (StatusCode::INTERNAL_SERVER_ERROR, code, v),
|
||||
None => (StatusCode::INTERNAL_SERVER_ERROR, code, String::from("业务错误")),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Json(status.to_reply()).into_response()
|
||||
let reply: Reply<()> = Reply {
|
||||
code: status.1,
|
||||
err: false,
|
||||
msg: status.2,
|
||||
data: None,
|
||||
};
|
||||
(status.0, Json(reply)).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,53 +0,0 @@
|
||||
use http::StatusCode;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Reply<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
#[serde(skip_serializing)]
|
||||
pub status: StatusCode,
|
||||
pub code: i32,
|
||||
pub err: bool,
|
||||
pub msg: String,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub data: Option<T>,
|
||||
}
|
||||
|
||||
pub enum Status<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
OK(Option<T>),
|
||||
Err(StatusCode, i32, String),
|
||||
}
|
||||
|
||||
impl<T> Status<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
pub fn to_reply(self) -> Reply<T> {
|
||||
let mut resp = Reply {
|
||||
status: StatusCode::OK,
|
||||
code: 0,
|
||||
err: false,
|
||||
msg: String::from("OK"),
|
||||
data: None,
|
||||
};
|
||||
|
||||
match self {
|
||||
Status::OK(data) => {
|
||||
resp.data = data;
|
||||
}
|
||||
Status::Err(status, code, msg) => {
|
||||
resp.status = status;
|
||||
resp.code = code;
|
||||
resp.err = true;
|
||||
resp.msg = msg;
|
||||
}
|
||||
}
|
||||
|
||||
resp
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user