index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 } 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. // 下拉框[上级]的配置项目
  116. const defaultProps = {
  117. checkStrictly: true,
  118. children: 'children',
  119. label: 'name',
  120. value: 'id'
  121. }
  122. const getUserList = async () => {
  123. const res = await getListSimpleUsersApi()
  124. userOption.value = res
  125. }
  126. // 获取下拉框[上级]的数据
  127. const getTree = async () => {
  128. deptOptions.value = []
  129. const res = await DeptApi.listSimpleDeptApi()
  130. let dept: Tree = { id: 0, name: '顶级部门', children: [] }
  131. dept.children = handleTree(res)
  132. deptOptions.value.push(dept)
  133. }
  134. const { gridOptions, getList, deleteData } = useVxeGrid<DeptApi.DeptVO>({
  135. allSchemas: allSchemas,
  136. treeConfig: treeConfig,
  137. getListApi: DeptApi.getDeptPageApi,
  138. deleteApi: DeptApi.deleteDeptApi
  139. })
  140. // ========== 新增/修改 ==========
  141. // 设置标题
  142. const setDialogTile = (type: string) => {
  143. dialogTitle.value = t('action.' + type)
  144. actionType.value = type
  145. dialogVisible.value = true
  146. }
  147. // 新增操作
  148. const handleCreate = async () => {
  149. setDialogTile('create')
  150. }
  151. // 修改操作
  152. const handleUpdate = async (rowId: number) => {
  153. setDialogTile('update')
  154. // 设置数据
  155. const res = await DeptApi.getDeptApi(rowId)
  156. await nextTick()
  157. unref(formRef)?.setValues(res)
  158. }
  159. // 提交新增/修改的表单
  160. const submitForm = async () => {
  161. const elForm = unref(formRef)?.getElFormRef()
  162. if (!elForm) return
  163. elForm.validate(async (valid) => {
  164. if (valid) {
  165. actionLoading.value = true
  166. // 提交请求
  167. try {
  168. const data = unref(formRef)?.formModel as DeptApi.DeptVO
  169. if (actionType.value === 'create') {
  170. await DeptApi.createDeptApi(data)
  171. message.success(t('common.createSuccess'))
  172. } else if (actionType.value === 'update') {
  173. await DeptApi.updateDeptApi(data)
  174. message.success(t('common.updateSuccess'))
  175. }
  176. dialogVisible.value = false
  177. } finally {
  178. actionLoading.value = false
  179. await getList(xGrid)
  180. }
  181. }
  182. })
  183. }
  184. // 删除操作
  185. const handleDelete = async (rowId: number) => {
  186. await deleteData(xGrid, rowId)
  187. }
  188. const userNicknameFormat = (row) => {
  189. if (!row || !row.leaderUserId) {
  190. return '未设置'
  191. }
  192. for (const user of userOption.value) {
  193. if (row.leaderUserId === user.id) {
  194. return user.nickname
  195. }
  196. }
  197. return '未知【' + row.leaderUserId + '】'
  198. }
  199. // ========== 初始化 ==========
  200. onMounted(async () => {
  201. await getUserList()
  202. await getTree()
  203. })
  204. </script>