ReplyForm.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <Dialog title="回复" v-model="dialogVisible">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="100px"
  8. v-loading="formLoading"
  9. >
  10. <el-form-item label="回复内容" prop="replyContent">
  11. <el-input type="textarea" v-model="formData.replyContent" />
  12. </el-form-item>
  13. </el-form>
  14. <template #footer>
  15. <el-button @click="submitReplyForm" type="primary" :disabled="formLoading">确 定 </el-button>
  16. <el-button @click="dialogVisible = false">取 消</el-button>
  17. </template>
  18. </Dialog>
  19. </template>
  20. <script setup lang="ts">
  21. import * as CommentApi from '@/api/mall/product/comment'
  22. import { ElInput } from 'element-plus'
  23. defineOptions({ name: 'ProductComment' })
  24. const message = useMessage() // 消息弹窗
  25. const { t } = useI18n() // 国际化
  26. const dialogVisible = ref(false) // 弹窗的是否展示
  27. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  28. const formData = ref({
  29. id: undefined,
  30. replyContent: undefined
  31. })
  32. const formRules = reactive({
  33. replyContent: [{ required: true, message: '回复内容不能为空', trigger: 'blur' }]
  34. })
  35. const formRef = ref() // 表单 Ref
  36. /** 打开弹窗 */
  37. const open = async (id?: number) => {
  38. resetForm()
  39. formData.value.id = id
  40. dialogVisible.value = true
  41. }
  42. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  43. /** 提交表单 */
  44. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  45. const submitReplyForm = async () => {
  46. // 校验表单
  47. const valid = await formRef?.value?.validate()
  48. if (!valid) return
  49. // 提交请求
  50. formLoading.value = true
  51. try {
  52. await CommentApi.replyComment(formData.value)
  53. message.success(t('common.createSuccess'))
  54. dialogVisible.value = false
  55. // 发送操作成功的事件
  56. emit('success')
  57. } finally {
  58. formLoading.value = false
  59. }
  60. }
  61. /** 重置表单 */
  62. const resetForm = () => {
  63. formData.value = {
  64. id: undefined,
  65. replyContent: undefined
  66. }
  67. formRef.value?.resetFields()
  68. }
  69. </script>