Compare commits
25 Commits
22cc89a6bd
...
b0f7d1ba9e
| Author | SHA1 | Date | |
|---|---|---|---|
| b0f7d1ba9e | |||
| 262a1b409f | |||
| d91119af8a | |||
| d57f812513 | |||
| 97d0aac734 | |||
| 3cf5451597 | |||
| 3ebd8b20a4 | |||
| 70fc358d72 | |||
| d7380bdc75 | |||
| 333bbe57eb | |||
| 8430f9dbaa | |||
| b8e19dcde6 | |||
| 2fbba63884 | |||
| d9fa9708ce | |||
| 104487f082 | |||
| 7806072b17 | |||
| 2f9d85edb6 | |||
| 9b72e6127f | |||
| 644255891e | |||
| ed88099cf0 | |||
| e45fe8128c | |||
| 29f031ca61 | |||
| c67ace3576 | |||
| 3c7903078c | |||
| 95a9d3e15d |
@@ -14,9 +14,11 @@ from .routers.wechat import router as wechat_router
|
|||||||
from .routers.alerts import router as alerts_router
|
from .routers.alerts import router as alerts_router
|
||||||
from .routers.cost import router as cost_router
|
from .routers.cost import router as cost_router
|
||||||
from .routers.quota import router as quota_router
|
from .routers.quota import router as quota_router
|
||||||
from .routers.tool_configs import router as tool_configs_router
|
from .routers.tasks import router as tasks_router
|
||||||
|
from .routers.notification_channels import router as notification_channels_router
|
||||||
from .middleware import TraceMiddleware, setup_exception_handlers, RequestLoggerMiddleware
|
from .middleware import TraceMiddleware, setup_exception_handlers, RequestLoggerMiddleware
|
||||||
from .middleware.trace import setup_logging
|
from .middleware.trace import setup_logging
|
||||||
|
from .services.scheduler import scheduler_service
|
||||||
|
|
||||||
# 配置日志(包含 TraceID)
|
# 配置日志(包含 TraceID)
|
||||||
setup_logging(level=logging.INFO, include_trace=True)
|
setup_logging(level=logging.INFO, include_trace=True)
|
||||||
@@ -67,7 +69,21 @@ app.include_router(wechat_router, prefix="/api")
|
|||||||
app.include_router(alerts_router, prefix="/api")
|
app.include_router(alerts_router, prefix="/api")
|
||||||
app.include_router(cost_router, prefix="/api")
|
app.include_router(cost_router, prefix="/api")
|
||||||
app.include_router(quota_router, prefix="/api")
|
app.include_router(quota_router, prefix="/api")
|
||||||
app.include_router(tool_configs_router, prefix="/api")
|
app.include_router(tasks_router)
|
||||||
|
app.include_router(notification_channels_router)
|
||||||
|
|
||||||
|
|
||||||
|
# 应用生命周期事件
|
||||||
|
@app.on_event("startup")
|
||||||
|
async def startup_event():
|
||||||
|
"""应用启动时启动调度器"""
|
||||||
|
scheduler_service.start()
|
||||||
|
|
||||||
|
|
||||||
|
@app.on_event("shutdown")
|
||||||
|
async def shutdown_event():
|
||||||
|
"""应用关闭时关闭调度器"""
|
||||||
|
scheduler_service.shutdown()
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ from .stats import AICallEvent, TenantUsageDaily
|
|||||||
from .logs import PlatformLog
|
from .logs import PlatformLog
|
||||||
from .alert import AlertRule, AlertRecord, NotificationChannel
|
from .alert import AlertRule, AlertRecord, NotificationChannel
|
||||||
from .pricing import ModelPricing, TenantBilling
|
from .pricing import ModelPricing, TenantBilling
|
||||||
|
from .scheduled_task import ScheduledTask, TaskLog, ScriptVar, Secret
|
||||||
|
from .notification_channel import TaskNotifyChannel
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"Tenant",
|
"Tenant",
|
||||||
@@ -24,5 +26,10 @@ __all__ = [
|
|||||||
"AlertRecord",
|
"AlertRecord",
|
||||||
"NotificationChannel",
|
"NotificationChannel",
|
||||||
"ModelPricing",
|
"ModelPricing",
|
||||||
"TenantBilling"
|
"TenantBilling",
|
||||||
|
"ScheduledTask",
|
||||||
|
"TaskLog",
|
||||||
|
"ScriptVar",
|
||||||
|
"Secret",
|
||||||
|
"TaskNotifyChannel"
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -18,11 +18,6 @@ class App(Base):
|
|||||||
# [{"code": "brainstorm", "name": "头脑风暴", "path": "/brainstorm"}, ...]
|
# [{"code": "brainstorm", "name": "头脑风暴", "path": "/brainstorm"}, ...]
|
||||||
tools = Column(Text)
|
tools = Column(Text)
|
||||||
|
|
||||||
# 配置项定义(JSON 数组)- 定义租户可配置的参数
|
|
||||||
# [{"key": "industry", "label": "行业类型", "type": "radio", "options": [...], "default": "...", "required": false}, ...]
|
|
||||||
# type: text(文本) | radio(单选) | select(下拉多选) | switch(开关)
|
|
||||||
config_schema = Column(Text)
|
|
||||||
|
|
||||||
# 是否需要企微JS-SDK
|
# 是否需要企微JS-SDK
|
||||||
require_jssdk = Column(SmallInteger, default=0) # 0-不需要 1-需要
|
require_jssdk = Column(SmallInteger, default=0) # 0-不需要 1-需要
|
||||||
|
|
||||||
|
|||||||
21
backend/app/models/notification_channel.py
Normal file
21
backend/app/models/notification_channel.py
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
"""任务通知渠道模型"""
|
||||||
|
from datetime import datetime
|
||||||
|
from sqlalchemy import Column, Integer, String, Enum, Boolean, DateTime
|
||||||
|
from ..database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class TaskNotifyChannel(Base):
|
||||||
|
"""任务通知渠道表(用于定时任务推送)"""
|
||||||
|
__tablename__ = "platform_task_notify_channels"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
tenant_id = Column(String(50), nullable=False)
|
||||||
|
channel_name = Column(String(100), nullable=False)
|
||||||
|
channel_type = Column(Enum('dingtalk_bot', 'wecom_bot'), nullable=False)
|
||||||
|
webhook_url = Column(String(500), nullable=False)
|
||||||
|
sign_secret = Column(String(200)) # 钉钉加签密钥
|
||||||
|
description = Column(String(255))
|
||||||
|
is_enabled = Column(Boolean, default=True)
|
||||||
|
|
||||||
|
created_at = Column(DateTime, default=datetime.now)
|
||||||
|
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
|
||||||
103
backend/app/models/scheduled_task.py
Normal file
103
backend/app/models/scheduled_task.py
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
"""定时任务相关模型"""
|
||||||
|
from datetime import datetime
|
||||||
|
from sqlalchemy import Column, BigInteger, Integer, String, Text, Enum, SmallInteger, TIMESTAMP, DateTime, JSON, Boolean
|
||||||
|
from ..database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class ScheduledTask(Base):
|
||||||
|
"""定时任务表"""
|
||||||
|
__tablename__ = "platform_scheduled_tasks"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
tenant_id = Column(String(50))
|
||||||
|
task_name = Column(String(100), nullable=False)
|
||||||
|
task_desc = Column(String(500))
|
||||||
|
|
||||||
|
# 调度配置
|
||||||
|
schedule_type = Column(Enum('simple', 'cron'), nullable=False, default='simple')
|
||||||
|
time_points = Column(JSON) # JSON数组 ["08:00", "12:00"]
|
||||||
|
cron_expression = Column(String(100))
|
||||||
|
timezone = Column(String(50), default='Asia/Shanghai')
|
||||||
|
|
||||||
|
# 执行类型
|
||||||
|
execution_type = Column(Enum('webhook', 'script'), nullable=False, default='script')
|
||||||
|
|
||||||
|
# Webhook配置
|
||||||
|
webhook_url = Column(String(500))
|
||||||
|
|
||||||
|
# 脚本配置
|
||||||
|
script_content = Column(Text)
|
||||||
|
script_deps = Column(Text) # 脚本依赖
|
||||||
|
|
||||||
|
# 输入参数
|
||||||
|
input_params = Column(JSON) # JSON格式
|
||||||
|
|
||||||
|
# 重试配置
|
||||||
|
retry_count = Column(Integer, default=0)
|
||||||
|
retry_interval = Column(Integer, default=60)
|
||||||
|
|
||||||
|
# 告警配置
|
||||||
|
alert_on_failure = Column(Boolean, default=False)
|
||||||
|
alert_webhook = Column(String(500))
|
||||||
|
|
||||||
|
# 通知配置
|
||||||
|
notify_channels = Column(JSON) # 通知渠道ID列表
|
||||||
|
notify_wecom_app_id = Column(Integer) # 企微应用ID
|
||||||
|
|
||||||
|
# 状态
|
||||||
|
is_enabled = Column(Boolean, default=True)
|
||||||
|
last_run_at = Column(DateTime)
|
||||||
|
last_run_status = Column(Enum('success', 'failed', 'running'))
|
||||||
|
last_run_message = Column(Text)
|
||||||
|
|
||||||
|
created_at = Column(DateTime, default=datetime.now)
|
||||||
|
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
|
||||||
|
|
||||||
|
|
||||||
|
class TaskLog(Base):
|
||||||
|
"""任务执行日志"""
|
||||||
|
__tablename__ = "platform_task_logs"
|
||||||
|
|
||||||
|
id = Column(BigInteger, primary_key=True, autoincrement=True)
|
||||||
|
task_id = Column(Integer, nullable=False)
|
||||||
|
tenant_id = Column(String(50))
|
||||||
|
trace_id = Column(String(100))
|
||||||
|
|
||||||
|
status = Column(Enum('running', 'success', 'failed'), nullable=False)
|
||||||
|
started_at = Column(DateTime, nullable=False)
|
||||||
|
finished_at = Column(DateTime)
|
||||||
|
duration_ms = Column(Integer)
|
||||||
|
|
||||||
|
output = Column(Text)
|
||||||
|
error = Column(Text)
|
||||||
|
retry_count = Column(Integer, default=0)
|
||||||
|
|
||||||
|
created_at = Column(TIMESTAMP, default=datetime.now)
|
||||||
|
|
||||||
|
|
||||||
|
class ScriptVar(Base):
|
||||||
|
"""脚本变量存储"""
|
||||||
|
__tablename__ = "platform_script_vars"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
task_id = Column(Integer, nullable=False)
|
||||||
|
tenant_id = Column(String(50))
|
||||||
|
var_key = Column(String(100), nullable=False)
|
||||||
|
var_value = Column(Text) # JSON格式
|
||||||
|
|
||||||
|
created_at = Column(TIMESTAMP, default=datetime.now)
|
||||||
|
updated_at = Column(TIMESTAMP, default=datetime.now, onupdate=datetime.now)
|
||||||
|
|
||||||
|
|
||||||
|
class Secret(Base):
|
||||||
|
"""密钥管理"""
|
||||||
|
__tablename__ = "platform_secrets"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
tenant_id = Column(String(50)) # NULL为全局
|
||||||
|
secret_key = Column(String(100), nullable=False)
|
||||||
|
secret_value = Column(Text, nullable=False)
|
||||||
|
description = Column(String(255))
|
||||||
|
|
||||||
|
created_at = Column(TIMESTAMP, default=datetime.now)
|
||||||
|
updated_at = Column(TIMESTAMP, default=datetime.now, onupdate=datetime.now)
|
||||||
@@ -23,10 +23,6 @@ class TenantApp(Base):
|
|||||||
# 功能权限
|
# 功能权限
|
||||||
allowed_tools = Column(Text) # JSON 数组
|
allowed_tools = Column(Text) # JSON 数组
|
||||||
|
|
||||||
# 自定义配置(JSON 数组)
|
|
||||||
# [{"key": "industry", "value": "medical_beauty", "remark": "医美行业"}, ...]
|
|
||||||
custom_configs = Column(Text)
|
|
||||||
|
|
||||||
status = Column(SmallInteger, default=1)
|
status = Column(SmallInteger, default=1)
|
||||||
created_at = Column(TIMESTAMP, default=datetime.now)
|
created_at = Column(TIMESTAMP, default=datetime.now)
|
||||||
updated_at = Column(TIMESTAMP, default=datetime.now, onupdate=datetime.now)
|
updated_at = Column(TIMESTAMP, default=datetime.now, onupdate=datetime.now)
|
||||||
|
|||||||
@@ -23,18 +23,6 @@ class ToolItem(BaseModel):
|
|||||||
path: str
|
path: str
|
||||||
|
|
||||||
|
|
||||||
class ConfigSchemaItem(BaseModel):
|
|
||||||
"""配置项定义"""
|
|
||||||
key: str # 配置键
|
|
||||||
label: str # 显示标签
|
|
||||||
type: str # text | radio | select | switch
|
|
||||||
options: Optional[List[str]] = None # radio/select 的选项值
|
|
||||||
option_labels: Optional[dict] = None # 选项显示名称 {"value": "显示名"}
|
|
||||||
default: Optional[str] = None # 默认值
|
|
||||||
placeholder: Optional[str] = None # 输入提示(text类型)
|
|
||||||
required: bool = False # 是否必填
|
|
||||||
|
|
||||||
|
|
||||||
class AppCreate(BaseModel):
|
class AppCreate(BaseModel):
|
||||||
"""创建应用"""
|
"""创建应用"""
|
||||||
app_code: str
|
app_code: str
|
||||||
@@ -42,7 +30,6 @@ class AppCreate(BaseModel):
|
|||||||
base_url: Optional[str] = None
|
base_url: Optional[str] = None
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
tools: Optional[List[ToolItem]] = None
|
tools: Optional[List[ToolItem]] = None
|
||||||
config_schema: Optional[List[ConfigSchemaItem]] = None
|
|
||||||
require_jssdk: bool = False
|
require_jssdk: bool = False
|
||||||
|
|
||||||
|
|
||||||
@@ -52,7 +39,6 @@ class AppUpdate(BaseModel):
|
|||||||
base_url: Optional[str] = None
|
base_url: Optional[str] = None
|
||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
tools: Optional[List[ToolItem]] = None
|
tools: Optional[List[ToolItem]] = None
|
||||||
config_schema: Optional[List[ConfigSchemaItem]] = None
|
|
||||||
require_jssdk: Optional[bool] = None
|
require_jssdk: Optional[bool] = None
|
||||||
status: Optional[int] = None
|
status: Optional[int] = None
|
||||||
|
|
||||||
@@ -133,7 +119,6 @@ async def create_app(
|
|||||||
base_url=data.base_url,
|
base_url=data.base_url,
|
||||||
description=data.description,
|
description=data.description,
|
||||||
tools=json.dumps([t.model_dump() for t in data.tools], ensure_ascii=False) if data.tools else None,
|
tools=json.dumps([t.model_dump() for t in data.tools], ensure_ascii=False) if data.tools else None,
|
||||||
config_schema=json.dumps([c.model_dump() for c in data.config_schema], ensure_ascii=False) if data.config_schema else None,
|
|
||||||
require_jssdk=1 if data.require_jssdk else 0,
|
require_jssdk=1 if data.require_jssdk else 0,
|
||||||
status=1
|
status=1
|
||||||
)
|
)
|
||||||
@@ -165,13 +150,6 @@ async def update_app(
|
|||||||
else:
|
else:
|
||||||
update_data['tools'] = None
|
update_data['tools'] = None
|
||||||
|
|
||||||
# 处理 config_schema JSON
|
|
||||||
if 'config_schema' in update_data:
|
|
||||||
if update_data['config_schema']:
|
|
||||||
update_data['config_schema'] = json.dumps([c.model_dump() if hasattr(c, 'model_dump') else c for c in update_data['config_schema']], ensure_ascii=False)
|
|
||||||
else:
|
|
||||||
update_data['config_schema'] = None
|
|
||||||
|
|
||||||
# 处理 require_jssdk
|
# 处理 require_jssdk
|
||||||
if 'require_jssdk' in update_data:
|
if 'require_jssdk' in update_data:
|
||||||
update_data['require_jssdk'] = 1 if update_data['require_jssdk'] else 0
|
update_data['require_jssdk'] = 1 if update_data['require_jssdk'] else 0
|
||||||
@@ -281,21 +259,6 @@ async def get_app_tools(
|
|||||||
return tools
|
return tools
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{app_code}/config-schema")
|
|
||||||
async def get_app_config_schema(
|
|
||||||
app_code: str,
|
|
||||||
user: User = Depends(get_current_user),
|
|
||||||
db: Session = Depends(get_db)
|
|
||||||
):
|
|
||||||
"""获取应用的配置项定义(用于租户订阅时渲染表单)"""
|
|
||||||
app = db.query(App).filter(App.app_code == app_code).first()
|
|
||||||
if not app:
|
|
||||||
raise HTTPException(status_code=404, detail="应用不存在")
|
|
||||||
|
|
||||||
config_schema = json.loads(app.config_schema) if app.config_schema else []
|
|
||||||
return config_schema
|
|
||||||
|
|
||||||
|
|
||||||
def format_app(app: App) -> dict:
|
def format_app(app: App) -> dict:
|
||||||
"""格式化应用数据"""
|
"""格式化应用数据"""
|
||||||
return {
|
return {
|
||||||
@@ -305,7 +268,6 @@ def format_app(app: App) -> dict:
|
|||||||
"base_url": app.base_url,
|
"base_url": app.base_url,
|
||||||
"description": app.description,
|
"description": app.description,
|
||||||
"tools": json.loads(app.tools) if app.tools else [],
|
"tools": json.loads(app.tools) if app.tools else [],
|
||||||
"config_schema": json.loads(app.config_schema) if app.config_schema else [],
|
|
||||||
"require_jssdk": bool(app.require_jssdk),
|
"require_jssdk": bool(app.require_jssdk),
|
||||||
"status": app.status,
|
"status": app.status,
|
||||||
"created_at": app.created_at,
|
"created_at": app.created_at,
|
||||||
|
|||||||
211
backend/app/routers/notification_channels.py
Normal file
211
backend/app/routers/notification_channels.py
Normal file
@@ -0,0 +1,211 @@
|
|||||||
|
"""通知渠道API路由"""
|
||||||
|
from typing import Optional, List
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import desc
|
||||||
|
|
||||||
|
from ..database import get_db
|
||||||
|
from ..models.notification_channel import TaskNotifyChannel
|
||||||
|
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/notification-channels", tags=["notification-channels"])
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== Schemas ====================
|
||||||
|
|
||||||
|
class ChannelCreate(BaseModel):
|
||||||
|
tenant_id: str
|
||||||
|
channel_name: str
|
||||||
|
channel_type: str # dingtalk_bot, wecom_bot
|
||||||
|
webhook_url: str
|
||||||
|
sign_secret: Optional[str] = None # 钉钉加签密钥
|
||||||
|
description: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class ChannelUpdate(BaseModel):
|
||||||
|
channel_name: Optional[str] = None
|
||||||
|
channel_type: Optional[str] = None
|
||||||
|
webhook_url: Optional[str] = None
|
||||||
|
sign_secret: Optional[str] = None
|
||||||
|
description: Optional[str] = None
|
||||||
|
is_enabled: Optional[bool] = None
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== CRUD ====================
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
async def list_channels(
|
||||||
|
tenant_id: Optional[str] = None,
|
||||||
|
channel_type: Optional[str] = None,
|
||||||
|
is_enabled: Optional[bool] = None,
|
||||||
|
page: int = Query(1, ge=1),
|
||||||
|
size: int = Query(50, ge=1, le=100),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""获取通知渠道列表"""
|
||||||
|
query = db.query(TaskNotifyChannel)
|
||||||
|
|
||||||
|
if tenant_id:
|
||||||
|
query = query.filter(TaskNotifyChannel.tenant_id == tenant_id)
|
||||||
|
if channel_type:
|
||||||
|
query = query.filter(TaskNotifyChannel.channel_type == channel_type)
|
||||||
|
if is_enabled is not None:
|
||||||
|
query = query.filter(TaskNotifyChannel.is_enabled == is_enabled)
|
||||||
|
|
||||||
|
total = query.count()
|
||||||
|
items = query.order_by(desc(TaskNotifyChannel.created_at)).offset((page - 1) * size).limit(size).all()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"total": total,
|
||||||
|
"items": [format_channel(c) for c in items]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{channel_id}")
|
||||||
|
async def get_channel(channel_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""获取渠道详情"""
|
||||||
|
channel = db.query(TaskNotifyChannel).filter(TaskNotifyChannel.id == channel_id).first()
|
||||||
|
if not channel:
|
||||||
|
raise HTTPException(status_code=404, detail="渠道不存在")
|
||||||
|
return format_channel(channel)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("")
|
||||||
|
async def create_channel(data: ChannelCreate, db: Session = Depends(get_db)):
|
||||||
|
"""创建通知渠道"""
|
||||||
|
channel = TaskNotifyChannel(
|
||||||
|
tenant_id=data.tenant_id,
|
||||||
|
channel_name=data.channel_name,
|
||||||
|
channel_type=data.channel_type,
|
||||||
|
webhook_url=data.webhook_url,
|
||||||
|
sign_secret=data.sign_secret,
|
||||||
|
description=data.description,
|
||||||
|
is_enabled=True
|
||||||
|
)
|
||||||
|
|
||||||
|
db.add(channel)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(channel)
|
||||||
|
|
||||||
|
return {"success": True, "id": channel.id}
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/{channel_id}")
|
||||||
|
async def update_channel(channel_id: int, data: ChannelUpdate, db: Session = Depends(get_db)):
|
||||||
|
"""更新通知渠道"""
|
||||||
|
channel = db.query(TaskNotifyChannel).filter(TaskNotifyChannel.id == channel_id).first()
|
||||||
|
if not channel:
|
||||||
|
raise HTTPException(status_code=404, detail="渠道不存在")
|
||||||
|
|
||||||
|
if data.channel_name is not None:
|
||||||
|
channel.channel_name = data.channel_name
|
||||||
|
if data.channel_type is not None:
|
||||||
|
channel.channel_type = data.channel_type
|
||||||
|
if data.webhook_url is not None:
|
||||||
|
channel.webhook_url = data.webhook_url
|
||||||
|
if data.sign_secret is not None:
|
||||||
|
channel.sign_secret = data.sign_secret
|
||||||
|
if data.description is not None:
|
||||||
|
channel.description = data.description
|
||||||
|
if data.is_enabled is not None:
|
||||||
|
channel.is_enabled = data.is_enabled
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{channel_id}")
|
||||||
|
async def delete_channel(channel_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""删除通知渠道"""
|
||||||
|
channel = db.query(TaskNotifyChannel).filter(TaskNotifyChannel.id == channel_id).first()
|
||||||
|
if not channel:
|
||||||
|
raise HTTPException(status_code=404, detail="渠道不存在")
|
||||||
|
|
||||||
|
db.delete(channel)
|
||||||
|
db.commit()
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/{channel_id}/test")
|
||||||
|
async def test_channel(channel_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""测试通知渠道"""
|
||||||
|
import httpx
|
||||||
|
import time
|
||||||
|
import hmac
|
||||||
|
import hashlib
|
||||||
|
import base64
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
|
channel = db.query(TaskNotifyChannel).filter(TaskNotifyChannel.id == channel_id).first()
|
||||||
|
if not channel:
|
||||||
|
raise HTTPException(status_code=404, detail="渠道不存在")
|
||||||
|
|
||||||
|
test_content = f"**测试消息**\n\n渠道名称: {channel.channel_name}\n发送时间: 测试中..."
|
||||||
|
|
||||||
|
try:
|
||||||
|
url = channel.webhook_url
|
||||||
|
|
||||||
|
if channel.channel_type == 'dingtalk_bot':
|
||||||
|
# 钉钉加签
|
||||||
|
if channel.sign_secret:
|
||||||
|
timestamp = str(round(time.time() * 1000))
|
||||||
|
string_to_sign = f'{timestamp}\n{channel.sign_secret}'
|
||||||
|
hmac_code = hmac.new(
|
||||||
|
channel.sign_secret.encode('utf-8'),
|
||||||
|
string_to_sign.encode('utf-8'),
|
||||||
|
digestmod=hashlib.sha256
|
||||||
|
).digest()
|
||||||
|
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
|
||||||
|
|
||||||
|
# 拼接签名参数
|
||||||
|
if '?' in url:
|
||||||
|
url = f"{url}×tamp={timestamp}&sign={sign}"
|
||||||
|
else:
|
||||||
|
url = f"{url}?timestamp={timestamp}&sign={sign}"
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
"msgtype": "markdown",
|
||||||
|
"markdown": {
|
||||||
|
"title": "渠道测试",
|
||||||
|
"text": test_content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else: # wecom_bot
|
||||||
|
payload = {
|
||||||
|
"msgtype": "markdown",
|
||||||
|
"markdown": {
|
||||||
|
"content": test_content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async with httpx.AsyncClient(timeout=10) as client:
|
||||||
|
response = await client.post(url, json=payload)
|
||||||
|
result = response.json()
|
||||||
|
|
||||||
|
# 钉钉返回 errcode=0,企微返回 errcode=0
|
||||||
|
if result.get('errcode') == 0:
|
||||||
|
return {"success": True, "message": "发送成功"}
|
||||||
|
else:
|
||||||
|
return {"success": False, "message": f"发送失败: {result}"}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return {"success": False, "message": f"发送失败: {str(e)}"}
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== Helpers ====================
|
||||||
|
|
||||||
|
def format_channel(channel: TaskNotifyChannel) -> dict:
|
||||||
|
"""格式化渠道数据"""
|
||||||
|
return {
|
||||||
|
"id": channel.id,
|
||||||
|
"tenant_id": channel.tenant_id,
|
||||||
|
"channel_name": channel.channel_name,
|
||||||
|
"channel_type": channel.channel_type,
|
||||||
|
"webhook_url": channel.webhook_url,
|
||||||
|
"sign_secret": channel.sign_secret,
|
||||||
|
"description": channel.description,
|
||||||
|
"is_enabled": channel.is_enabled,
|
||||||
|
"created_at": channel.created_at,
|
||||||
|
"updated_at": channel.updated_at
|
||||||
|
}
|
||||||
559
backend/app/routers/tasks.py
Normal file
559
backend/app/routers/tasks.py
Normal file
@@ -0,0 +1,559 @@
|
|||||||
|
"""定时任务API路由"""
|
||||||
|
import json
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional, List
|
||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query, BackgroundTasks
|
||||||
|
from pydantic import BaseModel
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import desc
|
||||||
|
|
||||||
|
from ..database import get_db
|
||||||
|
from ..models.scheduled_task import ScheduledTask, TaskLog, Secret
|
||||||
|
from ..services.scheduler import scheduler_service
|
||||||
|
from ..services.script_executor import ScriptExecutor
|
||||||
|
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/scheduled-tasks", tags=["scheduled-tasks"])
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== Schemas ====================
|
||||||
|
|
||||||
|
class TaskCreate(BaseModel):
|
||||||
|
tenant_id: Optional[str] = None
|
||||||
|
task_name: str
|
||||||
|
task_desc: Optional[str] = None
|
||||||
|
execution_type: str = 'script'
|
||||||
|
schedule_type: str = 'simple'
|
||||||
|
time_points: Optional[List[str]] = None
|
||||||
|
cron_expression: Optional[str] = None
|
||||||
|
webhook_url: Optional[str] = None
|
||||||
|
script_content: Optional[str] = None
|
||||||
|
input_params: Optional[dict] = None
|
||||||
|
retry_count: Optional[int] = 0
|
||||||
|
retry_interval: Optional[int] = 60
|
||||||
|
alert_on_failure: Optional[bool] = False
|
||||||
|
alert_webhook: Optional[str] = None
|
||||||
|
notify_channels: Optional[List[int]] = None # 通知渠道ID列表
|
||||||
|
notify_wecom_app_id: Optional[int] = None # 企微应用ID
|
||||||
|
|
||||||
|
|
||||||
|
class TaskUpdate(BaseModel):
|
||||||
|
tenant_id: Optional[str] = None
|
||||||
|
task_name: Optional[str] = None
|
||||||
|
task_desc: Optional[str] = None
|
||||||
|
execution_type: Optional[str] = None
|
||||||
|
schedule_type: Optional[str] = None
|
||||||
|
time_points: Optional[List[str]] = None
|
||||||
|
cron_expression: Optional[str] = None
|
||||||
|
webhook_url: Optional[str] = None
|
||||||
|
script_content: Optional[str] = None
|
||||||
|
input_params: Optional[dict] = None
|
||||||
|
retry_count: Optional[int] = None
|
||||||
|
retry_interval: Optional[int] = None
|
||||||
|
alert_on_failure: Optional[bool] = None
|
||||||
|
alert_webhook: Optional[str] = None
|
||||||
|
is_enabled: Optional[bool] = None
|
||||||
|
notify_channels: Optional[List[int]] = None
|
||||||
|
notify_wecom_app_id: Optional[int] = None
|
||||||
|
|
||||||
|
|
||||||
|
class SecretCreate(BaseModel):
|
||||||
|
tenant_id: Optional[str] = None
|
||||||
|
secret_key: str
|
||||||
|
secret_value: str
|
||||||
|
description: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class SecretUpdate(BaseModel):
|
||||||
|
secret_value: Optional[str] = None
|
||||||
|
description: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class TestScriptRequest(BaseModel):
|
||||||
|
script_content: str
|
||||||
|
tenant_id: Optional[str] = None
|
||||||
|
params: Optional[dict] = None
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== Static Routes (must be before dynamic routes) ====================
|
||||||
|
|
||||||
|
@router.get("/sdk-docs")
|
||||||
|
async def get_sdk_docs():
|
||||||
|
"""获取SDK文档"""
|
||||||
|
return {
|
||||||
|
"functions": [
|
||||||
|
{
|
||||||
|
"name": "log",
|
||||||
|
"signature": "log(message: str, level: str = 'INFO')",
|
||||||
|
"description": "记录日志",
|
||||||
|
"example": "log('处理完成', 'INFO')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "print",
|
||||||
|
"signature": "print(*args)",
|
||||||
|
"description": "打印输出",
|
||||||
|
"example": "print('Hello', 'World')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "ai",
|
||||||
|
"signature": "ai(prompt: str, system: str = None, model: str = None, temperature: float = 0.7)",
|
||||||
|
"description": "调用AI模型",
|
||||||
|
"example": "result = ai('生成一段问候语', system='你是友善的助手')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "dingtalk",
|
||||||
|
"signature": "dingtalk(webhook: str, content: str, title: str = None, at_all: bool = False)",
|
||||||
|
"description": "发送钉钉消息",
|
||||||
|
"example": "dingtalk(webhook_url, '# 标题\\n内容')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "wecom",
|
||||||
|
"signature": "wecom(webhook: str, content: str, msg_type: str = 'markdown')",
|
||||||
|
"description": "发送企微消息",
|
||||||
|
"example": "wecom(webhook_url, '消息内容')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "http_get",
|
||||||
|
"signature": "http_get(url: str, headers: dict = None, params: dict = None)",
|
||||||
|
"description": "发起GET请求",
|
||||||
|
"example": "resp = http_get('https://api.example.com/data')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "http_post",
|
||||||
|
"signature": "http_post(url: str, data: any = None, headers: dict = None)",
|
||||||
|
"description": "发起POST请求",
|
||||||
|
"example": "resp = http_post('https://api.example.com/submit', {'key': 'value'})"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "db_query",
|
||||||
|
"signature": "db_query(sql: str, params: dict = None)",
|
||||||
|
"description": "执行只读SQL查询",
|
||||||
|
"example": "rows = db_query('SELECT * FROM users WHERE status = :status', {'status': 1})"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "get_var",
|
||||||
|
"signature": "get_var(key: str, default: any = None)",
|
||||||
|
"description": "获取持久化变量",
|
||||||
|
"example": "counter = get_var('counter', 0)"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "set_var",
|
||||||
|
"signature": "set_var(key: str, value: any)",
|
||||||
|
"description": "设置持久化变量",
|
||||||
|
"example": "set_var('counter', counter + 1)"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "del_var",
|
||||||
|
"signature": "del_var(key: str)",
|
||||||
|
"description": "删除持久化变量",
|
||||||
|
"example": "del_var('temp_data')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "get_param",
|
||||||
|
"signature": "get_param(key: str, default: any = None)",
|
||||||
|
"description": "获取任务参数",
|
||||||
|
"example": "prompt = get_param('prompt', '默认提示词')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "get_params",
|
||||||
|
"signature": "get_params()",
|
||||||
|
"description": "获取所有任务参数",
|
||||||
|
"example": "params = get_params()"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "get_tenants",
|
||||||
|
"signature": "get_tenants(app_code: str = None)",
|
||||||
|
"description": "获取租户列表",
|
||||||
|
"example": "tenants = get_tenants('notification-service')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "get_tenant_config",
|
||||||
|
"signature": "get_tenant_config(tenant_id: str, app_code: str, key: str = None)",
|
||||||
|
"description": "获取租户的应用配置",
|
||||||
|
"example": "webhook = get_tenant_config('tenant1', 'notification-service', 'dingtalk_webhook')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "get_all_tenant_configs",
|
||||||
|
"signature": "get_all_tenant_configs(app_code: str)",
|
||||||
|
"description": "获取所有租户的应用配置",
|
||||||
|
"example": "configs = get_all_tenant_configs('notification-service')"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "get_secret",
|
||||||
|
"signature": "get_secret(key: str)",
|
||||||
|
"description": "获取密钥(优先租户级)",
|
||||||
|
"example": "api_key = get_secret('api_key')"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"variables": [
|
||||||
|
{"name": "task_id", "description": "当前任务ID"},
|
||||||
|
{"name": "tenant_id", "description": "当前租户ID"},
|
||||||
|
{"name": "trace_id", "description": "当前执行追踪ID"}
|
||||||
|
],
|
||||||
|
"libraries": [
|
||||||
|
{"name": "json", "description": "JSON处理"},
|
||||||
|
{"name": "re", "description": "正则表达式"},
|
||||||
|
{"name": "math", "description": "数学函数"},
|
||||||
|
{"name": "random", "description": "随机数"},
|
||||||
|
{"name": "hashlib", "description": "哈希函数"},
|
||||||
|
{"name": "base64", "description": "Base64编解码"},
|
||||||
|
{"name": "datetime", "description": "日期时间处理"},
|
||||||
|
{"name": "timedelta", "description": "时间差"},
|
||||||
|
{"name": "urlencode/quote/unquote", "description": "URL编码"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/test-script")
|
||||||
|
async def test_script(data: TestScriptRequest, db: Session = Depends(get_db)):
|
||||||
|
"""测试脚本执行"""
|
||||||
|
executor = ScriptExecutor(db)
|
||||||
|
result = executor.test_script(
|
||||||
|
script_content=data.script_content,
|
||||||
|
task_id=0,
|
||||||
|
tenant_id=data.tenant_id,
|
||||||
|
params=data.params
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/secrets")
|
||||||
|
async def list_secrets(
|
||||||
|
tenant_id: Optional[str] = None,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""获取密钥列表"""
|
||||||
|
query = db.query(Secret)
|
||||||
|
if tenant_id:
|
||||||
|
query = query.filter(Secret.tenant_id == tenant_id)
|
||||||
|
|
||||||
|
items = query.order_by(desc(Secret.created_at)).all()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"id": s.id,
|
||||||
|
"tenant_id": s.tenant_id,
|
||||||
|
"secret_key": s.secret_key,
|
||||||
|
"description": s.description,
|
||||||
|
"created_at": s.created_at,
|
||||||
|
"updated_at": s.updated_at
|
||||||
|
}
|
||||||
|
for s in items
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/secrets")
|
||||||
|
async def create_secret(data: SecretCreate, db: Session = Depends(get_db)):
|
||||||
|
"""创建密钥"""
|
||||||
|
secret = Secret(
|
||||||
|
tenant_id=data.tenant_id,
|
||||||
|
secret_key=data.secret_key,
|
||||||
|
secret_value=data.secret_value,
|
||||||
|
description=data.description
|
||||||
|
)
|
||||||
|
db.add(secret)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(secret)
|
||||||
|
|
||||||
|
return {"success": True, "id": secret.id}
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== Task CRUD ====================
|
||||||
|
|
||||||
|
@router.get("")
|
||||||
|
async def list_tasks(
|
||||||
|
tenant_id: Optional[str] = None,
|
||||||
|
status: Optional[int] = None,
|
||||||
|
page: int = Query(1, ge=1),
|
||||||
|
size: int = Query(20, ge=1, le=100),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""获取任务列表"""
|
||||||
|
query = db.query(ScheduledTask)
|
||||||
|
|
||||||
|
if tenant_id:
|
||||||
|
query = query.filter(ScheduledTask.tenant_id == tenant_id)
|
||||||
|
if status is not None:
|
||||||
|
is_enabled = status == 1
|
||||||
|
query = query.filter(ScheduledTask.is_enabled == is_enabled)
|
||||||
|
|
||||||
|
total = query.count()
|
||||||
|
items = query.order_by(desc(ScheduledTask.created_at)).offset((page - 1) * size).limit(size).all()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"total": total,
|
||||||
|
"items": [format_task(t) for t in items]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{task_id}")
|
||||||
|
async def get_task(task_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""获取任务详情"""
|
||||||
|
task = db.query(ScheduledTask).filter(ScheduledTask.id == task_id).first()
|
||||||
|
if not task:
|
||||||
|
raise HTTPException(status_code=404, detail="任务不存在")
|
||||||
|
return format_task(task, include_content=True)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("")
|
||||||
|
async def create_task(data: TaskCreate, db: Session = Depends(get_db)):
|
||||||
|
"""创建任务"""
|
||||||
|
task = ScheduledTask(
|
||||||
|
tenant_id=data.tenant_id,
|
||||||
|
task_name=data.task_name,
|
||||||
|
task_desc=data.task_desc,
|
||||||
|
execution_type=data.execution_type,
|
||||||
|
schedule_type=data.schedule_type,
|
||||||
|
time_points=data.time_points,
|
||||||
|
cron_expression=data.cron_expression,
|
||||||
|
webhook_url=data.webhook_url,
|
||||||
|
script_content=data.script_content,
|
||||||
|
input_params=data.input_params,
|
||||||
|
retry_count=data.retry_count,
|
||||||
|
retry_interval=data.retry_interval,
|
||||||
|
alert_on_failure=data.alert_on_failure,
|
||||||
|
alert_webhook=data.alert_webhook,
|
||||||
|
notify_channels=data.notify_channels,
|
||||||
|
notify_wecom_app_id=data.notify_wecom_app_id,
|
||||||
|
is_enabled=True
|
||||||
|
)
|
||||||
|
|
||||||
|
db.add(task)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(task)
|
||||||
|
|
||||||
|
# 添加到调度器
|
||||||
|
scheduler_service.add_task(task.id)
|
||||||
|
|
||||||
|
return {"success": True, "id": task.id}
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/{task_id}")
|
||||||
|
async def update_task(task_id: int, data: TaskUpdate, db: Session = Depends(get_db)):
|
||||||
|
"""更新任务"""
|
||||||
|
task = db.query(ScheduledTask).filter(ScheduledTask.id == task_id).first()
|
||||||
|
if not task:
|
||||||
|
raise HTTPException(status_code=404, detail="任务不存在")
|
||||||
|
|
||||||
|
# 更新字段
|
||||||
|
if data.tenant_id is not None:
|
||||||
|
task.tenant_id = data.tenant_id
|
||||||
|
if data.task_name is not None:
|
||||||
|
task.task_name = data.task_name
|
||||||
|
if data.task_desc is not None:
|
||||||
|
task.task_desc = data.task_desc
|
||||||
|
if data.execution_type is not None:
|
||||||
|
task.execution_type = data.execution_type
|
||||||
|
if data.schedule_type is not None:
|
||||||
|
task.schedule_type = data.schedule_type
|
||||||
|
if data.time_points is not None:
|
||||||
|
task.time_points = data.time_points
|
||||||
|
if data.cron_expression is not None:
|
||||||
|
task.cron_expression = data.cron_expression
|
||||||
|
if data.webhook_url is not None:
|
||||||
|
task.webhook_url = data.webhook_url
|
||||||
|
if data.script_content is not None:
|
||||||
|
task.script_content = data.script_content
|
||||||
|
if data.input_params is not None:
|
||||||
|
task.input_params = data.input_params
|
||||||
|
if data.retry_count is not None:
|
||||||
|
task.retry_count = data.retry_count
|
||||||
|
if data.retry_interval is not None:
|
||||||
|
task.retry_interval = data.retry_interval
|
||||||
|
if data.alert_on_failure is not None:
|
||||||
|
task.alert_on_failure = data.alert_on_failure
|
||||||
|
if data.alert_webhook is not None:
|
||||||
|
task.alert_webhook = data.alert_webhook
|
||||||
|
if data.notify_channels is not None:
|
||||||
|
task.notify_channels = data.notify_channels
|
||||||
|
if data.notify_wecom_app_id is not None:
|
||||||
|
task.notify_wecom_app_id = data.notify_wecom_app_id
|
||||||
|
if data.is_enabled is not None:
|
||||||
|
task.is_enabled = data.is_enabled
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# 更新调度器
|
||||||
|
if task.is_enabled:
|
||||||
|
scheduler_service.add_task(task.id)
|
||||||
|
else:
|
||||||
|
scheduler_service.remove_task(task.id)
|
||||||
|
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/{task_id}")
|
||||||
|
async def delete_task(task_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""删除任务"""
|
||||||
|
task = db.query(ScheduledTask).filter(ScheduledTask.id == task_id).first()
|
||||||
|
if not task:
|
||||||
|
raise HTTPException(status_code=404, detail="任务不存在")
|
||||||
|
|
||||||
|
# 从调度器移除
|
||||||
|
scheduler_service.remove_task(task_id)
|
||||||
|
|
||||||
|
# 删除相关日志
|
||||||
|
db.query(TaskLog).filter(TaskLog.task_id == task_id).delete()
|
||||||
|
|
||||||
|
db.delete(task)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/{task_id}/toggle")
|
||||||
|
async def toggle_task(task_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""启用/禁用任务"""
|
||||||
|
task = db.query(ScheduledTask).filter(ScheduledTask.id == task_id).first()
|
||||||
|
if not task:
|
||||||
|
raise HTTPException(status_code=404, detail="任务不存在")
|
||||||
|
|
||||||
|
task.is_enabled = not task.is_enabled
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
if task.is_enabled:
|
||||||
|
scheduler_service.add_task(task.id)
|
||||||
|
else:
|
||||||
|
scheduler_service.remove_task(task.id)
|
||||||
|
|
||||||
|
return {"success": True, "status": 1 if task.is_enabled else 0}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/{task_id}/run")
|
||||||
|
async def run_task(task_id: int, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
|
||||||
|
"""立即执行任务"""
|
||||||
|
task = db.query(ScheduledTask).filter(ScheduledTask.id == task_id).first()
|
||||||
|
if not task:
|
||||||
|
raise HTTPException(status_code=404, detail="任务不存在")
|
||||||
|
|
||||||
|
result = await scheduler_service.run_task_now(task_id)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== Task Logs ====================
|
||||||
|
|
||||||
|
@router.get("/{task_id}/logs")
|
||||||
|
async def get_task_logs(
|
||||||
|
task_id: int,
|
||||||
|
status: Optional[str] = None,
|
||||||
|
page: int = Query(1, ge=1),
|
||||||
|
size: int = Query(20, ge=1, le=100),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""获取任务执行日志"""
|
||||||
|
query = db.query(TaskLog).filter(TaskLog.task_id == task_id)
|
||||||
|
|
||||||
|
if status:
|
||||||
|
query = query.filter(TaskLog.status == status)
|
||||||
|
|
||||||
|
total = query.count()
|
||||||
|
items = query.order_by(desc(TaskLog.started_at)).offset((page - 1) * size).limit(size).all()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"total": total,
|
||||||
|
"items": [format_log(log) for log in items]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== Secrets (dynamic routes) ====================
|
||||||
|
|
||||||
|
@router.put("/secrets/{secret_id}")
|
||||||
|
async def update_secret(secret_id: int, data: SecretUpdate, db: Session = Depends(get_db)):
|
||||||
|
"""更新密钥"""
|
||||||
|
secret = db.query(Secret).filter(Secret.id == secret_id).first()
|
||||||
|
if not secret:
|
||||||
|
raise HTTPException(status_code=404, detail="密钥不存在")
|
||||||
|
|
||||||
|
if data.secret_value is not None:
|
||||||
|
secret.secret_value = data.secret_value
|
||||||
|
if data.description is not None:
|
||||||
|
secret.description = data.description
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/secrets/{secret_id}")
|
||||||
|
async def delete_secret(secret_id: int, db: Session = Depends(get_db)):
|
||||||
|
"""删除密钥"""
|
||||||
|
secret = db.query(Secret).filter(Secret.id == secret_id).first()
|
||||||
|
if not secret:
|
||||||
|
raise HTTPException(status_code=404, detail="密钥不存在")
|
||||||
|
|
||||||
|
db.delete(secret)
|
||||||
|
db.commit()
|
||||||
|
return {"success": True}
|
||||||
|
|
||||||
|
|
||||||
|
# ==================== Helpers ====================
|
||||||
|
|
||||||
|
def format_task(task: ScheduledTask, include_content: bool = False) -> dict:
|
||||||
|
"""格式化任务数据"""
|
||||||
|
time_points = task.time_points
|
||||||
|
if isinstance(time_points, str):
|
||||||
|
try:
|
||||||
|
time_points = json.loads(time_points)
|
||||||
|
except:
|
||||||
|
time_points = []
|
||||||
|
|
||||||
|
# 处理 notify_channels
|
||||||
|
notify_channels = task.notify_channels
|
||||||
|
if isinstance(notify_channels, str):
|
||||||
|
try:
|
||||||
|
notify_channels = json.loads(notify_channels)
|
||||||
|
except:
|
||||||
|
notify_channels = []
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"id": task.id,
|
||||||
|
"tenant_id": task.tenant_id,
|
||||||
|
"task_name": task.task_name,
|
||||||
|
"task_type": task.execution_type, # 前端使用 task_type
|
||||||
|
"schedule_type": task.schedule_type,
|
||||||
|
"time_points": time_points or [],
|
||||||
|
"cron_expression": task.cron_expression,
|
||||||
|
"status": 1 if task.is_enabled else 0, # 前端使用 status
|
||||||
|
"last_run_at": task.last_run_at,
|
||||||
|
"last_run_status": task.last_run_status,
|
||||||
|
"retry_count": task.retry_count,
|
||||||
|
"retry_interval": task.retry_interval,
|
||||||
|
"alert_on_failure": bool(task.alert_on_failure),
|
||||||
|
"alert_webhook": task.alert_webhook,
|
||||||
|
"notify_channels": notify_channels or [],
|
||||||
|
"notify_wecom_app_id": task.notify_wecom_app_id,
|
||||||
|
"created_at": task.created_at,
|
||||||
|
"updated_at": task.updated_at
|
||||||
|
}
|
||||||
|
|
||||||
|
if include_content:
|
||||||
|
data["webhook_url"] = task.webhook_url
|
||||||
|
data["script_content"] = task.script_content
|
||||||
|
input_params = task.input_params
|
||||||
|
if isinstance(input_params, str):
|
||||||
|
try:
|
||||||
|
input_params = json.loads(input_params)
|
||||||
|
except:
|
||||||
|
input_params = None
|
||||||
|
data["input_params"] = input_params
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def format_log(log: TaskLog) -> dict:
|
||||||
|
"""格式化日志数据"""
|
||||||
|
return {
|
||||||
|
"id": log.id,
|
||||||
|
"task_id": log.task_id,
|
||||||
|
"tenant_id": log.tenant_id,
|
||||||
|
"trace_id": log.trace_id,
|
||||||
|
"status": log.status,
|
||||||
|
"started_at": log.started_at,
|
||||||
|
"finished_at": log.finished_at,
|
||||||
|
"duration_ms": log.duration_ms,
|
||||||
|
"output": log.output,
|
||||||
|
"error": log.error,
|
||||||
|
"retry_count": log.retry_count,
|
||||||
|
"created_at": log.created_at
|
||||||
|
}
|
||||||
@@ -17,13 +17,6 @@ router = APIRouter(prefix="/tenant-apps", tags=["应用配置"])
|
|||||||
|
|
||||||
# Schemas
|
# Schemas
|
||||||
|
|
||||||
class CustomConfigItem(BaseModel):
|
|
||||||
"""自定义配置项"""
|
|
||||||
key: str # 配置键
|
|
||||||
value: str # 配置值
|
|
||||||
remark: Optional[str] = None # 备注说明
|
|
||||||
|
|
||||||
|
|
||||||
class TenantAppCreate(BaseModel):
|
class TenantAppCreate(BaseModel):
|
||||||
tenant_id: str
|
tenant_id: str
|
||||||
app_code: str = "tools"
|
app_code: str = "tools"
|
||||||
@@ -32,7 +25,6 @@ class TenantAppCreate(BaseModel):
|
|||||||
access_token: Optional[str] = None # 如果不传则自动生成
|
access_token: Optional[str] = None # 如果不传则自动生成
|
||||||
allowed_origins: Optional[List[str]] = None
|
allowed_origins: Optional[List[str]] = None
|
||||||
allowed_tools: Optional[List[str]] = None
|
allowed_tools: Optional[List[str]] = None
|
||||||
custom_configs: Optional[List[CustomConfigItem]] = None # 自定义配置
|
|
||||||
|
|
||||||
|
|
||||||
class TenantAppUpdate(BaseModel):
|
class TenantAppUpdate(BaseModel):
|
||||||
@@ -41,7 +33,6 @@ class TenantAppUpdate(BaseModel):
|
|||||||
access_token: Optional[str] = None
|
access_token: Optional[str] = None
|
||||||
allowed_origins: Optional[List[str]] = None
|
allowed_origins: Optional[List[str]] = None
|
||||||
allowed_tools: Optional[List[str]] = None
|
allowed_tools: Optional[List[str]] = None
|
||||||
custom_configs: Optional[List[CustomConfigItem]] = None # 自定义配置
|
|
||||||
status: Optional[int] = None
|
status: Optional[int] = None
|
||||||
|
|
||||||
|
|
||||||
@@ -120,7 +111,6 @@ async def create_tenant_app(
|
|||||||
access_token=access_token,
|
access_token=access_token,
|
||||||
allowed_origins=json.dumps(data.allowed_origins) if data.allowed_origins else None,
|
allowed_origins=json.dumps(data.allowed_origins) if data.allowed_origins else None,
|
||||||
allowed_tools=json.dumps(data.allowed_tools) if data.allowed_tools else None,
|
allowed_tools=json.dumps(data.allowed_tools) if data.allowed_tools else None,
|
||||||
custom_configs=json.dumps([c.model_dump() for c in data.custom_configs], ensure_ascii=False) if data.custom_configs else None,
|
|
||||||
status=1
|
status=1
|
||||||
)
|
)
|
||||||
db.add(app)
|
db.add(app)
|
||||||
@@ -149,14 +139,6 @@ async def update_tenant_app(
|
|||||||
update_data['allowed_origins'] = json.dumps(update_data['allowed_origins']) if update_data['allowed_origins'] else None
|
update_data['allowed_origins'] = json.dumps(update_data['allowed_origins']) if update_data['allowed_origins'] else None
|
||||||
if 'allowed_tools' in update_data:
|
if 'allowed_tools' in update_data:
|
||||||
update_data['allowed_tools'] = json.dumps(update_data['allowed_tools']) if update_data['allowed_tools'] else None
|
update_data['allowed_tools'] = json.dumps(update_data['allowed_tools']) if update_data['allowed_tools'] else None
|
||||||
if 'custom_configs' in update_data:
|
|
||||||
if update_data['custom_configs']:
|
|
||||||
update_data['custom_configs'] = json.dumps(
|
|
||||||
[c.model_dump() if hasattr(c, 'model_dump') else c for c in update_data['custom_configs']],
|
|
||||||
ensure_ascii=False
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
update_data['custom_configs'] = None
|
|
||||||
|
|
||||||
for key, value in update_data.items():
|
for key, value in update_data.items():
|
||||||
setattr(app, key, value)
|
setattr(app, key, value)
|
||||||
@@ -182,27 +164,6 @@ async def delete_tenant_app(
|
|||||||
return {"success": True}
|
return {"success": True}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{app_id}/token")
|
|
||||||
async def get_token(
|
|
||||||
app_id: int,
|
|
||||||
user: User = Depends(require_operator),
|
|
||||||
db: Session = Depends(get_db)
|
|
||||||
):
|
|
||||||
"""获取真实的 access_token(仅管理员可用)"""
|
|
||||||
app = db.query(TenantApp).filter(TenantApp.id == app_id).first()
|
|
||||||
if not app:
|
|
||||||
raise HTTPException(status_code=404, detail="应用配置不存在")
|
|
||||||
|
|
||||||
# 获取应用的 base_url
|
|
||||||
app_info = db.query(App).filter(App.app_code == app.app_code).first()
|
|
||||||
base_url = app_info.base_url if app_info else ""
|
|
||||||
|
|
||||||
return {
|
|
||||||
"access_token": app.access_token,
|
|
||||||
"base_url": base_url
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@router.post("/{app_id}/regenerate-token")
|
@router.post("/{app_id}/regenerate-token")
|
||||||
async def regenerate_token(
|
async def regenerate_token(
|
||||||
app_id: int,
|
app_id: int,
|
||||||
@@ -246,7 +207,6 @@ def format_tenant_app(app: TenantApp, mask_secret: bool = True, db: Session = No
|
|||||||
"access_token": "******" if mask_secret and app.access_token else app.access_token,
|
"access_token": "******" if mask_secret and app.access_token else app.access_token,
|
||||||
"allowed_origins": json.loads(app.allowed_origins) if app.allowed_origins else [],
|
"allowed_origins": json.loads(app.allowed_origins) if app.allowed_origins else [],
|
||||||
"allowed_tools": json.loads(app.allowed_tools) if app.allowed_tools else [],
|
"allowed_tools": json.loads(app.allowed_tools) if app.allowed_tools else [],
|
||||||
"custom_configs": json.loads(app.custom_configs) if app.custom_configs else [],
|
|
||||||
"status": app.status,
|
"status": app.status,
|
||||||
"created_at": app.created_at,
|
"created_at": app.created_at,
|
||||||
"updated_at": app.updated_at
|
"updated_at": app.updated_at
|
||||||
|
|||||||
609
backend/app/services/scheduler.py
Normal file
609
backend/app/services/scheduler.py
Normal file
@@ -0,0 +1,609 @@
|
|||||||
|
"""定时任务调度服务"""
|
||||||
|
import json
|
||||||
|
import httpx
|
||||||
|
import asyncio
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||||
|
from apscheduler.triggers.cron import CronTrigger
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from ..database import SessionLocal
|
||||||
|
from ..models.scheduled_task import ScheduledTask, TaskLog
|
||||||
|
from ..models.notification_channel import TaskNotifyChannel
|
||||||
|
from .script_executor import ScriptExecutor
|
||||||
|
|
||||||
|
|
||||||
|
class SchedulerService:
|
||||||
|
"""调度服务 - 管理定时任务的调度和执行"""
|
||||||
|
|
||||||
|
_instance: Optional['SchedulerService'] = None
|
||||||
|
_scheduler: Optional[AsyncIOScheduler] = None
|
||||||
|
|
||||||
|
def __new__(cls):
|
||||||
|
if cls._instance is None:
|
||||||
|
cls._instance = super().__new__(cls)
|
||||||
|
return cls._instance
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
if self._scheduler is None:
|
||||||
|
self._scheduler = AsyncIOScheduler(timezone='Asia/Shanghai')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def scheduler(self) -> AsyncIOScheduler:
|
||||||
|
return self._scheduler
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
"""启动调度器并加载所有任务"""
|
||||||
|
if not self._scheduler.running:
|
||||||
|
self._scheduler.start()
|
||||||
|
self._load_all_tasks()
|
||||||
|
print("调度器已启动")
|
||||||
|
|
||||||
|
def shutdown(self):
|
||||||
|
"""关闭调度器"""
|
||||||
|
if self._scheduler.running:
|
||||||
|
self._scheduler.shutdown()
|
||||||
|
print("调度器已关闭")
|
||||||
|
|
||||||
|
def _load_all_tasks(self):
|
||||||
|
"""从数据库加载所有启用的任务"""
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
tasks = db.query(ScheduledTask).filter(ScheduledTask.is_enabled == True).all()
|
||||||
|
for task in tasks:
|
||||||
|
self._add_task_to_scheduler(task)
|
||||||
|
print(f"已加载 {len(tasks)} 个定时任务")
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
def _add_task_to_scheduler(self, task: ScheduledTask):
|
||||||
|
"""将任务添加到调度器"""
|
||||||
|
job_id = f"task_{task.id}"
|
||||||
|
|
||||||
|
# 移除已存在的任务
|
||||||
|
if self._scheduler.get_job(job_id):
|
||||||
|
self._scheduler.remove_job(job_id)
|
||||||
|
|
||||||
|
if task.schedule_type == 'cron' and task.cron_expression:
|
||||||
|
# CRON模式
|
||||||
|
try:
|
||||||
|
trigger = CronTrigger.from_crontab(task.cron_expression, timezone='Asia/Shanghai')
|
||||||
|
self._scheduler.add_job(
|
||||||
|
self._execute_task,
|
||||||
|
trigger,
|
||||||
|
id=job_id,
|
||||||
|
args=[task.id],
|
||||||
|
replace_existing=True
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"任务 {task.id} CRON表达式解析失败: {e}")
|
||||||
|
|
||||||
|
elif task.schedule_type == 'simple' and task.time_points:
|
||||||
|
# 简单模式 - 多个时间点
|
||||||
|
try:
|
||||||
|
time_points = task.time_points if isinstance(task.time_points, list) else json.loads(task.time_points)
|
||||||
|
for i, time_point in enumerate(time_points):
|
||||||
|
hour, minute = map(int, time_point.split(':'))
|
||||||
|
sub_job_id = f"{job_id}_{i}"
|
||||||
|
self._scheduler.add_job(
|
||||||
|
self._execute_task,
|
||||||
|
CronTrigger(hour=hour, minute=minute, timezone='Asia/Shanghai'),
|
||||||
|
id=sub_job_id,
|
||||||
|
args=[task.id],
|
||||||
|
replace_existing=True
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"任务 {task.id} 时间点解析失败: {e}")
|
||||||
|
|
||||||
|
def add_task(self, task_id: int):
|
||||||
|
"""添加或更新任务调度"""
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
task = db.query(ScheduledTask).filter(ScheduledTask.id == task_id).first()
|
||||||
|
if task and task.is_enabled:
|
||||||
|
self._add_task_to_scheduler(task)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
def remove_task(self, task_id: int):
|
||||||
|
"""移除任务调度"""
|
||||||
|
job_id = f"task_{task_id}"
|
||||||
|
|
||||||
|
# 移除主任务
|
||||||
|
if self._scheduler.get_job(job_id):
|
||||||
|
self._scheduler.remove_job(job_id)
|
||||||
|
|
||||||
|
# 移除简单模式的子任务
|
||||||
|
for i in range(24): # 最多24个时间点
|
||||||
|
sub_job_id = f"{job_id}_{i}"
|
||||||
|
if self._scheduler.get_job(sub_job_id):
|
||||||
|
self._scheduler.remove_job(sub_job_id)
|
||||||
|
|
||||||
|
async def _execute_task(self, task_id: int):
|
||||||
|
"""执行任务(带重试)"""
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
task = db.query(ScheduledTask).filter(ScheduledTask.id == task_id).first()
|
||||||
|
if not task:
|
||||||
|
return
|
||||||
|
|
||||||
|
max_retries = task.retry_count or 0
|
||||||
|
retry_interval = task.retry_interval or 60
|
||||||
|
|
||||||
|
for attempt in range(max_retries + 1):
|
||||||
|
success, output, error = await self._execute_task_once(db, task)
|
||||||
|
|
||||||
|
if success:
|
||||||
|
return
|
||||||
|
|
||||||
|
# 如果还有重试机会
|
||||||
|
if attempt < max_retries:
|
||||||
|
print(f"任务 {task_id} 执行失败,{retry_interval}秒后重试 ({attempt + 1}/{max_retries})")
|
||||||
|
await asyncio.sleep(retry_interval)
|
||||||
|
else:
|
||||||
|
# 最后一次失败,发送告警
|
||||||
|
if task.alert_on_failure and task.alert_webhook:
|
||||||
|
await self._send_alert(task, error)
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
async def _execute_task_once(self, db: Session, task: ScheduledTask):
|
||||||
|
"""执行一次任务"""
|
||||||
|
trace_id = f"{int(datetime.now().timestamp())}-{task.id}"
|
||||||
|
started_at = datetime.now()
|
||||||
|
|
||||||
|
# 创建日志记录
|
||||||
|
log = TaskLog(
|
||||||
|
task_id=task.id,
|
||||||
|
tenant_id=task.tenant_id,
|
||||||
|
trace_id=trace_id,
|
||||||
|
status='running',
|
||||||
|
started_at=started_at
|
||||||
|
)
|
||||||
|
db.add(log)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(log)
|
||||||
|
|
||||||
|
success = False
|
||||||
|
output = ''
|
||||||
|
error = ''
|
||||||
|
result = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 解析输入参数
|
||||||
|
params = {}
|
||||||
|
if task.input_params:
|
||||||
|
params = task.input_params if isinstance(task.input_params, dict) else {}
|
||||||
|
|
||||||
|
if task.execution_type == 'webhook':
|
||||||
|
success, output, error = await self._execute_webhook(task)
|
||||||
|
else:
|
||||||
|
success, output, error, result = await self._execute_script(db, task, trace_id, params)
|
||||||
|
|
||||||
|
# 如果脚本执行成功且有返回内容,发送通知
|
||||||
|
if success and result and result.get('content'):
|
||||||
|
await self._send_notifications(db, task, result)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
error = str(e)
|
||||||
|
|
||||||
|
# 更新日志
|
||||||
|
finished_at = datetime.now()
|
||||||
|
duration_ms = int((finished_at - started_at).total_seconds() * 1000)
|
||||||
|
|
||||||
|
log.status = 'success' if success else 'failed'
|
||||||
|
log.finished_at = finished_at
|
||||||
|
log.duration_ms = duration_ms
|
||||||
|
log.output = output[:10000] if output else None # 限制长度
|
||||||
|
log.error = error[:5000] if error else None
|
||||||
|
|
||||||
|
# 更新任务状态
|
||||||
|
task.last_run_at = finished_at
|
||||||
|
task.last_run_status = 'success' if success else 'failed'
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
return success, output, error
|
||||||
|
|
||||||
|
async def _execute_webhook(self, task: ScheduledTask):
|
||||||
|
"""执行Webhook任务"""
|
||||||
|
try:
|
||||||
|
body = {}
|
||||||
|
if task.input_params:
|
||||||
|
body = task.input_params if isinstance(task.input_params, dict) else {}
|
||||||
|
|
||||||
|
async with httpx.AsyncClient(timeout=30) as client:
|
||||||
|
response = await client.post(task.webhook_url, json=body)
|
||||||
|
response.raise_for_status()
|
||||||
|
return True, response.text[:5000], ''
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return False, '', str(e)
|
||||||
|
|
||||||
|
async def _execute_script(self, db: Session, task: ScheduledTask, trace_id: str, params: dict):
|
||||||
|
"""执行脚本任务"""
|
||||||
|
if not task.script_content:
|
||||||
|
return False, '', '脚本内容为空', None
|
||||||
|
|
||||||
|
executor = ScriptExecutor(db)
|
||||||
|
success, output, error, result = executor.execute(
|
||||||
|
script_content=task.script_content,
|
||||||
|
task_id=task.id,
|
||||||
|
tenant_id=task.tenant_id,
|
||||||
|
trace_id=trace_id,
|
||||||
|
params=params,
|
||||||
|
timeout=300 # 默认超时
|
||||||
|
)
|
||||||
|
|
||||||
|
return success, output, error, result
|
||||||
|
|
||||||
|
async def _send_notifications(self, db: Session, task: ScheduledTask, result: dict):
|
||||||
|
"""发送通知到配置的渠道
|
||||||
|
|
||||||
|
result 格式:
|
||||||
|
- 简单格式: {'content': '内容', 'title': '标题'}
|
||||||
|
- 完整格式: {'msg_type': 'actionCard', 'title': '...', 'content': '...', 'buttons': [...]}
|
||||||
|
|
||||||
|
支持的 msg_type:
|
||||||
|
- text: 纯文本
|
||||||
|
- markdown: Markdown格式(默认)
|
||||||
|
- link: 链接消息
|
||||||
|
- actionCard: 交互卡片(带按钮)
|
||||||
|
- feedCard: 信息流卡片
|
||||||
|
- news: 图文消息(企微)
|
||||||
|
- template_card: 模板卡片(企微)
|
||||||
|
"""
|
||||||
|
content = result.get('content', '')
|
||||||
|
title = result.get('title', task.task_name)
|
||||||
|
|
||||||
|
if not content and result.get('msg_type') not in ('feedCard', 'news', 'template_card'):
|
||||||
|
return
|
||||||
|
|
||||||
|
# 获取通知渠道配置
|
||||||
|
channel_ids = task.notify_channels
|
||||||
|
if isinstance(channel_ids, str):
|
||||||
|
try:
|
||||||
|
channel_ids = json.loads(channel_ids)
|
||||||
|
except:
|
||||||
|
channel_ids = []
|
||||||
|
|
||||||
|
if not channel_ids:
|
||||||
|
channel_ids = []
|
||||||
|
|
||||||
|
# 发送到通知渠道
|
||||||
|
for channel_id in channel_ids:
|
||||||
|
try:
|
||||||
|
channel = db.query(TaskNotifyChannel).filter(
|
||||||
|
TaskNotifyChannel.id == channel_id,
|
||||||
|
TaskNotifyChannel.is_enabled == True
|
||||||
|
).first()
|
||||||
|
|
||||||
|
if not channel:
|
||||||
|
continue
|
||||||
|
|
||||||
|
await self._send_to_channel(channel, result)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"发送通知到渠道 {channel_id} 失败: {e}")
|
||||||
|
|
||||||
|
# 发送到企微应用
|
||||||
|
if task.notify_wecom_app_id:
|
||||||
|
try:
|
||||||
|
await self._send_to_wecom_app(db, task.notify_wecom_app_id, result, task.tenant_id)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"发送企微应用消息失败: {e}")
|
||||||
|
|
||||||
|
async def _send_to_channel(self, channel: TaskNotifyChannel, result: dict):
|
||||||
|
"""发送消息到通知渠道
|
||||||
|
|
||||||
|
钉钉支持: text, markdown, link, actionCard, feedCard
|
||||||
|
企微支持: text, markdown, image, news, template_card
|
||||||
|
"""
|
||||||
|
import time
|
||||||
|
import hmac
|
||||||
|
import hashlib
|
||||||
|
import base64
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
|
url = channel.webhook_url
|
||||||
|
msg_type = result.get('msg_type', 'markdown')
|
||||||
|
title = result.get('title', '通知')
|
||||||
|
content = result.get('content', '')
|
||||||
|
|
||||||
|
if channel.channel_type == 'dingtalk_bot':
|
||||||
|
# 钉钉加签
|
||||||
|
if channel.sign_secret:
|
||||||
|
timestamp = str(round(time.time() * 1000))
|
||||||
|
string_to_sign = f'{timestamp}\n{channel.sign_secret}'
|
||||||
|
hmac_code = hmac.new(
|
||||||
|
channel.sign_secret.encode('utf-8'),
|
||||||
|
string_to_sign.encode('utf-8'),
|
||||||
|
digestmod=hashlib.sha256
|
||||||
|
).digest()
|
||||||
|
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
|
||||||
|
|
||||||
|
if '?' in url:
|
||||||
|
url = f"{url}×tamp={timestamp}&sign={sign}"
|
||||||
|
else:
|
||||||
|
url = f"{url}?timestamp={timestamp}&sign={sign}"
|
||||||
|
|
||||||
|
payload = self._build_dingtalk_payload(msg_type, title, content, result)
|
||||||
|
else: # wecom_bot
|
||||||
|
payload = self._build_wecom_payload(msg_type, title, content, result)
|
||||||
|
|
||||||
|
async with httpx.AsyncClient(timeout=10) as client:
|
||||||
|
response = await client.post(url, json=payload)
|
||||||
|
resp = response.json()
|
||||||
|
if resp.get('errcode') != 0:
|
||||||
|
print(f"通知发送失败: {resp}")
|
||||||
|
|
||||||
|
def _build_dingtalk_payload(self, msg_type: str, title: str, content: str, result: dict) -> dict:
|
||||||
|
"""构建钉钉消息体
|
||||||
|
|
||||||
|
支持类型:
|
||||||
|
- text: 纯文本
|
||||||
|
- markdown: Markdown
|
||||||
|
- link: 链接消息
|
||||||
|
- actionCard: 交互卡片(整体跳转/独立跳转)
|
||||||
|
- feedCard: 信息流卡片
|
||||||
|
"""
|
||||||
|
if msg_type == 'text':
|
||||||
|
return {
|
||||||
|
"msgtype": "text",
|
||||||
|
"text": {"content": content},
|
||||||
|
"at": result.get('at', {})
|
||||||
|
}
|
||||||
|
|
||||||
|
elif msg_type == 'link':
|
||||||
|
return {
|
||||||
|
"msgtype": "link",
|
||||||
|
"link": {
|
||||||
|
"title": title,
|
||||||
|
"text": content,
|
||||||
|
"messageUrl": result.get('url', ''),
|
||||||
|
"picUrl": result.get('pic_url', '')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elif msg_type == 'actionCard':
|
||||||
|
buttons = result.get('buttons', [])
|
||||||
|
card = {
|
||||||
|
"title": title,
|
||||||
|
"text": content,
|
||||||
|
"btnOrientation": result.get('btn_orientation', '0') # 0-竖向 1-横向
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(buttons) == 1:
|
||||||
|
# 整体跳转
|
||||||
|
card["singleTitle"] = buttons[0].get('title', '查看详情')
|
||||||
|
card["singleURL"] = buttons[0].get('url', '')
|
||||||
|
elif len(buttons) > 1:
|
||||||
|
# 独立跳转
|
||||||
|
card["btns"] = [
|
||||||
|
{"title": btn.get('title', ''), "actionURL": btn.get('url', '')}
|
||||||
|
for btn in buttons
|
||||||
|
]
|
||||||
|
|
||||||
|
return {"msgtype": "actionCard", "actionCard": card}
|
||||||
|
|
||||||
|
elif msg_type == 'feedCard':
|
||||||
|
links = result.get('links', [])
|
||||||
|
return {
|
||||||
|
"msgtype": "feedCard",
|
||||||
|
"feedCard": {
|
||||||
|
"links": [
|
||||||
|
{
|
||||||
|
"title": link.get('title', ''),
|
||||||
|
"messageURL": link.get('url', ''),
|
||||||
|
"picURL": link.get('pic_url', '')
|
||||||
|
}
|
||||||
|
for link in links
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else: # markdown(默认)
|
||||||
|
return {
|
||||||
|
"msgtype": "markdown",
|
||||||
|
"markdown": {"title": title, "text": content},
|
||||||
|
"at": result.get('at', {})
|
||||||
|
}
|
||||||
|
|
||||||
|
def _build_wecom_payload(self, msg_type: str, title: str, content: str, result: dict) -> dict:
|
||||||
|
"""构建企微消息体
|
||||||
|
|
||||||
|
支持类型:
|
||||||
|
- text: 纯文本
|
||||||
|
- markdown: Markdown
|
||||||
|
- image: 图片
|
||||||
|
- news: 图文消息
|
||||||
|
- template_card: 模板卡片
|
||||||
|
"""
|
||||||
|
if msg_type == 'text':
|
||||||
|
payload = {
|
||||||
|
"msgtype": "text",
|
||||||
|
"text": {"content": content}
|
||||||
|
}
|
||||||
|
if result.get('mentioned_list'):
|
||||||
|
payload["text"]["mentioned_list"] = result.get('mentioned_list')
|
||||||
|
if result.get('mentioned_mobile_list'):
|
||||||
|
payload["text"]["mentioned_mobile_list"] = result.get('mentioned_mobile_list')
|
||||||
|
return payload
|
||||||
|
|
||||||
|
elif msg_type == 'image':
|
||||||
|
return {
|
||||||
|
"msgtype": "image",
|
||||||
|
"image": {
|
||||||
|
"base64": result.get('image_base64', ''),
|
||||||
|
"md5": result.get('image_md5', '')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elif msg_type == 'news':
|
||||||
|
articles = result.get('articles', [])
|
||||||
|
if not articles and content:
|
||||||
|
articles = [{
|
||||||
|
"title": title,
|
||||||
|
"description": content,
|
||||||
|
"url": result.get('url', ''),
|
||||||
|
"picurl": result.get('pic_url', '')
|
||||||
|
}]
|
||||||
|
return {
|
||||||
|
"msgtype": "news",
|
||||||
|
"news": {"articles": articles}
|
||||||
|
}
|
||||||
|
|
||||||
|
elif msg_type == 'template_card':
|
||||||
|
card_type = result.get('card_type', 'text_notice')
|
||||||
|
|
||||||
|
if card_type == 'text_notice':
|
||||||
|
# 文本通知卡片
|
||||||
|
return {
|
||||||
|
"msgtype": "template_card",
|
||||||
|
"template_card": {
|
||||||
|
"card_type": "text_notice",
|
||||||
|
"main_title": {"title": title, "desc": result.get('desc', '')},
|
||||||
|
"sub_title_text": content,
|
||||||
|
"horizontal_content_list": result.get('horizontal_list', []),
|
||||||
|
"jump_list": result.get('jump_list', []),
|
||||||
|
"card_action": result.get('card_action', {"type": 1, "url": ""})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elif card_type == 'news_notice':
|
||||||
|
# 图文展示卡片
|
||||||
|
return {
|
||||||
|
"msgtype": "template_card",
|
||||||
|
"template_card": {
|
||||||
|
"card_type": "news_notice",
|
||||||
|
"main_title": {"title": title, "desc": result.get('desc', '')},
|
||||||
|
"card_image": {"url": result.get('image_url', ''), "aspect_ratio": result.get('aspect_ratio', 1.3)},
|
||||||
|
"vertical_content_list": result.get('vertical_list', []),
|
||||||
|
"horizontal_content_list": result.get('horizontal_list', []),
|
||||||
|
"jump_list": result.get('jump_list', []),
|
||||||
|
"card_action": result.get('card_action', {"type": 1, "url": ""})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elif card_type == 'button_interaction':
|
||||||
|
# 按钮交互卡片
|
||||||
|
return {
|
||||||
|
"msgtype": "template_card",
|
||||||
|
"template_card": {
|
||||||
|
"card_type": "button_interaction",
|
||||||
|
"main_title": {"title": title, "desc": result.get('desc', '')},
|
||||||
|
"sub_title_text": content,
|
||||||
|
"horizontal_content_list": result.get('horizontal_list', []),
|
||||||
|
"button_list": result.get('buttons', []),
|
||||||
|
"card_action": result.get('card_action', {"type": 1, "url": ""})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else: # markdown(默认)
|
||||||
|
return {
|
||||||
|
"msgtype": "markdown",
|
||||||
|
"markdown": {"content": f"**{title}**\n\n{content}"}
|
||||||
|
}
|
||||||
|
|
||||||
|
async def _send_to_wecom_app(self, db: Session, app_id: int, result: dict, tenant_id: str):
|
||||||
|
"""发送消息到企微应用"""
|
||||||
|
from ..models.tenant_wechat_app import TenantWechatApp
|
||||||
|
|
||||||
|
app = db.query(TenantWechatApp).filter(TenantWechatApp.id == app_id).first()
|
||||||
|
if not app:
|
||||||
|
return
|
||||||
|
|
||||||
|
# 获取 access_token
|
||||||
|
access_token = await self._get_wecom_access_token(app.corp_id, app.app_secret)
|
||||||
|
if not access_token:
|
||||||
|
return
|
||||||
|
|
||||||
|
title = result.get('title', '通知')
|
||||||
|
content = result.get('content', '')
|
||||||
|
|
||||||
|
# 发送消息
|
||||||
|
url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}"
|
||||||
|
payload = {
|
||||||
|
"touser": result.get('touser', '@all'),
|
||||||
|
"msgtype": "markdown",
|
||||||
|
"agentid": app.agent_id,
|
||||||
|
"markdown": {
|
||||||
|
"content": f"**{title}**\n\n{content}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async with httpx.AsyncClient(timeout=10) as client:
|
||||||
|
response = await client.post(url, json=payload)
|
||||||
|
result = response.json()
|
||||||
|
if result.get('errcode') != 0:
|
||||||
|
print(f"企微应用消息发送失败: {result}")
|
||||||
|
|
||||||
|
async def _get_wecom_access_token(self, corp_id: str, app_secret: str) -> Optional[str]:
|
||||||
|
"""获取企微 access_token"""
|
||||||
|
url = f"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corp_id}&corpsecret={app_secret}"
|
||||||
|
|
||||||
|
async with httpx.AsyncClient(timeout=10) as client:
|
||||||
|
response = await client.get(url)
|
||||||
|
result = response.json()
|
||||||
|
if result.get('errcode') == 0:
|
||||||
|
return result.get('access_token')
|
||||||
|
else:
|
||||||
|
print(f"获取企微 access_token 失败: {result}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def _send_alert(self, task: ScheduledTask, error: str):
|
||||||
|
"""发送失败告警"""
|
||||||
|
if not task.alert_webhook:
|
||||||
|
return
|
||||||
|
|
||||||
|
content = f"""### 定时任务执行失败告警
|
||||||
|
|
||||||
|
**任务名称**: {task.task_name}
|
||||||
|
**任务ID**: {task.id}
|
||||||
|
**租户**: {task.tenant_id or '全局'}
|
||||||
|
**失败时间**: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
||||||
|
**错误信息**:
|
||||||
|
```
|
||||||
|
{error[:500] if error else '未知错误'}
|
||||||
|
```"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 判断是钉钉还是企微
|
||||||
|
if 'dingtalk' in task.alert_webhook or 'oapi.dingtalk.com' in task.alert_webhook:
|
||||||
|
payload = {
|
||||||
|
"msgtype": "markdown",
|
||||||
|
"markdown": {"title": "任务失败告警", "text": content}
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
payload = {
|
||||||
|
"msgtype": "markdown",
|
||||||
|
"markdown": {"content": content}
|
||||||
|
}
|
||||||
|
|
||||||
|
async with httpx.AsyncClient(timeout=10) as client:
|
||||||
|
await client.post(task.alert_webhook, json=payload)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"发送告警失败: {e}")
|
||||||
|
|
||||||
|
async def run_task_now(self, task_id: int) -> dict:
|
||||||
|
"""立即执行任务(手动触发)"""
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
task = db.query(ScheduledTask).filter(ScheduledTask.id == task_id).first()
|
||||||
|
if not task:
|
||||||
|
return {"success": False, "error": "任务不存在"}
|
||||||
|
|
||||||
|
success, output, error = await self._execute_task_once(db, task)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"success": success,
|
||||||
|
"output": output,
|
||||||
|
"error": error
|
||||||
|
}
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
|
# 全局调度器实例
|
||||||
|
scheduler_service = SchedulerService()
|
||||||
285
backend/app/services/script_executor.py
Normal file
285
backend/app/services/script_executor.py
Normal file
@@ -0,0 +1,285 @@
|
|||||||
|
"""脚本执行器 - 安全执行Python脚本"""
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
from io import StringIO
|
||||||
|
from typing import Any, Dict, Optional, Tuple
|
||||||
|
from datetime import datetime
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from .script_sdk import ScriptSDK
|
||||||
|
|
||||||
|
|
||||||
|
# 禁止导入的模块
|
||||||
|
FORBIDDEN_MODULES = {
|
||||||
|
'os', 'subprocess', 'shutil', 'pathlib',
|
||||||
|
'socket', 'ftplib', 'telnetlib', 'smtplib',
|
||||||
|
'pickle', 'shelve', 'marshal',
|
||||||
|
'ctypes', 'multiprocessing',
|
||||||
|
'__builtins__', 'builtins',
|
||||||
|
'importlib', 'imp',
|
||||||
|
'code', 'codeop', 'compile',
|
||||||
|
}
|
||||||
|
|
||||||
|
# 允许的内置函数
|
||||||
|
ALLOWED_BUILTINS = {
|
||||||
|
'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes',
|
||||||
|
'callable', 'chr', 'complex', 'dict', 'dir', 'divmod', 'enumerate',
|
||||||
|
'filter', 'float', 'format', 'frozenset', 'getattr', 'hasattr', 'hash',
|
||||||
|
'hex', 'id', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'list',
|
||||||
|
'map', 'max', 'min', 'next', 'object', 'oct', 'ord', 'pow', 'print',
|
||||||
|
'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',
|
||||||
|
'sorted', 'str', 'sum', 'tuple', 'type', 'vars', 'zip',
|
||||||
|
'True', 'False', 'None',
|
||||||
|
'Exception', 'BaseException', 'ValueError', 'TypeError', 'KeyError',
|
||||||
|
'IndexError', 'AttributeError', 'RuntimeError', 'StopIteration',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ScriptExecutor:
|
||||||
|
"""脚本执行器"""
|
||||||
|
|
||||||
|
def __init__(self, db: Session):
|
||||||
|
self.db = db
|
||||||
|
|
||||||
|
def execute(
|
||||||
|
self,
|
||||||
|
script_content: str,
|
||||||
|
task_id: int,
|
||||||
|
tenant_id: Optional[str] = None,
|
||||||
|
trace_id: Optional[str] = None,
|
||||||
|
params: Optional[Dict[str, Any]] = None,
|
||||||
|
timeout: int = 300
|
||||||
|
) -> Tuple[bool, str, str, Optional[Dict]]:
|
||||||
|
"""执行脚本
|
||||||
|
|
||||||
|
Args:
|
||||||
|
script_content: Python脚本内容
|
||||||
|
task_id: 任务ID
|
||||||
|
tenant_id: 租户ID
|
||||||
|
trace_id: 追踪ID
|
||||||
|
params: 输入参数
|
||||||
|
timeout: 超时秒数
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
(success, output, error, result)
|
||||||
|
result: 脚本返回值 {'content': '...', 'title': '...'}
|
||||||
|
"""
|
||||||
|
# 创建SDK实例
|
||||||
|
sdk = ScriptSDK(
|
||||||
|
db=self.db,
|
||||||
|
task_id=task_id,
|
||||||
|
tenant_id=tenant_id,
|
||||||
|
trace_id=trace_id,
|
||||||
|
params=params or {}
|
||||||
|
)
|
||||||
|
|
||||||
|
# 检查脚本安全性
|
||||||
|
check_result = self._check_script_safety(script_content)
|
||||||
|
if check_result:
|
||||||
|
return False, '', f"脚本安全检查失败: {check_result}", None
|
||||||
|
|
||||||
|
# 准备执行环境
|
||||||
|
safe_globals = self._create_safe_globals(sdk)
|
||||||
|
|
||||||
|
# 捕获输出
|
||||||
|
old_stdout = sys.stdout
|
||||||
|
old_stderr = sys.stderr
|
||||||
|
stdout_capture = StringIO()
|
||||||
|
stderr_capture = StringIO()
|
||||||
|
|
||||||
|
try:
|
||||||
|
sys.stdout = stdout_capture
|
||||||
|
sys.stderr = stderr_capture
|
||||||
|
|
||||||
|
# 编译并执行脚本
|
||||||
|
compiled = compile(script_content, '<script>', 'exec')
|
||||||
|
exec(compiled, safe_globals)
|
||||||
|
|
||||||
|
# 获取输出
|
||||||
|
stdout_output = stdout_capture.getvalue()
|
||||||
|
sdk_output = sdk.get_output()
|
||||||
|
|
||||||
|
# 合并输出
|
||||||
|
output = '\n'.join(filter(None, [sdk_output, stdout_output]))
|
||||||
|
|
||||||
|
# 获取脚本返回值(通过 __result__ 变量)
|
||||||
|
result = safe_globals.get('__result__')
|
||||||
|
if result is None and 'result' in safe_globals:
|
||||||
|
result = safe_globals.get('result')
|
||||||
|
|
||||||
|
# 如果返回的是字符串,包装成字典
|
||||||
|
if isinstance(result, str):
|
||||||
|
result = {'content': result}
|
||||||
|
elif result is not None and not isinstance(result, dict):
|
||||||
|
result = {'content': str(result)}
|
||||||
|
|
||||||
|
return True, output, '', result
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"{type(e).__name__}: {str(e)}\n{traceback.format_exc()}"
|
||||||
|
return False, sdk.get_output(), error_msg, None
|
||||||
|
|
||||||
|
finally:
|
||||||
|
sys.stdout = old_stdout
|
||||||
|
sys.stderr = old_stderr
|
||||||
|
|
||||||
|
def _check_script_safety(self, script_content: str) -> Optional[str]:
|
||||||
|
"""检查脚本安全性
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
错误消息,如果安全则返回None
|
||||||
|
"""
|
||||||
|
# 检查危险导入
|
||||||
|
import_patterns = [
|
||||||
|
'import os', 'from os',
|
||||||
|
'import subprocess', 'from subprocess',
|
||||||
|
'import shutil', 'from shutil',
|
||||||
|
'import socket', 'from socket',
|
||||||
|
'__import__',
|
||||||
|
'eval(', 'exec(',
|
||||||
|
'compile(',
|
||||||
|
'open(', # 禁止文件操作
|
||||||
|
]
|
||||||
|
|
||||||
|
script_lower = script_content.lower()
|
||||||
|
for pattern in import_patterns:
|
||||||
|
if pattern.lower() in script_lower:
|
||||||
|
return f"禁止使用: {pattern}"
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _create_safe_globals(self, sdk: ScriptSDK) -> Dict[str, Any]:
|
||||||
|
"""创建安全的执行环境"""
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
import hashlib
|
||||||
|
import base64
|
||||||
|
import time
|
||||||
|
import collections
|
||||||
|
from datetime import datetime, date, timedelta
|
||||||
|
from urllib.parse import urlencode, quote, unquote
|
||||||
|
|
||||||
|
# 允许导入的模块白名单
|
||||||
|
ALLOWED_MODULES = {
|
||||||
|
'json': json,
|
||||||
|
're': re,
|
||||||
|
'math': math,
|
||||||
|
'random': random,
|
||||||
|
'hashlib': hashlib,
|
||||||
|
'base64': base64,
|
||||||
|
'time': time,
|
||||||
|
'datetime': __import__('datetime'),
|
||||||
|
'collections': collections,
|
||||||
|
}
|
||||||
|
|
||||||
|
def safe_import(name, globals=None, locals=None, fromlist=(), level=0):
|
||||||
|
"""受限的 import 函数"""
|
||||||
|
if name in ALLOWED_MODULES:
|
||||||
|
return ALLOWED_MODULES[name]
|
||||||
|
raise ImportError(f"不允许导入模块: {name}。已内置可用: {', '.join(ALLOWED_MODULES.keys())}")
|
||||||
|
|
||||||
|
# 安全的内置函数
|
||||||
|
safe_builtins = {name: getattr(__builtins__, name, None)
|
||||||
|
for name in ALLOWED_BUILTINS
|
||||||
|
if hasattr(__builtins__, name) or name in dir(__builtins__)}
|
||||||
|
|
||||||
|
# 如果 __builtins__ 是字典
|
||||||
|
if isinstance(__builtins__, dict):
|
||||||
|
safe_builtins = {name: __builtins__.get(name)
|
||||||
|
for name in ALLOWED_BUILTINS
|
||||||
|
if name in __builtins__}
|
||||||
|
|
||||||
|
# 添加受限的 __import__
|
||||||
|
safe_builtins['__import__'] = safe_import
|
||||||
|
|
||||||
|
# 添加常用异常
|
||||||
|
safe_builtins['Exception'] = Exception
|
||||||
|
safe_builtins['ValueError'] = ValueError
|
||||||
|
safe_builtins['TypeError'] = TypeError
|
||||||
|
safe_builtins['KeyError'] = KeyError
|
||||||
|
safe_builtins['IndexError'] = IndexError
|
||||||
|
safe_builtins['ImportError'] = ImportError
|
||||||
|
|
||||||
|
return {
|
||||||
|
'__builtins__': safe_builtins,
|
||||||
|
'__name__': '__main__',
|
||||||
|
|
||||||
|
# SDK函数(全局可用)
|
||||||
|
'log': sdk.log,
|
||||||
|
'print': sdk.print,
|
||||||
|
'ai': sdk.ai,
|
||||||
|
'dingtalk': sdk.dingtalk,
|
||||||
|
'wecom': sdk.wecom,
|
||||||
|
'http_get': sdk.http_get,
|
||||||
|
'http_post': sdk.http_post,
|
||||||
|
'db_query': sdk.db_query,
|
||||||
|
'get_var': sdk.get_var,
|
||||||
|
'set_var': sdk.set_var,
|
||||||
|
'del_var': sdk.del_var,
|
||||||
|
'get_param': sdk.get_param,
|
||||||
|
'get_params': sdk.get_params,
|
||||||
|
'get_tenants': sdk.get_tenants,
|
||||||
|
'get_tenant_config': sdk.get_tenant_config,
|
||||||
|
'get_all_tenant_configs': sdk.get_all_tenant_configs,
|
||||||
|
'get_secret': sdk.get_secret,
|
||||||
|
|
||||||
|
# 当前上下文
|
||||||
|
'task_id': sdk.task_id,
|
||||||
|
'tenant_id': sdk.tenant_id,
|
||||||
|
'trace_id': sdk.trace_id,
|
||||||
|
|
||||||
|
# 安全的标准库
|
||||||
|
'json': json,
|
||||||
|
're': re,
|
||||||
|
'math': math,
|
||||||
|
'random': random,
|
||||||
|
'hashlib': hashlib,
|
||||||
|
'base64': base64,
|
||||||
|
'datetime': datetime,
|
||||||
|
'date': date,
|
||||||
|
'timedelta': timedelta,
|
||||||
|
'time': time,
|
||||||
|
'urlencode': urlencode,
|
||||||
|
'quote': quote,
|
||||||
|
'unquote': unquote,
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_script(
|
||||||
|
self,
|
||||||
|
script_content: str,
|
||||||
|
task_id: int = 0,
|
||||||
|
tenant_id: Optional[str] = None,
|
||||||
|
params: Optional[Dict[str, Any]] = None
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""测试脚本(用于调试)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
{
|
||||||
|
"success": bool,
|
||||||
|
"output": str,
|
||||||
|
"error": str,
|
||||||
|
"duration_ms": int,
|
||||||
|
"result": dict
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
start_time = datetime.now()
|
||||||
|
|
||||||
|
success, output, error, result = self.execute(
|
||||||
|
script_content=script_content,
|
||||||
|
task_id=task_id,
|
||||||
|
tenant_id=tenant_id,
|
||||||
|
trace_id=f"test-{start_time.timestamp()}",
|
||||||
|
params=params
|
||||||
|
)
|
||||||
|
|
||||||
|
duration_ms = int((datetime.now() - start_time).total_seconds() * 1000)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"success": success,
|
||||||
|
"output": output,
|
||||||
|
"error": error,
|
||||||
|
"duration_ms": duration_ms,
|
||||||
|
"result": result
|
||||||
|
}
|
||||||
479
backend/app/services/script_sdk.py
Normal file
479
backend/app/services/script_sdk.py
Normal file
@@ -0,0 +1,479 @@
|
|||||||
|
"""脚本执行SDK - 为Python脚本提供内置功能"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import httpx
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Any, Dict, List, Optional
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
|
||||||
|
class ScriptSDK:
|
||||||
|
"""脚本SDK - 提供AI、通知、数据库、HTTP、变量存储等功能"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
db: Session,
|
||||||
|
task_id: int,
|
||||||
|
tenant_id: Optional[str] = None,
|
||||||
|
trace_id: Optional[str] = None,
|
||||||
|
params: Optional[Dict[str, Any]] = None
|
||||||
|
):
|
||||||
|
self.db = db
|
||||||
|
self.task_id = task_id
|
||||||
|
self.tenant_id = tenant_id
|
||||||
|
self.trace_id = trace_id
|
||||||
|
self.params = params or {}
|
||||||
|
|
||||||
|
self._logs: List[Dict] = []
|
||||||
|
self._output: List[str] = []
|
||||||
|
self._tenants_cache: Dict = {}
|
||||||
|
|
||||||
|
# AI 配置
|
||||||
|
self._ai_base_url = os.getenv('OPENAI_BASE_URL', 'https://api.4sapi.net/v1')
|
||||||
|
self._ai_api_key = os.getenv('OPENAI_API_KEY', 'sk-9yMCXjRGANbacz20kJY8doSNy6Rf446aYwmgGIuIXQ7DAyBw')
|
||||||
|
self._ai_model = os.getenv('OPENAI_MODEL', 'gemini-2.5-flash')
|
||||||
|
|
||||||
|
# ==================== 参数获取 ====================
|
||||||
|
|
||||||
|
def get_param(self, key: str, default: Any = None) -> Any:
|
||||||
|
"""获取任务参数
|
||||||
|
|
||||||
|
Args:
|
||||||
|
key: 参数名
|
||||||
|
default: 默认值
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
参数值
|
||||||
|
"""
|
||||||
|
return self.params.get(key, default)
|
||||||
|
|
||||||
|
def get_params(self) -> Dict[str, Any]:
|
||||||
|
"""获取所有任务参数
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
所有参数字典
|
||||||
|
"""
|
||||||
|
return self.params.copy()
|
||||||
|
|
||||||
|
# ==================== 日志 ====================
|
||||||
|
|
||||||
|
def log(self, message: str, level: str = 'INFO') -> None:
|
||||||
|
"""记录日志
|
||||||
|
|
||||||
|
Args:
|
||||||
|
message: 日志内容
|
||||||
|
level: 日志级别 (INFO, WARN, ERROR)
|
||||||
|
"""
|
||||||
|
log_entry = {
|
||||||
|
'time': datetime.now().isoformat(),
|
||||||
|
'level': level.upper(),
|
||||||
|
'message': message
|
||||||
|
}
|
||||||
|
self._logs.append(log_entry)
|
||||||
|
self._output.append(f"[{level.upper()}] {message}")
|
||||||
|
|
||||||
|
def print(self, *args, **kwargs) -> None:
|
||||||
|
"""打印输出(兼容print)"""
|
||||||
|
message = ' '.join(str(arg) for arg in args)
|
||||||
|
self._output.append(message)
|
||||||
|
|
||||||
|
def get_logs(self) -> List[Dict]:
|
||||||
|
"""获取所有日志"""
|
||||||
|
return self._logs
|
||||||
|
|
||||||
|
def get_output(self) -> str:
|
||||||
|
"""获取所有输出"""
|
||||||
|
return '\n'.join(self._output)
|
||||||
|
|
||||||
|
# ==================== AI 调用 ====================
|
||||||
|
|
||||||
|
def ai(
|
||||||
|
self,
|
||||||
|
prompt: str,
|
||||||
|
system: Optional[str] = None,
|
||||||
|
model: Optional[str] = None,
|
||||||
|
temperature: float = 0.7,
|
||||||
|
max_tokens: int = 2000
|
||||||
|
) -> str:
|
||||||
|
"""调用AI模型
|
||||||
|
|
||||||
|
Args:
|
||||||
|
prompt: 用户提示词
|
||||||
|
system: 系统提示词
|
||||||
|
model: 模型名称(默认gemini-2.5-flash)
|
||||||
|
temperature: 温度参数
|
||||||
|
max_tokens: 最大token数
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
AI响应内容
|
||||||
|
"""
|
||||||
|
model = model or self._ai_model
|
||||||
|
|
||||||
|
messages = []
|
||||||
|
if system:
|
||||||
|
messages.append({"role": "system", "content": system})
|
||||||
|
messages.append({"role": "user", "content": prompt})
|
||||||
|
|
||||||
|
try:
|
||||||
|
with httpx.Client(timeout=60) as client:
|
||||||
|
response = client.post(
|
||||||
|
f"{self._ai_base_url}/chat/completions",
|
||||||
|
headers={
|
||||||
|
"Authorization": f"Bearer {self._ai_api_key}",
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
json={
|
||||||
|
"model": model,
|
||||||
|
"messages": messages,
|
||||||
|
"temperature": temperature,
|
||||||
|
"max_tokens": max_tokens
|
||||||
|
}
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
data = response.json()
|
||||||
|
content = data['choices'][0]['message']['content']
|
||||||
|
self.log(f"AI调用成功: {len(content)} 字符")
|
||||||
|
return content
|
||||||
|
except Exception as e:
|
||||||
|
self.log(f"AI调用失败: {str(e)}", 'ERROR')
|
||||||
|
raise
|
||||||
|
|
||||||
|
# ==================== 通知 ====================
|
||||||
|
|
||||||
|
def dingtalk(self, webhook: str, content: str, title: Optional[str] = None, at_all: bool = False) -> bool:
|
||||||
|
"""发送钉钉消息
|
||||||
|
|
||||||
|
Args:
|
||||||
|
webhook: 钉钉机器人webhook地址
|
||||||
|
content: 消息内容(支持Markdown)
|
||||||
|
title: 消息标题
|
||||||
|
at_all: 是否@所有人
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
是否发送成功
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
payload = {
|
||||||
|
"msgtype": "markdown",
|
||||||
|
"markdown": {
|
||||||
|
"title": title or "通知",
|
||||||
|
"text": content + ("\n@所有人" if at_all else "")
|
||||||
|
},
|
||||||
|
"at": {"isAtAll": at_all}
|
||||||
|
}
|
||||||
|
|
||||||
|
with httpx.Client(timeout=10) as client:
|
||||||
|
response = client.post(webhook, json=payload)
|
||||||
|
response.raise_for_status()
|
||||||
|
result = response.json()
|
||||||
|
success = result.get('errcode') == 0
|
||||||
|
self.log(f"钉钉消息发送{'成功' if success else '失败'}")
|
||||||
|
return success
|
||||||
|
except Exception as e:
|
||||||
|
self.log(f"钉钉消息发送失败: {str(e)}", 'ERROR')
|
||||||
|
return False
|
||||||
|
|
||||||
|
def wecom(self, webhook: str, content: str, msg_type: str = 'markdown') -> bool:
|
||||||
|
"""发送企业微信消息
|
||||||
|
|
||||||
|
Args:
|
||||||
|
webhook: 企微机器人webhook地址
|
||||||
|
content: 消息内容
|
||||||
|
msg_type: 消息类型 (text, markdown)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
是否发送成功
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if msg_type == 'markdown':
|
||||||
|
payload = {
|
||||||
|
"msgtype": "markdown",
|
||||||
|
"markdown": {"content": content}
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
payload = {
|
||||||
|
"msgtype": "text",
|
||||||
|
"text": {"content": content}
|
||||||
|
}
|
||||||
|
|
||||||
|
with httpx.Client(timeout=10) as client:
|
||||||
|
response = client.post(webhook, json=payload)
|
||||||
|
response.raise_for_status()
|
||||||
|
result = response.json()
|
||||||
|
success = result.get('errcode') == 0
|
||||||
|
self.log(f"企微消息发送{'成功' if success else '失败'}")
|
||||||
|
return success
|
||||||
|
except Exception as e:
|
||||||
|
self.log(f"企微消息发送失败: {str(e)}", 'ERROR')
|
||||||
|
return False
|
||||||
|
|
||||||
|
# ==================== HTTP 请求 ====================
|
||||||
|
|
||||||
|
def http_get(self, url: str, headers: Optional[Dict] = None, params: Optional[Dict] = None, timeout: int = 30) -> Dict:
|
||||||
|
"""发起HTTP GET请求
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
{"status": 200, "data": ..., "text": "..."}
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
with httpx.Client(timeout=timeout) as client:
|
||||||
|
response = client.get(url, headers=headers, params=params)
|
||||||
|
return {
|
||||||
|
"status": response.status_code,
|
||||||
|
"data": response.json() if response.headers.get('content-type', '').startswith('application/json') else None,
|
||||||
|
"text": response.text
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
self.log(f"HTTP GET 失败: {str(e)}", 'ERROR')
|
||||||
|
raise
|
||||||
|
|
||||||
|
def http_post(self, url: str, data: Any = None, headers: Optional[Dict] = None, timeout: int = 30) -> Dict:
|
||||||
|
"""发起HTTP POST请求
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
{"status": 200, "data": ..., "text": "..."}
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
with httpx.Client(timeout=timeout) as client:
|
||||||
|
response = client.post(url, json=data, headers=headers)
|
||||||
|
return {
|
||||||
|
"status": response.status_code,
|
||||||
|
"data": response.json() if response.headers.get('content-type', '').startswith('application/json') else None,
|
||||||
|
"text": response.text
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
self.log(f"HTTP POST 失败: {str(e)}", 'ERROR')
|
||||||
|
raise
|
||||||
|
|
||||||
|
# ==================== 数据库查询(只读)====================
|
||||||
|
|
||||||
|
def db_query(self, sql: str, params: Optional[Dict] = None) -> List[Dict]:
|
||||||
|
"""执行只读SQL查询
|
||||||
|
|
||||||
|
Args:
|
||||||
|
sql: SQL语句(必须是SELECT)
|
||||||
|
params: 参数字典
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
查询结果列表
|
||||||
|
"""
|
||||||
|
sql_upper = sql.strip().upper()
|
||||||
|
if not sql_upper.startswith('SELECT'):
|
||||||
|
raise ValueError("只允许执行SELECT查询")
|
||||||
|
|
||||||
|
# 禁止危险操作
|
||||||
|
forbidden = ['INSERT', 'UPDATE', 'DELETE', 'DROP', 'TRUNCATE', 'ALTER', 'CREATE']
|
||||||
|
for word in forbidden:
|
||||||
|
if word in sql_upper:
|
||||||
|
raise ValueError(f"禁止执行 {word} 操作")
|
||||||
|
|
||||||
|
try:
|
||||||
|
from sqlalchemy import text
|
||||||
|
result = self.db.execute(text(sql), params or {})
|
||||||
|
columns = result.keys()
|
||||||
|
rows = [dict(zip(columns, row)) for row in result.fetchall()]
|
||||||
|
self.log(f"SQL查询返回 {len(rows)} 条记录")
|
||||||
|
return rows
|
||||||
|
except Exception as e:
|
||||||
|
self.log(f"SQL查询失败: {str(e)}", 'ERROR')
|
||||||
|
raise
|
||||||
|
|
||||||
|
# ==================== 变量存储 ====================
|
||||||
|
|
||||||
|
def get_var(self, key: str, default: Any = None) -> Any:
|
||||||
|
"""获取持久化变量
|
||||||
|
|
||||||
|
Args:
|
||||||
|
key: 变量名
|
||||||
|
default: 默认值
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
变量值
|
||||||
|
"""
|
||||||
|
from ..models.scheduled_task import ScriptVar
|
||||||
|
|
||||||
|
var = self.db.query(ScriptVar).filter(
|
||||||
|
ScriptVar.task_id == self.task_id,
|
||||||
|
ScriptVar.tenant_id == self.tenant_id,
|
||||||
|
ScriptVar.var_key == key
|
||||||
|
).first()
|
||||||
|
|
||||||
|
if var and var.var_value:
|
||||||
|
try:
|
||||||
|
return json.loads(var.var_value)
|
||||||
|
except:
|
||||||
|
return var.var_value
|
||||||
|
return default
|
||||||
|
|
||||||
|
def set_var(self, key: str, value: Any) -> None:
|
||||||
|
"""设置持久化变量
|
||||||
|
|
||||||
|
Args:
|
||||||
|
key: 变量名
|
||||||
|
value: 变量值(会JSON序列化)
|
||||||
|
"""
|
||||||
|
from ..models.scheduled_task import ScriptVar
|
||||||
|
|
||||||
|
var = self.db.query(ScriptVar).filter(
|
||||||
|
ScriptVar.task_id == self.task_id,
|
||||||
|
ScriptVar.tenant_id == self.tenant_id,
|
||||||
|
ScriptVar.var_key == key
|
||||||
|
).first()
|
||||||
|
|
||||||
|
value_json = json.dumps(value, ensure_ascii=False)
|
||||||
|
|
||||||
|
if var:
|
||||||
|
var.var_value = value_json
|
||||||
|
else:
|
||||||
|
var = ScriptVar(
|
||||||
|
task_id=self.task_id,
|
||||||
|
tenant_id=self.tenant_id,
|
||||||
|
var_key=key,
|
||||||
|
var_value=value_json
|
||||||
|
)
|
||||||
|
self.db.add(var)
|
||||||
|
|
||||||
|
self.db.commit()
|
||||||
|
self.log(f"变量 {key} 已保存")
|
||||||
|
|
||||||
|
def del_var(self, key: str) -> bool:
|
||||||
|
"""删除持久化变量"""
|
||||||
|
from ..models.scheduled_task import ScriptVar
|
||||||
|
|
||||||
|
result = self.db.query(ScriptVar).filter(
|
||||||
|
ScriptVar.task_id == self.task_id,
|
||||||
|
ScriptVar.tenant_id == self.tenant_id,
|
||||||
|
ScriptVar.var_key == key
|
||||||
|
).delete()
|
||||||
|
|
||||||
|
self.db.commit()
|
||||||
|
return result > 0
|
||||||
|
|
||||||
|
# ==================== 租户配置 ====================
|
||||||
|
|
||||||
|
def get_tenants(self, app_code: Optional[str] = None) -> List[Dict]:
|
||||||
|
"""获取租户列表
|
||||||
|
|
||||||
|
Args:
|
||||||
|
app_code: 可选,按应用代码筛选
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
租户列表 [{"tenant_id": ..., "tenant_name": ...}, ...]
|
||||||
|
"""
|
||||||
|
from ..models.tenant import Tenant
|
||||||
|
from ..models.tenant_app import TenantApp
|
||||||
|
|
||||||
|
if app_code:
|
||||||
|
# 获取订阅了该应用的租户
|
||||||
|
tenant_ids = self.db.query(TenantApp.tenant_id).filter(
|
||||||
|
TenantApp.app_code == app_code,
|
||||||
|
TenantApp.status == 1
|
||||||
|
).all()
|
||||||
|
tenant_ids = [t[0] for t in tenant_ids]
|
||||||
|
|
||||||
|
tenants = self.db.query(Tenant).filter(
|
||||||
|
Tenant.code.in_(tenant_ids),
|
||||||
|
Tenant.status == 'active'
|
||||||
|
).all()
|
||||||
|
else:
|
||||||
|
tenants = self.db.query(Tenant).filter(Tenant.status == 'active').all()
|
||||||
|
|
||||||
|
return [{"tenant_id": t.code, "tenant_name": t.name} for t in tenants]
|
||||||
|
|
||||||
|
def get_tenant_config(self, tenant_id: str, app_code: str, key: Optional[str] = None) -> Any:
|
||||||
|
"""获取租户的应用配置
|
||||||
|
|
||||||
|
Args:
|
||||||
|
tenant_id: 租户ID
|
||||||
|
app_code: 应用代码
|
||||||
|
key: 配置键(可选,不提供则返回所有配置)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
配置值或配置字典
|
||||||
|
"""
|
||||||
|
from ..models.tenant_app import TenantApp
|
||||||
|
|
||||||
|
tenant_app = self.db.query(TenantApp).filter(
|
||||||
|
TenantApp.tenant_id == tenant_id,
|
||||||
|
TenantApp.app_code == app_code
|
||||||
|
).first()
|
||||||
|
|
||||||
|
if not tenant_app:
|
||||||
|
return None if key else {}
|
||||||
|
|
||||||
|
# 解析 custom_configs
|
||||||
|
configs = {}
|
||||||
|
if hasattr(tenant_app, 'custom_configs') and tenant_app.custom_configs:
|
||||||
|
try:
|
||||||
|
configs = json.loads(tenant_app.custom_configs) if isinstance(tenant_app.custom_configs, str) else tenant_app.custom_configs
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if key:
|
||||||
|
return configs.get(key)
|
||||||
|
return configs
|
||||||
|
|
||||||
|
def get_all_tenant_configs(self, app_code: str) -> List[Dict]:
|
||||||
|
"""获取所有租户的应用配置
|
||||||
|
|
||||||
|
Args:
|
||||||
|
app_code: 应用代码
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
[{"tenant_id": ..., "tenant_name": ..., "configs": {...}}, ...]
|
||||||
|
"""
|
||||||
|
from ..models.tenant import Tenant
|
||||||
|
from ..models.tenant_app import TenantApp
|
||||||
|
|
||||||
|
tenant_apps = self.db.query(TenantApp).filter(
|
||||||
|
TenantApp.app_code == app_code,
|
||||||
|
TenantApp.status == 1
|
||||||
|
).all()
|
||||||
|
|
||||||
|
result = []
|
||||||
|
for ta in tenant_apps:
|
||||||
|
tenant = self.db.query(Tenant).filter(Tenant.code == ta.tenant_id).first()
|
||||||
|
configs = {}
|
||||||
|
if hasattr(ta, 'custom_configs') and ta.custom_configs:
|
||||||
|
try:
|
||||||
|
configs = json.loads(ta.custom_configs) if isinstance(ta.custom_configs, str) else ta.custom_configs
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
result.append({
|
||||||
|
"tenant_id": ta.tenant_id,
|
||||||
|
"tenant_name": tenant.name if tenant else ta.tenant_id,
|
||||||
|
"configs": configs
|
||||||
|
})
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
# ==================== 密钥管理 ====================
|
||||||
|
|
||||||
|
def get_secret(self, key: str) -> Optional[str]:
|
||||||
|
"""获取密钥(优先租户级,其次全局)
|
||||||
|
|
||||||
|
Args:
|
||||||
|
key: 密钥名
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
密钥值
|
||||||
|
"""
|
||||||
|
from ..models.scheduled_task import Secret
|
||||||
|
|
||||||
|
# 先查租户级
|
||||||
|
if self.tenant_id:
|
||||||
|
secret = self.db.query(Secret).filter(
|
||||||
|
Secret.tenant_id == self.tenant_id,
|
||||||
|
Secret.secret_key == key
|
||||||
|
).first()
|
||||||
|
if secret:
|
||||||
|
return secret.secret_value
|
||||||
|
|
||||||
|
# 再查全局
|
||||||
|
secret = self.db.query(Secret).filter(
|
||||||
|
Secret.tenant_id.is_(None),
|
||||||
|
Secret.secret_key == key
|
||||||
|
).first()
|
||||||
|
|
||||||
|
return secret.secret_value if secret else None
|
||||||
@@ -12,3 +12,4 @@ python-multipart>=0.0.6
|
|||||||
httpx>=0.26.0
|
httpx>=0.26.0
|
||||||
redis>=5.0.0
|
redis>=5.0.0
|
||||||
openpyxl>=3.1.0
|
openpyxl>=3.1.0
|
||||||
|
apscheduler>=3.10.0
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
FROM python:3.11-slim
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
# 设置时区为上海
|
||||||
|
ENV TZ=Asia/Shanghai
|
||||||
|
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# 安装依赖(使用阿里云镜像)
|
# 安装依赖(使用阿里云镜像)
|
||||||
|
|||||||
358
docs/scheduled-tasks.md
Normal file
358
docs/scheduled-tasks.md
Normal file
@@ -0,0 +1,358 @@
|
|||||||
|
# 定时任务系统文档
|
||||||
|
|
||||||
|
## 功能概述
|
||||||
|
|
||||||
|
平台定时任务系统,支持 Python 脚本或 Webhook 定时执行,执行结果可自动推送到钉钉/企微机器人。
|
||||||
|
|
||||||
|
**核心能力**:
|
||||||
|
- 脚本执行:安全沙箱运行 Python 脚本,内置 AI、HTTP、数据库等 SDK
|
||||||
|
- 调度方式:指定时间点(多选)或 CRON 表达式
|
||||||
|
- 消息推送:支持钉钉/企微机器人所有消息格式(markdown、actionCard、feedCard 等)
|
||||||
|
- 失败处理:支持重试和告警通知
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 数据库表
|
||||||
|
|
||||||
|
### platform_scheduled_tasks(定时任务表)
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE TABLE platform_scheduled_tasks (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
tenant_id VARCHAR(50) COMMENT '租户ID,空为全局任务',
|
||||||
|
task_name VARCHAR(100) NOT NULL COMMENT '任务名称',
|
||||||
|
task_desc VARCHAR(500) COMMENT '任务描述',
|
||||||
|
schedule_type ENUM('simple', 'cron') NOT NULL DEFAULT 'simple',
|
||||||
|
time_points JSON COMMENT '时间点列表 ["08:00", "12:00"]',
|
||||||
|
cron_expression VARCHAR(100) COMMENT 'CRON表达式',
|
||||||
|
timezone VARCHAR(50) DEFAULT 'Asia/Shanghai',
|
||||||
|
execution_type ENUM('webhook', 'script') NOT NULL DEFAULT 'script',
|
||||||
|
webhook_url VARCHAR(500),
|
||||||
|
script_content TEXT COMMENT 'Python脚本内容',
|
||||||
|
script_deps TEXT COMMENT '脚本依赖',
|
||||||
|
input_params JSON COMMENT '输入参数',
|
||||||
|
retry_count INT DEFAULT 0,
|
||||||
|
retry_interval INT DEFAULT 60,
|
||||||
|
alert_on_failure TINYINT(1) DEFAULT 0,
|
||||||
|
alert_webhook VARCHAR(500),
|
||||||
|
notify_channels JSON COMMENT '通知渠道ID列表',
|
||||||
|
notify_wecom_app_id INT COMMENT '企微应用ID',
|
||||||
|
is_enabled TINYINT(1) DEFAULT 1,
|
||||||
|
last_run_at DATETIME,
|
||||||
|
last_run_status ENUM('success', 'failed', 'running'),
|
||||||
|
last_run_message TEXT,
|
||||||
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### platform_task_notify_channels(通知渠道表)
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE TABLE platform_task_notify_channels (
|
||||||
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
tenant_id VARCHAR(50) NOT NULL COMMENT '租户ID',
|
||||||
|
channel_name VARCHAR(100) NOT NULL COMMENT '渠道名称',
|
||||||
|
channel_type ENUM('dingtalk_bot', 'wecom_bot') NOT NULL,
|
||||||
|
webhook_url VARCHAR(500) NOT NULL,
|
||||||
|
sign_secret VARCHAR(200) COMMENT '钉钉加签密钥',
|
||||||
|
description VARCHAR(255),
|
||||||
|
is_enabled TINYINT(1) DEFAULT 1,
|
||||||
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### platform_task_logs(执行日志表)
|
||||||
|
|
||||||
|
```sql
|
||||||
|
CREATE TABLE platform_task_logs (
|
||||||
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
task_id INT NOT NULL,
|
||||||
|
tenant_id VARCHAR(50),
|
||||||
|
trace_id VARCHAR(100),
|
||||||
|
status ENUM('running', 'success', 'failed'),
|
||||||
|
started_at DATETIME,
|
||||||
|
finished_at DATETIME,
|
||||||
|
duration_ms INT,
|
||||||
|
output TEXT,
|
||||||
|
error TEXT,
|
||||||
|
retry_count INT DEFAULT 0,
|
||||||
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 后端文件结构
|
||||||
|
|
||||||
|
```
|
||||||
|
backend/app/
|
||||||
|
├── models/
|
||||||
|
│ ├── scheduled_task.py # ScheduledTask, TaskLog, ScriptVar, Secret 模型
|
||||||
|
│ └── notification_channel.py # TaskNotifyChannel 模型
|
||||||
|
├── routers/
|
||||||
|
│ ├── tasks.py # 定时任务 API (/api/scheduled-tasks)
|
||||||
|
│ └── notification_channels.py # 通知渠道 API (/api/notification-channels)
|
||||||
|
└── services/
|
||||||
|
├── scheduler.py # APScheduler 调度服务
|
||||||
|
├── script_executor.py # 脚本执行器(安全沙箱)
|
||||||
|
└── script_sdk.py # 脚本内置 SDK
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 脚本 SDK 文档
|
||||||
|
|
||||||
|
### 内置函数
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 日志
|
||||||
|
log(message) # 记录日志
|
||||||
|
print(message) # 打印输出
|
||||||
|
|
||||||
|
# AI 调用
|
||||||
|
ai(prompt, system=None, model='deepseek-chat') # 调用 AI
|
||||||
|
|
||||||
|
# 通知发送(直接发送,不走 result)
|
||||||
|
dingtalk(webhook_url, content, title='通知')
|
||||||
|
wecom(webhook_url, content)
|
||||||
|
|
||||||
|
# HTTP 请求
|
||||||
|
http_get(url, headers=None, params=None)
|
||||||
|
http_post(url, data=None, json=None, headers=None)
|
||||||
|
|
||||||
|
# 数据库查询(只读)
|
||||||
|
db_query(sql, params=None)
|
||||||
|
|
||||||
|
# 变量存储(跨执行持久化)
|
||||||
|
get_var(key, default=None)
|
||||||
|
set_var(key, value)
|
||||||
|
del_var(key)
|
||||||
|
|
||||||
|
# 任务参数
|
||||||
|
get_param(key, default=None) # 获取单个参数
|
||||||
|
get_params() # 获取所有参数
|
||||||
|
|
||||||
|
# 租户相关
|
||||||
|
get_tenants() # 获取所有租户
|
||||||
|
get_tenant_config(tenant_id, app_code, key) # 获取租户配置
|
||||||
|
get_all_tenant_configs(app_code, key) # 获取所有租户的配置
|
||||||
|
|
||||||
|
# 密钥
|
||||||
|
get_secret(key) # 获取密钥
|
||||||
|
```
|
||||||
|
|
||||||
|
### 内置变量
|
||||||
|
|
||||||
|
```python
|
||||||
|
task_id # 当前任务ID
|
||||||
|
tenant_id # 当前租户ID(可能为空)
|
||||||
|
trace_id # 追踪ID
|
||||||
|
```
|
||||||
|
|
||||||
|
### 内置模块(无需 import)
|
||||||
|
|
||||||
|
```python
|
||||||
|
datetime # datetime.now(), datetime.strptime()
|
||||||
|
date # date.today()
|
||||||
|
timedelta # timedelta(days=1)
|
||||||
|
time # time.sleep(), time.time()
|
||||||
|
json # json.dumps(), json.loads()
|
||||||
|
re # re.search(), re.match()
|
||||||
|
math # math.ceil(), math.floor()
|
||||||
|
random # random.randint(), random.choice()
|
||||||
|
hashlib # hashlib.md5()
|
||||||
|
base64 # base64.b64encode()
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 消息格式(result 变量)
|
||||||
|
|
||||||
|
### 基础格式(默认 markdown)
|
||||||
|
|
||||||
|
```python
|
||||||
|
result = {
|
||||||
|
'content': 'Markdown 内容',
|
||||||
|
'title': '消息标题'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 钉钉 ActionCard(交互卡片)
|
||||||
|
|
||||||
|
```python
|
||||||
|
result = {
|
||||||
|
'msg_type': 'actionCard',
|
||||||
|
'title': '卡片标题',
|
||||||
|
'content': '''### 正文内容
|
||||||
|
| 列1 | 列2 |
|
||||||
|
|:---:|:---:|
|
||||||
|
| A | B |
|
||||||
|
''',
|
||||||
|
'btn_orientation': '1', # 0-竖向 1-横向
|
||||||
|
'buttons': [
|
||||||
|
{'title': '按钮1', 'url': 'https://...'},
|
||||||
|
{'title': '按钮2', 'url': 'https://...'}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 钉钉 FeedCard(信息流)
|
||||||
|
|
||||||
|
```python
|
||||||
|
result = {
|
||||||
|
'msg_type': 'feedCard',
|
||||||
|
'links': [
|
||||||
|
{'title': '标题1', 'url': 'https://...', 'pic_url': 'https://...'},
|
||||||
|
{'title': '标题2', 'url': 'https://...', 'pic_url': 'https://...'}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 钉钉 Link(链接消息)
|
||||||
|
|
||||||
|
```python
|
||||||
|
result = {
|
||||||
|
'msg_type': 'link',
|
||||||
|
'title': '链接标题',
|
||||||
|
'content': '链接描述',
|
||||||
|
'url': 'https://...',
|
||||||
|
'pic_url': 'https://...'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 企微 News(图文消息)
|
||||||
|
|
||||||
|
```python
|
||||||
|
result = {
|
||||||
|
'msg_type': 'news',
|
||||||
|
'articles': [
|
||||||
|
{
|
||||||
|
'title': '文章标题',
|
||||||
|
'description': '文章描述',
|
||||||
|
'url': 'https://...',
|
||||||
|
'picurl': 'https://...'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 企微 Template Card(模板卡片)
|
||||||
|
|
||||||
|
```python
|
||||||
|
result = {
|
||||||
|
'msg_type': 'template_card',
|
||||||
|
'card_type': 'text_notice', # text_notice / news_notice / button_interaction
|
||||||
|
'title': '卡片标题',
|
||||||
|
'content': '卡片内容',
|
||||||
|
'horizontal_list': [
|
||||||
|
{'keyname': '申请人', 'value': '张三'},
|
||||||
|
{'keyname': '金额', 'value': '¥5,000'}
|
||||||
|
],
|
||||||
|
'jump_list': [
|
||||||
|
{'type': 1, 'title': '查看详情', 'url': 'https://...'}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## API 端点
|
||||||
|
|
||||||
|
### 定时任务
|
||||||
|
|
||||||
|
| 方法 | 路径 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| GET | /api/scheduled-tasks | 任务列表 |
|
||||||
|
| GET | /api/scheduled-tasks/{id} | 任务详情 |
|
||||||
|
| POST | /api/scheduled-tasks | 创建任务 |
|
||||||
|
| PUT | /api/scheduled-tasks/{id} | 更新任务 |
|
||||||
|
| DELETE | /api/scheduled-tasks/{id} | 删除任务 |
|
||||||
|
| POST | /api/scheduled-tasks/{id}/toggle | 启用/禁用 |
|
||||||
|
| POST | /api/scheduled-tasks/{id}/run | 立即执行 |
|
||||||
|
| GET | /api/scheduled-tasks/{id}/logs | 执行日志 |
|
||||||
|
| POST | /api/scheduled-tasks/test-script | 测试脚本 |
|
||||||
|
| GET | /api/scheduled-tasks/sdk-docs | SDK 文档 |
|
||||||
|
|
||||||
|
### 通知渠道
|
||||||
|
|
||||||
|
| 方法 | 路径 | 说明 |
|
||||||
|
|------|------|------|
|
||||||
|
| GET | /api/notification-channels | 渠道列表 |
|
||||||
|
| POST | /api/notification-channels | 创建渠道 |
|
||||||
|
| PUT | /api/notification-channels/{id} | 更新渠道 |
|
||||||
|
| DELETE | /api/notification-channels/{id} | 删除渠道 |
|
||||||
|
| POST | /api/notification-channels/{id}/test | 测试渠道 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 前端文件
|
||||||
|
|
||||||
|
```
|
||||||
|
frontend/src/views/
|
||||||
|
├── scheduled-tasks/
|
||||||
|
│ └── index.vue # 定时任务管理页面
|
||||||
|
└── notification-channels/
|
||||||
|
└── index.vue # 通知渠道管理页面
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 示例脚本
|
||||||
|
|
||||||
|
### 基础示例
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 无需 import,模块已内置
|
||||||
|
log('任务开始执行')
|
||||||
|
|
||||||
|
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||||
|
prompt = get_param('prompt', '默认提示词')
|
||||||
|
|
||||||
|
content = ai(prompt, system='你是一个助手')
|
||||||
|
|
||||||
|
result = {
|
||||||
|
'title': '每日推送',
|
||||||
|
'content': f'**生成时间**: {now}\n\n{content}'
|
||||||
|
}
|
||||||
|
|
||||||
|
log('任务执行完成')
|
||||||
|
```
|
||||||
|
|
||||||
|
### 复杂卡片示例
|
||||||
|
|
||||||
|
```python
|
||||||
|
log('生成销售日报')
|
||||||
|
|
||||||
|
now = datetime.now()
|
||||||
|
today = now.strftime('%Y年%m月%d日')
|
||||||
|
|
||||||
|
# 模拟数据
|
||||||
|
revenue = random.randint(50000, 150000)
|
||||||
|
|
||||||
|
result = {
|
||||||
|
'msg_type': 'actionCard',
|
||||||
|
'title': f'销售日报 | {today}',
|
||||||
|
'content': f'''### 今日业绩
|
||||||
|
|
||||||
|
| 指标 | 数值 |
|
||||||
|
|:---:|:---:|
|
||||||
|
| 销售额 | **¥{revenue:,}** |
|
||||||
|
| 订单数 | **{random.randint(40, 80)}** |
|
||||||
|
|
||||||
|
> 点击查看详情
|
||||||
|
''',
|
||||||
|
'buttons': [
|
||||||
|
{'title': '查看详情', 'url': 'https://example.com/report'}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 部署信息
|
||||||
|
|
||||||
|
- **测试环境**: https://platform.test.ai.ireborn.com.cn
|
||||||
|
- **数据库**: new_qiqi (测试) / new_platform_prod (生产)
|
||||||
|
- **Docker 容器**: platform-backend-test / platform-frontend-test
|
||||||
@@ -16,7 +16,9 @@
|
|||||||
"element-plus": "^2.5.0",
|
"element-plus": "^2.5.0",
|
||||||
"@element-plus/icons-vue": "^2.3.0",
|
"@element-plus/icons-vue": "^2.3.0",
|
||||||
"echarts": "^5.4.0",
|
"echarts": "^5.4.0",
|
||||||
"dayjs": "^1.11.0"
|
"dayjs": "^1.11.0",
|
||||||
|
"monaco-editor": "^0.45.0",
|
||||||
|
"@monaco-editor/loader": "^1.4.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@vitejs/plugin-vue": "^5.0.0",
|
"@vitejs/plugin-vue": "^5.0.0",
|
||||||
|
|||||||
@@ -18,9 +18,21 @@ function parseApiError(error) {
|
|||||||
status: 500
|
status: 500
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 网络错误(后端未启动、网络断开等)
|
||||||
if (!error.response) {
|
if (!error.response) {
|
||||||
|
if (error.code === 'ECONNABORTED') {
|
||||||
|
result.code = 'TIMEOUT_ERROR'
|
||||||
|
result.message = '请求超时,请稍后重试'
|
||||||
|
result.status = 0
|
||||||
|
} else if (error.message?.includes('Network Error')) {
|
||||||
|
result.code = 'SERVICE_UNAVAILABLE'
|
||||||
|
result.message = '服务暂时不可用,请稍后重试'
|
||||||
|
result.status = 503
|
||||||
|
} else {
|
||||||
result.code = 'NETWORK_ERROR'
|
result.code = 'NETWORK_ERROR'
|
||||||
result.message = '网络连接失败,请检查网络后重试'
|
result.message = '网络连接失败,请检查网络后重试'
|
||||||
|
result.status = 0
|
||||||
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,18 +52,23 @@ function parseApiError(error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 跳转到错误页面
|
* 跳转到错误页面(使用 sessionStorage + replace,不影响浏览器历史)
|
||||||
*/
|
*/
|
||||||
function navigateToErrorPage(errorInfo) {
|
function navigateToErrorPage(errorInfo) {
|
||||||
router.push({
|
// 记录当前页面路径(用于返回)
|
||||||
name: 'Error',
|
sessionStorage.setItem('errorFromPath', router.currentRoute.value.fullPath)
|
||||||
query: {
|
|
||||||
|
// 保存错误信息到 sessionStorage(不会显示在 URL 中)
|
||||||
|
sessionStorage.setItem('errorInfo', JSON.stringify({
|
||||||
code: errorInfo.code,
|
code: errorInfo.code,
|
||||||
message: errorInfo.message,
|
message: errorInfo.message,
|
||||||
trace_id: errorInfo.traceId,
|
traceId: errorInfo.traceId,
|
||||||
status: String(errorInfo.status)
|
status: errorInfo.status,
|
||||||
}
|
timestamp: Date.now()
|
||||||
})
|
}))
|
||||||
|
|
||||||
|
// 使用 replace 而不是 push,这样浏览器返回时不会停留在错误页
|
||||||
|
router.replace({ name: 'Error' })
|
||||||
}
|
}
|
||||||
|
|
||||||
// 请求拦截器
|
// 请求拦截器
|
||||||
@@ -75,6 +92,15 @@ api.interceptors.response.use(
|
|||||||
|
|
||||||
console.error(`[API Error] ${errorInfo.code}: ${errorInfo.message}${traceLog}`)
|
console.error(`[API Error] ${errorInfo.code}: ${errorInfo.message}${traceLog}`)
|
||||||
|
|
||||||
|
// 严重错误列表(跳转错误页)
|
||||||
|
const criticalErrors = [
|
||||||
|
'INTERNAL_ERROR',
|
||||||
|
'SERVICE_UNAVAILABLE',
|
||||||
|
'GATEWAY_ERROR',
|
||||||
|
'NETWORK_ERROR',
|
||||||
|
'TIMEOUT_ERROR'
|
||||||
|
]
|
||||||
|
|
||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
localStorage.removeItem('token')
|
localStorage.removeItem('token')
|
||||||
localStorage.removeItem('user')
|
localStorage.removeItem('user')
|
||||||
@@ -82,8 +108,8 @@ api.interceptors.response.use(
|
|||||||
ElMessage.error('登录已过期,请重新登录')
|
ElMessage.error('登录已过期,请重新登录')
|
||||||
} else if (error.response?.status === 403) {
|
} else if (error.response?.status === 403) {
|
||||||
ElMessage.error('没有权限执行此操作')
|
ElMessage.error('没有权限执行此操作')
|
||||||
} else if (['INTERNAL_ERROR', 'SERVICE_UNAVAILABLE', 'GATEWAY_ERROR'].includes(errorInfo.code)) {
|
} else if (criticalErrors.includes(errorInfo.code)) {
|
||||||
// 严重错误跳转到错误页面
|
// 严重错误(包括网络错误、服务不可用)跳转到错误页面
|
||||||
navigateToErrorPage(errorInfo)
|
navigateToErrorPage(errorInfo)
|
||||||
} else {
|
} else {
|
||||||
// 普通错误显示消息
|
// 普通错误显示消息
|
||||||
|
|||||||
@@ -17,7 +17,9 @@ const menuItems = computed(() => {
|
|||||||
{ path: '/tenant-wechat-apps', title: '企微应用', icon: 'ChatDotRound' },
|
{ path: '/tenant-wechat-apps', title: '企微应用', icon: 'ChatDotRound' },
|
||||||
{ path: '/app-config', title: '租户订阅', icon: 'Setting' },
|
{ path: '/app-config', title: '租户订阅', icon: 'Setting' },
|
||||||
{ path: '/stats', title: '统计分析', icon: 'TrendCharts' },
|
{ path: '/stats', title: '统计分析', icon: 'TrendCharts' },
|
||||||
{ path: '/logs', title: '日志查看', icon: 'Document' }
|
{ path: '/logs', title: '日志查看', icon: 'Document' },
|
||||||
|
{ path: '/scheduled-tasks', title: '定时任务', icon: 'Clock' },
|
||||||
|
{ path: '/notification-channels', title: '通知渠道', icon: 'Bell' }
|
||||||
]
|
]
|
||||||
|
|
||||||
// 管理员才能看到用户管理
|
// 管理员才能看到用户管理
|
||||||
|
|||||||
@@ -72,6 +72,18 @@ const routes = [
|
|||||||
name: 'Users',
|
name: 'Users',
|
||||||
component: () => import('@/views/users/index.vue'),
|
component: () => import('@/views/users/index.vue'),
|
||||||
meta: { title: '用户管理', icon: 'User', role: 'admin' }
|
meta: { title: '用户管理', icon: 'User', role: 'admin' }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: 'scheduled-tasks',
|
||||||
|
name: 'ScheduledTasks',
|
||||||
|
component: () => import('@/views/scheduled-tasks/index.vue'),
|
||||||
|
meta: { title: '定时任务', icon: 'Clock' }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: 'notification-channels',
|
||||||
|
name: 'NotificationChannels',
|
||||||
|
component: () => import('@/views/notification-channels/index.vue'),
|
||||||
|
meta: { title: '通知渠道', icon: 'Bell' }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, reactive, onMounted, computed, watch } from 'vue'
|
import { ref, reactive, onMounted, computed, watch } from 'vue'
|
||||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||||
import { Delete, Plus, CopyDocument } from '@element-plus/icons-vue'
|
|
||||||
import api from '@/api'
|
import api from '@/api'
|
||||||
import { useAuthStore } from '@/stores/auth'
|
import { useAuthStore } from '@/stores/auth'
|
||||||
|
|
||||||
@@ -19,14 +18,11 @@ const query = reactive({
|
|||||||
|
|
||||||
// 租户列表
|
// 租户列表
|
||||||
const tenantList = ref([])
|
const tenantList = ref([])
|
||||||
const tenantMap = ref({}) // code -> name 映射
|
|
||||||
|
|
||||||
// 应用列表(从应用管理获取)
|
// 应用列表(从应用管理获取)
|
||||||
const appList = ref([])
|
const appList = ref([])
|
||||||
const appMap = ref({}) // app_code -> app_name 映射
|
|
||||||
const appRequireJssdk = ref({}) // app_code -> require_jssdk
|
const appRequireJssdk = ref({}) // app_code -> require_jssdk
|
||||||
const appBaseUrl = ref({}) // app_code -> base_url
|
const appBaseUrl = ref({}) // app_code -> base_url
|
||||||
const appConfigSchema = ref({}) // app_code -> config_schema
|
|
||||||
|
|
||||||
// 企微应用列表(按租户)
|
// 企微应用列表(按租户)
|
||||||
const wechatAppList = ref([])
|
const wechatAppList = ref([])
|
||||||
@@ -40,8 +36,7 @@ const form = reactive({
|
|||||||
tenant_id: '',
|
tenant_id: '',
|
||||||
app_code: '',
|
app_code: '',
|
||||||
app_name: '',
|
app_name: '',
|
||||||
wechat_app_id: null,
|
wechat_app_id: null
|
||||||
custom_configs: [] // 自定义配置 [{key, value, remark}]
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// 当前选择的应用是否需要 JS-SDK
|
// 当前选择的应用是否需要 JS-SDK
|
||||||
@@ -49,44 +44,6 @@ const currentAppRequireJssdk = computed(() => {
|
|||||||
return appRequireJssdk.value[form.app_code] || false
|
return appRequireJssdk.value[form.app_code] || false
|
||||||
})
|
})
|
||||||
|
|
||||||
// 当前应用的配置项定义
|
|
||||||
const currentConfigSchema = computed(() => {
|
|
||||||
return appConfigSchema.value[form.app_code] || []
|
|
||||||
})
|
|
||||||
|
|
||||||
// 配置值映射(方便读写)
|
|
||||||
const configValues = computed(() => {
|
|
||||||
const map = {}
|
|
||||||
form.custom_configs.forEach(c => {
|
|
||||||
map[c.key] = c
|
|
||||||
})
|
|
||||||
return map
|
|
||||||
})
|
|
||||||
|
|
||||||
// 获取配置值
|
|
||||||
function getConfigValue(key) {
|
|
||||||
return configValues.value[key]?.value || ''
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置配置值
|
|
||||||
function setConfigValue(key, value, remark = '') {
|
|
||||||
const existing = form.custom_configs.find(c => c.key === key)
|
|
||||||
if (existing) {
|
|
||||||
existing.value = value
|
|
||||||
if (remark) existing.remark = remark
|
|
||||||
} else {
|
|
||||||
form.custom_configs.push({ key, value, remark })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取选项显示名称
|
|
||||||
function getOptionLabel(schema, optionValue) {
|
|
||||||
if (schema.option_labels && schema.option_labels[optionValue]) {
|
|
||||||
return schema.option_labels[optionValue]
|
|
||||||
}
|
|
||||||
return optionValue
|
|
||||||
}
|
|
||||||
|
|
||||||
// 验证 app_code 必须是有效的应用
|
// 验证 app_code 必须是有效的应用
|
||||||
const validateAppCode = (rule, value, callback) => {
|
const validateAppCode = (rule, value, callback) => {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
@@ -113,14 +70,6 @@ watch(() => form.tenant_id, async (newVal) => {
|
|||||||
form.wechat_app_id = null
|
form.wechat_app_id = null
|
||||||
})
|
})
|
||||||
|
|
||||||
// 监听应用选择变化,初始化配置默认值
|
|
||||||
watch(() => form.app_code, (newVal) => {
|
|
||||||
if (newVal && !editingId.value) {
|
|
||||||
// 新建时,根据 schema 初始化默认值
|
|
||||||
initConfigDefaults()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// 查看 Token 对话框
|
// 查看 Token 对话框
|
||||||
const tokenDialogVisible = ref(false)
|
const tokenDialogVisible = ref(false)
|
||||||
const currentToken = ref('')
|
const currentToken = ref('')
|
||||||
@@ -130,59 +79,26 @@ async function fetchTenants() {
|
|||||||
try {
|
try {
|
||||||
const res = await api.get('/api/tenants', { params: { size: 1000 } })
|
const res = await api.get('/api/tenants', { params: { size: 1000 } })
|
||||||
tenantList.value = res.data.items || []
|
tenantList.value = res.data.items || []
|
||||||
// 构建 code -> name 映射
|
|
||||||
const map = {}
|
|
||||||
tenantList.value.forEach(t => {
|
|
||||||
map[t.code] = t.name
|
|
||||||
})
|
|
||||||
tenantMap.value = map
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('获取租户列表失败:', e)
|
console.error('获取租户列表失败:', e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取租户中文名
|
|
||||||
function getTenantName(code) {
|
|
||||||
return tenantMap.value[code] || code
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取应用中文名
|
|
||||||
function getAppName(code) {
|
|
||||||
return appMap.value[code] || code
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fetchApps() {
|
async function fetchApps() {
|
||||||
try {
|
try {
|
||||||
const res = await api.get('/api/apps', { params: { size: 100 } })
|
const res = await api.get('/api/apps', { params: { size: 100 } })
|
||||||
const apps = res.data.items || []
|
const apps = res.data.items || []
|
||||||
appList.value = apps.map(a => ({ app_code: a.app_code, app_name: a.app_name }))
|
appList.value = apps.map(a => ({ app_code: a.app_code, app_name: a.app_name }))
|
||||||
|
|
||||||
// 构建 app_code -> app_name 映射
|
|
||||||
const map = {}
|
|
||||||
for (const app of apps) {
|
for (const app of apps) {
|
||||||
map[app.app_code] = app.app_name
|
|
||||||
appRequireJssdk.value[app.app_code] = app.require_jssdk || false
|
appRequireJssdk.value[app.app_code] = app.require_jssdk || false
|
||||||
appBaseUrl.value[app.app_code] = app.base_url || ''
|
appBaseUrl.value[app.app_code] = app.base_url || ''
|
||||||
appConfigSchema.value[app.app_code] = app.config_schema || []
|
|
||||||
}
|
}
|
||||||
appMap.value = map
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('获取应用列表失败:', e)
|
console.error('获取应用列表失败:', e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据 schema 初始化配置默认值
|
|
||||||
function initConfigDefaults() {
|
|
||||||
const schema = currentConfigSchema.value
|
|
||||||
if (!schema.length) return
|
|
||||||
|
|
||||||
form.custom_configs = schema.map(s => ({
|
|
||||||
key: s.key,
|
|
||||||
value: s.default || '',
|
|
||||||
remark: ''
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fetchWechatApps(tenantId) {
|
async function fetchWechatApps(tenantId) {
|
||||||
if (!tenantId) {
|
if (!tenantId) {
|
||||||
wechatAppList.value = []
|
wechatAppList.value = []
|
||||||
@@ -226,8 +142,7 @@ function handleCreate() {
|
|||||||
tenant_id: '',
|
tenant_id: '',
|
||||||
app_code: '',
|
app_code: '',
|
||||||
app_name: '',
|
app_name: '',
|
||||||
wechat_app_id: null,
|
wechat_app_id: null
|
||||||
custom_configs: []
|
|
||||||
})
|
})
|
||||||
wechatAppList.value = []
|
wechatAppList.value = []
|
||||||
dialogVisible.value = true
|
dialogVisible.value = true
|
||||||
@@ -236,49 +151,16 @@ function handleCreate() {
|
|||||||
async function handleEdit(row) {
|
async function handleEdit(row) {
|
||||||
editingId.value = row.id
|
editingId.value = row.id
|
||||||
dialogTitle.value = '编辑应用订阅'
|
dialogTitle.value = '编辑应用订阅'
|
||||||
|
|
||||||
// 先获取 schema
|
|
||||||
const schema = appConfigSchema.value[row.app_code] || []
|
|
||||||
|
|
||||||
// 合并已有配置和 schema 默认值
|
|
||||||
const existingConfigs = row.custom_configs || []
|
|
||||||
const existingMap = {}
|
|
||||||
existingConfigs.forEach(c => { existingMap[c.key] = c })
|
|
||||||
|
|
||||||
// 构建完整的配置列表(包含 schema 中的所有配置项)
|
|
||||||
const mergedConfigs = schema.map(s => ({
|
|
||||||
key: s.key,
|
|
||||||
value: existingMap[s.key]?.value ?? s.default ?? '',
|
|
||||||
remark: existingMap[s.key]?.remark ?? ''
|
|
||||||
}))
|
|
||||||
|
|
||||||
// 添加 schema 中没有但已存在的配置(兼容旧数据)
|
|
||||||
existingConfigs.forEach(c => {
|
|
||||||
if (!schema.find(s => s.key === c.key)) {
|
|
||||||
mergedConfigs.push({ ...c })
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.assign(form, {
|
Object.assign(form, {
|
||||||
tenant_id: row.tenant_id,
|
tenant_id: row.tenant_id,
|
||||||
app_code: row.app_code,
|
app_code: row.app_code,
|
||||||
app_name: row.app_name || '',
|
app_name: row.app_name || '',
|
||||||
wechat_app_id: row.wechat_app_id || null,
|
wechat_app_id: row.wechat_app_id || null
|
||||||
custom_configs: mergedConfigs
|
|
||||||
})
|
})
|
||||||
await fetchWechatApps(row.tenant_id)
|
await fetchWechatApps(row.tenant_id)
|
||||||
dialogVisible.value = true
|
dialogVisible.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// 自定义配置管理(用于没有 schema 定义时的手动添加)
|
|
||||||
function addCustomConfig() {
|
|
||||||
form.custom_configs.push({ key: '', value: '', remark: '' })
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeCustomConfig(index) {
|
|
||||||
form.custom_configs.splice(index, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
async function handleSubmit() {
|
async function handleSubmit() {
|
||||||
await formRef.value.validate()
|
await formRef.value.validate()
|
||||||
@@ -350,44 +232,9 @@ function handleCopyUrl() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function handleViewToken(row) {
|
async function handleViewToken(row) {
|
||||||
try {
|
// 这里需要后端返回真实 token,暂时用 placeholder
|
||||||
const res = await api.get(`/api/tenant-apps/${row.id}/token`)
|
// 实际生产中可能需要单独 API 获取
|
||||||
currentToken.value = res.data.access_token
|
showToken(row.access_token === '******' ? '需要调用API获取' : row.access_token, row.app_code)
|
||||||
currentAppUrl.value = res.data.base_url || ''
|
|
||||||
tokenDialogVisible.value = true
|
|
||||||
} catch (e) {
|
|
||||||
// 错误已在拦截器处理
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 快速选择租户标签
|
|
||||||
function selectTenant(code) {
|
|
||||||
if (query.tenant_id === code) {
|
|
||||||
query.tenant_id = '' // 再次点击取消选中
|
|
||||||
} else {
|
|
||||||
query.tenant_id = code
|
|
||||||
}
|
|
||||||
handleSearch()
|
|
||||||
}
|
|
||||||
|
|
||||||
// 复制带 token 的链接
|
|
||||||
async function copyTokenLink(row) {
|
|
||||||
try {
|
|
||||||
const res = await api.get(`/api/tenant-apps/${row.id}/token`)
|
|
||||||
const token = res.data.access_token
|
|
||||||
const baseUrl = res.data.base_url || appBaseUrl.value[row.app_code] || ''
|
|
||||||
|
|
||||||
if (!baseUrl) {
|
|
||||||
ElMessage.warning('该应用未配置访问地址')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = `${baseUrl}?token=${token}`
|
|
||||||
await navigator.clipboard.writeText(url)
|
|
||||||
ElMessage.success('链接已复制')
|
|
||||||
} catch (e) {
|
|
||||||
ElMessage.error('复制失败')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
@@ -413,32 +260,16 @@ onMounted(() => {
|
|||||||
</el-alert>
|
</el-alert>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 租户快速筛选标签 -->
|
|
||||||
<div class="tenant-tags">
|
|
||||||
<span class="tag-label">租户筛选:</span>
|
|
||||||
<el-tag
|
|
||||||
v-for="tenant in tenantList"
|
|
||||||
:key="tenant.code"
|
|
||||||
:type="query.tenant_id === tenant.code ? '' : 'info'"
|
|
||||||
:effect="query.tenant_id === tenant.code ? 'dark' : 'plain'"
|
|
||||||
class="tenant-tag"
|
|
||||||
@click="selectTenant(tenant.code)"
|
|
||||||
>
|
|
||||||
{{ tenant.name }}
|
|
||||||
</el-tag>
|
|
||||||
<el-tag
|
|
||||||
v-if="query.tenant_id"
|
|
||||||
type="danger"
|
|
||||||
effect="plain"
|
|
||||||
class="tenant-tag clear-tag"
|
|
||||||
@click="selectTenant('')"
|
|
||||||
>
|
|
||||||
清除筛选
|
|
||||||
</el-tag>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- 搜索栏 -->
|
<!-- 搜索栏 -->
|
||||||
<div class="search-bar">
|
<div class="search-bar">
|
||||||
|
<el-select v-model="query.tenant_id" placeholder="选择租户" clearable filterable style="width: 200px">
|
||||||
|
<el-option
|
||||||
|
v-for="tenant in tenantList"
|
||||||
|
:key="tenant.code"
|
||||||
|
:label="`${tenant.name} (${tenant.code})`"
|
||||||
|
:value="tenant.code"
|
||||||
|
/>
|
||||||
|
</el-select>
|
||||||
<el-select v-model="query.app_code" placeholder="选择应用" clearable style="width: 150px">
|
<el-select v-model="query.app_code" placeholder="选择应用" clearable style="width: 150px">
|
||||||
<el-option v-for="app in appList" :key="app.app_code" :label="app.app_name" :value="app.app_code" />
|
<el-option v-for="app in appList" :key="app.app_code" :label="app.app_name" :value="app.app_code" />
|
||||||
</el-select>
|
</el-select>
|
||||||
@@ -448,33 +279,10 @@ onMounted(() => {
|
|||||||
<!-- 表格 -->
|
<!-- 表格 -->
|
||||||
<el-table v-loading="loading" :data="tableData" style="width: 100%">
|
<el-table v-loading="loading" :data="tableData" style="width: 100%">
|
||||||
<el-table-column prop="id" label="ID" width="60" />
|
<el-table-column prop="id" label="ID" width="60" />
|
||||||
<el-table-column label="租户" width="120">
|
<el-table-column prop="tenant_id" label="租户ID" width="120" />
|
||||||
<template #default="{ row }">
|
<el-table-column prop="app_code" label="应用代码" width="150" />
|
||||||
<span class="cell-name">{{ getTenantName(row.tenant_id) }}</span>
|
<el-table-column prop="app_name" label="备注名称" width="150" />
|
||||||
</template>
|
<el-table-column label="企微应用" width="180">
|
||||||
</el-table-column>
|
|
||||||
<el-table-column label="应用" width="150">
|
|
||||||
<template #default="{ row }">
|
|
||||||
<span class="cell-name">{{ getAppName(row.app_code) }}</span>
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
<el-table-column prop="app_name" label="备注" width="120" />
|
|
||||||
<el-table-column label="快捷链接" width="100">
|
|
||||||
<template #default="{ row }">
|
|
||||||
<el-button
|
|
||||||
v-if="row.access_token && appBaseUrl[row.app_code]"
|
|
||||||
type="primary"
|
|
||||||
link
|
|
||||||
size="small"
|
|
||||||
@click="copyTokenLink(row)"
|
|
||||||
>
|
|
||||||
<el-icon><CopyDocument /></el-icon>
|
|
||||||
复制
|
|
||||||
</el-button>
|
|
||||||
<span v-else style="color: #c0c4cc; font-size: 12px">-</span>
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
<el-table-column label="企微应用" width="140">
|
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
<template v-if="row.wechat_app">
|
<template v-if="row.wechat_app">
|
||||||
<el-tag type="success" size="small">{{ row.wechat_app.name }}</el-tag>
|
<el-tag type="success" size="small">{{ row.wechat_app.name }}</el-tag>
|
||||||
@@ -482,26 +290,24 @@ onMounted(() => {
|
|||||||
<el-tag v-else type="info" size="small">未关联</el-tag>
|
<el-tag v-else type="info" size="small">未关联</el-tag>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column label="配置" width="80">
|
<el-table-column label="Token 状态" width="120">
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
<el-tag v-if="row.custom_configs && row.custom_configs.length > 0" type="primary" size="small">
|
<el-tag v-if="row.access_token" type="success" size="small">已生成</el-tag>
|
||||||
{{ row.custom_configs.length }} 项
|
<el-tag v-else type="danger" size="small">未生成</el-tag>
|
||||||
</el-tag>
|
|
||||||
<span v-else style="color: #909399; font-size: 12px">-</span>
|
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column label="状态" width="70">
|
<el-table-column label="状态" width="80">
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
<el-tag :type="row.status === 1 ? 'success' : 'info'" size="small">
|
<el-tag :type="row.status === 1 ? 'success' : 'info'" size="small">
|
||||||
{{ row.status === 1 ? '启用' : '禁用' }}
|
{{ row.status === 1 ? '启用' : '禁用' }}
|
||||||
</el-tag>
|
</el-tag>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column label="操作" width="240" fixed="right">
|
<el-table-column label="操作" width="280" fixed="right">
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
<el-button v-if="authStore.isOperator" type="primary" link size="small" @click="handleEdit(row)">编辑</el-button>
|
<el-button v-if="authStore.isOperator" type="primary" link size="small" @click="handleEdit(row)">编辑</el-button>
|
||||||
<el-button v-if="authStore.isOperator" type="success" link size="small" @click="handleViewToken(row)">Token</el-button>
|
<el-button v-if="authStore.isOperator" type="success" link size="small" @click="handleViewToken(row)">查看Token</el-button>
|
||||||
<el-button v-if="authStore.isOperator" type="warning" link size="small" @click="handleRegenerateToken(row)">重置</el-button>
|
<el-button v-if="authStore.isOperator" type="warning" link size="small" @click="handleRegenerateToken(row)">重置Token</el-button>
|
||||||
<el-button v-if="authStore.isOperator" type="danger" link size="small" @click="handleDelete(row)">删除</el-button>
|
<el-button v-if="authStore.isOperator" type="danger" link size="small" @click="handleDelete(row)">删除</el-button>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
@@ -519,7 +325,7 @@ onMounted(() => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 编辑对话框 -->
|
<!-- 编辑对话框 -->
|
||||||
<el-dialog v-model="dialogVisible" :title="dialogTitle" width="750px">
|
<el-dialog v-model="dialogVisible" :title="dialogTitle" width="550px">
|
||||||
<el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
|
<el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
|
||||||
<el-form-item label="租户" prop="tenant_id">
|
<el-form-item label="租户" prop="tenant_id">
|
||||||
<el-select
|
<el-select
|
||||||
@@ -568,119 +374,6 @@ onMounted(() => {
|
|||||||
</div>
|
</div>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<!-- 自定义配置 -->
|
|
||||||
<template v-if="currentConfigSchema.length > 0 || form.custom_configs.length > 0">
|
|
||||||
<el-divider content-position="left">应用配置</el-divider>
|
|
||||||
|
|
||||||
<div class="custom-configs-section">
|
|
||||||
<!-- 根据 schema 渲染表单 -->
|
|
||||||
<template v-for="(schema, index) in currentConfigSchema" :key="schema.key">
|
|
||||||
<div class="config-item-schema">
|
|
||||||
<div class="config-label">
|
|
||||||
{{ schema.label }}
|
|
||||||
<span v-if="schema.required" class="required-star">*</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- text 类型 -->
|
|
||||||
<template v-if="schema.type === 'text'">
|
|
||||||
<el-input
|
|
||||||
v-model="form.custom_configs[index].value"
|
|
||||||
type="textarea"
|
|
||||||
:rows="2"
|
|
||||||
:autosize="{ minRows: 2, maxRows: 12 }"
|
|
||||||
:placeholder="schema.placeholder || '请输入'"
|
|
||||||
/>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<!-- radio 类型 -->
|
|
||||||
<template v-else-if="schema.type === 'radio'">
|
|
||||||
<el-radio-group v-model="form.custom_configs[index].value">
|
|
||||||
<el-radio
|
|
||||||
v-for="opt in schema.options"
|
|
||||||
:key="opt"
|
|
||||||
:value="opt"
|
|
||||||
>
|
|
||||||
{{ getOptionLabel(schema, opt) }}
|
|
||||||
</el-radio>
|
|
||||||
</el-radio-group>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<!-- select 类型 -->
|
|
||||||
<template v-else-if="schema.type === 'select'">
|
|
||||||
<el-select v-model="form.custom_configs[index].value" placeholder="请选择" style="width: 100%">
|
|
||||||
<el-option
|
|
||||||
v-for="opt in schema.options"
|
|
||||||
:key="opt"
|
|
||||||
:label="getOptionLabel(schema, opt)"
|
|
||||||
:value="opt"
|
|
||||||
/>
|
|
||||||
</el-select>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<!-- switch 类型 -->
|
|
||||||
<template v-else-if="schema.type === 'switch'">
|
|
||||||
<el-switch
|
|
||||||
v-model="form.custom_configs[index].value"
|
|
||||||
active-value="true"
|
|
||||||
inactive-value="false"
|
|
||||||
/>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<!-- 备注输入 -->
|
|
||||||
<el-input
|
|
||||||
v-model="form.custom_configs[index].remark"
|
|
||||||
placeholder="备注(可选)"
|
|
||||||
style="margin-top: 8px; width: 300px"
|
|
||||||
size="small"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<!-- 没有 schema 定义时显示手动配置 -->
|
|
||||||
<template v-if="currentConfigSchema.length === 0">
|
|
||||||
<div v-for="(config, index) in form.custom_configs" :key="index" class="config-item">
|
|
||||||
<div class="config-row">
|
|
||||||
<el-input
|
|
||||||
v-model="config.key"
|
|
||||||
placeholder="配置键 (如: industry)"
|
|
||||||
style="width: 150px"
|
|
||||||
/>
|
|
||||||
<el-input
|
|
||||||
v-model="config.remark"
|
|
||||||
placeholder="备注说明"
|
|
||||||
style="width: 180px; margin-left: 8px"
|
|
||||||
/>
|
|
||||||
<el-button
|
|
||||||
type="danger"
|
|
||||||
:icon="Delete"
|
|
||||||
circle
|
|
||||||
size="small"
|
|
||||||
style="margin-left: 8px"
|
|
||||||
@click="removeCustomConfig(index)"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<el-input
|
|
||||||
v-model="config.value"
|
|
||||||
type="textarea"
|
|
||||||
:rows="3"
|
|
||||||
:autosize="{ minRows: 2, maxRows: 10 }"
|
|
||||||
placeholder="配置值(支持超长文本,如提示词等)"
|
|
||||||
style="margin-top: 8px"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<el-button type="primary" plain @click="addCustomConfig" style="margin-top: 8px">
|
|
||||||
<el-icon><Plus /></el-icon>
|
|
||||||
添加配置项
|
|
||||||
</el-button>
|
|
||||||
|
|
||||||
<div v-if="form.custom_configs.length === 0" class="config-empty-tip">
|
|
||||||
该应用暂无配置项定义
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</el-form>
|
</el-form>
|
||||||
<template #footer>
|
<template #footer>
|
||||||
<el-button @click="dialogVisible = false">取消</el-button>
|
<el-button @click="dialogVisible = false">取消</el-button>
|
||||||
@@ -738,43 +431,6 @@ onMounted(() => {
|
|||||||
margin-bottom: 16px;
|
margin-bottom: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 租户标签筛选 */
|
|
||||||
.tenant-tags {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
gap: 8px;
|
|
||||||
margin-bottom: 16px;
|
|
||||||
padding: 12px 16px;
|
|
||||||
background: #f5f7fa;
|
|
||||||
border-radius: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.tag-label {
|
|
||||||
color: #606266;
|
|
||||||
font-size: 13px;
|
|
||||||
font-weight: 500;
|
|
||||||
margin-right: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.tenant-tag {
|
|
||||||
cursor: pointer;
|
|
||||||
transition: all 0.2s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.tenant-tag:hover {
|
|
||||||
transform: translateY(-1px);
|
|
||||||
}
|
|
||||||
|
|
||||||
.clear-tag {
|
|
||||||
margin-left: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.cell-name {
|
|
||||||
font-weight: 500;
|
|
||||||
color: #303133;
|
|
||||||
}
|
|
||||||
|
|
||||||
.token-dialog-content {
|
.token-dialog-content {
|
||||||
padding: 0 10px;
|
padding: 0 10px;
|
||||||
}
|
}
|
||||||
@@ -788,47 +444,4 @@ onMounted(() => {
|
|||||||
margin-bottom: 8px;
|
margin-bottom: 8px;
|
||||||
color: #303133;
|
color: #303133;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 自定义配置样式 */
|
|
||||||
.custom-configs-section {
|
|
||||||
padding: 0 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-item {
|
|
||||||
background: #f5f7fa;
|
|
||||||
border-radius: 8px;
|
|
||||||
padding: 12px;
|
|
||||||
margin-bottom: 12px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-item-schema {
|
|
||||||
background: #f5f7fa;
|
|
||||||
border-radius: 8px;
|
|
||||||
padding: 16px;
|
|
||||||
margin-bottom: 12px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-label {
|
|
||||||
font-weight: 500;
|
|
||||||
color: #303133;
|
|
||||||
margin-bottom: 10px;
|
|
||||||
font-size: 14px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.required-star {
|
|
||||||
color: #f56c6c;
|
|
||||||
margin-left: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-row {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-empty-tip {
|
|
||||||
color: #909399;
|
|
||||||
font-size: 13px;
|
|
||||||
text-align: center;
|
|
||||||
padding: 20px 0;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, reactive, onMounted } from 'vue'
|
import { ref, reactive, onMounted } from 'vue'
|
||||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||||
import { Delete, Plus } from '@element-plus/icons-vue'
|
|
||||||
import api from '@/api'
|
import api from '@/api'
|
||||||
import { useAuthStore } from '@/stores/auth'
|
import { useAuthStore } from '@/stores/auth'
|
||||||
|
|
||||||
@@ -15,14 +14,6 @@ const query = reactive({
|
|||||||
size: 20
|
size: 20
|
||||||
})
|
})
|
||||||
|
|
||||||
// 配置项类型选项
|
|
||||||
const configTypes = [
|
|
||||||
{ value: 'text', label: '文本输入' },
|
|
||||||
{ value: 'radio', label: '单选' },
|
|
||||||
{ value: 'select', label: '下拉选择' },
|
|
||||||
{ value: 'switch', label: '开关' }
|
|
||||||
]
|
|
||||||
|
|
||||||
// 对话框
|
// 对话框
|
||||||
const dialogVisible = ref(false)
|
const dialogVisible = ref(false)
|
||||||
const dialogTitle = ref('')
|
const dialogTitle = ref('')
|
||||||
@@ -33,8 +24,7 @@ const form = reactive({
|
|||||||
app_name: '',
|
app_name: '',
|
||||||
base_url: '',
|
base_url: '',
|
||||||
description: '',
|
description: '',
|
||||||
require_jssdk: false,
|
require_jssdk: false
|
||||||
config_schema: [] // 配置项定义
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const rules = {
|
const rules = {
|
||||||
@@ -73,8 +63,7 @@ function handleCreate() {
|
|||||||
app_name: '',
|
app_name: '',
|
||||||
base_url: '',
|
base_url: '',
|
||||||
description: '',
|
description: '',
|
||||||
require_jssdk: false,
|
require_jssdk: false
|
||||||
config_schema: []
|
|
||||||
})
|
})
|
||||||
dialogVisible.value = true
|
dialogVisible.value = true
|
||||||
}
|
}
|
||||||
@@ -87,55 +76,11 @@ function handleEdit(row) {
|
|||||||
app_name: row.app_name,
|
app_name: row.app_name,
|
||||||
base_url: row.base_url || '',
|
base_url: row.base_url || '',
|
||||||
description: row.description || '',
|
description: row.description || '',
|
||||||
require_jssdk: row.require_jssdk || false,
|
require_jssdk: row.require_jssdk || false
|
||||||
config_schema: row.config_schema ? row.config_schema.map(c => ({
|
|
||||||
...c,
|
|
||||||
options: c.options || [],
|
|
||||||
option_labels: c.option_labels || {}
|
|
||||||
})) : []
|
|
||||||
})
|
})
|
||||||
dialogVisible.value = true
|
dialogVisible.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// 配置项管理
|
|
||||||
function addConfigItem() {
|
|
||||||
form.config_schema.push({
|
|
||||||
key: '',
|
|
||||||
label: '',
|
|
||||||
type: 'text',
|
|
||||||
options: [],
|
|
||||||
option_labels: {},
|
|
||||||
default: '',
|
|
||||||
placeholder: '',
|
|
||||||
required: false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeConfigItem(index) {
|
|
||||||
form.config_schema.splice(index, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 选项管理(radio/select 类型)
|
|
||||||
function addOption(config) {
|
|
||||||
const optionKey = `option_${config.options.length + 1}`
|
|
||||||
config.options.push(optionKey)
|
|
||||||
config.option_labels[optionKey] = ''
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeOption(config, index) {
|
|
||||||
const optionKey = config.options[index]
|
|
||||||
config.options.splice(index, 1)
|
|
||||||
delete config.option_labels[optionKey]
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateOptionKey(config, index, newKey) {
|
|
||||||
const oldKey = config.options[index]
|
|
||||||
const oldLabel = config.option_labels[oldKey]
|
|
||||||
delete config.option_labels[oldKey]
|
|
||||||
config.options[index] = newKey
|
|
||||||
config.option_labels[newKey] = oldLabel || ''
|
|
||||||
}
|
|
||||||
|
|
||||||
async function handleSubmit() {
|
async function handleSubmit() {
|
||||||
await formRef.value.validate()
|
await formRef.value.validate()
|
||||||
|
|
||||||
@@ -209,14 +154,6 @@ onMounted(() => {
|
|||||||
<el-table-column prop="app_code" label="应用代码" width="150" />
|
<el-table-column prop="app_code" label="应用代码" width="150" />
|
||||||
<el-table-column prop="app_name" label="应用名称" width="180" />
|
<el-table-column prop="app_name" label="应用名称" width="180" />
|
||||||
<el-table-column prop="base_url" label="访问地址" min-width="300" show-overflow-tooltip />
|
<el-table-column prop="base_url" label="访问地址" min-width="300" show-overflow-tooltip />
|
||||||
<el-table-column label="配置项" width="90">
|
|
||||||
<template #default="{ row }">
|
|
||||||
<el-tag v-if="row.config_schema && row.config_schema.length > 0" type="primary" size="small">
|
|
||||||
{{ row.config_schema.length }} 项
|
|
||||||
</el-tag>
|
|
||||||
<span v-else style="color: #909399; font-size: 12px">-</span>
|
|
||||||
</template>
|
|
||||||
</el-table-column>
|
|
||||||
<el-table-column label="JS-SDK" width="90">
|
<el-table-column label="JS-SDK" width="90">
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
<el-tag :type="row.require_jssdk ? 'warning' : 'info'" size="small">
|
<el-tag :type="row.require_jssdk ? 'warning' : 'info'" size="small">
|
||||||
@@ -254,7 +191,7 @@ onMounted(() => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 编辑对话框 -->
|
<!-- 编辑对话框 -->
|
||||||
<el-dialog v-model="dialogVisible" :title="dialogTitle" width="800px">
|
<el-dialog v-model="dialogVisible" :title="dialogTitle" width="550px">
|
||||||
<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
|
<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
|
||||||
<el-form-item label="应用代码" prop="app_code">
|
<el-form-item label="应用代码" prop="app_code">
|
||||||
<el-input v-model="form.app_code" :disabled="!!editingId" placeholder="唯一标识,如: brainstorm" />
|
<el-input v-model="form.app_code" :disabled="!!editingId" placeholder="唯一标识,如: brainstorm" />
|
||||||
@@ -275,69 +212,6 @@ onMounted(() => {
|
|||||||
<el-switch v-model="form.require_jssdk" />
|
<el-switch v-model="form.require_jssdk" />
|
||||||
<span style="margin-left: 12px; color: #909399; font-size: 12px">开启后租户需关联企微应用</span>
|
<span style="margin-left: 12px; color: #909399; font-size: 12px">开启后租户需关联企微应用</span>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
|
||||||
<!-- 配置项定义 -->
|
|
||||||
<el-divider content-position="left">配置项定义</el-divider>
|
|
||||||
<div class="config-schema-section">
|
|
||||||
<div class="config-schema-tip">
|
|
||||||
定义租户订阅时可配置的参数,如行业类型、提示词等
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-for="(config, index) in form.config_schema" :key="index" class="config-schema-item">
|
|
||||||
<div class="config-header">
|
|
||||||
<span class="config-index">#{{ index + 1 }}</span>
|
|
||||||
<el-button type="danger" :icon="Delete" circle size="small" @click="removeConfigItem(index)" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="config-row">
|
|
||||||
<el-input v-model="config.key" placeholder="配置键(如:industry)" style="width: 140px" />
|
|
||||||
<el-input v-model="config.label" placeholder="显示标签(如:行业类型)" style="width: 160px" />
|
|
||||||
<el-select v-model="config.type" placeholder="类型" style="width: 120px">
|
|
||||||
<el-option v-for="t in configTypes" :key="t.value" :label="t.label" :value="t.value" />
|
|
||||||
</el-select>
|
|
||||||
<el-checkbox v-model="config.required">必填</el-checkbox>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- text 类型:显示 placeholder -->
|
|
||||||
<div v-if="config.type === 'text'" class="config-row" style="margin-top: 8px">
|
|
||||||
<el-input v-model="config.placeholder" placeholder="输入提示文字" style="width: 300px" />
|
|
||||||
<el-input v-model="config.default" placeholder="默认值" style="width: 200px" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- switch 类型:显示默认值 -->
|
|
||||||
<div v-if="config.type === 'switch'" class="config-row" style="margin-top: 8px">
|
|
||||||
<span style="color: #606266; margin-right: 8px">默认值:</span>
|
|
||||||
<el-switch v-model="config.default" active-value="true" inactive-value="false" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!-- radio/select 类型:显示选项编辑 -->
|
|
||||||
<div v-if="config.type === 'radio' || config.type === 'select'" class="config-options">
|
|
||||||
<div class="options-label">选项列表:</div>
|
|
||||||
<div v-for="(opt, optIndex) in config.options" :key="optIndex" class="option-row">
|
|
||||||
<el-input
|
|
||||||
:model-value="opt"
|
|
||||||
@update:model-value="v => updateOptionKey(config, optIndex, v)"
|
|
||||||
placeholder="选项值(如:medical)"
|
|
||||||
style="width: 140px"
|
|
||||||
/>
|
|
||||||
<el-input
|
|
||||||
v-model="config.option_labels[opt]"
|
|
||||||
placeholder="显示名(如:医美)"
|
|
||||||
style="width: 140px"
|
|
||||||
/>
|
|
||||||
<el-radio v-model="config.default" :value="opt">默认</el-radio>
|
|
||||||
<el-button type="danger" :icon="Delete" circle size="small" @click="removeOption(config, optIndex)" />
|
|
||||||
</div>
|
|
||||||
<el-button type="primary" plain size="small" @click="addOption(config)">
|
|
||||||
<el-icon><Plus /></el-icon> 添加选项
|
|
||||||
</el-button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<el-button type="primary" plain @click="addConfigItem" style="margin-top: 12px">
|
|
||||||
<el-icon><Plus /></el-icon> 添加配置项
|
|
||||||
</el-button>
|
|
||||||
</div>
|
|
||||||
</el-form>
|
</el-form>
|
||||||
<template #footer>
|
<template #footer>
|
||||||
<el-button @click="dialogVisible = false">取消</el-button>
|
<el-button @click="dialogVisible = false">取消</el-button>
|
||||||
@@ -351,61 +225,4 @@ onMounted(() => {
|
|||||||
.page-tip {
|
.page-tip {
|
||||||
margin-bottom: 16px;
|
margin-bottom: 16px;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 配置项定义样式 */
|
|
||||||
.config-schema-section {
|
|
||||||
padding: 0 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-schema-tip {
|
|
||||||
color: #909399;
|
|
||||||
font-size: 13px;
|
|
||||||
margin-bottom: 12px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-schema-item {
|
|
||||||
background: #f5f7fa;
|
|
||||||
border-radius: 8px;
|
|
||||||
padding: 12px;
|
|
||||||
margin-bottom: 12px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-header {
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
align-items: center;
|
|
||||||
margin-bottom: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-index {
|
|
||||||
font-weight: 600;
|
|
||||||
color: #409eff;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-row {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 8px;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
}
|
|
||||||
|
|
||||||
.config-options {
|
|
||||||
margin-top: 10px;
|
|
||||||
padding: 10px;
|
|
||||||
background: #fff;
|
|
||||||
border-radius: 6px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.options-label {
|
|
||||||
font-size: 13px;
|
|
||||||
color: #606266;
|
|
||||||
margin-bottom: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.option-row {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 8px;
|
|
||||||
margin-bottom: 8px;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -1,23 +1,27 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
/**
|
/**
|
||||||
* 统一错误页面
|
* 统一错误页面
|
||||||
|
* - 从 sessionStorage 读取错误信息,不污染 URL
|
||||||
|
* - 使用 replace 跳转,支持浏览器返回
|
||||||
*/
|
*/
|
||||||
import { computed, ref } from 'vue'
|
import { ref, onMounted, onUnmounted } from 'vue'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import { ElButton, ElMessage } from 'element-plus'
|
import { ElButton, ElMessage } from 'element-plus'
|
||||||
|
|
||||||
const route = useRoute()
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
const errorCode = computed(() => route.query.code || 'UNKNOWN_ERROR')
|
// 错误信息
|
||||||
const errorMessage = computed(() => route.query.message || '发生了未知错误')
|
const errorCode = ref('UNKNOWN_ERROR')
|
||||||
const traceId = computed(() => route.query.trace_id || '')
|
const errorMessage = ref('发生了未知错误')
|
||||||
const statusCode = computed(() => route.query.status || '500')
|
const traceId = ref('')
|
||||||
|
const statusCode = ref('500')
|
||||||
const copied = ref(false)
|
const copied = ref(false)
|
||||||
|
|
||||||
const errorConfig = computed(() => {
|
// 记录来源页面(用于返回)
|
||||||
const configs = {
|
const fromPath = ref('')
|
||||||
|
|
||||||
|
// 错误类型配置
|
||||||
|
const errorConfigs = {
|
||||||
'UNAUTHORIZED': { icon: 'Lock', title: '访问受限', color: '#e67e22' },
|
'UNAUTHORIZED': { icon: 'Lock', title: '访问受限', color: '#e67e22' },
|
||||||
'FORBIDDEN': { icon: 'CircleClose', title: '权限不足', color: '#e74c3c' },
|
'FORBIDDEN': { icon: 'CircleClose', title: '权限不足', color: '#e74c3c' },
|
||||||
'NOT_FOUND': { icon: 'Search', title: '页面未找到', color: '#3498db' },
|
'NOT_FOUND': { icon: 'Search', title: '页面未找到', color: '#3498db' },
|
||||||
@@ -25,9 +29,41 @@ const errorConfig = computed(() => {
|
|||||||
'INTERNAL_ERROR': { icon: 'Close', title: '服务器错误', color: '#e74c3c' },
|
'INTERNAL_ERROR': { icon: 'Close', title: '服务器错误', color: '#e74c3c' },
|
||||||
'SERVICE_UNAVAILABLE': { icon: 'Tools', title: '服务暂时不可用', color: '#9b59b6' },
|
'SERVICE_UNAVAILABLE': { icon: 'Tools', title: '服务暂时不可用', color: '#9b59b6' },
|
||||||
'NETWORK_ERROR': { icon: 'Connection', title: '网络连接失败', color: '#95a5a6' },
|
'NETWORK_ERROR': { icon: 'Connection', title: '网络连接失败', color: '#95a5a6' },
|
||||||
|
'TIMEOUT_ERROR': { icon: 'Timer', title: '请求超时', color: '#95a5a6' },
|
||||||
'UNKNOWN_ERROR': { icon: 'QuestionFilled', title: '未知错误', color: '#7f8c8d' }
|
'UNKNOWN_ERROR': { icon: 'QuestionFilled', title: '未知错误', color: '#7f8c8d' }
|
||||||
|
}
|
||||||
|
|
||||||
|
const errorConfig = ref(errorConfigs['UNKNOWN_ERROR'])
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
// 从 sessionStorage 读取错误信息
|
||||||
|
const stored = sessionStorage.getItem('errorInfo')
|
||||||
|
if (stored) {
|
||||||
|
try {
|
||||||
|
const info = JSON.parse(stored)
|
||||||
|
// 检查时效性(5分钟内有效)
|
||||||
|
if (Date.now() - info.timestamp < 5 * 60 * 1000) {
|
||||||
|
errorCode.value = info.code || 'UNKNOWN_ERROR'
|
||||||
|
errorMessage.value = info.message || '发生了未知错误'
|
||||||
|
traceId.value = info.traceId || ''
|
||||||
|
statusCode.value = String(info.status || 500)
|
||||||
|
errorConfig.value = errorConfigs[errorCode.value] || errorConfigs['UNKNOWN_ERROR']
|
||||||
}
|
}
|
||||||
return configs[errorCode.value] || configs['UNKNOWN_ERROR']
|
// 读取后清除(避免刷新时重复显示旧错误)
|
||||||
|
sessionStorage.removeItem('errorInfo')
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Failed to parse error info', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录来源页面
|
||||||
|
fromPath.value = sessionStorage.getItem('errorFromPath') || '/dashboard'
|
||||||
|
sessionStorage.removeItem('errorFromPath')
|
||||||
|
})
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
// 确保清理
|
||||||
|
sessionStorage.removeItem('errorInfo')
|
||||||
})
|
})
|
||||||
|
|
||||||
const copyTraceId = async () => {
|
const copyTraceId = async () => {
|
||||||
@@ -44,7 +80,15 @@ const copyTraceId = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const goHome = () => router.push('/dashboard')
|
const goHome = () => router.push('/dashboard')
|
||||||
const retry = () => router.back()
|
|
||||||
|
// 返回之前的页面
|
||||||
|
const goBack = () => {
|
||||||
|
if (fromPath.value && fromPath.value !== '/error') {
|
||||||
|
router.push(fromPath.value)
|
||||||
|
} else {
|
||||||
|
router.push('/dashboard')
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -72,7 +116,7 @@ const retry = () => router.back()
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="action-buttons">
|
<div class="action-buttons">
|
||||||
<el-button type="primary" @click="retry">重试</el-button>
|
<el-button type="primary" @click="goBack">返回</el-button>
|
||||||
<el-button @click="goHome">返回首页</el-button>
|
<el-button @click="goHome">返回首页</el-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
317
frontend/src/views/notification-channels/index.vue
Normal file
317
frontend/src/views/notification-channels/index.vue
Normal file
@@ -0,0 +1,317 @@
|
|||||||
|
<script setup>
|
||||||
|
import { ref, reactive, onMounted } from 'vue'
|
||||||
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||||
|
import api from '@/api'
|
||||||
|
|
||||||
|
const loading = ref(false)
|
||||||
|
const tableData = ref([])
|
||||||
|
const total = ref(0)
|
||||||
|
const query = reactive({
|
||||||
|
page: 1,
|
||||||
|
size: 50,
|
||||||
|
tenant_id: ''
|
||||||
|
})
|
||||||
|
|
||||||
|
// 租户列表
|
||||||
|
const tenants = ref([])
|
||||||
|
|
||||||
|
// 对话框
|
||||||
|
const dialogVisible = ref(false)
|
||||||
|
const dialogTitle = ref('')
|
||||||
|
const editingId = ref(null)
|
||||||
|
const formRef = ref(null)
|
||||||
|
const form = reactive({
|
||||||
|
tenant_id: '',
|
||||||
|
channel_name: '',
|
||||||
|
channel_type: 'dingtalk_bot',
|
||||||
|
webhook_url: '',
|
||||||
|
sign_secret: '',
|
||||||
|
description: ''
|
||||||
|
})
|
||||||
|
|
||||||
|
const rules = {
|
||||||
|
tenant_id: [{ required: true, message: '请选择租户', trigger: 'change' }],
|
||||||
|
channel_name: [{ required: true, message: '请输入渠道名称', trigger: 'blur' }],
|
||||||
|
channel_type: [{ required: true, message: '请选择渠道类型', trigger: 'change' }],
|
||||||
|
webhook_url: [{ required: true, message: '请输入 Webhook 地址', trigger: 'blur' }]
|
||||||
|
}
|
||||||
|
|
||||||
|
const channelTypes = [
|
||||||
|
{ value: 'dingtalk_bot', label: '钉钉机器人' },
|
||||||
|
{ value: 'wecom_bot', label: '企微机器人' }
|
||||||
|
]
|
||||||
|
|
||||||
|
async function fetchList() {
|
||||||
|
loading.value = true
|
||||||
|
try {
|
||||||
|
const params = { ...query }
|
||||||
|
if (!params.tenant_id) delete params.tenant_id
|
||||||
|
|
||||||
|
const res = await api.get('/api/notification-channels', { params })
|
||||||
|
tableData.value = res.data.items || []
|
||||||
|
total.value = res.data.total || 0
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchTenants() {
|
||||||
|
try {
|
||||||
|
const res = await api.get('/api/tenants', { params: { size: 1000 } })
|
||||||
|
tenants.value = res.data.items || []
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleSearch() {
|
||||||
|
query.page = 1
|
||||||
|
fetchList()
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleCreate() {
|
||||||
|
editingId.value = null
|
||||||
|
dialogTitle.value = '新建通知渠道'
|
||||||
|
Object.assign(form, {
|
||||||
|
tenant_id: '',
|
||||||
|
channel_name: '',
|
||||||
|
channel_type: 'dingtalk_bot',
|
||||||
|
webhook_url: '',
|
||||||
|
sign_secret: '',
|
||||||
|
description: ''
|
||||||
|
})
|
||||||
|
dialogVisible.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleEdit(row) {
|
||||||
|
editingId.value = row.id
|
||||||
|
dialogTitle.value = '编辑通知渠道'
|
||||||
|
Object.assign(form, {
|
||||||
|
tenant_id: row.tenant_id,
|
||||||
|
channel_name: row.channel_name,
|
||||||
|
channel_type: row.channel_type,
|
||||||
|
webhook_url: row.webhook_url,
|
||||||
|
sign_secret: row.sign_secret || '',
|
||||||
|
description: row.description || ''
|
||||||
|
})
|
||||||
|
dialogVisible.value = true
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleSubmit() {
|
||||||
|
await formRef.value.validate()
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (editingId.value) {
|
||||||
|
await api.put(`/api/notification-channels/${editingId.value}`, form)
|
||||||
|
ElMessage.success('更新成功')
|
||||||
|
} else {
|
||||||
|
await api.post('/api/notification-channels', form)
|
||||||
|
ElMessage.success('创建成功')
|
||||||
|
}
|
||||||
|
dialogVisible.value = false
|
||||||
|
fetchList()
|
||||||
|
} catch (e) {
|
||||||
|
// 错误已在拦截器处理
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDelete(row) {
|
||||||
|
await ElMessageBox.confirm(`确定删除渠道 "${row.channel_name}" 吗?`, '提示', {
|
||||||
|
type: 'warning'
|
||||||
|
})
|
||||||
|
|
||||||
|
try {
|
||||||
|
await api.delete(`/api/notification-channels/${row.id}`)
|
||||||
|
ElMessage.success('删除成功')
|
||||||
|
fetchList()
|
||||||
|
} catch (e) {
|
||||||
|
// 错误已在拦截器处理
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleToggle(row) {
|
||||||
|
try {
|
||||||
|
await api.put(`/api/notification-channels/${row.id}`, {
|
||||||
|
is_enabled: !row.is_enabled
|
||||||
|
})
|
||||||
|
ElMessage.success(row.is_enabled ? '已禁用' : '已启用')
|
||||||
|
fetchList()
|
||||||
|
} catch (e) {
|
||||||
|
// 错误已在拦截器处理
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleTest(row) {
|
||||||
|
try {
|
||||||
|
ElMessage.info('发送测试消息中...')
|
||||||
|
const res = await api.post(`/api/notification-channels/${row.id}/test`)
|
||||||
|
if (res.data.success) {
|
||||||
|
ElMessage.success('测试消息发送成功')
|
||||||
|
} else {
|
||||||
|
ElMessage.error(`发送失败: ${res.data.message}`)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// 错误已在拦截器处理
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTenantName(tenantId) {
|
||||||
|
const tenant = tenants.value.find(t => t.code === tenantId)
|
||||||
|
return tenant ? tenant.name : tenantId
|
||||||
|
}
|
||||||
|
|
||||||
|
function getChannelTypeName(type) {
|
||||||
|
const item = channelTypes.find(t => t.value === type)
|
||||||
|
return item ? item.label : type
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
fetchList()
|
||||||
|
fetchTenants()
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div class="page-container">
|
||||||
|
<div class="page-header">
|
||||||
|
<div class="title">通知渠道管理</div>
|
||||||
|
<el-button type="primary" @click="handleCreate">
|
||||||
|
<el-icon><Plus /></el-icon>
|
||||||
|
新建渠道
|
||||||
|
</el-button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="page-tip">
|
||||||
|
<el-alert type="info" :closable="false">
|
||||||
|
通知渠道用于定时任务执行后发送消息。支持钉钉机器人和企微机器人。
|
||||||
|
脚本中设置 <code>result = {'content': '消息内容', 'title': '标题'}</code> 变量,任务执行后会自动发送到配置的渠道。
|
||||||
|
</el-alert>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 筛选 -->
|
||||||
|
<div class="filter-bar">
|
||||||
|
<el-select v-model="query.tenant_id" placeholder="全部租户" clearable style="width: 180px">
|
||||||
|
<el-option v-for="t in tenants" :key="t.code" :label="t.name" :value="t.code" />
|
||||||
|
</el-select>
|
||||||
|
<el-button type="primary" @click="handleSearch">搜索</el-button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 表格 -->
|
||||||
|
<el-table v-loading="loading" :data="tableData" style="width: 100%">
|
||||||
|
<el-table-column prop="id" label="ID" width="60" />
|
||||||
|
<el-table-column label="租户" width="120">
|
||||||
|
<template #default="{ row }">
|
||||||
|
{{ getTenantName(row.tenant_id) }}
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column prop="channel_name" label="渠道名称" min-width="150" />
|
||||||
|
<el-table-column label="类型" width="120">
|
||||||
|
<template #default="{ row }">
|
||||||
|
<el-tag :type="row.channel_type === 'dingtalk_bot' ? 'primary' : 'success'" size="small">
|
||||||
|
{{ getChannelTypeName(row.channel_type) }}
|
||||||
|
</el-tag>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column prop="webhook_url" label="Webhook" min-width="200" show-overflow-tooltip />
|
||||||
|
<el-table-column label="状态" width="80">
|
||||||
|
<template #default="{ row }">
|
||||||
|
<el-tag :type="row.is_enabled ? 'success' : 'info'" size="small">
|
||||||
|
{{ row.is_enabled ? '启用' : '禁用' }}
|
||||||
|
</el-tag>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column label="操作" width="220" fixed="right">
|
||||||
|
<template #default="{ row }">
|
||||||
|
<el-button type="primary" link size="small" @click="handleEdit(row)">编辑</el-button>
|
||||||
|
<el-button type="info" link size="small" @click="handleTest(row)">测试</el-button>
|
||||||
|
<el-button :type="row.is_enabled ? 'warning' : 'success'" link size="small" @click="handleToggle(row)">
|
||||||
|
{{ row.is_enabled ? '禁用' : '启用' }}
|
||||||
|
</el-button>
|
||||||
|
<el-button type="danger" link size="small" @click="handleDelete(row)">删除</el-button>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
</el-table>
|
||||||
|
|
||||||
|
<!-- 编辑对话框 -->
|
||||||
|
<el-dialog v-model="dialogVisible" :title="dialogTitle" width="550px">
|
||||||
|
<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
|
||||||
|
<el-form-item label="所属租户" prop="tenant_id">
|
||||||
|
<el-select v-model="form.tenant_id" placeholder="选择租户" style="width: 100%">
|
||||||
|
<el-option v-for="t in tenants" :key="t.code" :label="t.name" :value="t.code" />
|
||||||
|
</el-select>
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item label="渠道名称" prop="channel_name">
|
||||||
|
<el-input v-model="form.channel_name" placeholder="如: 销售群机器人" />
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item label="渠道类型" prop="channel_type">
|
||||||
|
<el-select v-model="form.channel_type" style="width: 100%">
|
||||||
|
<el-option v-for="t in channelTypes" :key="t.value" :label="t.label" :value="t.value" />
|
||||||
|
</el-select>
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item label="Webhook" prop="webhook_url">
|
||||||
|
<el-input v-model="form.webhook_url" placeholder="机器人 Webhook 地址" />
|
||||||
|
<div class="form-tip">
|
||||||
|
<template v-if="form.channel_type === 'dingtalk_bot'">
|
||||||
|
钉钉机器人 Webhook 格式: https://oapi.dingtalk.com/robot/send?access_token=xxx
|
||||||
|
</template>
|
||||||
|
<template v-else>
|
||||||
|
企微机器人 Webhook 格式: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx
|
||||||
|
</template>
|
||||||
|
</div>
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item v-if="form.channel_type === 'dingtalk_bot'" label="加签密钥">
|
||||||
|
<el-input v-model="form.sign_secret" placeholder="SEC开头的密钥(可选)" />
|
||||||
|
<div class="form-tip">
|
||||||
|
如果创建机器人时选择了「加签」安全设置,请填写密钥(以 SEC 开头)
|
||||||
|
</div>
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item label="描述">
|
||||||
|
<el-input v-model="form.description" type="textarea" :rows="2" placeholder="渠道描述(可选)" />
|
||||||
|
</el-form-item>
|
||||||
|
</el-form>
|
||||||
|
<template #footer>
|
||||||
|
<el-button @click="dialogVisible = false">取消</el-button>
|
||||||
|
<el-button type="primary" @click="handleSubmit">保存</el-button>
|
||||||
|
</template>
|
||||||
|
</el-dialog>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.page-header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-header .title {
|
||||||
|
font-size: 20px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-tip {
|
||||||
|
margin-bottom: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page-tip code {
|
||||||
|
background: #f5f7fa;
|
||||||
|
padding: 2px 6px;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.filter-bar {
|
||||||
|
display: flex;
|
||||||
|
gap: 12px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-tip {
|
||||||
|
color: #909399;
|
||||||
|
font-size: 12px;
|
||||||
|
margin-top: 4px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
1187
frontend/src/views/scheduled-tasks/index.vue
Normal file
1187
frontend/src/views/scheduled-tasks/index.vue
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user