您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

53 行
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}