feat(channel-service): add Python FastAPI pywechat channel service
- Add FastAPI project under channel-service/ - Implement mock pywechat client adapter (login, send, heartbeat) - Add /v1/wechat-personal/login/*, /v1/wechat-personal/send/* APIs - Add /v1/heartbeat and /v1/monitor/status for client health - Add /monitor HTML page with Web Components - Add pytest suite and Dockerfile - Update .gitignore for Python/Node artifacts
This commit is contained in:
53
channel-service/app/services/login_service.py
Normal file
53
channel-service/app/services/login_service.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
from app.clients.pywechat_client import ClientRegistry, LoginStatus, OnlineStatus, registry
|
||||
|
||||
|
||||
class LoginService:
|
||||
def __init__(self, registry: ClientRegistry):
|
||||
self.registry = registry
|
||||
|
||||
def start_login(self, account_id: str, account_name: Optional[str] = None) -> dict:
|
||||
client = self.registry.get_or_create(account_id, account_name)
|
||||
login_id = client.start_login()
|
||||
return {
|
||||
"login_id": login_id,
|
||||
"qr_code_url": client.state.qr_code_url,
|
||||
"status": client.state.login_status.value,
|
||||
}
|
||||
|
||||
def get_status(self, account_id: str) -> dict:
|
||||
client = self.registry.get(account_id)
|
||||
if client is None:
|
||||
return {"found": False, "account_id": account_id}
|
||||
state = client.get_state()
|
||||
return {
|
||||
"found": True,
|
||||
"account_id": state.account_id,
|
||||
"account_name": state.account_name,
|
||||
"login_status": state.login_status.value,
|
||||
"online_status": state.online_status.value,
|
||||
"last_heartbeat_at": state.last_heartbeat_at,
|
||||
"risk_level": state.risk_level,
|
||||
"sent_count_today": state.sent_count_today,
|
||||
}
|
||||
|
||||
def confirm_login(self, account_id: str) -> dict:
|
||||
client = self.registry.get(account_id)
|
||||
if client is None:
|
||||
return {"success": False, "error": "account not found"}
|
||||
client.confirm_login()
|
||||
return {"success": True, "status": client.state.login_status.value}
|
||||
|
||||
def expire_login(self, account_id: str) -> dict:
|
||||
client = self.registry.get(account_id)
|
||||
if client is None:
|
||||
return {"success": False, "error": "account not found"}
|
||||
client.expire_login()
|
||||
return {"success": True, "status": client.state.login_status.value}
|
||||
|
||||
|
||||
login_service = LoginService(registry)
|
||||
Reference in New Issue
Block a user