123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <template>
- <el-dialog title="新增违禁词" v-model="state.dialogVisible" width="769px" draggable @close="close">
- <el-form :model="state.ruleForm" ref="ruleFormRef" label-width="100px">
- <el-row :gutter="10">
- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
- <el-form-item label="违禁词" prop="name" :rules="[{ required: true, message: '请输入违禁词', trigger: 'blur' }]">
- <el-input v-model="state.ruleForm.name" placeholder="请输入违禁词" clearable></el-input>
- </el-form-item>
- </el-col>
- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
- <el-form-item label="违禁词分类" prop="classify" :rules="[{ required: true, message: '请选择违禁词分类', trigger: 'change' }]">
- <el-select v-model="state.ruleForm.classify" placeholder="请选择违禁词分类" class="w100">
- <el-option v-for="item in props.prohibitedClassify" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
- </el-select>
- </el-form-item>
- </el-col>
- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
- <el-form-item label="违禁词属性" prop="type" :rules="[{ required: true, message: '请选择违禁词属性', trigger: 'change' }]">
- <el-select v-model="state.ruleForm.type" placeholder="请选择违禁词属性" class="w100">
- <el-option v-for="item in props.prohibitedType" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
- </el-select>
- </el-form-item>
- </el-col>
- <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
- <el-form-item label="违禁同义词" prop="synonym" :rules="[{ required: false, message: '请选择违禁同义词', trigger: 'change' }]">
- <el-tag v-for="tag in dynamicTags" :key="tag" class="mr10 mb10" size="large" closable :disable-transitions="false" @close="handleClose(tag)">
- {{ tag }}
- </el-tag>
- <el-input
- v-if="inputVisible"
- ref="InputRef"
- v-model="inputValue"
- @keyup.enter="handleInputConfirm"
- @blur="handleInputConfirm"
- style="max-width: 200px"
- class="mb10"
- />
- <el-button v-else @click="showInput" class="mb10"> 添加同义词 </el-button>
- </el-form-item>
- </el-col>
- </el-row>
- </el-form>
- <template #footer>
- <span class="dialog-footer">
- <el-button @click="closeDialog" class="default-button">取 消</el-button>
- <el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="state.loading">确 定 </el-button>
- </span>
- </template>
- </el-dialog>
- </template>
- <script setup lang="ts" name="qualityLexiconAdd">
- import { nextTick, reactive, ref } from 'vue';
- import { ElInput, ElMessage, FormInstance } from 'element-plus';
- import { lexiconAdd } from '/@/api/quality/lexicon';
- // 定义子组件向父组件传值/事件
- const emit = defineEmits(['updateList']);
- const props = defineProps({
- prohibitedClassify: {
- type: Array,
- default: () => [],
- },
- prohibitedType: {
- type: Array,
- default: () => [],
- },
- });
- const inputValue = ref('');
- const dynamicTags = ref<EmptyArrayType>([]);
- const inputVisible = ref(false);
- const InputRef = ref<InstanceType<typeof ElInput>>();
- // 删除同义词
- const handleClose = (tag: string) => {
- dynamicTags.value.splice(dynamicTags.value.indexOf(tag), 1);
- };
- // 展示输入框
- const showInput = () => {
- inputVisible.value = true;
- nextTick(() => {
- InputRef.value!.input!.focus();
- });
- };
- // 确定添加
- const handleInputConfirm = () => {
- if (inputValue.value) {
- dynamicTags.value.push(inputValue.value);
- }
- inputVisible.value = false;
- inputValue.value = '';
- };
- // 定义变量内容
- const ruleFormRef = ref<FormInstance>();
- const state = reactive<any>({
- dialogVisible: false, // 弹窗显示隐藏
- ruleForm: {
- name: '', // 违禁词
- classify: '', // 违禁词分类
- type: '', // 违禁词属性
- },
- loading: false, // 确定按钮loading
- });
- // 打开弹窗
- const openDialog = async () => {
- state.dialogVisible = true;
- };
- // 关闭弹窗
- const closeDialog = () => {
- state.dialogVisible = false;
- };
- const close = () => {
- dynamicTags.value = [];
- ruleFormRef.value?.clearValidate();
- ruleFormRef.value?.resetFields();
- };
- // 新增
- const onSubmit = (formEl: FormInstance | undefined) => {
- if (!formEl) return;
- formEl.validate((valid: boolean) => {
- if (!valid) return;
- state.loading = true;
- const request = {
- ...state.ruleForm,
- synonym: dynamicTags.value.join(','),
- };
- lexiconAdd(request)
- .then(() => {
- emit('updateList');
- closeDialog(); // 关闭弹窗
- ElMessage.success('操作成功');
- state.loading = false;
- })
- .catch(() => {
- emit('updateList');
- state.loading = false;
- });
- });
- };
- // 暴露变量
- defineExpose({
- openDialog,
- closeDialog,
- });
- </script>
|