更新定时任务页面和前端构建

This commit is contained in:
2025-12-14 23:09:27 +08:00
parent 1ec0d80f6c
commit dab29347bd
18 changed files with 121 additions and 41 deletions

View File

@@ -14,6 +14,19 @@ from services.tasks import submit_account_task
api_schedules_bp = Blueprint("api_schedules", __name__)
_HHMM_RE = re.compile(r"^(\d{1,2}):(\d{2})$")
def _normalize_hhmm(value: object) -> str | None:
match = _HHMM_RE.match(str(value or "").strip())
if not match:
return None
hour = int(match.group(1))
minute = int(match.group(2))
if hour < 0 or hour > 23 or minute < 0 or minute > 59:
return None
return f"{hour:02d}:{minute:02d}"
@api_schedules_bp.route("/api/schedules", methods=["GET"])
@login_required
@@ -34,7 +47,7 @@ def get_user_schedules_api():
@login_required
def create_user_schedule_api():
"""创建用户定时任务"""
data = request.json
data = request.json or {}
name = data.get("name", "我的定时任务")
schedule_time = data.get("schedule_time", "08:00")
@@ -46,7 +59,8 @@ def create_user_schedule_api():
random_delay = int(data.get("random_delay", 0) or 0)
account_ids = data.get("account_ids", [])
if not re.match(r"^\\d{2}:\\d{2}$", schedule_time):
normalized_time = _normalize_hhmm(schedule_time)
if not normalized_time:
return jsonify({"error": "时间格式不正确,应为 HH:MM"}), 400
if random_delay not in (0, 1):
return jsonify({"error": "random_delay必须是0或1"}), 400
@@ -54,7 +68,7 @@ def create_user_schedule_api():
schedule_id = database.create_user_schedule(
user_id=current_user.id,
name=name,
schedule_time=schedule_time,
schedule_time=normalized_time,
weekdays=weekdays,
browse_type=browse_type,
enable_screenshot=enable_screenshot,
@@ -96,7 +110,7 @@ def update_schedule_api(schedule_id):
if schedule["user_id"] != current_user.id:
return jsonify({"error": "无权访问"}), 403
data = request.json
data = request.json or {}
allowed_fields = [
"name",
"schedule_time",
@@ -111,8 +125,10 @@ def update_schedule_api(schedule_id):
update_data = {k: v for k, v in data.items() if k in allowed_fields}
if "schedule_time" in update_data:
if not re.match(r"^\\d{2}:\\d{2}$", update_data["schedule_time"]):
return jsonify({"error": "时间格式不正确"}), 400
normalized_time = _normalize_hhmm(update_data["schedule_time"])
if not normalized_time:
return jsonify({"error": "时间格式不正确,应为 HH:MM"}), 400
update_data["schedule_time"] = normalized_time
if "random_delay" in update_data:
try:
update_data["random_delay"] = int(update_data.get("random_delay") or 0)