|
|
@@ -1,30 +1,59 @@
|
|
|
+import time
|
|
|
from typing import List
|
|
|
from fastapi import APIRouter, Query, Depends
|
|
|
from app.core.redis import get_redis_client
|
|
|
from redis.asyncio import Redis
|
|
|
from app.schemas.user import UserOut
|
|
|
from app.schemas.troov import TroovRate
|
|
|
+from app.schemas.sms import ShortMessageDetail
|
|
|
from app.services.troov_service import get_rate_by_date
|
|
|
+from app.services.sms_service import save_short_message, query_short_message
|
|
|
|
|
|
# 公共路由
|
|
|
-public_router = APIRouter(prefix="/api", tags=["troov"])
|
|
|
+public_router = APIRouter(tags=["public"])
|
|
|
+# 受保护路由
|
|
|
+protected_router = APIRouter(tags=["protected"])
|
|
|
|
|
|
-@public_router.get("/ping")
|
|
|
+@public_router.get("/ping", summary="心跳检测")
|
|
|
def ping():
|
|
|
return {"message": "pong"}
|
|
|
|
|
|
-# 受保护路由
|
|
|
-protected_router = APIRouter(prefix="/api", tags=["troov"])
|
|
|
+@public_router.get("/sms/upload", summary="上报短信", response_model=ShortMessageDetail)
|
|
|
+def sms_upload(
|
|
|
+ phone: str = Query(..., description="手机号"),
|
|
|
+ message: str = Query(..., description="短信内容"),
|
|
|
+ max_ttl: int = Query(300, description="短信保存时间(秒)"),
|
|
|
+ redis_client: Redis = Depends(get_redis_client)
|
|
|
+):
|
|
|
+ """
|
|
|
+ 保存短信到 Redis
|
|
|
+ """
|
|
|
+ received_at = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
|
|
|
+ msg = save_short_message(redis_client, phone, message, received_at, max_ttl)
|
|
|
+ return msg
|
|
|
|
|
|
-@protected_router.get("/users", response_model=List[UserOut])
|
|
|
+@protected_router.get("/users", summary="查询用户", response_model=List[UserOut])
|
|
|
def get_users():
|
|
|
return [
|
|
|
{"id": 1, "name": "Alice"},
|
|
|
{"id": 2, "name": "Bob"}
|
|
|
]
|
|
|
|
|
|
-@protected_router.get("/troov/rate", response_model=List[TroovRate])
|
|
|
+@protected_router.get("/troov/rate", summary="TROOV 查询rate", response_model=List[TroovRate])
|
|
|
def troov_rate(date: str = Query(..., description="查询的日期, 格式: YYYY-MM-DD"),
|
|
|
redis_client: Redis = Depends(get_redis_client)):
|
|
|
# 调用 service 层获取数据
|
|
|
return get_rate_by_date(redis_client, date)
|
|
|
+
|
|
|
+@protected_router.get("/sms/download", summary="读取短信", response_model=List[ShortMessageDetail])
|
|
|
+def sms_download(
|
|
|
+ phone: str = Query(..., description="手机号"),
|
|
|
+ keyword: str = Query('', description="短信内容关键字"),
|
|
|
+ sent_at: str = Query('', description="筛选时间(可选)"),
|
|
|
+ redis_client: Redis = Depends(get_redis_client)
|
|
|
+):
|
|
|
+ """
|
|
|
+ 查询短信(支持关键字和时间过滤)
|
|
|
+ """
|
|
|
+ results = query_short_message(redis_client, phone, keyword or None, sent_at or None)
|
|
|
+ return results
|