feat: 立即开始停止学习并整理学时历史与界面

This commit is contained in:
2026-11-15 04:16:40 +08:00
parent ce2d6e08dd
commit f0a2bdd854
25 changed files with 1748 additions and 580 deletions
+55
View File
@@ -23,6 +23,7 @@ from euniv.service import (
fetch_home_exams,
fetch_home_summary,
fetch_home_trainings,
fetch_study_history,
fetch_training_detail,
login_and_store,
revoke_credential,
@@ -57,6 +58,10 @@ class CourseWorkersBody(_BaseModel):
workers: int
class StudyControlBody(_BaseModel):
action: str
@asynccontextmanager
async def lifespan(_app: FastAPI):
init_db()
@@ -336,6 +341,56 @@ def autostudy_state(request: Request, db: Session = Depends(get_db)) -> dict:
return autostudy_status()
@app.get("/api/settings/study-control")
def get_study_control(request: Request, db: Session = Depends(get_db)) -> dict:
from euniv.autostudy import autostudy_status
from euniv.study_control import view_of
employee_id = _signed_in(request, db)
account = db.get(Account, employee_id)
if account is None:
raise HTTPException(status_code=401, detail="未登录")
payload = view_of(account)
live = (autostudy_status().get("users") or {}).get(employee_id) or {}
payload["state"] = str(live.get("state") or "")
payload["course"] = str(live.get("course") or "")
payload["active"] = bool(live) and payload["state"] not in {"已达标", "已停止", "会话失效", "会话过期"}
if payload["active"]:
payload["started"] = True
return payload
@app.post("/api/settings/study-control")
def post_study_control(body: StudyControlBody, request: Request, db: Session = Depends(get_db)) -> dict:
from euniv.autostudy import request_stop, request_wake
from euniv.study_control import start_now, stop_now
employee_id = _signed_in(request, db)
account = db.get(Account, employee_id)
if account is None:
raise HTTPException(status_code=401, detail="未登录")
action = (body.action or "").strip().lower()
if action == "start":
start_now(account)
db.commit()
request_wake()
elif action == "stop":
stop_now(account)
db.commit()
request_stop(employee_id)
else:
raise HTTPException(status_code=400, detail="请选择开始或停止")
return get_study_control(request, db)
@app.get("/api/home/history")
def home_history(request: Request, db: Session = Depends(get_db)) -> dict:
try:
return fetch_study_history(db, _signed_in(request, db))
except RuntimeError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
@app.get("/api/training")
def training_detail(path: str, request: Request, db: Session = Depends(get_db)) -> dict:
try: