feat: 飞书机器人按租户路由 群组绑定租户 + 独立凭证 + 知识库隔离

1. 新增 resolve_tenant_by_chat_id() 根据飞书群 chat_id 查找绑定的租户
2. 新增 get_tenant_feishu_config() 获取租户级飞书凭证
3. FeishuService 支持传入自定义 app_id/app_secret(租户级别)
4. feishu_bot.py 收到消息时自动解析租户,使用租户凭证回复
5. feishu_longconn_service.py 同样按 chat_id 解析租户并传递 tenant_id
6. 租户管理 UI 新增飞书配置字段:App ID、App Secret、绑定群 Chat ID
7. 租户列表展示飞书绑定状态和群数量
8. 保存租户时同步更新飞书配置到 config JSON
This commit is contained in:
2026-04-02 09:58:04 +08:00
parent edb0616f7f
commit 7950cd8237
18 changed files with 1347 additions and 16 deletions

View File

@@ -130,3 +130,45 @@ def delete_tenant(tenant_id):
except Exception as e:
logger.error(f"删除租户失败: {e}")
return jsonify({"error": str(e)}), 500
def resolve_tenant_by_chat_id(chat_id: str) -> str:
"""
根据飞书 chat_id 查找对应的 tenant_id。
遍历所有租户的 config.feishu.chat_groups匹配则返回该 tenant_id。
未匹配时返回 DEFAULT_TENANT。
"""
try:
with db_manager.get_session() as session:
tenants = session.query(Tenant).filter(Tenant.is_active == True).all()
for t in tenants:
if not t.config:
continue
try:
cfg = json.loads(t.config)
except (json.JSONDecodeError, TypeError):
continue
feishu_cfg = cfg.get('feishu', {})
chat_groups = feishu_cfg.get('chat_groups', [])
if chat_id in chat_groups:
logger.info(f"飞书群 {chat_id} 匹配到租户 {t.tenant_id}")
return t.tenant_id
except Exception as e:
logger.error(f"解析飞书群租户映射失败: {e}")
return DEFAULT_TENANT
def get_tenant_feishu_config(tenant_id: str) -> dict:
"""
获取租户的飞书配置。
返回 {'app_id': ..., 'app_secret': ..., 'chat_groups': [...]} 或空字典。
"""
try:
with db_manager.get_session() as session:
tenant = session.query(Tenant).filter(Tenant.tenant_id == tenant_id).first()
if tenant and tenant.config:
cfg = json.loads(tenant.config)
return cfg.get('feishu', {})
except Exception as e:
logger.error(f"获取租户飞书配置失败: {e}")
return {}