form.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <xn-form-container
  3. :title="formData.id ? '编辑帖子' : '增加帖子'"
  4. :height="550"
  5. :get-container="false"
  6. :visible="visible"
  7. :destroy-on-close="true"
  8. @close="onClose"
  9. placement="bottom"
  10. :mask="false"
  11. style="width: 960px;left: calc(50% - 960px / 2);"
  12. >
  13. <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
  14. <a-row :gutter="16">
  15. <a-col :span="8">
  16. <a-form-item label="标题:" name="postTitle">
  17. <a-input v-model:value="formData.postTitle" placeholder="请输入标题" allow-clear />
  18. </a-form-item>
  19. </a-col>
  20. <a-col :span="8">
  21. <a-form-item label="分类:" name="typeId">
  22. <a-select
  23. v-model:value="formData.typeId"
  24. show-search
  25. placeholder="请选择分类"
  26. style="width: 100%"
  27. :options="typeOptions"
  28. :filter-option="filterOption"
  29. ></a-select>
  30. </a-form-item>
  31. </a-col>
  32. <a-col :span="8">
  33. <a-form-item label="指向:" name="appointUserArr">
  34. <a-select
  35. v-model:value="formData.appointUserArr"
  36. mode="multiple"
  37. show-search
  38. placeholder="请选择"
  39. style="width: 100%"
  40. :options="usertypeOptions"
  41. :filter-option="filterOption"
  42. ></a-select>
  43. </a-form-item>
  44. </a-col>
  45. <a-col :span="24">
  46. <a-form-item label="内容:" name="postContent">
  47. <xn-editor v-model="formData.postContent" placeholder="请输入内容" :height="300"></xn-editor>
  48. </a-form-item>
  49. </a-col>
  50. </a-row>
  51. </a-form>
  52. <template #footer>
  53. <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
  54. <a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
  55. </template>
  56. </xn-form-container>
  57. </template>
  58. <script setup>
  59. import { required } from '@/utils/formRules'
  60. import SnowflakeId from 'snowflake-id'
  61. import tool from '@/utils/tool'
  62. import forumApi from '@/api/forum/forumApi'
  63. import XnEditor from '@/components/Editor/index.vue'
  64. const userInfo = tool.data.get('USER_INFO')
  65. const typeOptions = ref([])
  66. // 默认是关闭状态
  67. const visible = ref(false)
  68. const emit = defineEmits({ successful: null })
  69. const formRef = ref()
  70. const treeData = ref([])
  71. // 表单数据,也就是默认给一些数据
  72. const formData = ref({
  73. postType: 0
  74. })
  75. // 默认展开的节点(顶级)
  76. const defaultExpandedKeys = ref([0])
  77. const submitLoading = ref(false)
  78. // 模块ID
  79. const moduleId = ref('')
  80. const usertypeOptions = ref([])
  81. const filterOption = (input, option) => {
  82. return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
  83. }
  84. // 打开抽屉
  85. const onOpen = (record, module) => {
  86. moduleId.value = module
  87. visible.value = true
  88. forumApi.forumTypeList().then((data) => {
  89. typeOptions.value = data.map((r) => {
  90. return {
  91. label: r.typeName,
  92. value: r.typeId,
  93. ...r
  94. }
  95. })
  96. })
  97. forumApi.allUserList().then((data) => {
  98. usertypeOptions.value = data.map((r) => {
  99. return {
  100. label: r.name,
  101. value: r.id,
  102. ...r
  103. }
  104. })
  105. })
  106. if (module) {
  107. if (record.appointUser) {
  108. record.appointUserArr = record.appointUser.split(',')
  109. }
  110. formData.value = Object.assign(formData.value, record)
  111. }
  112. }
  113. // 关闭抽屉
  114. const onClose = () => {
  115. formRef.value.resetFields()
  116. visible.value = false
  117. }
  118. // 默认要校验的
  119. const formRules = {
  120. postTitle: [required('请输入标题')],
  121. typeId: [required('请选择分类')],
  122. postContent: [required('请输入内容')]
  123. }
  124. const categoryOptions = tool.dictList('MENU_TYPE')
  125. const visibleOptions = tool.dictList('MENU_VISIBLE')
  126. // 验证并提交数据
  127. const onSubmit = () => {
  128. formRef.value
  129. .validate()
  130. .then(() => {
  131. submitLoading.value = true
  132. if (formData.value.appointUserArr && formData.value.appointUserArr.length > 1) {
  133. formData.value.appointUser = formData.value.appointUserArr.join(',')
  134. }
  135. forumApi.submitForm(formData.value, formData.value.postId).then(() => {
  136. onClose()
  137. emit('successful')
  138. })
  139. })
  140. .finally(() => {
  141. submitLoading.value = false
  142. })
  143. }
  144. // 调用这个函数将子组件的一些数据和方法暴露出去
  145. defineExpose({
  146. onOpen
  147. })
  148. </script>