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