form.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <xn-form-container
  3. :title="formData.postId ? '编辑帖子信息表' : '增加帖子信息表'"
  4. :width="700"
  5. :visible="visible"
  6. :destroy-on-close="true"
  7. @close="onClose"
  8. >
  9. <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
  10. <a-form-item label="分类:" name="typeId">
  11. <a-select
  12. v-model:value="formData.typeId"
  13. show-search
  14. placeholder="请选择分类"
  15. style="width: 100%"
  16. :options="typeOptions"
  17. :filter-option="filterOption"
  18. ></a-select>
  19. </a-form-item>
  20. <a-form-item label="用户" name="userId">
  21. <a-select
  22. v-model:value="formData.userId"
  23. show-search
  24. placeholder="请选择用户"
  25. style="width: 100%"
  26. :options="usertypeOptions"
  27. :filter-option="filterOption"
  28. ></a-select>
  29. </a-form-item>
  30. <a-form-item label="帖子标题:" name="postTitle">
  31. <a-input v-model:value="formData.postTitle" placeholder="请输入帖子标题" allow-clear />
  32. </a-form-item>
  33. <a-form-item label="帖子内容:" name="postContent">
  34. <xn-editor v-model="formData.postContent" placeholder="请输入内容" :height="400"></xn-editor>
  35. </a-form-item>
  36. <a-form-item label="是否置顶:" name="isTop">
  37. <a-select
  38. v-model:value="formData.isTop"
  39. placeholder="请选择是否置顶"
  40. style="width: 100%"
  41. :options="topOptions"
  42. :filter-option="filterOption"
  43. ></a-select>
  44. </a-form-item>
  45. <!-- <a-form-item label="浏览次数:" name="viewCount">
  46. <a-input v-model:value="formData.viewCount" placeholder="请输入浏览次数" allow-clear />
  47. </a-form-item>
  48. <a-form-item label="回复次数:" name="replyCount">
  49. <a-input v-model:value="formData.replyCount" placeholder="请输入回复次数" allow-clear />
  50. </a-form-item>
  51. <a-form-item label="点赞次数:" name="likeCount">
  52. <a-input v-model:value="formData.likeCount" placeholder="请输入点赞次数" allow-clear />
  53. </a-form-item> -->
  54. <a-form-item label="帖子类型:" name="postType">
  55. <a-select
  56. v-model:value="formData.postType"
  57. show-search
  58. placeholder="请选择类型"
  59. style="width: 100%"
  60. :options="invitationOptions"
  61. :filter-option="filterOption"
  62. ></a-select>
  63. </a-form-item>
  64. <a-form-item label="定向用户:" name="appointUserArr">
  65. <a-select
  66. v-model:value="formData.appointUserArr"
  67. show-search
  68. mode="multiple"
  69. placeholder="请选择用户"
  70. style="width: 100%"
  71. :options="usertypeOptions"
  72. :filter-option="filterOption"
  73. ></a-select>
  74. </a-form-item>
  75. </a-form>
  76. <template #footer>
  77. <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
  78. <a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
  79. </template>
  80. </xn-form-container>
  81. </template>
  82. <script setup name="forumPostInfoForm">
  83. import { cloneDeep } from 'lodash-es'
  84. import { required } from '@/utils/formRules'
  85. import forumPostInfoApi from '@/api/forum/forumPostInfoApi'
  86. import forumApi from '@/api/forum/forumApi'
  87. import XnEditor from '@/components/Editor/index.vue'
  88. // 抽屉状态
  89. const visible = ref(false)
  90. const emit = defineEmits({ successful: null })
  91. const formRef = ref()
  92. // 表单数据
  93. const formData = ref({})
  94. const submitLoading = ref(false)
  95. //分类
  96. const typeOptions = ref([])
  97. //用户
  98. const usertypeOptions = ref([])
  99. //置顶
  100. const topOptions = ref([
  101. {
  102. label: '普通',
  103. value: 0
  104. },
  105. {
  106. label: '置顶',
  107. value: 1
  108. }
  109. ])
  110. //帖子类型
  111. const invitationOptions = ref([
  112. {
  113. label: '普通帖子',
  114. value: 0
  115. },
  116. {
  117. label: '技术支持',
  118. value: 1
  119. },
  120. {
  121. label: '内容纠错',
  122. value: 2
  123. }
  124. ])
  125. // 打开抽屉
  126. const onOpen = (record) => {
  127. visible.value = true
  128. forumApi.forumTypeList().then((data) => {
  129. typeOptions.value = data.map((r) => {
  130. return {
  131. label: r.typeName,
  132. value: r.typeId,
  133. ...r
  134. }
  135. })
  136. })
  137. forumApi.allUserList().then((data) => {
  138. usertypeOptions.value = data.map((r) => {
  139. return {
  140. label: r.name,
  141. value: r.id,
  142. ...r
  143. }
  144. })
  145. })
  146. if (record) {
  147. if (record.appointUser) {
  148. record.appointUserArr = record.appointUser.split(',')
  149. }
  150. let recordData = cloneDeep(record)
  151. formData.value = Object.assign({}, recordData)
  152. }
  153. }
  154. // 关闭抽屉
  155. const onClose = () => {
  156. formRef.value.resetFields()
  157. formData.value = {}
  158. visible.value = false
  159. }
  160. const filterOption = (input, option) => {
  161. return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0
  162. }
  163. // 默认要校验的
  164. const formRules = {}
  165. // 验证并提交数据
  166. const onSubmit = () => {
  167. formRef.value.validate().then(() => {
  168. submitLoading.value = true
  169. const formDataParam = cloneDeep(formData.value)
  170. forumPostInfoApi
  171. .forumPostInfoSubmitForm(formDataParam, formDataParam.postId)
  172. .then(() => {
  173. if (formData.value.appointUserArr && formData.value.appointUserArr.length > 1) {
  174. formData.value.appointUser = formData.value.appointUserArr.join(',')
  175. }
  176. onClose()
  177. emit('successful')
  178. })
  179. .finally(() => {
  180. submitLoading.value = false
  181. })
  182. })
  183. }
  184. // 抛出函数
  185. defineExpose({
  186. onOpen
  187. })
  188. </script>