index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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, reactive, 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 { required } from '@/utils/formRules.js'
  83. import { useI18n } from '@/hooks/web/useI18n'
  84. import { useMessage } from '@/hooks/web/useMessage'
  85. import { useVxeGrid } from '@/hooks/web/useVxeGrid'
  86. import { FormExpose } from '@/components/Form'
  87. import { allSchemas } from './dept.data'
  88. import * as DeptApi from '@/api/system/dept'
  89. import { getListSimpleUsersApi, UserVO } from '@/api/system/user'
  90. const { t } = useI18n() // 国际化
  91. const message = useMessage() // 消息弹窗
  92. // 列表相关的变量
  93. const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
  94. const treeConfig = {
  95. transform: true,
  96. rowField: 'id',
  97. parentField: 'parentId',
  98. expandAll: true
  99. }
  100. // 弹窗相关的变量
  101. const dialogVisible = ref(false) // 是否显示弹出层
  102. const dialogTitle = ref('edit') // 弹出层标题
  103. const actionType = ref('') // 操作按钮的类型
  104. const actionLoading = ref(false) // 遮罩层
  105. const formRef = ref<FormExpose>() // 表单 Ref
  106. const deptOptions = ref() // 树形结构
  107. const userOption = ref<UserVO[]>([])
  108. // 新增和修改的表单校验
  109. const rules = reactive({
  110. name: [required],
  111. sort: [required],
  112. path: [required],
  113. status: [required]
  114. })
  115. const getUserList = async () => {
  116. const res = await getListSimpleUsersApi()
  117. userOption.value = res
  118. }
  119. // 获取下拉框[上级]的数据
  120. const getTree = async () => {
  121. deptOptions.value = []
  122. const res = await DeptApi.listSimpleDeptApi()
  123. let dept: Tree = { id: 0, name: '顶级部门', children: [] }
  124. dept.children = handleTree(res)
  125. deptOptions.value.push(dept)
  126. }
  127. const { gridOptions, getList, deleteData } = useVxeGrid<DeptApi.DeptVO>({
  128. allSchemas: allSchemas,
  129. treeConfig: treeConfig,
  130. getListApi: DeptApi.getDeptPageApi,
  131. deleteApi: DeptApi.deleteDeptApi
  132. })
  133. // ========== 新增/修改 ==========
  134. // 设置标题
  135. const setDialogTile = (type: string) => {
  136. dialogTitle.value = t('action.' + type)
  137. actionType.value = type
  138. dialogVisible.value = true
  139. }
  140. // 新增操作
  141. const handleCreate = async () => {
  142. setDialogTile('create')
  143. }
  144. // 修改操作
  145. const handleUpdate = async (rowId: number) => {
  146. setDialogTile('update')
  147. // 设置数据
  148. const res = await DeptApi.getDeptApi(rowId)
  149. await nextTick()
  150. unref(formRef)?.setValues(res)
  151. }
  152. // 提交新增/修改的表单
  153. const submitForm = async () => {
  154. const elForm = unref(formRef)?.getElFormRef()
  155. if (!elForm) return
  156. elForm.validate(async (valid) => {
  157. if (valid) {
  158. actionLoading.value = true
  159. // 提交请求
  160. try {
  161. const data = unref(formRef)?.formModel as DeptApi.DeptVO
  162. if (actionType.value === 'create') {
  163. await DeptApi.createDeptApi(data)
  164. message.success(t('common.createSuccess'))
  165. } else if (actionType.value === 'update') {
  166. await DeptApi.updateDeptApi(data)
  167. message.success(t('common.updateSuccess'))
  168. }
  169. dialogVisible.value = false
  170. } finally {
  171. actionLoading.value = false
  172. await getList(xGrid)
  173. }
  174. }
  175. })
  176. }
  177. // 删除操作
  178. const handleDelete = async (rowId: number) => {
  179. await deleteData(xGrid, rowId)
  180. }
  181. const userNicknameFormat = (row) => {
  182. if (!row || !row.leaderUserId) {
  183. return '未设置'
  184. }
  185. for (const user of userOption.value) {
  186. if (row.leaderUserId === user.id) {
  187. return user.nickname
  188. }
  189. }
  190. return '未知【' + row.leaderUserId + '】'
  191. }
  192. // ========== 初始化 ==========
  193. onMounted(async () => {
  194. await getUserList()
  195. await getTree()
  196. })
  197. </script>