ソースを参照

fast api 视图函数接收请求参数的方法。

tanlie 14 時間 前
コミット
f890e33b4b

+ 1 - 1
FastApi/app/mobile/__init__.py

@@ -1,5 +1,5 @@
 """
-__init__.py -
+__init__.py - mobile views package
 auther: tanlie
 date: 2026/8/30
 """

+ 5 - 0
FastApi/app/mobile/req/__init__.py

@@ -0,0 +1,5 @@
+"""
+__init__.py -
+auther: tanlie
+date: 2026/8/30
+"""

+ 24 - 0
FastApi/app/mobile/req/user.py

@@ -0,0 +1,24 @@
+"""
+user -
+auther: tanlie
+date: 2026/8/30
+"""
+from pydantic import BaseModel, Field, field_validator
+
+
+class UserLoginReq(BaseModel):
+    """
+    用户登录请求参数
+    """
+    mobile: str = Field(description="手机号", pattern="^(?:\\?86)?1[3-9]\\d{9}$")
+    code: str = Field(description="验证码")
+    captcha: str = Field(description="图形验证码")
+    captchaKey: str = Field(description="验证码Key")
+    loginType: int = Field(default=None, description="登录类型,1:手机号验证码登录,2:手机号密码登录", ge=1, le=10)
+
+    @field_validator("code")
+    @classmethod
+    def validate_code(cls, value):
+        if len(value) != 6:
+            raise ValueError("验证码必须是6个字符")
+        return value

+ 10 - 4
FastApi/app/mobile/views/user_views.py

@@ -3,26 +3,32 @@ user - mobile user views
 auther: tanlie
 date: 2026/8/30
 """
-from fastapi import APIRouter
+from fastapi import APIRouter, Query, Body
+
+from app.mobile.req.user import UserLoginReq
 
 user = APIRouter(prefix="/dgapi/mobile/user", tags=["mobile user"])
 
 
 @user.get("/login/captcha", summary="获取验证码")
-def get_captcha():
+def get_captcha(type: int = Query(default=1, description="验证码类型,1:登录验证码,2:注册验证码")):
+    print(f"获取验证码,type={type}")
     return {"message": "get captcha"}
 
 
 @user.post("/login", summary="登录")
-def login():
+def login(req: UserLoginReq):
+    print(f"用户登录,请求参数:{req}")
     return {"message": "login"}
 
 
 @user.post("/logout", summary="登出")
-def logout():
+def logout(userId: int = Body(description="用户ID")):
+    print(f"用户登出,userId={userId}")
     return {"message": "logout"}
 
 
 @user.get("/wish-summary", summary="心愿单摘要")
 def wish_summary():
+    print("获取心愿单摘要")
     return "心愿单"

+ 3 - 1
FastApi/main.py

@@ -4,6 +4,8 @@ python version api for wishing tree
 author: tanlie
 date: 2026/8/30
 """
+from http.cookiejar import debug
+
 from app import create_app
 
 app = create_app()
@@ -11,4 +13,4 @@ app = create_app()
 if __name__ == "__main__":
     import uvicorn
 
-    uvicorn.run("main:app", host="0.0.0.0", port=8000)
+    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)