feat: 自动学习功能,后台定时发送心跳积累学时

This commit is contained in:
2026-11-15 01:01:45 +08:00
parent 2acf310e0c
commit ffa563d97c
13 changed files with 959 additions and 16 deletions
+42 -4
View File
@@ -31,18 +31,29 @@ from euniv.service import (
)
from euniv.session import clear_session, read_employee_id, write_session
from pydantic import BaseModel as _BaseModel
class MonthlyTargetBody(_BaseModel):
monthly_target: int
@asynccontextmanager
async def lifespan(_app: FastAPI):
init_db()
ensure_admin_seed()
load_admin_identity()
stop = None
stop_refresh = None
stop_study = None
if os.environ.get("EUNIV_DISABLE_REFRESH") != "1":
stop = start_credential_refresh()
stop_refresh = start_credential_refresh()
from euniv.autostudy import start_autostudy
stop_study = start_autostudy()
yield
if stop is not None:
stop.set()
if stop_study is not None:
stop_study.stop()
if stop_refresh is not None:
stop_refresh.set()
app = FastAPI(title="EunivTools", lifespan=lifespan)
@@ -186,6 +197,33 @@ def exam_detail(path: str, request: Request, db: Session = Depends(get_db)) -> d
raise HTTPException(status_code=400, detail=str(exc)) from exc
@app.get("/api/settings/monthly-target")
def get_monthly_target(request: Request, db: Session = Depends(get_db)) -> dict:
employee_id = _signed_in(request, db)
account = db.get(Account, employee_id)
return {"monthly_target": account.monthly_target if account else 600}
@app.put("/api/settings/monthly-target")
def put_monthly_target(body: MonthlyTargetBody, request: Request, db: Session = Depends(get_db)) -> dict:
employee_id = _signed_in(request, db)
if not 0 <= body.monthly_target <= 9999:
raise HTTPException(status_code=400, detail="每月学时目标须为 0 到 9999 的整数")
account = db.get(Account, employee_id)
if account is None:
raise HTTPException(status_code=401, detail="未登录")
account.monthly_target = body.monthly_target
db.commit()
return {"monthly_target": account.monthly_target}
@app.get("/api/autostudy/status")
def autostudy_state(request: Request, db: Session = Depends(get_db)) -> dict:
_signed_in(request, db)
from euniv.autostudy import autostudy_status
return autostudy_status()
@app.get("/api/training")
def training_detail(path: str, request: Request, db: Session = Depends(get_db)) -> dict:
try: