ForcePilot/backend/package/yuxi/channel/extensions/pinduoduo/security.py
Kris 1476e82ef0 feat(channel): 添加拼多多渠道扩展
新增拼多多(Pinduoduo)渠道扩展,支持在 Yuxi 平台中集成拼多多电商客服渠道。

包含以下功能模块:
- client: 拼多多 API 客户端封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- pairing: 用户配对与绑定
- security: 安全校验
- signature: 请求签名验证
- token: Token 管理
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- tools: Agent 工具集成
- window: 窗口管理
- types: 类型定义
2026-05-21 11:33:31 +08:00

63 lines
2.1 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.

from __future__ import annotations
import logging
from yuxi.channel.extensions.pinduoduo.types import PddDmPolicy, PinduoduoAccount
logger = logging.getLogger(__name__)
class PinduoduoSecurity:
def __init__(self, account: PinduoduoAccount):
self._account = account
self._policy = PddDmPolicy(account.dm_policy)
self._allowlist: set[str] = set()
self._paired: set[str] = set()
@property
def policy(self) -> PddDmPolicy:
return self._policy
def resolve_dm_policy(self) -> str:
return self._policy.value
def check_dm_access(self, buyer_id: str) -> tuple[bool, str | None]:
policy = self._resolve_effective_policy(buyer_id)
if policy == "open":
return True, None
if policy == "disabled":
return False, "DM 功能已禁用"
if policy == "pairing":
return False, "请先输入配对码(发送 #pair XXXXXX"
if policy == "blocked":
return False, "您不在白名单中,无法访问"
return False, "未知策略"
def _resolve_effective_policy(self, buyer_id: str) -> str:
normalized = buyer_id.strip().lower()
if self._policy == PddDmPolicy.OPEN:
return "open"
if self._policy == PddDmPolicy.DISABLED:
return "disabled"
if self._policy == PddDmPolicy.PAIRING:
if normalized in self._paired:
return "open"
return "pairing"
if self._policy == PddDmPolicy.ALLOWLIST:
if normalized in self._allowlist:
return "open"
return "blocked"
return "blocked"
def mark_paired(self, buyer_id: str) -> None:
self._paired.add(buyer_id.strip().lower())
def add_to_allowlist(self, buyer_id: str) -> None:
self._allowlist.add(buyer_id.strip().lower())
def remove_from_allowlist(self, buyer_id: str) -> None:
self._allowlist.discard(buyer_id.strip().lower())
def is_in_allowlist(self, buyer_id: str) -> bool:
return buyer_id.strip().lower() in self._allowlist