form.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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
  18. v-model:value="formData.postTitle"
  19. placeholder="请输入标题"
  20. max
  21. allow-clear
  22. show-count
  23. :maxlength="100"
  24. />
  25. </a-form-item>
  26. </a-col>
  27. <a-col :span="8">
  28. <a-form-item label="分类:" name="typeId">
  29. <a-select
  30. v-model:value="formData.typeId"
  31. placeholder="请选择分类"
  32. style="width: 100%"
  33. :options="typeOptions"
  34. ></a-select>
  35. </a-form-item>
  36. </a-col>
  37. <a-col :span="8">
  38. <a-form-item label="指向:" name="appointUserArr">
  39. <a-select
  40. v-model:value="formData.appointUserArr"
  41. mode="multiple"
  42. show-search
  43. placeholder="请选择"
  44. style="width: 100%"
  45. :options="usertypeOptions"
  46. :filter-option="filterOption"
  47. ></a-select>
  48. <!-- @popupScroll="popupScroll" -->
  49. </a-form-item>
  50. </a-col>
  51. <a-col :span="24">
  52. <a-form-item label="内容:" name="postContent">
  53. <xn-editor v-model="formData.postContent" placeholder="请输入内容" :height="300"></xn-editor>
  54. </a-form-item>
  55. </a-col>
  56. </a-row>
  57. </a-form>
  58. <template #footer>
  59. <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
  60. <a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
  61. </template>
  62. </xn-form-container>
  63. </template>
  64. <script setup>
  65. import { required } from '@/utils/formRules'
  66. import SnowflakeId from 'snowflake-id'
  67. import tool from '@/utils/tool'
  68. import forumApi from '@/api/forum/forumApi'
  69. import XnEditor from '@/components/Editor/index.vue'
  70. const userInfo = tool.data.get('USER_INFO')
  71. const typeOptions = ref([])
  72. // 默认是关闭状态
  73. const visible = ref(false)
  74. const emit = defineEmits({ successful: null })
  75. const formRef = ref()
  76. const treeData = ref([])
  77. // 表单数据,也就是默认给一些数据
  78. const formData = ref({
  79. postType: 0
  80. })
  81. // 默认展开的节点(顶级)
  82. const defaultExpandedKeys = ref([0])
  83. const submitLoading = ref(false)
  84. // 模块ID
  85. const moduleId = ref('')
  86. const usertypeOptions = ref([])
  87. const filterOption = (input, option) => {
  88. return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0
  89. }
  90. const userPagination = ref({
  91. size: 99999999,
  92. current: 1
  93. })
  94. const userTotal = ref(0)
  95. //获取用户
  96. const getUserAllList = (add) => {
  97. forumApi.allUserList(userPagination.value).then((res) => {
  98. userTotal.value = res.total
  99. let userList = res.records.map((r) => {
  100. return {
  101. label: `${r.name}-${r.eduIdentityName ?? ''}`,
  102. value: r.id,
  103. ...r
  104. }
  105. })
  106. usertypeOptions.value = add ? [...usertypeOptions.value, ...userList] : userList
  107. })
  108. }
  109. const popupScroll = (el) => {
  110. let offset = 0
  111. if (el.target.scrollHeight - el.target.scrollTop - offset <= el.target.clientHeight) {
  112. if (userPagination.value.size * userPagination.value.current < userTotal.value) {
  113. userPagination.value.current += 1
  114. getUserAllList(true)
  115. }
  116. }
  117. }
  118. // 打开抽屉
  119. const onOpen = (record, module) => {
  120. moduleId.value = module
  121. visible.value = true
  122. forumApi.forumTypeList().then((data) => {
  123. typeOptions.value = data.map((r) => {
  124. return {
  125. label: r.typeName,
  126. value: r.typeId,
  127. ...r
  128. }
  129. })
  130. })
  131. getUserAllList()
  132. if (module) {
  133. if (record?.appointUser) {
  134. record.appointUserArr = record.appointUser.split(',')
  135. }
  136. formData.value = Object.assign(formData.value, record)
  137. }
  138. }
  139. // 关闭抽屉
  140. const onClose = () => {
  141. formRef.value.resetFields()
  142. visible.value = false
  143. }
  144. // 默认要校验的
  145. const formRules = {
  146. postTitle: [required('请输入标题')],
  147. typeId: [required('请选择分类')],
  148. postContent: [required('请输入内容')]
  149. }
  150. const categoryOptions = tool.dictList('MENU_TYPE')
  151. const visibleOptions = tool.dictList('MENU_VISIBLE')
  152. // 验证并提交数据
  153. const onSubmit = () => {
  154. submitLoading.value = true
  155. formRef.value.validate().then(() => {
  156. if (formData.value.appointUserArr && formData.value.appointUserArr.length > 1) {
  157. formData.value.appointUser = formData.value.appointUserArr.join(',')
  158. }
  159. forumApi
  160. .submitForm(formData.value, formData.value.postId)
  161. .then(() => {
  162. onClose()
  163. emit('successful')
  164. })
  165. .finally(() => {
  166. submitLoading.value = false
  167. })
  168. })
  169. }
  170. // 调用这个函数将子组件的一些数据和方法暴露出去
  171. defineExpose({
  172. onOpen
  173. })
  174. </script>