2026-06-14 16:49:18 +08:00
|
|
|
|
# 联软LV7000配置管理
|
|
|
|
|
|
"""
|
|
|
|
|
|
从system_configs表读取联软API配置,构建LianruanClient实例。
|
|
|
|
|
|
|
|
|
|
|
|
联软配置键(前缀 integration_lianruan_):
|
|
|
|
|
|
- integration_lianruan_base_url: 联软API地址(如 http://192.168.x.x:30098)
|
|
|
|
|
|
- integration_lianruan_api_account: API账号
|
|
|
|
|
|
- integration_lianruan_api_password: API密码
|
|
|
|
|
|
- integration_lianruan_validate_key: 验证密钥(可选)
|
|
|
|
|
|
|
|
|
|
|
|
配置方式:管理后台 → 系统集成 → 联软LV7000 → 填入账号密码
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
2026-07-17 23:08:59 +08:00
|
|
|
|
from sqlalchemy import select
|
2026-06-14 16:49:18 +08:00
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
|
|
from app.integrations.lianruan.client import LianruanClient
|
|
|
|
|
|
from app.integrations.lianruan.exceptions import LianruanConfigError
|
|
|
|
|
|
from app.models.system_config import SystemConfig
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
2026-07-17 23:08:59 +08:00
|
|
|
|
# 联软配置在 system_configs 表中的 key 前缀
|
|
|
|
|
|
LIANRUAN_CONFIG_PREFIX = "integration_lianruan_"
|
2026-06-14 16:49:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def get_lianruan_client(db: AsyncSession) -> LianruanClient:
|
2026-07-17 23:08:59 +08:00
|
|
|
|
"""从系统配置表构建联软API客户端。
|
2026-06-14 16:49:18 +08:00
|
|
|
|
|
2026-07-17 23:08:59 +08:00
|
|
|
|
读取 integration_lianruan_ 前缀的配置项,构建 LianruanClient 实例。
|
|
|
|
|
|
如果任何必填配置缺失,抛出 LianruanConfigError。
|
2026-06-14 16:49:18 +08:00
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
db: 数据库会话
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
2026-07-17 23:08:59 +08:00
|
|
|
|
LianruanClient: 已配置的联软API客户端实例
|
2026-06-14 16:49:18 +08:00
|
|
|
|
|
|
|
|
|
|
Raises:
|
2026-07-17 23:08:59 +08:00
|
|
|
|
LianruanConfigError: 缺少必填配置
|
2026-06-14 16:49:18 +08:00
|
|
|
|
"""
|
2026-07-17 23:08:59 +08:00
|
|
|
|
# 读取四个配置
|
|
|
|
|
|
result = await db.execute(
|
|
|
|
|
|
select(SystemConfig).where(
|
|
|
|
|
|
SystemConfig.config_key.startswith(LIANRUAN_CONFIG_PREFIX)
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
|
|
|
|
|
configs = list(result.scalars().all())
|
|
|
|
|
|
|
|
|
|
|
|
# 构建 key→value 映射
|
|
|
|
|
|
config_map = {cfg.config_key: cfg.config_value for cfg in configs}
|
|
|
|
|
|
|
|
|
|
|
|
base_url = config_map.get(f"{LIANRUAN_CONFIG_PREFIX}base_url", "")
|
|
|
|
|
|
api_account = config_map.get(f"{LIANRUAN_CONFIG_PREFIX}api_account", "")
|
|
|
|
|
|
api_password = config_map.get(f"{LIANRUAN_CONFIG_PREFIX}api_password", "")
|
|
|
|
|
|
validate_key = config_map.get(f"{LIANRUAN_CONFIG_PREFIX}validate_key", "")
|
|
|
|
|
|
|
|
|
|
|
|
# 校验必填项
|
|
|
|
|
|
missing = []
|
|
|
|
|
|
if not base_url:
|
|
|
|
|
|
missing.append("Base URL")
|
|
|
|
|
|
if not api_account:
|
|
|
|
|
|
missing.append("API账号")
|
|
|
|
|
|
if not api_password:
|
|
|
|
|
|
missing.append("API密码")
|
|
|
|
|
|
|
|
|
|
|
|
if missing:
|
|
|
|
|
|
raise LianruanConfigError(f"联软API未配置:缺少{', '.join(missing)}")
|
2026-06-14 16:49:18 +08:00
|
|
|
|
|
|
|
|
|
|
return LianruanClient(
|
2026-07-17 23:08:59 +08:00
|
|
|
|
base_url=base_url,
|
|
|
|
|
|
api_account=api_account,
|
|
|
|
|
|
api_password=api_password,
|
|
|
|
|
|
validate_key=validate_key or None,
|
2026-06-14 16:49:18 +08:00
|
|
|
|
)
|