You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 satır
1.7KB

  1. from fastapi import APIRouter,Request
  2. from pydantic import BaseModel
  3. from fastapi import APIRouter, Depends
  4. from pydantic import BaseModel, ValidationError
  5. from services.gewe_service import GeWeService,get_gewe_service
  6. from services.redis_service import RedisService
  7. from model.models import AgentConfig,validate_wxid
  8. config_router = APIRouter(prefix="/api/wxchat")
  9. # 定义请求体的 Pydantic 模型
  10. class GetConfigRequest(BaseModel):
  11. wxid: str
  12. class SaveConfigRequest(BaseModel):
  13. wxid: str
  14. data: dict
  15. @config_router.post("/getconfig",response_model=None)
  16. @validate_wxid
  17. async def get_config(request: Request, body: GetConfigRequest):
  18. wxid = body.wxid
  19. # k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  20. # if not k:
  21. # return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  22. config=await request.app.state.gewe_service.get_wxchat_config_from_cache_async(wxid)
  23. return config
  24. @config_router.post("/saveconfig",response_model=None)
  25. @validate_wxid
  26. async def save_config(request: Request, body: SaveConfigRequest):
  27. wxid = body.wxid
  28. data = body.data
  29. # k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  30. # if not k:
  31. # return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  32. try:
  33. # 使用 Pydantic 严格校验数据类型和结构
  34. validated_config = AgentConfig.model_validate(data)
  35. except ValidationError as e:
  36. return {'code': 407, 'message': e.errors().__str__()}
  37. await request.app.state.gewe_service.save_wxchat_config_async(wxid, data)
  38. return {'wxid': wxid, 'config': data}