新增京东(JD)渠道扩展,支持在 Yuxi 平台中集成京东客服渠道。 包含以下功能模块: - client: 京东 API 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - pairing: 用户配对与绑定 - security: 安全校验 - signature: 请求签名验证 - crypto: 加解密处理 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - business: 业务逻辑处理 - types: 类型定义
58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
import random
|
|
import time
|
|
|
|
CODE_LENGTH = 6
|
|
CODE_TTL_SECONDS = 600
|
|
PENDING_MAX = 3
|
|
RATE_LIMIT_SECONDS = 3600
|
|
|
|
|
|
class JDPairing:
|
|
def __init__(self):
|
|
self._pending: dict[str, dict] = {}
|
|
self._user_last_code_at: dict[str, float] = {}
|
|
|
|
def generate_code(self, user_id: str) -> str | None:
|
|
now = time.time()
|
|
|
|
if user_id in self._user_last_code_at:
|
|
elapsed = now - self._user_last_code_at[user_id]
|
|
if elapsed < RATE_LIMIT_SECONDS:
|
|
return None
|
|
|
|
if len(self._pending) >= PENDING_MAX:
|
|
oldest = min(self._pending.values(), key=lambda p: p["created_at"])
|
|
if now - oldest["created_at"] < CODE_TTL_SECONDS:
|
|
return None
|
|
|
|
expired_user = next(k for k, v in self._pending.items() if v == oldest)
|
|
del self._pending[expired_user]
|
|
|
|
code = _generate_code()
|
|
self._pending[user_id] = {"code": code, "created_at": now}
|
|
self._user_last_code_at[user_id] = now
|
|
return code
|
|
|
|
def verify(self, user_id: str, code: str) -> bool:
|
|
entry = self._pending.get(user_id)
|
|
if not entry:
|
|
return False
|
|
|
|
if time.time() - entry["created_at"] > CODE_TTL_SECONDS:
|
|
del self._pending[user_id]
|
|
return False
|
|
|
|
if entry["code"] != code.upper().strip():
|
|
return False
|
|
|
|
del self._pending[user_id]
|
|
return True
|
|
|
|
def get_pending_count(self) -> int:
|
|
return len(self._pending)
|
|
|
|
|
|
def _generate_code() -> str:
|
|
no_ambiguous = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
|
|
return "".join(random.choices(no_ambiguous, k=CODE_LENGTH))
|