修改时区显示问题

This commit is contained in:
2026-07-14 14:13:29 +08:00
parent 21ff42653c
commit 9637e5ba79
6 changed files with 90 additions and 37 deletions
+11 -9
View File
@@ -1,4 +1,6 @@
from datetime import datetime
from datetime import datetime, timezone, timedelta
CST = timezone(timedelta(hours=8))
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy import select
@@ -117,8 +119,8 @@ async def _handle_daily_login_credits(db: AsyncSession, user: User) -> None:
credits = int(credits_result.scalar_one_or_none() or "0")
if credits <= 0:
return
today = datetime.now().date()
today = datetime.now(CST).date()
if user.last_login_at:
last_login_date = user.last_login_at.date()
if last_login_date >= today:
@@ -160,7 +162,7 @@ async def login(req: LoginRequest, db: AsyncSession = Depends(get_db)):
)
await _handle_daily_login_credits(db, user)
user.last_login_at = datetime.now()
user.last_login_at = datetime.now(CST)
await db.flush()
return _token_response(user, req.remember_me)
@@ -191,7 +193,7 @@ async def sms_login(req: SmsLoginRequest, db: AsyncSession = Depends(get_db)):
)
await _handle_daily_login_credits(db, user)
user.last_login_at = datetime.now()
user.last_login_at = datetime.now(CST)
await db.flush()
return _token_response(user, req.remember_me)
@@ -223,7 +225,7 @@ async def register(req: RegisterRequest, db: AsyncSession = Depends(get_db)):
username=req.phone,
phone=req.phone,
hashed_password=hash_password(req.password),
password_set_at=datetime.now(),
password_set_at=datetime.now(CST),
credits=register_credits,
is_admin=False,
user_type="frontend",
@@ -295,7 +297,7 @@ async def set_password(
)
current_user.hashed_password = hash_password(req.new_password)
current_user.password_set_at = datetime.now()
current_user.password_set_at = datetime.now(CST)
await db.flush()
return {"message": "密码设置成功", "must_set_password": False}
@@ -319,7 +321,7 @@ async def change_password(
)
current_user.hashed_password = hash_password(req.new_password)
current_user.password_set_at = datetime.now()
current_user.password_set_at = datetime.now(CST)
await db.flush()
return {"message": "密码修改成功"}
@@ -382,7 +384,7 @@ async def admin_login(req: LoginRequest, db: AsyncSession = Depends(get_db)):
detail="该账号不是管理员账号",
)
user.last_login_at = datetime.now()
user.last_login_at = datetime.now(CST)
await db.flush()
token = create_access_token(user.id, req.remember_me)