2026-02-14 16:50:02 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
"""
|
|
|
|
|
|
DRF 序列化器。
|
|
|
|
|
|
"""
|
|
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
2026-03-05 10:27:28 +08:00
|
|
|
|
from server.models import (
|
2026-03-06 10:47:46 +08:00
|
|
|
|
BossAccount, TaskLog, RecruitFilterSnapshot, ChatScript, ContactRecord, SystemConfig,
|
2026-03-05 10:27:28 +08:00
|
|
|
|
FollowUpConfig, FollowUpScript, FollowUpRecord
|
|
|
|
|
|
)
|
2026-02-14 16:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ────────────────────────── 账号 ──────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
class BossAccountSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
"""账号序列化器(读取用)。"""
|
|
|
|
|
|
# 运行时补充的字段(非数据库字段)
|
|
|
|
|
|
worker_name = serializers.CharField(read_only=True, default="")
|
|
|
|
|
|
worker_online = serializers.BooleanField(read_only=True, default=False)
|
|
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = BossAccount
|
|
|
|
|
|
fields = [
|
|
|
|
|
|
"id", "worker_id", "browser_id", "browser_name",
|
2026-02-27 13:56:15 +08:00
|
|
|
|
"boss_username", "boss_id", "is_logged_in",
|
2026-02-14 16:50:02 +08:00
|
|
|
|
"current_task_id", "current_task_status",
|
|
|
|
|
|
"checked_at", "created_at", "updated_at",
|
|
|
|
|
|
"worker_name", "worker_online",
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AccountBindSerializer(serializers.Serializer):
|
|
|
|
|
|
"""添加账号请求。"""
|
|
|
|
|
|
browser_name = serializers.CharField(max_length=128)
|
|
|
|
|
|
worker_id = serializers.CharField(max_length=64)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ────────────────────────── 任务 ──────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
class TaskCreateSerializer(serializers.Serializer):
|
|
|
|
|
|
"""提交任务请求。"""
|
|
|
|
|
|
task_type = serializers.CharField(max_length=64)
|
2026-02-27 15:42:13 +08:00
|
|
|
|
id = serializers.IntegerField(required=False, allow_null=True, default=None) # 账号 ID
|
|
|
|
|
|
account_id = serializers.IntegerField(required=False, allow_null=True, default=None) # 同上,别名
|
2026-02-27 21:08:30 +08:00
|
|
|
|
boss_id = serializers.CharField(max_length=64, required=False, allow_blank=True, default="") # 即 /api/accounts 返回的 id(账号主键)
|
2026-02-14 16:50:02 +08:00
|
|
|
|
worker_id = serializers.CharField(max_length=64, required=False, allow_blank=True, default="")
|
|
|
|
|
|
account_name = serializers.CharField(max_length=128, required=False, allow_blank=True, default="")
|
2026-03-06 13:29:10 +08:00
|
|
|
|
params = serializers.CharField(required=False, allow_blank=True, default="")
|
2026-02-14 16:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TaskOutSerializer(serializers.Serializer):
|
|
|
|
|
|
"""任务信息响应。"""
|
|
|
|
|
|
task_id = serializers.CharField()
|
|
|
|
|
|
task_type = serializers.CharField()
|
|
|
|
|
|
status = serializers.CharField()
|
|
|
|
|
|
worker_id = serializers.CharField(allow_null=True)
|
|
|
|
|
|
account_name = serializers.CharField(allow_null=True)
|
|
|
|
|
|
params = serializers.DictField()
|
|
|
|
|
|
progress = serializers.CharField(allow_null=True)
|
|
|
|
|
|
result = serializers.JSONField(allow_null=True)
|
|
|
|
|
|
error = serializers.CharField(allow_null=True)
|
2026-03-01 23:18:23 +08:00
|
|
|
|
created_at = serializers.CharField()
|
|
|
|
|
|
updated_at = serializers.CharField()
|
2026-02-14 16:50:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ────────────────────────── Worker ──────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
class BrowserProfileSerializer(serializers.Serializer):
|
|
|
|
|
|
"""浏览器窗口信息。"""
|
|
|
|
|
|
id = serializers.CharField()
|
|
|
|
|
|
name = serializers.CharField()
|
|
|
|
|
|
remark = serializers.CharField()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WorkerOutSerializer(serializers.Serializer):
|
|
|
|
|
|
"""Worker 信息响应。"""
|
|
|
|
|
|
worker_id = serializers.CharField()
|
|
|
|
|
|
worker_name = serializers.CharField()
|
|
|
|
|
|
browsers = BrowserProfileSerializer(many=True)
|
|
|
|
|
|
online = serializers.BooleanField()
|
|
|
|
|
|
current_task_id = serializers.CharField(allow_null=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ────────────────────────── 认证 ──────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
class LoginSerializer(serializers.Serializer):
|
|
|
|
|
|
"""登录请求。"""
|
|
|
|
|
|
username = serializers.CharField(max_length=64)
|
|
|
|
|
|
password = serializers.CharField(max_length=128)
|
2026-02-14 17:58:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ────────────────────────── 话术 ──────────────────────────
|
|
|
|
|
|
|
2026-03-06 10:05:49 +08:00
|
|
|
|
class RecruitFilterSnapshotSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = RecruitFilterSnapshot
|
|
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
read_only_fields = ["id", "synced_at", "created_at"]
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-02-14 17:58:29 +08:00
|
|
|
|
class ChatScriptSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
script_type_display = serializers.CharField(source="get_script_type_display", read_only=True)
|
|
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = ChatScript
|
|
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
read_only_fields = ["id", "created_at", "updated_at"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ────────────────────────── 联系记录 ──────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
class ContactRecordSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = ContactRecord
|
|
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
read_only_fields = ["id", "created_at"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ────────────────────────── 系统配置 ──────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
class SystemConfigSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = SystemConfig
|
|
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
read_only_fields = ["updated_at"]
|
2026-03-05 10:27:28 +08:00
|
|
|
|
|
|
|
|
|
|
# ────────────────────────── 复聊配置 ──────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
class FollowUpScriptSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
"""复聊话术序列化器。"""
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = FollowUpScript
|
|
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
read_only_fields = ["id", "created_at"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FollowUpConfigSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
"""复聊配置序列化器(包含关联的话术列表)。"""
|
|
|
|
|
|
scripts = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = FollowUpConfig
|
|
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
read_only_fields = ["id", "created_at", "updated_at"]
|
|
|
|
|
|
|
|
|
|
|
|
def get_scripts(self, obj):
|
|
|
|
|
|
"""获取该配置下的所有话术。"""
|
|
|
|
|
|
scripts = FollowUpScript.objects.filter(config_id=obj.id, is_active=True).order_by('day_number', 'order')
|
|
|
|
|
|
return FollowUpScriptSerializer(scripts, many=True).data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FollowUpRecordSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
"""复聊记录序列化器。"""
|
|
|
|
|
|
class Meta:
|
|
|
|
|
|
model = FollowUpRecord
|
|
|
|
|
|
fields = "__all__"
|
|
|
|
|
|
read_only_fields = ["id", "sent_at"]
|