Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

53 lines
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. @config_router.post("/getconfig",response_model=None)
  15. @validate_wxid
  16. async def get_config(request: Request, body: GetConfigRequest):
  17. wxid = body.wxid
  18. # k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  19. # if not k:
  20. # return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  21. config=await request.app.state.gewe_service.get_wxchat_config_from_cache_async(wxid)
  22. return config
  23. @config_router.post("/saveconfig",response_model=None)
  24. @validate_wxid
  25. async def save_config(request: Request, body: SaveConfigRequest):
  26. wxid = body.get("wxid")
  27. data = body.get("data")
  28. # k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  29. # if not k:
  30. # return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  31. try:
  32. # 使用 Pydantic 严格校验数据类型和结构
  33. validated_config = AgentConfig.model_validate(data)
  34. except ValidationError as e:
  35. return {'code': 407, 'message': e.errors().__str__()}
  36. await request.app.state.gewe_service.save_wxchat_config_async(wxid, data)
  37. return {'wxid': wxid, 'config': data}