form.vue 4.7 KB

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