Lexicon-add.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <el-dialog title="新增违禁词" v-model="state.dialogVisible" width="769px" draggable @close="close">
  3. <el-form :model="state.ruleForm" ref="ruleFormRef" label-width="100px">
  4. <el-row :gutter="10">
  5. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  6. <el-form-item label="违禁词" prop="name" :rules="[{ required: true, message: '请输入违禁词', trigger: 'blur' }]">
  7. <el-input v-model="state.ruleForm.name" placeholder="请输入违禁词" clearable></el-input>
  8. </el-form-item>
  9. </el-col>
  10. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  11. <el-form-item label="违禁词分类" prop="classify" :rules="[{ required: true, message: '请选择违禁词分类', trigger: 'change' }]">
  12. <el-select v-model="state.ruleForm.classify" placeholder="请选择违禁词分类" class="w100">
  13. <el-option v-for="item in props.prohibitedClassify" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
  14. </el-select>
  15. </el-form-item>
  16. </el-col>
  17. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  18. <el-form-item label="违禁词属性" prop="type" :rules="[{ required: true, message: '请选择违禁词属性', trigger: 'change' }]">
  19. <el-select v-model="state.ruleForm.type" placeholder="请选择违禁词属性" class="w100">
  20. <el-option v-for="item in props.prohibitedType" :value="item.dicDataValue" :key="item.dicDataValue" :label="item.dicDataName" />
  21. </el-select>
  22. </el-form-item>
  23. </el-col>
  24. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  25. <el-form-item label="违禁同义词" prop="synonym" :rules="[{ required: false, message: '请选择违禁同义词', trigger: 'change' }]">
  26. <el-tag v-for="tag in dynamicTags" :key="tag" class="mr10 mb10" size="large" closable :disable-transitions="false" @close="handleClose(tag)">
  27. {{ tag }}
  28. </el-tag>
  29. <el-input
  30. v-if="inputVisible"
  31. ref="InputRef"
  32. v-model="inputValue"
  33. @keyup.enter="handleInputConfirm"
  34. @blur="handleInputConfirm"
  35. style="max-width: 200px"
  36. class="mb10"
  37. />
  38. <el-button v-else @click="showInput" class="mb10"> 添加同义词 </el-button>
  39. </el-form-item>
  40. </el-col>
  41. </el-row>
  42. </el-form>
  43. <template #footer>
  44. <span class="dialog-footer">
  45. <el-button @click="closeDialog" class="default-button">取 消</el-button>
  46. <el-button type="primary" @click="onSubmit(ruleFormRef)" :loading="state.loading">确 定 </el-button>
  47. </span>
  48. </template>
  49. </el-dialog>
  50. </template>
  51. <script setup lang="ts" name="qualityLexiconAdd">
  52. import { nextTick, reactive, ref } from 'vue';
  53. import { ElInput, ElMessage, FormInstance } from 'element-plus';
  54. import { lexiconAdd } from '/@/api/quality/lexicon';
  55. // 定义子组件向父组件传值/事件
  56. const emit = defineEmits(['updateList']);
  57. const props = defineProps({
  58. prohibitedClassify: {
  59. type: Array,
  60. default: () => [],
  61. },
  62. prohibitedType: {
  63. type: Array,
  64. default: () => [],
  65. },
  66. });
  67. const inputValue = ref('');
  68. const dynamicTags = ref<EmptyArrayType>([]);
  69. const inputVisible = ref(false);
  70. const InputRef = ref<InstanceType<typeof ElInput>>();
  71. // 删除同义词
  72. const handleClose = (tag: string) => {
  73. dynamicTags.value.splice(dynamicTags.value.indexOf(tag), 1);
  74. };
  75. // 展示输入框
  76. const showInput = () => {
  77. inputVisible.value = true;
  78. nextTick(() => {
  79. InputRef.value!.input!.focus();
  80. });
  81. };
  82. // 确定添加
  83. const handleInputConfirm = () => {
  84. if (inputValue.value) {
  85. dynamicTags.value.push(inputValue.value);
  86. }
  87. inputVisible.value = false;
  88. inputValue.value = '';
  89. };
  90. // 定义变量内容
  91. const ruleFormRef = ref<FormInstance>();
  92. const state = reactive<any>({
  93. dialogVisible: false, // 弹窗显示隐藏
  94. ruleForm: {
  95. name: '', // 违禁词
  96. classify: '', // 违禁词分类
  97. type: '', // 违禁词属性
  98. },
  99. loading: false, // 确定按钮loading
  100. });
  101. // 打开弹窗
  102. const openDialog = async () => {
  103. state.dialogVisible = true;
  104. };
  105. // 关闭弹窗
  106. const closeDialog = () => {
  107. state.dialogVisible = false;
  108. };
  109. const close = () => {
  110. dynamicTags.value = [];
  111. ruleFormRef.value?.clearValidate();
  112. ruleFormRef.value?.resetFields();
  113. };
  114. // 新增
  115. const onSubmit = (formEl: FormInstance | undefined) => {
  116. if (!formEl) return;
  117. formEl.validate((valid: boolean) => {
  118. if (!valid) return;
  119. state.loading = true;
  120. const request = {
  121. ...state.ruleForm,
  122. synonym: dynamicTags.value.join(','),
  123. };
  124. lexiconAdd(request)
  125. .then(() => {
  126. emit('updateList');
  127. closeDialog(); // 关闭弹窗
  128. ElMessage.success('操作成功');
  129. state.loading = false;
  130. })
  131. .catch(() => {
  132. emit('updateList');
  133. state.loading = false;
  134. });
  135. });
  136. };
  137. // 暴露变量
  138. defineExpose({
  139. openDialog,
  140. closeDialog,
  141. });
  142. </script>