Ver Fonte

操作数据库

tanlie há 1 mês atrás
pai
commit
fe60de355b
2 ficheiros alterados com 10 adições e 10 exclusões
  1. 1 1
      FastApi/app/core/db.py
  2. 9 9
      FastApi/app/mobile/views/user_views.py

+ 1 - 1
FastApi/app/core/db.py

@@ -12,7 +12,7 @@ Install dependencies: pip install sqlalchemy pymysql
 import os
 from typing import Generator
 
-# Try to auto-load a local .env file if python-dotenv is available.
+# Try to autoload a local .env file if python-dotenv is available.
 # Python does not read .env files automatically, so without this the app
 # will only see environment variables explicitly set in the process.
 try:

+ 9 - 9
FastApi/app/mobile/views/user_views.py

@@ -8,9 +8,9 @@ import logging
 from fastapi import APIRouter, Body, Query, Depends
 from sqlalchemy.orm import Session
 
+from app.core.db import get_db
 from app.core.response import success_response, error_response
 from app.mobile.req.user import UserLoginReq
-from app.core.db import get_db
 from app.models.user import User
 
 user = APIRouter(prefix="/dgapi/mobile/user", tags=["mobile user"])
@@ -43,18 +43,18 @@ def wish_summary():
 
 
 @user.get("/userinfo", summary="获取用户信息")
-def get_userinfo(userId: int = Query(..., description="用户ID"), db: Session = Depends(get_db)):
+def get_userinfo(userId: int = Query(description="用户ID"), db: Session = Depends(get_db)):
     """查询数据库并返回用户信息"""
     logger.info("获取用户信息,userId=%s", userId)
-    user = db.query(User).filter(User.id == userId).first()
-    if not user:
+    res = db.query(User).filter(User.id == userId).first()
+    if not res:
         return error_response(msg="用户不存在", code=404)
 
     data = {
-        "id": user.id,
-        "username": user.username,
-        "mobile": user.mobile,
-        "email": user.email,
-        "created_at": user.created_at.isoformat() if user.created_at is not None else None,
+        "id": res.id,
+        "username": res.username,
+        "mobile": res.mobile,
+        "email": res.email,
+        "createdAt": res.created_at.isoformat() if res.created_at is not None else None,
     }
     return success_response(data=data, msg="查询成功")