迁移wechat_ipad配置到环境变量并清理本地状态文件

This commit is contained in:
liuwei
2026-05-07 10:22:40 +08:00
parent fe8b7171fd
commit d0acd33b66
16 changed files with 271 additions and 67 deletions

View File

@@ -1,7 +0,0 @@
server_url = "http://192.168.2.170:8059/"
wxid = "wxid_72ow1edm3kea22"
device_id = "4978fc0fd191cf45a4e55fae7936b153"
device_name = "shui niu's Pad"
server_ip = "192.168.2.170"
server_port = "8059"
login_time = "2025-05-15 10:51:22"

View File

@@ -0,0 +1,8 @@
server_url = "http://127.0.0.1:8059/"
server_ip = "127.0.0.1"
server_port = "8059"
server_type = "legacy_855"
wxid = ""
device_id = ""
device_name = ""
login_time = ""

View File

@@ -1,4 +1,5 @@
import asyncio
import os
import time
from typing import Any, Awaitable, Callable
@@ -40,7 +41,7 @@ class Legacy855RuntimeMixin:
self,
*,
ipad_config: dict,
config_path: str,
state_path: str,
logger,
on_login_ready: AsyncCallback,
on_history_message: AsyncCallback,
@@ -52,7 +53,7 @@ class Legacy855RuntimeMixin:
"""启动 855 provider 的完整运行时。
参数说明:
1. `ipad_config` 与 `config_path` 由上层传入provider 只负责更新和落盘登录态;
1. `ipad_config` 与 `state_path` 由上层传入provider 只负责更新和落盘登录态;
2. `on_*` 回调保持尽量少,只暴露业务层真正需要接手的几个时机;
3. 这样既避免 `Robot` 再写协议细节,也不额外引入复杂的事件总线或状态机层。
"""
@@ -70,7 +71,7 @@ class Legacy855RuntimeMixin:
device_name=device_name,
device_id=device_id,
ipad_config=ipad_config,
config_path=config_path,
state_path=state_path,
logger=logger,
)
@@ -160,7 +161,7 @@ class Legacy855RuntimeMixin:
device_name: str,
device_id: str,
ipad_config: dict,
config_path: str,
state_path: str,
logger,
) -> None:
"""保证当前 provider 已完成登录,并把登录结果写回配置。
@@ -168,7 +169,7 @@ class Legacy855RuntimeMixin:
这里沿用现有 855 的行为:
1. 优先复用缓存唤醒;
2. 唤醒失败或无缓存时回退到二维码登录;
3. 登录成功后继续把 wxid / device 信息写回 `config.toml`,保持现有部署习惯不变
3. 登录成功后把 wxid / device 信息写回本地状态文件,不再要求用户手工维护 TOML
"""
if await self.is_logged_in(wxid):
self.wxid = wxid
@@ -212,10 +213,39 @@ class Legacy855RuntimeMixin:
ipad_config["device_name"] = device_name
ipad_config["device_id"] = device_id
ipad_config["login_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
with open(config_path, "w", encoding="utf-8") as f:
toml.dump(ipad_config, f)
self._save_runtime_state(
state_path=state_path,
state_payload={
"wxid": self.wxid,
"device_name": device_name,
"device_id": device_id,
"login_time": ipad_config["login_time"],
},
logger=logger,
)
break
@staticmethod
def _save_runtime_state(*, state_path: str, state_payload: dict[str, Any], logger) -> None:
"""把运行期登录状态写入本地缓存文件。
这里刻意只保存动态字段:
1. server_url / server_ip / server_port 已经统一走 `.env`
2. 本地状态文件只承载登录缓存,避免用户再维护两套静态配置;
3. 路径所在目录不存在时自动创建,兼容首次启动与 Docker 挂载目录。
"""
try:
normalized_path = str(state_path or "").strip()
if not normalized_path:
return
state_dir = os.path.dirname(normalized_path)
if state_dir:
os.makedirs(state_dir, exist_ok=True)
with open(normalized_path, "w", encoding="utf-8") as f:
toml.dump(state_payload, f)
except Exception as e:
logger.warning(f"写入 wechat_ipad 本地状态失败: path={state_path}, error={e}")
def _apply_login_result(self, *, data: dict, logger) -> None:
"""把登录接口返回的用户信息统一写回当前 provider。"""
acct_section = data.get("acctSectResp", {}) or {}