form.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <xn-form-container
  3. :title="formData.id ? '编辑组织' : '增加组织'"
  4. :width="550"
  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="parentId">
  11. <a-tree-select
  12. v-model:value="formData.parentId"
  13. style="width: 100%"
  14. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  15. placeholder="请选择上级组织"
  16. allow-clear
  17. tree-default-expand-all
  18. :tree-data="treeData"
  19. :field-names="{
  20. children: 'children',
  21. label: 'name',
  22. value: 'id'
  23. }"
  24. selectable="false"
  25. tree-line
  26. />
  27. </a-form-item>
  28. <a-form-item label="组织名称:" name="name">
  29. <a-input v-model:value="formData.name" placeholder="请输入组织名称" allow-clear />
  30. </a-form-item>
  31. <!-- <a-form-item label="组织分类:" name="category">
  32. <a-select
  33. v-model:value="formData.category"
  34. :options="orgCategoryOptions"
  35. style="width: 100%"
  36. placeholder="请选择组织分类"
  37. />
  38. </a-form-item> -->
  39. <a-form-item label="排序:" name="sortCode">
  40. <a-input-number style="width: 100%" v-model:value="formData.sortCode" :max="100" />
  41. </a-form-item>
  42. <!-- <a-form-item label="指定主管:" name="directorId">
  43. <a-button type="link" style="padding-left: 0px" @click="openSelector(formData.directorId)">选择</a-button>
  44. <a-tag v-if="formData.directorId && formData.directorName" color="orange" closable @close="closeUserTag">{{
  45. formData.directorName
  46. }}</a-tag>
  47. <a-input v-show="false" v-model:value="formData.directorId" />
  48. </a-form-item> -->
  49. </a-form>
  50. <template #footer>
  51. <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
  52. <a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
  53. </template>
  54. <user-selector-plus
  55. ref="userSelectorPlusRef"
  56. :org-tree-api="selectorApiFunction.orgTreeApi"
  57. :user-page-api="selectorApiFunction.userPageApi"
  58. :checked-user-list-api="selectorApiFunction.checkedUserListApi"
  59. :radio-model="true"
  60. @onBack="userBack"
  61. />
  62. </xn-form-container>
  63. </template>
  64. <script setup name="orgForm">
  65. import { required } from '@/utils/formRules'
  66. import orgApi from '@/api/organization/organization'
  67. import userCenterApi from '@/api/sys/userCenterApi'
  68. import UserSelectorPlus from '@/components/Selector/userSelectorPlus.vue'
  69. import tool from '@/utils/tool'
  70. // 定义emit事件
  71. const emit = defineEmits({ successful: null })
  72. // 默认是关闭状态
  73. let visible = $ref(false)
  74. let userSelectorPlusRef = ref()
  75. const formRef = ref()
  76. // 表单数据,也就是默认给一些数据
  77. const formData = ref({})
  78. // 定义机构元素
  79. const treeData = ref([])
  80. const submitLoading = ref(false)
  81. // 打开抽屉
  82. const onOpen = (record, parentId) => {
  83. visible = true
  84. formData.value = {
  85. sortCode: 99
  86. }
  87. if (parentId) {
  88. formData.value.parentId = parentId
  89. }
  90. if (record) {
  91. const param = {
  92. id: record.id
  93. }
  94. orgApi.orgDetail(param).then((data) => {
  95. formData.value = Object.assign({}, data)
  96. })
  97. }
  98. // 获取机构树并加入顶级
  99. orgApi.orgOrgTreeSelector().then((res) => {
  100. treeData.value = [
  101. {
  102. id: 0,
  103. parentId: '-1',
  104. name: '顶级',
  105. children: res
  106. }
  107. ]
  108. })
  109. }
  110. // 关闭抽屉
  111. const onClose = () => {
  112. visible = false
  113. }
  114. // 默认要校验的
  115. const formRules = {
  116. name: [required('请输入组织名称')],
  117. // category: [required('请选择组织分类')],
  118. sortCode: [required('请选择排序')]
  119. }
  120. // 机构分类字典
  121. const orgCategoryOptions = tool.dictList('ORG_CATEGORY')
  122. // 打开人员选择器,选择主管
  123. const openSelector = (id) => {
  124. let checkedUserIds = []
  125. checkedUserIds.push(id)
  126. userSelectorPlusRef.value.showUserPlusModal(checkedUserIds)
  127. }
  128. // 人员选择器回调
  129. const userBack = (value) => {
  130. if (value.length > 0) {
  131. formData.value.directorId = value[0].id
  132. formData.value.directorName = value[0].name
  133. } else {
  134. formData.value.directorId = ''
  135. formData.value.directorName = ''
  136. }
  137. }
  138. // 通过小标签删除主管
  139. const closeUserTag = () => {
  140. formData.value.directorId = ''
  141. formData.value.directorName = ''
  142. }
  143. // 验证并提交数据
  144. const onSubmit = () => {
  145. formRef.value.validate().then(() => {
  146. submitLoading.value = true
  147. orgApi
  148. .submitForm(formData.value, formData.value.id)
  149. .then(() => {
  150. visible = false
  151. emit('successful')
  152. })
  153. .finally(() => {
  154. submitLoading.value = false
  155. })
  156. })
  157. }
  158. // 传递设计器需要的API
  159. const selectorApiFunction = {
  160. orgTreeApi: (param) => {
  161. return orgApi.orgOrgTreeSelector(param).then((data) => {
  162. return Promise.resolve(data)
  163. })
  164. },
  165. userPageApi: (param) => {
  166. return orgApi.orgUserSelector(param).then((data) => {
  167. return Promise.resolve(data)
  168. })
  169. },
  170. checkedUserListApi: (param) => {
  171. return userCenterApi.userCenterGetUserListByIdList(param).then((data) => {
  172. return Promise.resolve(data)
  173. })
  174. }
  175. }
  176. // 调用这个函数将子组件的一些数据和方法暴露出去
  177. defineExpose({
  178. onOpen
  179. })
  180. </script>