130 lines
4.4 KiB
Python
130 lines
4.4 KiB
Python
"""Feishu configuration routes."""
|
||
|
||
from __future__ import annotations
|
||
|
||
from fastapi import APIRouter, Depends, HTTPException
|
||
from sqlalchemy.ext.asyncio import AsyncSession
|
||
from sqlalchemy.future import select
|
||
|
||
from ...database import get_db
|
||
from ...dependencies.auth import get_current_user
|
||
from ...dependencies.tenant import get_tenant_id
|
||
from ...models import Integration, User
|
||
from ...services.auth import has_any_role
|
||
from ...services.feishu_integration import get_feishu_service
|
||
from ...services.feishu_long_connection import get_feishu_long_connection_manager
|
||
from .schemas import FeishuConfigRequest
|
||
|
||
router = APIRouter()
|
||
|
||
|
||
@router.post("/config")
|
||
async def save_feishu_config(
|
||
request: FeishuConfigRequest,
|
||
current_user: User = Depends(get_current_user),
|
||
db: AsyncSession = Depends(get_db),
|
||
tenant_id: int = Depends(get_tenant_id),
|
||
):
|
||
"""
|
||
保存飞书配置
|
||
|
||
配置信息说明:
|
||
- app_id: 飞书应用的 App ID,从飞书开放平台获取
|
||
- app_secret: 飞书应用的 App Secret
|
||
|
||
获取步骤:
|
||
1. 访问 https://open.feishu.cn/
|
||
2. 创建企业自建应用
|
||
3. 在「凭证与基础信息」页面获取 App ID 和 App Secret
|
||
4. 添加权限:im:message, im:message:group_at_msg, im:chat
|
||
5. 订阅事件:接收消息 v1 (message.receive_v1)
|
||
"""
|
||
if not has_any_role(str(current_user.role or ""), ["ADMIN", "BOSS"]):
|
||
raise HTTPException(status_code=403, detail="只有管理员可以配置飞书集成")
|
||
|
||
try:
|
||
event_mode = "long_connection"
|
||
service = get_feishu_service(db)
|
||
await service.save_config(
|
||
app_id=request.app_id,
|
||
app_secret=request.app_secret,
|
||
encrypt_key="",
|
||
verification_token="",
|
||
event_mode=event_mode,
|
||
tenant_id=tenant_id,
|
||
)
|
||
manager = get_feishu_long_connection_manager()
|
||
if manager:
|
||
await manager.restart()
|
||
|
||
next_steps = [
|
||
"1. 飞书应用事件订阅至少包含:接收消息 v1 (message.receive_v1)",
|
||
"2. 当前模式:long_connection",
|
||
"3. 后端已自动重启飞书长连接",
|
||
]
|
||
|
||
return {
|
||
"status": "success",
|
||
"message": "飞书配置已保存,并已切换到长连接模式",
|
||
"next_steps": next_steps,
|
||
}
|
||
except HTTPException:
|
||
raise
|
||
except Exception as exc:
|
||
raise HTTPException(status_code=500, detail=f"保存配置失败: {str(exc)}")
|
||
|
||
|
||
@router.get("/config")
|
||
async def get_feishu_config(
|
||
current_user: User = Depends(get_current_user),
|
||
db: AsyncSession = Depends(get_db),
|
||
tenant_id: int = Depends(get_tenant_id),
|
||
):
|
||
"""获取飞书配置状态。管理员可获取完整配置用于回填修改表单。"""
|
||
try:
|
||
service = get_feishu_service(db)
|
||
await service.load_config(tenant_id)
|
||
manager = get_feishu_long_connection_manager()
|
||
can_view_secrets = has_any_role(str(current_user.role or ""), ["ADMIN", "BOSS"])
|
||
|
||
return {
|
||
"is_configured": service.is_configured(),
|
||
"app_id": service.app_id,
|
||
"app_secret": service.app_secret if can_view_secrets else None,
|
||
"event_mode": "long_connection",
|
||
"long_connection": manager.status() if manager else None,
|
||
}
|
||
except Exception as exc:
|
||
raise HTTPException(status_code=500, detail=f"获取配置失败: {str(exc)}")
|
||
|
||
|
||
@router.delete("/config")
|
||
async def delete_feishu_config(
|
||
current_user: User = Depends(get_current_user),
|
||
db: AsyncSession = Depends(get_db),
|
||
tenant_id: int = Depends(get_tenant_id),
|
||
):
|
||
"""删除飞书配置。"""
|
||
if not has_any_role(str(current_user.role or ""), ["ADMIN", "BOSS"]):
|
||
raise HTTPException(status_code=403, detail="只有管理员可以删除配置")
|
||
|
||
try:
|
||
result = await db.execute(
|
||
select(Integration).where(
|
||
Integration.tenant_id == tenant_id,
|
||
Integration.type == "feishu",
|
||
)
|
||
)
|
||
integration = result.scalar_one_or_none()
|
||
if integration:
|
||
integration.is_active = False
|
||
await db.commit()
|
||
|
||
manager = get_feishu_long_connection_manager()
|
||
if manager:
|
||
await manager.restart()
|
||
|
||
return {"status": "success", "message": "飞书配置已删除"}
|
||
except Exception as exc:
|
||
raise HTTPException(status_code=500, detail=f"删除配置失败: {str(exc)}")
|