Files
tw/utils/designer_roster.py
2026-02-27 16:03:04 +08:00

42 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
设计师在线状态 - 请求外部查询服务(转人工时按需调用)
接口: GET /online 返回 {online_users: ["lz", "ZuoWei"], ...}
本端用 online_users 同步本地 designer_online 表后派单。
"""
import os
import logging
from typing import List, Set
logger = logging.getLogger(__name__)
# 设计师在线查询服务,.env 配置 DESIGNER_ROSTER_API如 http://huichang.online:8001/online
_API_URL = os.getenv("DESIGNER_ROSTER_API", "")
async def _fetch_online_users() -> Set[str]:
"""请求 GET /online返回当前在线用户的 user_id 集合"""
if not _API_URL:
return set()
try:
import httpx
async with httpx.AsyncClient(timeout=10) as client:
r = await client.get(_API_URL)
r.raise_for_status()
data = r.json()
users = data.get("online_users", [])
return set(users) if isinstance(users, list) else set()
except Exception as e:
logger.debug(f"设计师在线查询失败: {e}")
return set()
async def poll_and_update_roster() -> None:
"""请求查询服务,同步在线状态到本地数据库"""
from db.designer_roster_db import update_online, get_all_wechat_user_ids
online_users = await _fetch_online_users()
all_ids = get_all_wechat_user_ids()
for uid in all_ids:
update_online(uid, uid in online_users)