form.vue 4.4 KB

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