File size: 2,750 Bytes
2dc6fe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44a4285
 
 
 
 
 
 
2dc6fe4
 
 
 
 
 
 
 
 
 
 
 
 
44a4285
 
 
 
 
 
 
 
 
2dc6fe4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import annotations

import random

from fastapi import APIRouter, Header, HTTPException
from pydantic import BaseModel

from api.support import require_admin, require_identity
from services.auth_service import auth_service
from services.config import config


class CheckinRequest(BaseModel):
    mode: str = "checkin"


def create_router() -> APIRouter:
    router = APIRouter()

    @router.get("/api/admin/checkin")
    async def get_admin_checkin(authorization: str | None = Header(default=None)):
        require_admin(authorization)
        return {**auth_service.checkin_admin_summary(), "ads": config.get().get("checkin_ads", [])}

    @router.get("/api/checkin")
    async def get_checkin(authorization: str | None = Header(default=None)):
        identity = require_identity(authorization)
        if identity.get("role") != "normal":
            raise HTTPException(status_code=403, detail={"error": "仅普通用户可以签到"})
        try:
            summary = auth_service.checkin_summary(str(identity.get("id") or ""))
        except ValueError as exc:
            raise HTTPException(status_code=404, detail={"error": str(exc)}) from exc
        return {
            **summary,
            "credits": auth_service.credit_summary(identity),
            "ads": config.get().get("checkin_ads", []),
            "content": config.checkin_content,
            "credit_valid_days": config.checkin_credit_valid_days,
        }

    @router.post("/api/checkin")
    async def perform_checkin(body: CheckinRequest, authorization: str | None = Header(default=None)):
        identity = require_identity(authorization)
        if identity.get("role") != "normal":
            raise HTTPException(status_code=403, detail={"error": "仅普通用户可以签到"})
        mode = "random" if body.mode == "random" else "checkin"
        reward = random.randint(config.checkin_random_min, config.checkin_random_max) if mode == "random" else max(1, config.checkin_daily_credits)
        try:
            result = auth_service.perform_checkin(str(identity.get("id") or ""), mode=mode, reward=reward)
            summary = auth_service.checkin_summary(str(identity.get("id") or ""))
        except ValueError as exc:
            raise HTTPException(status_code=409, detail={"error": str(exc)}) from exc
        return {
            **summary,
            "result": result,
            "random_range": {"min": config.checkin_random_min, "max": config.checkin_random_max},
            "credits": auth_service.credit_summary(identity),
            "ads": config.get().get("checkin_ads", []),
            "content": config.checkin_content,
            "credit_valid_days": config.checkin_credit_valid_days,
        }

    return router