from fastapi import FastAPI, Request from fastapi.responses import JSONResponse import json import anyio from app.utils.app_tools import AppTools, setup_global_logger from app.module.F6_Plugin_module import F6PluginModule from app.module.module import F6Module from app.module.other_module import OtherPluginModule from app.config import Config app = FastAPI(title="简道云FastAPI服务") @app.on_event("startup") def on_startup(): """应用启动时初始化""" app.state.app_tools = AppTools(Config) app.state.logger = setup_global_logger(Config) app.state.f6_module = F6Module() app.state.f6_plugin_module = F6PluginModule() app.state.other_module = OtherPluginModule() def get_action_map() -> dict: """获取操作映射表""" f6_module = app.state.f6_module f6_plugin_module = app.state.f6_plugin_module other_module = app.state.other_module return { 'login_in': f6_module.accept_login_message, 'get_company_information': f6_module.get_company_information, 'get_store_information': f6_module.get_store_information, "keep_alive": f6_module.get_keep_heart, 'check_file': f6_plugin_module.check_file, 'create_brand': f6_plugin_module.create_brand, 'delete_history': f6_plugin_module.delete_history, 'delete_customer': f6_plugin_module.delete_customer, 'delete_cars': f6_plugin_module.delete_cars, 'sms_signature_status': other_module.sms_signature_status, 'modify_customer_info': f6_plugin_module.modify_customer_info, } @app.post("/webhook") async def webhook(request: Request): """ 接受前端请求后将任务放入消息队列 Returns: any: 返回任务处理的结果 """ logger = app.state.logger app_tools: AppTools = app.state.app_tools # 获取请求数据 data = await request.json() header = request.headers # 解码请求头 decoded_header = app_tools.decode_headers(header) # 获取操作映射表 action_map = get_action_map() action = decoded_header.get('Action') # 处理 F6_Plugin 特殊逻辑 if action == 'F6_Plugin': check = decoded_header.get('Check') if check == '否': handler = app.state.f6_plugin_module.check_file elif check == '是': print(data) sub_action = data.get('Action') print(sub_action) handler = action_map.get(sub_action, lambda x: {'msg': '未执行'}) else: return JSONResponse({'msg': '未知的操作'}) else: handler = action_map.get(action, lambda x: {'msg': '未知的操作'}) # 将任务放入消息队列 response_queue = app_tools.enqueue_task(handler, data) # 等待任务处理结果 result = await anyio.to_thread.run_sync(response_queue.get) print(handler) logger.info(json.dumps(result, ensure_ascii=False, indent=4)) return JSONResponse(result) if __name__ == '__main__': import uvicorn uvicorn.run("fastapi_app.main:app", host="0.0.0.0", port=5003, reload=False)