| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- 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.utils.response import success
- from app.schemas.common import ApiResponse
- from app.schemas.sms import ShortMessageDetail, SmsSendIn
- from app.services.sms_service import query_short_message, save_short_message, send_sms
- public_router = APIRouter()
- protected_router = APIRouter()
- admin_required_router = APIRouter()
- @admin_required_router.get("/sms/upload", summary="上报短信", tags=["短信接口"], response_model=ApiResponse[ShortMessageDetail])
- async 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 = await save_short_message(redis_client, phone, message, received_at, max_ttl)
- return success(data=msg)
- @admin_required_router.get("/sms/download", summary="读取短信", tags=["短信接口"], response_model=ApiResponse[List[ShortMessageDetail]])
- async def sms_download(
- phone: str = Query(..., description="手机号"),
- keyword: str = Query('', description="短信内容关键字"),
- sent_at: str = Query('', description="筛选时间(可选)"),
- redis_client: Redis = Depends(get_redis_client)
- ):
- """
- 查询短信(支持关键字和时间过滤)
- """
- obj = await query_short_message(redis_client, phone, keyword or None, sent_at or None)
- return success(data=obj)
- @admin_required_router.post("/sms/send", summary="发送短信", tags=["短信接口"], response_model=ApiResponse[str])
- async def sms_send(
- payload: SmsSendIn,
- ):
- res = await send_sms(
- send_to=payload.send_to,
- sender=payload.sender,
- content=payload.content,
- )
- return success(data=res)
|