接入GetLoginStatus增强864登录态判定
This commit is contained in:
@@ -7,6 +7,54 @@ from wechat_ipad.providers.server_864.base import Server864APIClientBase
|
||||
class LoginMixin(Server864APIClientBase):
|
||||
"""864 登录相关接口。"""
|
||||
|
||||
@staticmethod
|
||||
def _normalize_online_text(value) -> str:
|
||||
"""把 864 各种登录态字段压平成便于判断的字符串。"""
|
||||
return str(value or "").strip().lower()
|
||||
|
||||
def _is_online_from_login_status_payload(self, data: dict | None) -> bool:
|
||||
"""根据 `GetLoginStatus` 的返回结构判断当前是否在线。
|
||||
|
||||
设计说明:
|
||||
1. 864 不同版本对在线态字段命名并不统一,可能是 `loginState/status/state/isLogin` 中的任意一种;
|
||||
2. 这里集中做一次宽松识别,避免上层 runtime 到处散落同类判断;
|
||||
3. 后续如果 864 新版本再补字段,只需要在这里扩展,不必改多处业务逻辑。
|
||||
"""
|
||||
payload = dict(data or {})
|
||||
normalized_login_state = self._normalize_online_text(
|
||||
self._pick_first(payload, "loginState", "LoginState", "status_text", "statusText")
|
||||
)
|
||||
normalized_status = self._normalize_online_text(
|
||||
self._pick_first(payload, "status", "Status", "state_text", "stateText")
|
||||
)
|
||||
state_value = self._pick_first(payload, "state", "State")
|
||||
login_flag = self._pick_first(payload, "isLogin", "IsLogin", "online", "Online", "isOnline", "IsOnline")
|
||||
|
||||
if normalized_login_state in {"online", "已登录", "在线"}:
|
||||
return True
|
||||
if normalized_status in {"online", "已登录", "在线"}:
|
||||
return True
|
||||
if isinstance(login_flag, bool):
|
||||
return login_flag
|
||||
if str(login_flag or "").strip().lower() in {"true", "1", "online"}:
|
||||
return True
|
||||
try:
|
||||
normalized_state_value = int(state_value or 0)
|
||||
except (TypeError, ValueError):
|
||||
normalized_state_value = 0
|
||||
return normalized_state_value in {1, 2}
|
||||
|
||||
def _extract_login_identity_from_status(self, data: dict | None) -> dict:
|
||||
"""从 `GetLoginStatus` 返回中提取尽可能多的账号身份字段。"""
|
||||
payload = dict(data or {})
|
||||
return {
|
||||
"wxid": str(self._pick_first(payload, "wxid", "Wxid", "UserName", "userName") or "").strip(),
|
||||
"nickname": str(self._pick_first(payload, "nick_name", "nickName", "NickName", "nickname") or "").strip(),
|
||||
"alias": str(self._pick_first(payload, "alias", "Alias", "wechatId", "WeChatId") or "").strip(),
|
||||
"phone": str(self._pick_first(payload, "mobile", "Mobile", "phone", "Phone") or "").strip(),
|
||||
"signature": str(self._pick_first(payload, "signature", "Signature") or "").strip(),
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _normalize_login_way(login_way: str) -> str:
|
||||
"""标准化 864 `GetLoginQrCodeNewX` 的 way 参数。"""
|
||||
@@ -146,12 +194,20 @@ class LoginMixin(Server864APIClientBase):
|
||||
|
||||
async def get_login_status(self, auto_login: bool = True) -> dict:
|
||||
"""获取 864 在线状态。"""
|
||||
return await self._request_data(
|
||||
data = await self._request_data(
|
||||
"get",
|
||||
"/login/GetLoginStatus",
|
||||
params={"autoLogin": str(bool(auto_login)).lower()},
|
||||
timeout=20,
|
||||
)
|
||||
normalized = dict(data or {})
|
||||
# 把最常用的在线态判断和身份字段提前归一化:
|
||||
# 1. 这样上层只要消费 `is_online` / `wxid` / `nickname` 这些统一键,不必感知原始 swagger 字段差异;
|
||||
# 2. 与 `CheckLoginStatus` 一样,Dashboard 和 runtime 都能共享同一份兼容结果;
|
||||
# 3. 也方便后续把 864 的不同 server 版本收敛到更薄的一层 provider 适配。
|
||||
normalized["is_online"] = self._is_online_from_login_status_payload(normalized)
|
||||
normalized.update(self._extract_login_identity_from_status(normalized))
|
||||
return normalized
|
||||
|
||||
async def log_out(self) -> bool:
|
||||
"""退出当前 864 登录态。"""
|
||||
|
||||
Reference in New Issue
Block a user