2025-11-22 16:39:01 +08:00
|
|
|
|
<template>
|
2025-12-05 15:45:14 +08:00
|
|
|
|
<div class="script-edit-dialog">
|
2025-12-01 16:30:31 +08:00
|
|
|
|
<el-dialog title="" v-model="isShowDialog" width="769px" @close="onDialogClose">
|
|
|
|
|
|
<el-form ref="formRef" :model="formData" :rules="rules" size="default" label-width="90px">
|
2025-11-24 16:55:18 +08:00
|
|
|
|
<el-row :gutter="35">
|
|
|
|
|
|
<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
|
2026-04-11 16:11:00 +08:00
|
|
|
|
<el-form-item label="数据集">
|
|
|
|
|
|
<el-input v-model="formData.datasetName" placeholder="数据集" disabled />
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
</el-col>
|
|
|
|
|
|
<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
|
|
|
|
|
|
<el-form-item label="场景类型">
|
|
|
|
|
|
<el-input v-model="formData.sceneType" placeholder="场景类型" disabled />
|
2025-11-24 16:55:18 +08:00
|
|
|
|
</el-form-item>
|
|
|
|
|
|
</el-col>
|
|
|
|
|
|
<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
|
2026-04-11 16:11:00 +08:00
|
|
|
|
<el-form-item label="话术内容" prop="questionContent">
|
|
|
|
|
|
<Editor v-model="formData.questionContent" height="400px" :key="editorKey" placeholder="请输入话术内容" :disableExceptEmotion="true" />
|
2025-11-24 16:55:18 +08:00
|
|
|
|
</el-form-item>
|
|
|
|
|
|
</el-col>
|
|
|
|
|
|
</el-row>
|
|
|
|
|
|
</el-form>
|
|
|
|
|
|
<template #footer>
|
|
|
|
|
|
<span class="dialog-footer">
|
|
|
|
|
|
<el-button @click="onCancel" size="default">取 消</el-button>
|
2025-11-25 17:02:31 +08:00
|
|
|
|
<el-button type="primary" @click="onSubmit" size="default" :loading="loading">
|
|
|
|
|
|
{{ formData.id === 0 ? '新 增' : '修 改' }}
|
|
|
|
|
|
</el-button>
|
2025-11-24 16:55:18 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</el-dialog>
|
|
|
|
|
|
</div>
|
2025-11-22 16:39:01 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
2025-11-25 17:02:31 +08:00
|
|
|
|
<script lang="ts" setup>
|
2026-04-11 16:11:00 +08:00
|
|
|
|
import { ref, reactive, toRefs, nextTick, onMounted } from 'vue';
|
2025-11-25 17:02:31 +08:00
|
|
|
|
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
|
2025-11-24 16:55:18 +08:00
|
|
|
|
import Editor from '/@/components/editor/index.vue';
|
2026-04-11 16:11:00 +08:00
|
|
|
|
import { addScript, updateScript, getScriptDetail } from '/@/api/customerService/script';
|
|
|
|
|
|
import { listknowledges } from '/@/api/knowledge/dataset';
|
2025-11-22 16:39:01 +08:00
|
|
|
|
|
2025-11-25 17:02:31 +08:00
|
|
|
|
// 定义类型接口
|
2025-11-22 16:39:01 +08:00
|
|
|
|
interface DialogRow {
|
2026-04-11 16:11:00 +08:00
|
|
|
|
id?: number;
|
|
|
|
|
|
datasetId: string | number;
|
|
|
|
|
|
datasetName?: string;
|
|
|
|
|
|
sceneType: number;
|
|
|
|
|
|
questionContent: string;
|
2025-11-22 16:39:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-25 17:02:31 +08:00
|
|
|
|
// 定义事件
|
|
|
|
|
|
const emit = defineEmits<{
|
2025-12-05 15:45:14 +08:00
|
|
|
|
(e: 'refresh'): void;
|
2025-11-25 17:02:31 +08:00
|
|
|
|
}>();
|
|
|
|
|
|
|
2026-04-11 16:11:00 +08:00
|
|
|
|
// 定义数据集选项类型
|
|
|
|
|
|
interface DatasetOption {
|
|
|
|
|
|
label: string;
|
|
|
|
|
|
value: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-25 17:02:31 +08:00
|
|
|
|
// 响应式数据
|
|
|
|
|
|
const state = reactive({
|
|
|
|
|
|
loading: false,
|
|
|
|
|
|
isShowDialog: false,
|
2026-04-11 16:11:00 +08:00
|
|
|
|
datasets: [] as DatasetOption[], // 数据集列表
|
2025-12-01 16:30:31 +08:00
|
|
|
|
editorKey: 0, // 用于强制重新渲染编辑器
|
2025-11-25 17:02:31 +08:00
|
|
|
|
formData: {
|
|
|
|
|
|
id: 0,
|
2026-04-11 16:11:00 +08:00
|
|
|
|
datasetId: 0,
|
|
|
|
|
|
datasetName: '',
|
|
|
|
|
|
sceneType: 0,
|
|
|
|
|
|
questionContent: '',
|
2025-12-01 16:30:31 +08:00
|
|
|
|
} as DialogRow,
|
2025-11-25 17:02:31 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 表单验证规则
|
|
|
|
|
|
const rules: FormRules = {
|
2026-04-11 16:11:00 +08:00
|
|
|
|
questionContent: [{ required: true, message: '话术内容不能为空', trigger: 'blur' }],
|
2025-11-25 17:02:31 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 模板引用
|
|
|
|
|
|
const formRef = ref<FormInstance>();
|
|
|
|
|
|
|
|
|
|
|
|
// 解构状态数据
|
2025-12-01 16:30:31 +08:00
|
|
|
|
const { loading, isShowDialog, formData, editorKey } = toRefs(state);
|
2025-11-25 17:02:31 +08:00
|
|
|
|
|
2026-04-11 16:11:00 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 加载数据集列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
const loadDatasets = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const response = await listknowledges({ pageNum: 1, pageSize: 100 });
|
|
|
|
|
|
if (response.data && response.data.list) {
|
|
|
|
|
|
state.datasets = response.data.list.map((item: any) => ({
|
|
|
|
|
|
label: item.name,
|
|
|
|
|
|
value: item.id,
|
|
|
|
|
|
}));
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
ElMessage.error('加载数据集列表失败');
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 生命周期
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
|
loadDatasets();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-25 17:02:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 打开对话框
|
|
|
|
|
|
* @param row - 可选的编辑数据
|
|
|
|
|
|
*/
|
2026-04-11 16:11:00 +08:00
|
|
|
|
const openDialog = async (row?: DialogRow) => {
|
2025-11-25 17:02:31 +08:00
|
|
|
|
resetForm();
|
2025-12-01 16:30:31 +08:00
|
|
|
|
|
2026-04-11 16:11:00 +08:00
|
|
|
|
// 重新加载数据集列表,确保数据是最新的
|
|
|
|
|
|
await loadDatasets();
|
|
|
|
|
|
|
|
|
|
|
|
if (row && row.id) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 加载话术详情
|
|
|
|
|
|
const response = await getScriptDetail({ id: row.id });
|
|
|
|
|
|
if (response.data) {
|
|
|
|
|
|
// 确保datasetId是字符串类型,与datasets选项的value类型一致
|
|
|
|
|
|
const detailData = {
|
|
|
|
|
|
...response.data,
|
|
|
|
|
|
datasetId: String(response.data.datasetId),
|
|
|
|
|
|
datasetName: '',
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 查找对应的数据集名称
|
|
|
|
|
|
const dataset = state.datasets.find((d) => d.value === detailData.datasetId);
|
|
|
|
|
|
if (dataset) {
|
|
|
|
|
|
detailData.datasetName = dataset.label;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
state.formData = detailData;
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
ElMessage.error('获取话术详情失败');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-12-01 16:30:31 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 新增模式,确保清空数据
|
|
|
|
|
|
state.formData = {
|
|
|
|
|
|
id: 0,
|
2026-04-11 16:11:00 +08:00
|
|
|
|
datasetId: 0,
|
|
|
|
|
|
datasetName: '',
|
|
|
|
|
|
sceneType: 0,
|
|
|
|
|
|
questionContent: '',
|
2025-12-01 16:30:31 +08:00
|
|
|
|
};
|
2025-11-25 17:02:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-11 16:11:00 +08:00
|
|
|
|
// 强制重新渲染编辑器
|
2025-12-01 16:30:31 +08:00
|
|
|
|
state.editorKey++;
|
|
|
|
|
|
|
2025-11-25 17:02:31 +08:00
|
|
|
|
state.isShowDialog = true;
|
2025-12-01 16:30:31 +08:00
|
|
|
|
|
|
|
|
|
|
// 确保 DOM 更新后处理
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
|
if (formRef.value) {
|
|
|
|
|
|
formRef.value.clearValidate();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2025-11-25 17:02:31 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-01 16:30:31 +08:00
|
|
|
|
* 对话框关闭时的处理
|
2025-11-25 17:02:31 +08:00
|
|
|
|
*/
|
2025-12-01 16:30:31 +08:00
|
|
|
|
const onDialogClose = () => {
|
|
|
|
|
|
resetForm();
|
2025-11-25 17:02:31 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 取消操作
|
|
|
|
|
|
*/
|
|
|
|
|
|
const onCancel = () => {
|
2025-12-01 16:30:31 +08:00
|
|
|
|
state.isShowDialog = false;
|
2025-11-25 17:02:31 +08:00
|
|
|
|
};
|
2025-11-22 16:39:01 +08:00
|
|
|
|
|
2025-11-25 17:02:31 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 提交表单
|
|
|
|
|
|
*/
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
|
|
if (!formRef.value) return;
|
2025-11-22 16:39:01 +08:00
|
|
|
|
|
2025-11-25 17:02:31 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 表单验证
|
|
|
|
|
|
const valid = await formRef.value.validate();
|
|
|
|
|
|
if (!valid) return;
|
2025-11-22 16:39:01 +08:00
|
|
|
|
|
2026-04-11 16:11:00 +08:00
|
|
|
|
// 确保数据类型正确
|
|
|
|
|
|
const submitData = {
|
|
|
|
|
|
...state.formData,
|
|
|
|
|
|
datasetId: String(state.formData.datasetId),
|
|
|
|
|
|
sceneType: Number(state.formData.sceneType),
|
|
|
|
|
|
};
|
2025-12-01 16:54:21 +08:00
|
|
|
|
|
2025-11-25 17:02:31 +08:00
|
|
|
|
state.loading = true;
|
|
|
|
|
|
|
|
|
|
|
|
if (state.formData.id === 0) {
|
|
|
|
|
|
// 新增模式
|
2026-04-11 16:11:00 +08:00
|
|
|
|
await addScript(submitData);
|
2025-11-25 17:02:31 +08:00
|
|
|
|
ElMessage.success('添加成功');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 编辑模式
|
2026-04-11 16:11:00 +08:00
|
|
|
|
await updateScript(submitData);
|
2025-11-25 17:02:31 +08:00
|
|
|
|
ElMessage.success('修改成功');
|
2025-11-24 16:55:18 +08:00
|
|
|
|
}
|
2025-11-22 16:39:01 +08:00
|
|
|
|
|
2025-11-25 17:02:31 +08:00
|
|
|
|
// 关闭对话框并刷新列表
|
2025-12-01 16:30:31 +08:00
|
|
|
|
state.isShowDialog = false;
|
2025-12-05 15:45:14 +08:00
|
|
|
|
emit('refresh');
|
2025-11-25 17:02:31 +08:00
|
|
|
|
} catch (error) {
|
2026-01-05 10:05:25 +08:00
|
|
|
|
// 错误已由请求拦截器统一处理
|
2025-11-25 17:02:31 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
state.loading = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 重置表单
|
|
|
|
|
|
*/
|
|
|
|
|
|
const resetForm = () => {
|
|
|
|
|
|
state.formData = {
|
|
|
|
|
|
id: 0,
|
2026-04-11 16:11:00 +08:00
|
|
|
|
datasetId: 0,
|
|
|
|
|
|
datasetName: '',
|
|
|
|
|
|
sceneType: 0,
|
|
|
|
|
|
questionContent: '',
|
2025-11-25 17:02:31 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 重置表单验证状态
|
|
|
|
|
|
nextTick(() => {
|
2025-12-01 16:30:31 +08:00
|
|
|
|
if (formRef.value) {
|
|
|
|
|
|
formRef.value.clearValidate();
|
|
|
|
|
|
}
|
2025-11-25 17:02:31 +08:00
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 暴露方法给父组件使用
|
|
|
|
|
|
defineExpose({
|
|
|
|
|
|
openDialog,
|
2025-11-22 16:39:01 +08:00
|
|
|
|
});
|
|
|
|
|
|
</script>
|