index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <ContentWrap>
  3. <!-- 搜索工作栏 -->
  4. <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
  5. <el-form-item label="部门名称" prop="title">
  6. <el-input v-model="queryParams.name" placeholder="请输入部门名称" clearable />
  7. </el-form-item>
  8. <el-form-item label="部门状态" prop="status">
  9. <el-select v-model="queryParams.status" placeholder="请选择" clearable>
  10. <el-option
  11. v-for="dict in getDictOptions(DICT_TYPE.COMMON_STATUS)"
  12. :key="parseInt(dict.value)"
  13. :label="dict.label"
  14. :value="parseInt(dict.value)"
  15. />
  16. </el-select>
  17. </el-form-item>
  18. <el-form-item>
  19. <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
  20. <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
  21. </el-form-item>
  22. </el-form>
  23. <el-row :gutter="10" class="mb8">
  24. <el-col :span="1.5">
  25. <el-button
  26. type="primary"
  27. plain
  28. @click="openModal('create')"
  29. v-hasPermi="['system:dept:create']"
  30. ><Icon icon="ep:plus" class="mr-5px" /> 新增</el-button
  31. >
  32. </el-col>
  33. <el-col :span="1.5">
  34. <el-button type="danger" plain @click="toggleExpandAll"
  35. ><Icon icon="ep:sort" class="mr-5px" /> 展开/折叠</el-button
  36. >
  37. </el-col>
  38. </el-row>
  39. <!-- 列表 -->
  40. <el-table
  41. v-if="refreshTable"
  42. v-loading="loading"
  43. :data="deptDatas"
  44. row-key="id"
  45. :default-expand-all="isExpandAll"
  46. :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
  47. >
  48. <el-table-column prop="name" label="部门名称" width="260" />
  49. <el-table-column prop="leader" label="负责人" :formatter="userNicknameFormat" width="120" />
  50. <el-table-column prop="sort" label="排序" width="200" />
  51. <el-table-column prop="status" label="状态" width="100">
  52. <template #default="scope">
  53. <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
  54. </template>
  55. </el-table-column>
  56. <el-table-column
  57. label="创建时间"
  58. align="center"
  59. prop="createTime"
  60. width="180"
  61. :formatter="dateFormatter"
  62. />
  63. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  64. <template #default="scope">
  65. <el-button
  66. link
  67. type="primary"
  68. icon="el-icon-edit"
  69. @click="openModal('update', scope.row.id)"
  70. v-hasPermi="['system:dept:update']"
  71. >修改</el-button
  72. >
  73. <el-button
  74. v-if="scope.row.parentId !== 0"
  75. link
  76. type="danger"
  77. icon="el-icon-delete"
  78. @click="handleDelete(scope.row.id)"
  79. v-hasPermi="['system:dept:delete']"
  80. >删除</el-button
  81. >
  82. </template>
  83. </el-table-column>
  84. </el-table>
  85. </ContentWrap>
  86. <!-- 添加或修改部门对话框 -->
  87. <dept-form ref="modalRef" @success="getList" />
  88. </template>
  89. <script setup lang="ts" name="Dept">
  90. import { handleTree } from '@/utils/tree'
  91. import * as DeptApi from '@/api/system/dept'
  92. import { getListSimpleUsersApi, UserVO } from '@/api/system/user'
  93. import { DICT_TYPE, getDictOptions } from '@/utils/dict'
  94. import DeptForm from './form.vue'
  95. import { dateFormatter } from '@/utils/formatTime'
  96. const message = useMessage() // 消息弹窗
  97. const { t } = useI18n() // 国际化
  98. // 搜索变量
  99. const queryParams = reactive({
  100. title: '',
  101. name: undefined,
  102. status: undefined,
  103. pageNo: 1,
  104. pageSize: 100
  105. })
  106. const queryFormRef = ref() // 搜索的表单
  107. const deptDatas = ref() // 数据变量
  108. const userOption = ref<UserVO[]>([])
  109. const isExpandAll = ref(true) // 是否展开,默认全部展开
  110. const refreshTable = ref(true) // 重新渲染表格状态
  111. const loading = ref(true) // 列表的加载中
  112. //获取用户列表
  113. const getUserList = async () => {
  114. const res = await getListSimpleUsersApi()
  115. userOption.value = res
  116. }
  117. /** 展开/折叠操作 */
  118. const toggleExpandAll = () => {
  119. refreshTable.value = false
  120. isExpandAll.value = !isExpandAll.value
  121. console.log(isExpandAll.value)
  122. nextTick(() => {
  123. refreshTable.value = true
  124. })
  125. }
  126. /** 搜索按钮操作 */
  127. const handleQuery = () => {
  128. getList()
  129. }
  130. /** 删除按钮操作 */
  131. const handleDelete = async (id: number) => {
  132. try {
  133. // 删除的二次确认
  134. await message.delConfirm()
  135. // 发起删除
  136. await DeptApi.deleteDeptApi(id)
  137. message.success(t('common.delSuccess'))
  138. // 刷新列表
  139. await getList()
  140. } catch {}
  141. }
  142. /** 查询部门列表 */
  143. const getList = async () => {
  144. loading.value = true
  145. try {
  146. const res = await DeptApi.getDeptPageApi(queryParams)
  147. deptDatas.value = handleTree(res)
  148. } finally {
  149. loading.value = false
  150. }
  151. }
  152. /** 重置按钮操作 */
  153. const resetQuery = () => {
  154. queryParams.pageNo = 1
  155. queryParams.name = undefined
  156. queryParams.status = undefined
  157. queryFormRef.value.resetFields()
  158. handleQuery()
  159. }
  160. /** 添加/修改操作 */
  161. const modalRef = ref()
  162. const openModal = (type: string, id?: number) => {
  163. modalRef.value.openModal(type, id, userOption.value)
  164. }
  165. const userNicknameFormat = (row) => {
  166. if (!row || !row.leaderUserId) {
  167. return '未设置'
  168. }
  169. for (const user of userOption.value) {
  170. if (row.leaderUserId === user.id) {
  171. return user.nickname
  172. }
  173. }
  174. return '未知【' + row.leaderUserId + '】'
  175. }
  176. // ========== 初始化 ==========
  177. onMounted(async () => {
  178. await getUserList()
  179. await getList()
  180. })
  181. </script>