Files
admin-ui/src/views/customerService/script/component/editRole.vue

196 lines
4.3 KiB
Vue
Raw Normal View History

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">
2025-12-01 16:30:31 +08:00
<el-form-item label="标签" prop="tag">
<el-input v-model="formData.tag" placeholder="请输入标签" clearable />
2025-11-24 16:55:18 +08:00
</el-form-item>
</el-col>
2025-11-25 17:02:31 +08:00
<!-- 富文本编辑器 -->
2025-11-24 16:55:18 +08:00
<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
2025-12-01 16:54:21 +08:00
<el-form-item label="话术" prop="content">
2025-12-01 16:30:31 +08:00
<Editor v-model="formData.content" height="400px" :key="editorKey" placeholder="请输入产品详情" />
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>
import { ref, reactive, toRefs, nextTick } from 'vue';
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
2025-11-24 16:55:18 +08:00
import Editor from '/@/components/editor/index.vue';
2025-12-05 15:45:14 +08:00
import { addScript, updateScript } from '/@/api/customerService/script';
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 {
2025-11-24 16:55:18 +08:00
id: number;
2025-11-27 15:37:13 +08:00
tag: string;
creator: string;
content: string;
2025-12-01 16:30:31 +08:00
modifier: 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
}>();
// 响应式数据
const state = reactive({
loading: false,
isShowDialog: false,
2025-12-01 16:30:31 +08:00
editorKey: 0, // 用于强制重新渲染编辑器
2025-11-25 17:02:31 +08:00
formData: {
id: 0,
2025-11-27 15:37:13 +08:00
tag: '',
2025-11-25 17:02:31 +08:00
content: '',
2025-11-27 15:37:13 +08:00
creator: '',
2025-11-28 17:17:07 +08:00
modifier: '',
2025-12-01 16:30:31 +08:00
} as DialogRow,
2025-11-25 17:02:31 +08:00
});
// 表单验证规则
const rules: FormRules = {
2025-12-03 15:29:05 +08:00
tag: [
{ required: true, message: '标签名称不能为空', trigger: 'blur' },
{ max: 64, message: '标签长度最多 64 个字符', trigger: 'blur' },
],
content: [
{ required: true, message: '产品详情不能为空', trigger: 'blur' },
{ max: 8126, message: '产品名称长度最多 8126 个字符', 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
/**
* 打开对话框
* @param row - 可选的编辑数据
*/
const openDialog = (row?: DialogRow) => {
resetForm();
2025-12-01 16:30:31 +08:00
2025-11-25 17:02:31 +08:00
if (row) {
2025-12-01 16:30:31 +08:00
// 深拷贝数据,避免引用问题
state.formData = { ...row };
} else {
// 新增模式,确保清空数据
state.formData = {
id: 0,
tag: '',
content: '',
creator: '',
modifier: '',
};
2025-11-25 17:02:31 +08:00
}
2025-12-01 16:30:31 +08:00
// 更新编辑器 key 强制重新渲染
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
2025-12-01 16:54:21 +08:00
// 额外验证话术内容
if (!state.formData.content || state.formData.content === '<p><br></p>' || state.formData.content.trim() === '<p><br></p>') {
ElMessage.warning('话术内容不能为空');
return;
}
2025-11-25 17:02:31 +08:00
state.loading = true;
if (state.formData.id === 0) {
// 新增模式
2025-12-05 15:45:14 +08:00
await addScript(state.formData);
2025-11-25 17:02:31 +08:00
ElMessage.success('添加成功');
} else {
// 编辑模式
2025-12-05 15:45:14 +08:00
await updateScript(state.formData);
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) {
console.error('提交失败:', error);
ElMessage.error('操作失败');
} finally {
state.loading = false;
}
};
/**
* 重置表单
*/
const resetForm = () => {
state.formData = {
id: 0,
2025-11-27 15:37:13 +08:00
tag: '',
2025-11-25 17:02:31 +08:00
content: '',
2025-11-27 15:37:13 +08:00
creator: '',
2025-11-28 18:03:52 +08:00
modifier: '',
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>