Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

config_endpoint.py 3.0KB

il y a 1 mois
il y a 1 semaine
il y a 1 mois
il y a 1 semaine
il y a 1 mois
il y a 3 semaines
il y a 1 mois
il y a 1 semaine
il y a 3 semaines
il y a 1 mois
il y a 3 semaines
il y a 1 mois
il y a 3 semaines
il y a 1 mois
il y a 1 semaine
il y a 3 semaines
il y a 1 mois
il y a 3 semaines
il y a 1 mois
il y a 3 semaines
il y a 1 mois
il y a 1 semaine
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 typing import Dict, Any
  6. from services.gewe_service import GeWeService,get_gewe_service
  7. from services.redis_service import RedisService
  8. from model.models import AgentConfig,validate_wxid
  9. config_router = APIRouter(prefix="/api")
  10. # 定义请求体的 Pydantic 模型
  11. class GetConfigRequest(BaseModel):
  12. wxid: str
  13. class SaveConfigRequest(BaseModel):
  14. wxid: str
  15. data: dict
  16. @config_router.post("/wxchat/getconfig",response_model=None)
  17. #@validate_wxid
  18. async def get_config(request: Request, body: GetConfigRequest):
  19. wxid = body.wxid
  20. if not wxid:
  21. return {"code": 400, "message": "wxid 不能为空"}
  22. k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  23. if not k:
  24. return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  25. login_status=loginfo.get('status','0')
  26. if login_status != '1':
  27. return {"code": 401, "message": f"{wxid} 已经离线"}
  28. config=await request.app.state.gewe_service.get_wxchat_config_from_cache_async(wxid)
  29. return config
  30. @config_router.post("/wxchat/saveconfig",response_model=None)
  31. #@validate_wxid
  32. async def save_config(request: Request, body: SaveConfigRequest):
  33. wxid = body.wxid
  34. data = body.data
  35. # k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  36. # if not k:
  37. # return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  38. if not wxid:
  39. return {"code": 400, "message": "wxid 不能为空"}
  40. k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  41. if not k:
  42. return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  43. login_status=loginfo.get('status','0')
  44. if login_status != '1':
  45. return {"code": 401, "message": f"{wxid} 已经离线"}
  46. try:
  47. # 使用 Pydantic 严格校验数据类型和结构
  48. validated_config = AgentConfig.model_validate(data)
  49. except ValidationError as e:
  50. return {'code': 407, 'message': e.errors().__str__()}
  51. await request.app.state.gewe_service.save_wxchat_config_async(wxid, data)
  52. return {'wxid': wxid, 'config': data}
  53. @config_router.post("/global/getconfig",response_model=None)
  54. async def get_global_config(request: Request):
  55. #await request.app.state.gewe_service.get_login_info_by_wxid_async()
  56. config=await request.app.state.gewe_service.get_global_config_from_cache_async()
  57. return config
  58. @config_router.post("/global/saveconfig",response_model=None)
  59. async def save_global_config(request: Request, body: Dict[str, Any]):
  60. #await request.app.state.gewe_service.get_login_info_by_wxid_async()
  61. await request.app.state.gewe_service.save_global_config_async(body)
  62. return {'message': '操作成功'}