修复企微服务热重载后丢失初始化的问题
get_wechat_service() 增加 _initialized 检查, 未初始化时自动调用 init(settings.wechat), 避免 uvicorn 热重载后单例重建但未 init 导致 enabled=False。
This commit is contained in:
@@ -32,6 +32,7 @@ class WeChatService:
|
||||
|
||||
def __init__(self):
|
||||
self._enabled = False
|
||||
self._initialized = False
|
||||
self._corp_id = ""
|
||||
self._agent_id = ""
|
||||
self._secret = ""
|
||||
@@ -46,6 +47,7 @@ class WeChatService:
|
||||
|
||||
def init(self, config):
|
||||
"""初始化企微配置"""
|
||||
self._initialized = True
|
||||
self._enabled = config.enabled and bool(config.corp_id) and bool(config.secret)
|
||||
self._corp_id = config.corp_id
|
||||
self._agent_id = config.agent_id
|
||||
@@ -685,4 +687,7 @@ def get_wechat_service() -> WeChatService:
|
||||
global _wechat_service
|
||||
if _wechat_service is None:
|
||||
_wechat_service = WeChatService()
|
||||
if not _wechat_service._initialized:
|
||||
from app.config import settings
|
||||
_wechat_service.init(settings.wechat)
|
||||
return _wechat_service
|
||||
|
||||
Reference in New Issue
Block a user