index.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <ContentWrap>
  3. <!-- 列表 -->
  4. <vxe-grid ref="xGrid" v-bind="gridOptions" show-overflow class="xtable-scrollbar">
  5. <template #toolbar_buttons>
  6. <!-- 操作:新增 -->
  7. <XButton
  8. type="primary"
  9. preIcon="ep:zoom-in"
  10. :title="t('action.add')"
  11. v-hasPermi="['system:dept:create']"
  12. @click="handleCreate()"
  13. />
  14. <XButton title="展开所有" @click="xGrid?.setAllTreeExpand(true)" />
  15. <XButton title="关闭所有" @click="xGrid?.clearTreeExpand()" />
  16. </template>
  17. <template #leaderUserId_default="{ row }">
  18. <span>{{ userNicknameFormat(row) }}</span>
  19. </template>
  20. <template #actionbtns_default="{ row }">
  21. <!-- 操作:修改 -->
  22. <XTextButton
  23. preIcon="ep:edit"
  24. :title="t('action.edit')"
  25. v-hasPermi="['system:dept:update']"
  26. @click="handleUpdate(row.id)"
  27. />
  28. <!-- 操作:删除 -->
  29. <XTextButton
  30. preIcon="ep:delete"
  31. :title="t('action.del')"
  32. v-hasPermi="['system:dept:delete']"
  33. @click="handleDelete(row.id)"
  34. />
  35. </template>
  36. </vxe-grid>
  37. </ContentWrap>
  38. <!-- 添加或修改菜单对话框 -->
  39. <XModal id="deptModel" v-model="dialogVisible" :title="dialogTitle">
  40. <!-- 对话框(添加 / 修改) -->
  41. <Form ref="formRef" :schema="allSchemas.formSchema" :rules="rules">
  42. <template #parentId="form">
  43. <el-tree-select
  44. node-key="id"
  45. v-model="form['parentId']"
  46. :props="defaultProps"
  47. :data="deptOptions"
  48. :default-expanded-keys="[100]"
  49. check-strictly
  50. />
  51. </template>
  52. <template #leaderUserId="form">
  53. <el-select v-model="form['leaderUserId']">
  54. <el-option
  55. v-for="item in userOption"
  56. :key="item.id"
  57. :label="item.nickname"
  58. :value="item.id"
  59. />
  60. </el-select>
  61. </template>
  62. </Form>
  63. <template #footer>
  64. <!-- 按钮:保存 -->
  65. <XButton
  66. v-if="['create', 'update'].includes(actionType)"
  67. type="primary"
  68. :loading="actionLoading"
  69. @click="submitForm()"
  70. :title="t('action.save')"
  71. />
  72. <!-- 按钮:关闭 -->
  73. <XButton :loading="actionLoading" @click="dialogVisible = false" :title="t('dialog.close')" />
  74. </template>
  75. </XModal>
  76. </template>
  77. <script setup lang="ts" name="Dept">
  78. import { nextTick, onMounted, ref, unref } from 'vue'
  79. import { ElSelect, ElTreeSelect, ElOption } from 'element-plus'
  80. import { VxeGridInstance } from 'vxe-table'
  81. import { handleTree, defaultProps } from '@/utils/tree'
  82. import { useI18n } from '@/hooks/web/useI18n'
  83. import { useMessage } from '@/hooks/web/useMessage'
  84. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  85. import { FormExpose } from '@/components/Form'
  86. import { allSchemas, rules } from './dept.data'
  87. import * as DeptApi from '@/api/system/dept'
  88. import { getListSimpleUsersApi, UserVO } from '@/api/system/user'
  89. const { t } = useI18n() // 国际化
  90. const message = useMessage() // 消息弹窗
  91. // 列表相关的变量
  92. const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
  93. const treeConfig = {
  94. transform: true,
  95. rowField: 'id',
  96. parentField: 'parentId',
  97. expandAll: true
  98. }
  99. // 弹窗相关的变量
  100. const dialogVisible = ref(false) // 是否显示弹出层
  101. const dialogTitle = ref('edit') // 弹出层标题
  102. const actionType = ref('') // 操作按钮的类型
  103. const actionLoading = ref(false) // 遮罩层
  104. const formRef = ref<FormExpose>() // 表单 Ref
  105. const deptOptions = ref() // 树形结构
  106. const userOption = ref<UserVO[]>([])
  107. const getUserList = async () => {
  108. const res = await getListSimpleUsersApi()
  109. userOption.value = res
  110. }
  111. // 获取下拉框[上级]的数据
  112. const getTree = async () => {
  113. deptOptions.value = []
  114. const res = await DeptApi.listSimpleDeptApi()
  115. let dept: Tree = { id: 0, name: '顶级部门', children: [] }
  116. dept.children = handleTree(res)
  117. deptOptions.value.push(dept)
  118. }
  119. const { gridOptions, getList, deleteData } = useVxeGrid<DeptApi.DeptVO>({
  120. allSchemas: allSchemas,
  121. treeConfig: treeConfig,
  122. getListApi: DeptApi.getDeptPageApi,
  123. deleteApi: DeptApi.deleteDeptApi
  124. })
  125. // ========== 新增/修改 ==========
  126. // 设置标题
  127. const setDialogTile = (type: string) => {
  128. dialogTitle.value = t('action.' + type)
  129. actionType.value = type
  130. dialogVisible.value = true
  131. }
  132. // 新增操作
  133. const handleCreate = async () => {
  134. setDialogTile('create')
  135. }
  136. // 修改操作
  137. const handleUpdate = async (rowId: number) => {
  138. setDialogTile('update')
  139. // 设置数据
  140. const res = await DeptApi.getDeptApi(rowId)
  141. await nextTick()
  142. unref(formRef)?.setValues(res)
  143. }
  144. // 提交新增/修改的表单
  145. const submitForm = async () => {
  146. const elForm = unref(formRef)?.getElFormRef()
  147. if (!elForm) return
  148. elForm.validate(async (valid) => {
  149. if (valid) {
  150. actionLoading.value = true
  151. // 提交请求
  152. try {
  153. const data = unref(formRef)?.formModel as DeptApi.DeptVO
  154. if (actionType.value === 'create') {
  155. await DeptApi.createDeptApi(data)
  156. message.success(t('common.createSuccess'))
  157. } else if (actionType.value === 'update') {
  158. await DeptApi.updateDeptApi(data)
  159. message.success(t('common.updateSuccess'))
  160. }
  161. dialogVisible.value = false
  162. } finally {
  163. actionLoading.value = false
  164. await getList(xGrid)
  165. }
  166. }
  167. })
  168. }
  169. // 删除操作
  170. const handleDelete = async (rowId: number) => {
  171. await deleteData(xGrid, rowId)
  172. }
  173. const userNicknameFormat = (row) => {
  174. if (!row || !row.leaderUserId) {
  175. return '未设置'
  176. }
  177. for (const user of userOption.value) {
  178. if (row.leaderUserId === user.id) {
  179. return user.nickname
  180. }
  181. }
  182. return '未知【' + row.leaderUserId + '】'
  183. }
  184. // ========== 初始化 ==========
  185. onMounted(async () => {
  186. await getUserList()
  187. await getTree()
  188. })
  189. </script>