form.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <xn-form-container
  3. :title="formData.id ? '编辑主题' : '增加主题'"
  4. :width="700"
  5. :visible="visible"
  6. :destroy-on-close="true"
  7. @close="onClose"
  8. >
  9. <a-alert
  10. class="mb-3"
  11. message="欢迎来到论坛 — 衷心感谢你参与讨论!
  12. 标题是否清晰明了地描述了主题?看
  13. 起来有意思么?
  14. 谁会感兴趣?
  15. 它为什么重要?
  16. 你期望从社区中获得什么样的回应?
  17. 选择常用的词句以便别人能找到你的主题。若想要给主题和类似主题分组,选择一个分类。"
  18. type="warning"
  19. />
  20. <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
  21. <a-row :gutter="16">
  22. <a-col :span="12">
  23. <a-form-item label="标题:" name="postTitle">
  24. <a-input v-model:value="formData.postTitle" placeholder="请输入显示名称" allow-clear />
  25. </a-form-item>
  26. </a-col>
  27. <a-col :span="12">
  28. <a-form-item label="分类:" name="typeId">
  29. <a-select
  30. v-model:value="formData.typeId"
  31. show-search
  32. placeholder="所有分类"
  33. style="width: 200px"
  34. :options="typeOptions"
  35. :filter-option="filterOption"
  36. ></a-select>
  37. </a-form-item>
  38. </a-col>
  39. <a-col :span="24">
  40. <a-form-item label="内容:" name="postContent">
  41. <xn-editor v-model="formData.postContent" placeholder="请输入" :height="400"></xn-editor>
  42. </a-form-item>
  43. </a-col>
  44. </a-row>
  45. </a-form>
  46. <template #footer>
  47. <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
  48. <a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
  49. </template>
  50. </xn-form-container>
  51. </template>
  52. <script setup>
  53. import { required } from '@/utils/formRules'
  54. import SnowflakeId from 'snowflake-id'
  55. import tool from '@/utils/tool'
  56. import forumApi from '@/api/forum/forumApi'
  57. import XnEditor from '@/components/Editor/index.vue'
  58. const userInfo = tool.data.get('USER_INFO')
  59. const typeOptions = ref([])
  60. // 默认是关闭状态
  61. const visible = ref(false)
  62. const emit = defineEmits({ successful: null })
  63. const formRef = ref()
  64. const treeData = ref([])
  65. // 表单数据,也就是默认给一些数据
  66. const formData = ref({
  67. postType: 0
  68. })
  69. // 默认展开的节点(顶级)
  70. const defaultExpandedKeys = ref([0])
  71. const submitLoading = ref(false)
  72. // 模块ID
  73. const moduleId = ref('')
  74. const filterOption = (input, option) => {
  75. return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
  76. }
  77. // 打开抽屉
  78. const onOpen = (record, module) => {
  79. moduleId.value = module
  80. visible.value = true
  81. forumApi.forumTypeList().then((data) => {
  82. typeOptions.value = data.map((r) => {
  83. return {
  84. label: r.typeName,
  85. value: r.typeId,
  86. ...r
  87. }
  88. })
  89. })
  90. if (module) {
  91. formData.value = Object.assign(formData.value, record)
  92. }
  93. }
  94. // 关闭抽屉
  95. const onClose = () => {
  96. formRef.value.resetFields()
  97. visible.value = false
  98. }
  99. // 默认要校验的
  100. const formRules = {
  101. postTitle: [required('请输入标题')],
  102. typeId: [required('请选择分类')],
  103. postContent: [required('请输入内容')]
  104. }
  105. const categoryOptions = tool.dictList('MENU_TYPE')
  106. const visibleOptions = tool.dictList('MENU_VISIBLE')
  107. // 验证并提交数据
  108. const onSubmit = () => {
  109. formRef.value
  110. .validate()
  111. .then(() => {
  112. submitLoading.value = true
  113. forumApi.submitForm(formData.value, formData.value.postId).then(() => {
  114. onClose()
  115. emit('successful')
  116. })
  117. })
  118. .finally(() => {
  119. submitLoading.value = false
  120. })
  121. }
  122. // 调用这个函数将子组件的一些数据和方法暴露出去
  123. defineExpose({
  124. onOpen
  125. })
  126. </script>