统一请求格式为 device/language/data 结构
This commit is contained in:
@@ -27,6 +27,13 @@ struct RegisterRequest {
|
|||||||
password: String,
|
password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ApiRequest<T> {
|
||||||
|
device: i32,
|
||||||
|
language: i32,
|
||||||
|
data: T,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct ApiResponse<T> {
|
struct ApiResponse<T> {
|
||||||
success: bool,
|
success: bool,
|
||||||
@@ -71,12 +78,15 @@ async fn main() {
|
|||||||
|
|
||||||
async fn register_handler(
|
async fn register_handler(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Json(payload): Json<RegisterRequest>,
|
Json(req): Json<ApiRequest<RegisterRequest>>,
|
||||||
) -> (StatusCode, Json<ApiResponse<RegisterData>>) {
|
) -> (StatusCode, Json<ApiResponse<RegisterData>>) {
|
||||||
info!("Registration attempt for user: {}", payload.username);
|
info!(
|
||||||
|
"Registration attempt for user: {}, device: {}, language: {}",
|
||||||
|
req.data.username, req.device, req.language
|
||||||
|
);
|
||||||
|
|
||||||
// 参数校验
|
// 参数校验
|
||||||
if let Err(e) = payload.validate() {
|
if let Err(e) = req.data.validate() {
|
||||||
return (
|
return (
|
||||||
StatusCode::BAD_REQUEST,
|
StatusCode::BAD_REQUEST,
|
||||||
Json(ApiResponse {
|
Json(ApiResponse {
|
||||||
@@ -91,7 +101,7 @@ async fn register_handler(
|
|||||||
let existing: Option<(Uuid,)> = sqlx::query_as(
|
let existing: Option<(Uuid,)> = sqlx::query_as(
|
||||||
"SELECT id FROM user_login_account WHERE account = $1 AND deleted = FALSE"
|
"SELECT id FROM user_login_account WHERE account = $1 AND deleted = FALSE"
|
||||||
)
|
)
|
||||||
.bind(&payload.username)
|
.bind(&req.data.username)
|
||||||
.fetch_optional(&state.db)
|
.fetch_optional(&state.db)
|
||||||
.await
|
.await
|
||||||
.unwrap_or(None);
|
.unwrap_or(None);
|
||||||
@@ -108,7 +118,7 @@ async fn register_handler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 密码哈希
|
// 密码哈希
|
||||||
let password_hash = match hash(&payload.password, bcrypt::DEFAULT_COST) {
|
let password_hash = match hash(&req.data.password, bcrypt::DEFAULT_COST) {
|
||||||
Ok(h) => h,
|
Ok(h) => h,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("Password hashing failed: {}", e);
|
warn!("Password hashing failed: {}", e);
|
||||||
@@ -166,7 +176,7 @@ async fn register_handler(
|
|||||||
)
|
)
|
||||||
.bind(account_id)
|
.bind(account_id)
|
||||||
.bind(user_id)
|
.bind(user_id)
|
||||||
.bind(&payload.username)
|
.bind(&req.data.username)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
@@ -206,7 +216,7 @@ async fn register_handler(
|
|||||||
|
|
||||||
match tx.commit().await {
|
match tx.commit().await {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
info!("User {} registered with id {}", payload.username, user_id);
|
info!("User {} registered with id {}", req.data.username, user_id);
|
||||||
(
|
(
|
||||||
StatusCode::CREATED,
|
StatusCode::CREATED,
|
||||||
Json(ApiResponse {
|
Json(ApiResponse {
|
||||||
|
|||||||
Reference in New Issue
Block a user