選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

config_endpoint.py 3.3KB

1ヶ月前
1ヶ月前
1ヶ月前
3週間前
1ヶ月前
3週間前
1ヶ月前
3週間前
1ヶ月前
3週間前
1ヶ月前
1ヶ月前
3週間前
1ヶ月前
3週間前
1ヶ月前
3週間前
1ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. print(config)
  30. try:
  31. # 使用 Pydantic 严格校验数据类型和结构
  32. validated_config = AgentConfig.model_validate(config)
  33. except ValidationError as e:
  34. return {'code': 407, 'message': e.errors().__str__()}
  35. return validated_config
  36. @config_router.post("/wxchat/saveconfig",response_model=None)
  37. #@validate_wxid
  38. async def save_config(request: Request, body: SaveConfigRequest):
  39. wxid = body.wxid
  40. data = body.data
  41. # k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  42. # if not k:
  43. # return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  44. if not wxid:
  45. return {"code": 400, "message": "wxid 不能为空"}
  46. k,loginfo=await request.app.state.gewe_service.get_login_info_by_wxid_async(wxid)
  47. if not k:
  48. return {"code":404,"message":f"{wxid} 没有对应的登录信息"}
  49. login_status=loginfo.get('status','0')
  50. if login_status != '1':
  51. return {"code": 401, "message": f"{wxid} 已经离线"}
  52. try:
  53. # 使用 Pydantic 严格校验数据类型和结构
  54. validated_config = AgentConfig.model_validate(data)
  55. except ValidationError as e:
  56. return {'code': 407, 'message': e.errors().__str__()}
  57. await request.app.state.gewe_service.save_wxchat_config_async(wxid, data)
  58. return {'wxid': wxid, 'config': data}
  59. @config_router.post("/global/getconfig",response_model=None)
  60. async def get_global_config(request: Request):
  61. #await request.app.state.gewe_service.get_login_info_by_wxid_async()
  62. config=await request.app.state.gewe_service.get_global_config_from_cache_async()
  63. return config
  64. @config_router.post("/global/saveconfig",response_model=None)
  65. async def save_global_config(request: Request, body: Dict[str, Any]):
  66. #await request.app.state.gewe_service.get_login_info_by_wxid_async()
  67. await request.app.state.gewe_service.save_global_config_async(body)
  68. return {'message': '操作成功'}