From dec0f824194d7aff8013934450bcc7bffb110922 Mon Sep 17 00:00:00 2001 From: H Vs Date: Sat, 22 Feb 2025 16:54:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/config_reources.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/resources/config_reources.py b/resources/config_reources.py index 98b663e..d845367 100644 --- a/resources/config_reources.py +++ b/resources/config_reources.py @@ -1,7 +1,11 @@ from flask_restful import Resource, reqparse +from pydantic import BaseModel, ValidationError from flask import jsonify,request,json from common import redis_helper,utils from wechat import gewe_chat +from dataclasses import dataclass, asdict +from typing import List +import json @@ -16,7 +20,13 @@ class GetWxchatConfigResource(Resource): config=self.wxchat.get_wxchat_config_from_cache(wxid) return jsonify(config) - +@dataclass +class AgentConfig(BaseModel): + chatroomIdWhiteList: List[str] = [] + agentTokenId: str + agentEnabled: bool + addContactsFromChatroomIdWhiteList: List[str] = [] + chatWaitingMsgEnabled: bool class SaveWxchatConfigResource(Resource): def __init__(self): @@ -29,5 +39,17 @@ class SaveWxchatConfigResource(Resource): data = req.get("data") # hash_key="__AI_OPS_WX__:WXCHAT_CONFIG" # redis_helper.redis_helper.update_hash_field(hash_key, wxid, json.dumps(data,ensure_ascii=False)) + + + try: + # 使用 Pydantic 严格校验数据类型和结构 + validated_config = AgentConfig.model_validate(data) + except ValidationError as e: + + response=jsonify({'code': 407, 'message': e.errors().__str__()}) + response.status_code = 407 + return response + + self.wxchat.save_wxchat_config(wxid, data) return jsonify({'wxid': wxid, 'config': data}) \ No newline at end of file