import os from yuxi.channel.extensions.jd.types import JDAccount class JDConfig: ENV_MAP = { "app_key": "JD_APP_KEY", "app_secret": "JD_APP_SECRET", "shop_id": "JD_SHOP_ID", "dm_policy": "JD_DM_POLICY", } def list_account_ids(self, config: dict | None = None) -> list[str]: channels = (config or {}).get("channels", {}) jd_config = channels.get("jd", {}) accounts = jd_config.get("accounts", {}) if accounts: return list(accounts.keys()) if self._env_or_config("app_key", config): return ["default"] return [] def resolve_account(self, account_id: str = "default", config: dict | None = None) -> JDAccount: channels = (config or {}).get("channels", {}) jd_config = channels.get("jd", {}) accounts = jd_config.get("accounts", {}) account_data = accounts.get(account_id, {}) return JDAccount( account_id=account_id, app_key=account_data.get("app_key") or self._env_or_config("app_key") or "", app_secret=account_data.get("app_secret") or self._env_or_config("app_secret") or "", shop_id=account_data.get("shop_id") or self._env_or_config("shop_id") or "", access_token=account_data.get("access_token"), refresh_token=account_data.get("refresh_token"), webhook_token=account_data.get("webhook_token"), dm_policy=account_data.get("dm_policy") or self._env_or_config("dm_policy") or "pairing", webhook_sign_algorithm=account_data.get("webhook_sign_algorithm", "hmac-sha256"), allow_from=account_data.get("allow_from", []), ) def is_configured(self, account: dict | None = None) -> bool: if account: return bool(account.get("app_key") and account.get("app_secret") and account.get("shop_id")) return bool( self._env_or_config("app_key") and self._env_or_config("app_secret") and self._env_or_config("shop_id") ) def _env_or_config(self, key: str, config: dict | None = None) -> str | None: env_key = self.ENV_MAP.get(key, "") if env_key: val = os.getenv(env_key) if val: return val return None def config_schema(self) -> dict: return { "$schema": "https://json-schema.org/draft-07/schema#", "type": "object", "title": "京东/咚咚渠道配置", "properties": { "appKey": { "type": "string", "title": "App Key", "description": "京东开放平台应用 App Key", }, "appSecret": { "type": "string", "title": "App Secret", "x-ui-password": True, "description": "京东开放平台应用 App Secret", }, "shopId": { "type": "string", "title": "店铺 ID", "description": "京东店铺 ID(用于标识发送方)", }, "webhookToken": { "type": "string", "title": "Webhook Token", "x-ui-password": True, "description": "咚咚消息推送签名验证 Token(如配置)", }, "dmPolicy": { "type": "string", "title": "DM 策略", "enum": ["pairing", "allowlist", "open", "disabled"], "default": "pairing", "description": "私聊安全策略:pairing=配对码, allowlist=白名单, open=完全开放", }, "webhookSignAlgorithm": { "type": "string", "title": "Webhook 签名算法", "enum": ["hmac-sha256", "md5"], "default": "hmac-sha256", "description": "咚咚 Webhook 消息推送签名算法", }, }, "required": ["appKey", "appSecret", "shopId"], }