SensitiveWordTestForm.vue 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <Dialog v-model="dialogVisible" title="检测敏感词">
  3. <el-form
  4. ref="formRef"
  5. v-loading="formLoading"
  6. :model="formData"
  7. :rules="formRules"
  8. label-width="80px"
  9. >
  10. <el-form-item label="文本" prop="text">
  11. <el-input v-model="formData.text" placeholder="请输入测试文本" type="textarea" />
  12. </el-form-item>
  13. <el-form-item label="标签" prop="tags">
  14. <el-select
  15. v-model="formData.tags"
  16. allow-create
  17. filterable
  18. multiple
  19. placeholder="请选择标签"
  20. style="width: 380px"
  21. >
  22. <el-option v-for="tag in tagList" :key="tag" :label="tag" :value="tag" />
  23. </el-select>
  24. </el-form-item>
  25. </el-form>
  26. <template #footer>
  27. <el-button :disabled="formLoading" type="primary" @click="submitForm">检 测</el-button>
  28. <el-button @click="dialogVisible = false">取 消</el-button>
  29. </template>
  30. </Dialog>
  31. </template>
  32. <script lang="ts" setup>
  33. import * as SensitiveWordApi from '@/api/system/sensitiveWord'
  34. defineOptions({ name: 'SystemSensitiveWordTestForm' })
  35. const message = useMessage() // 消息弹窗
  36. const dialogVisible = ref(false) // 弹窗的是否展示
  37. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  38. const formData = ref({
  39. text: '',
  40. tags: []
  41. })
  42. const formRules = reactive({
  43. text: [{ required: true, message: '测试文本不能为空', trigger: 'blur' }],
  44. tags: [{ required: true, message: '标签不能为空', trigger: 'blur' }]
  45. })
  46. const formRef = ref() // 表单 Ref
  47. const tagList = ref([]) // 标签数组
  48. /** 打开弹窗 */
  49. const open = async () => {
  50. dialogVisible.value = true
  51. resetForm()
  52. // 获得 Tag 标签列表
  53. tagList.value = await SensitiveWordApi.getSensitiveWordTagList()
  54. }
  55. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  56. /** 提交表单 */
  57. const submitForm = async () => {
  58. // 校验表单
  59. if (!formRef) return
  60. const valid = await formRef.value.validate()
  61. if (!valid) return
  62. // 提交请求
  63. formLoading.value = true
  64. try {
  65. const form = formData.value as unknown as SensitiveWordApi.SensitiveWordTestReqVO
  66. const data = await SensitiveWordApi.validateText(form)
  67. if (data.length === 0) {
  68. message.success('不包含敏感词!')
  69. return
  70. }
  71. message.warning('包含敏感词:' + data.join(', '))
  72. dialogVisible.value = false
  73. } finally {
  74. formLoading.value = false
  75. }
  76. }
  77. /** 重置表单 */
  78. const resetForm = () => {
  79. formData.value = {
  80. text: '',
  81. tags: []
  82. }
  83. formRef.value?.resetFields()
  84. }
  85. </script>