forked from xiaohei/taiji-AI-PAD
更新mode网关
This commit is contained in:
@@ -28,6 +28,8 @@ from app.schemas import (
|
||||
ResourceApplicationRequest,
|
||||
ChannelBillingResponse,
|
||||
ApplyProviderRequest,
|
||||
UpdateTenantStatusRequest,
|
||||
UpdateTenantPermissionsRequest,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/channel", tags=["渠道合作伙伴"])
|
||||
@@ -357,6 +359,176 @@ async def set_tenant_credit_limit(
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/tenants/{tenant_id}", response_model=SuccessResponse)
|
||||
async def delete_tenant(
|
||||
tenant_id: str,
|
||||
principal: dict = Depends(require_auth),
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
删除租户(软删除)
|
||||
|
||||
将租户状态标记为inactive,保留数据但禁止使用
|
||||
"""
|
||||
_verify_channel_permission(principal)
|
||||
channel_id = principal.get("claims", {}).get("channelId")
|
||||
|
||||
# 验证租户属于该渠道
|
||||
result = await db.execute(
|
||||
select(User).where(
|
||||
and_(
|
||||
User.id == tenant_id,
|
||||
User.channel_id == channel_id
|
||||
)
|
||||
)
|
||||
)
|
||||
tenant = result.scalar_one_or_none()
|
||||
|
||||
if not tenant:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="租户不存在或不属于该渠道"
|
||||
)
|
||||
|
||||
# 检查租户是否还有余额
|
||||
if float(tenant.balance) > 0:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"租户还有余额 {float(tenant.balance):.2f},请先处理余额后再删除"
|
||||
)
|
||||
|
||||
# 软删除:标记为不活跃
|
||||
tenant.status = "inactive"
|
||||
await db.commit()
|
||||
|
||||
return SuccessResponse(
|
||||
data={
|
||||
"id": str(tenant.id),
|
||||
"name": tenant.name,
|
||||
},
|
||||
message="租户已删除"
|
||||
)
|
||||
|
||||
|
||||
@router.put("/tenants/{tenant_id}/status", response_model=SuccessResponse)
|
||||
async def update_tenant_status(
|
||||
tenant_id: str,
|
||||
req: UpdateTenantStatusRequest,
|
||||
principal: dict = Depends(require_auth),
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
更新租户状态
|
||||
|
||||
可设置状态:
|
||||
- active: 正常使用
|
||||
- inactive: 已停用(软删除)
|
||||
- suspended: 暂停使用(临时停用,可恢复)
|
||||
"""
|
||||
_verify_channel_permission(principal)
|
||||
channel_id = principal.get("claims", {}).get("channelId")
|
||||
|
||||
# 验证租户属于该渠道
|
||||
result = await db.execute(
|
||||
select(User).where(
|
||||
and_(
|
||||
User.id == tenant_id,
|
||||
User.channel_id == channel_id
|
||||
)
|
||||
)
|
||||
)
|
||||
tenant = result.scalar_one_or_none()
|
||||
|
||||
if not tenant:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="租户不存在或不属于该渠道"
|
||||
)
|
||||
|
||||
old_status = tenant.status
|
||||
tenant.status = req.status
|
||||
await db.commit()
|
||||
|
||||
return SuccessResponse(
|
||||
data={
|
||||
"tenantId": str(tenant.id),
|
||||
"name": tenant.name,
|
||||
"oldStatus": old_status,
|
||||
"newStatus": req.status,
|
||||
},
|
||||
message=f"租户状态已更新为 {req.status}"
|
||||
)
|
||||
|
||||
|
||||
@router.put("/tenants/{tenant_id}/permissions", response_model=SuccessResponse)
|
||||
async def update_tenant_permissions(
|
||||
tenant_id: str,
|
||||
req: UpdateTenantPermissionsRequest,
|
||||
principal: dict = Depends(require_auth),
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
更新租户权限
|
||||
|
||||
可配置的权限列表包括:
|
||||
- use:platform_agents - 使用平台Agent
|
||||
- use:custom_agents - 使用自定义Agent
|
||||
- create:agents - 创建Agent
|
||||
- read:billing - 查看计费信息
|
||||
- export:data - 导出数据
|
||||
"""
|
||||
_verify_channel_permission(principal)
|
||||
channel_id = principal.get("claims", {}).get("channelId")
|
||||
|
||||
# 验证租户属于该渠道
|
||||
result = await db.execute(
|
||||
select(User).where(
|
||||
and_(
|
||||
User.id == tenant_id,
|
||||
User.channel_id == channel_id
|
||||
)
|
||||
)
|
||||
)
|
||||
tenant = result.scalar_one_or_none()
|
||||
|
||||
if not tenant:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="租户不存在或不属于该渠道"
|
||||
)
|
||||
|
||||
# 有效权限列表
|
||||
valid_permissions = {
|
||||
"use:platform_agents",
|
||||
"use:custom_agents",
|
||||
"create:agents",
|
||||
"read:billing",
|
||||
"export:data",
|
||||
}
|
||||
|
||||
# 验证权限
|
||||
invalid_permissions = set(req.permissions) - valid_permissions
|
||||
if invalid_permissions:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"无效的权限: {', '.join(invalid_permissions)}"
|
||||
)
|
||||
|
||||
# 更新用户的permissions字段(假设User模型有permissions JSON字段)
|
||||
# 如果没有该字段,可以存储在metadata或创建新表
|
||||
tenant.permissions = req.permissions
|
||||
await db.commit()
|
||||
|
||||
return SuccessResponse(
|
||||
data={
|
||||
"tenantId": str(tenant.id),
|
||||
"name": tenant.name,
|
||||
"permissions": req.permissions,
|
||||
},
|
||||
message="租户权限已更新"
|
||||
)
|
||||
|
||||
|
||||
# ============= 资源申请 =============
|
||||
|
||||
@router.post("/resources/apply", response_model=SuccessResponse)
|
||||
|
||||
Reference in New Issue
Block a user