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.

config_endpoint.py 2.4KB

1 ay önce
3 hafta önce
1 ay önce
3 hafta önce
1 ay önce
3 hafta önce
1 ay önce
3 hafta önce
1 ay önce
3 hafta önce
1 ay önce
3 hafta önce
1 ay önce
3 hafta önce
1 ay önce
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. if not wxid:
  20. return {"code": 400, "message": "wxid 不能为空"}
  21. k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  22. if not k:
  23. return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  24. login_status=loginfo.get('status','0')
  25. if login_status != '1':
  26. return {"code": 401, "message": f"{wxid} 已经离线"}
  27. config=await request.app.state.gewe_service.get_wxchat_config_from_cache_async(wxid)
  28. return config
  29. @config_router.post("/saveconfig",response_model=None)
  30. #@validate_wxid
  31. async def save_config(request: Request, body: SaveConfigRequest):
  32. wxid = body.wxid
  33. data = body.data
  34. # k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  35. # if not k:
  36. # return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  37. if not wxid:
  38. return {"code": 400, "message": "wxid 不能为空"}
  39. k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  40. if not k:
  41. return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  42. login_status=loginfo.get('status','0')
  43. if login_status != '1':
  44. return {"code": 401, "message": f"{wxid} 已经离线"}
  45. try:
  46. # 使用 Pydantic 严格校验数据类型和结构
  47. validated_config = AgentConfig.model_validate(data)
  48. except ValidationError as e:
  49. return {'code': 407, 'message': e.errors().__str__()}
  50. await request.app.state.gewe_service.save_wxchat_config_async(wxid, data)
  51. return {'wxid': wxid, 'config': data}