index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. <!-- 操作工具栏 -->
  42. <Form ref="formRef" :schema="allSchemas.formSchema" :rules="rules">
  43. <template #parentId>
  44. <el-tree-select
  45. node-key="id"
  46. v-model="deptParentId"
  47. :props="defaultProps"
  48. :data="deptOptions"
  49. :default-expanded-keys="[100]"
  50. check-strictly
  51. />
  52. </template>
  53. <template #leaderUserId>
  54. <el-select v-model="leaderUserId">
  55. <el-option
  56. v-for="item in userOption"
  57. :key="parseInt(item.id)"
  58. :label="item.nickname"
  59. :value="parseInt(item.id)"
  60. />
  61. </el-select>
  62. </template>
  63. </Form>
  64. <template #footer>
  65. <!-- 按钮:保存 -->
  66. <XButton
  67. v-if="['create', 'update'].includes(actionType)"
  68. type="primary"
  69. :loading="actionLoading"
  70. @click="submitForm()"
  71. :title="t('action.save')"
  72. />
  73. <!-- 按钮:关闭 -->
  74. <XButton :loading="actionLoading" @click="dialogVisible = false" :title="t('dialog.close')" />
  75. </template>
  76. </XModal>
  77. </template>
  78. <script setup lang="ts">
  79. import { nextTick, onMounted, reactive, ref, unref } from 'vue'
  80. import { useI18n } from '@/hooks/web/useI18n'
  81. import { useMessage } from '@/hooks/web/useMessage'
  82. import { VxeGridInstance } from 'vxe-table'
  83. import { ElSelect, ElTreeSelect, ElOption } from 'element-plus'
  84. import { allSchemas } from './dept.data'
  85. import * as DeptApi from '@/api/system/dept'
  86. import { getListSimpleUsersApi } from '@/api/system/user'
  87. import { required } from '@/utils/formRules.js'
  88. import { handleTree } from '@/utils/tree'
  89. import { FormExpose } from '@/components/Form'
  90. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  91. const { t } = useI18n() // 国际化
  92. const message = useMessage() // 消息弹窗
  93. // 列表相关的变量
  94. const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
  95. const treeConfig = {
  96. transform: true,
  97. rowField: 'id',
  98. parentField: 'parentId',
  99. expandAll: true
  100. }
  101. const { gridOptions, getList, deleteData } = useVxeGrid<DeptApi.DeptVO>({
  102. allSchemas: allSchemas,
  103. treeConfig: treeConfig,
  104. getListApi: DeptApi.getDeptPageApi,
  105. deleteApi: DeptApi.deleteDeptApi
  106. })
  107. // 弹窗相关的变量
  108. const dialogVisible = ref(false) // 是否显示弹出层
  109. const dialogTitle = ref('edit') // 弹出层标题
  110. const actionType = ref('') // 操作按钮的类型
  111. const actionLoading = ref(false) // 遮罩层
  112. const deptParentId = ref(0) // 上级ID
  113. const leaderUserId = ref()
  114. const formRef = ref<FormExpose>() // 表单 Ref
  115. const deptOptions = ref() // 树形结构
  116. const userOption = ref()
  117. // 新增和修改的表单校验
  118. const rules = reactive({
  119. name: [required],
  120. sort: [required],
  121. path: [required],
  122. status: [required]
  123. })
  124. // 下拉框[上级]的配置项目
  125. const defaultProps = {
  126. checkStrictly: true,
  127. children: 'children',
  128. label: 'name',
  129. value: 'id'
  130. }
  131. // 获取下拉框[上级]的数据
  132. const getTree = async () => {
  133. deptOptions.value = []
  134. const res = await DeptApi.listSimpleDeptApi()
  135. let dept: Tree = { id: 0, name: '顶级部门', children: [] }
  136. dept.children = handleTree(res)
  137. deptOptions.value.push(dept)
  138. }
  139. const getUserList = async () => {
  140. const res = await getListSimpleUsersApi()
  141. userOption.value = res
  142. }
  143. // ========== 新增/修改 ==========
  144. // 设置标题
  145. const setDialogTile = (type: string) => {
  146. dialogTitle.value = t('action.' + type)
  147. actionType.value = type
  148. dialogVisible.value = true
  149. }
  150. // 新增操作
  151. const handleCreate = async () => {
  152. deptParentId.value = 0
  153. leaderUserId.value = null
  154. setDialogTile('create')
  155. }
  156. // 修改操作
  157. const handleUpdate = async (rowId: number) => {
  158. setDialogTile('update')
  159. // 设置数据
  160. const res = await DeptApi.getDeptApi(rowId)
  161. deptParentId.value = res.parentId
  162. leaderUserId.value = res.leaderUserId
  163. await nextTick()
  164. unref(formRef)?.setValues(res)
  165. }
  166. // 提交新增/修改的表单
  167. const submitForm = async () => {
  168. const elForm = unref(formRef)?.getElFormRef()
  169. if (!elForm) return
  170. elForm.validate(async (valid) => {
  171. if (valid) {
  172. actionLoading.value = true
  173. // 提交请求
  174. try {
  175. const data = unref(formRef)?.formModel as DeptApi.DeptVO
  176. data.parentId = deptParentId.value
  177. data.leaderUserId = leaderUserId.value
  178. if (dialogTitle.value.startsWith('新增')) {
  179. await DeptApi.createDeptApi(data)
  180. message.success(t('common.createSuccess'))
  181. } else if (dialogTitle.value.startsWith('修改')) {
  182. await DeptApi.updateDeptApi(data)
  183. message.success(t('common.updateSuccess'))
  184. }
  185. dialogVisible.value = false
  186. } finally {
  187. actionLoading.value = false
  188. await getList(xGrid)
  189. }
  190. }
  191. })
  192. }
  193. // 删除操作
  194. const handleDelete = async (rowId: number) => {
  195. await deleteData(xGrid, rowId)
  196. }
  197. const userNicknameFormat = (row) => {
  198. if (!row || !row.leaderUserId || row.leaderUserId == null) {
  199. return '未设置'
  200. }
  201. for (const user of userOption.value) {
  202. if (row.leaderUserId === user.id) {
  203. return user.nickname
  204. }
  205. }
  206. return '未知【' + row.leaderUserId + '】'
  207. }
  208. // ========== 初始化 ==========
  209. onMounted(async () => {
  210. await getUserList()
  211. await getTree()
  212. })
  213. </script>