index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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" :userOption="userOption" />
  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. // 搜索的表单
  107. const queryFormRef = ref()
  108. // 数据变量
  109. const deptDatas = ref()
  110. const userOption = ref<UserVO[]>([])
  111. // 是否展开,默认全部展开
  112. const isExpandAll = ref(true)
  113. // 重新渲染表格状态
  114. const refreshTable = ref(true)
  115. // 列表的加载中
  116. const loading = ref(true)
  117. //获取用户列表
  118. const getUserList = async () => {
  119. const res = await getListSimpleUsersApi()
  120. userOption.value = res
  121. }
  122. /** 展开/折叠操作 */
  123. const toggleExpandAll = () => {
  124. refreshTable.value = false
  125. isExpandAll.value = !isExpandAll.value
  126. console.log(isExpandAll.value)
  127. nextTick(() => {
  128. refreshTable.value = true
  129. })
  130. }
  131. /** 搜索按钮操作 */
  132. const handleQuery = () => {
  133. getList()
  134. }
  135. /** 删除按钮操作 */
  136. const handleDelete = async (id: number) => {
  137. try {
  138. // 删除的二次确认
  139. await message.delConfirm()
  140. // 发起删除
  141. await DeptApi.deleteDeptApi(id)
  142. message.success(t('common.delSuccess'))
  143. // 刷新列表
  144. await getList()
  145. } catch {}
  146. }
  147. /** 查询部门列表 */
  148. const getList = async () => {
  149. loading.value = true
  150. try {
  151. const res = await DeptApi.getDeptPageApi(queryParams)
  152. deptDatas.value = handleTree(res)
  153. } finally {
  154. loading.value = false
  155. }
  156. }
  157. /** 重置按钮操作 */
  158. const resetQuery = () => {
  159. queryParams.pageNo = 1
  160. queryParams.name = undefined
  161. queryParams.status = undefined
  162. queryFormRef.value.resetFields()
  163. handleQuery()
  164. }
  165. /** 添加/修改操作 */
  166. const modalRef = ref()
  167. const openModal = (type: string, id?: number) => {
  168. modalRef.value.openModal(type, id)
  169. }
  170. const userNicknameFormat = (row) => {
  171. if (!row || !row.leaderUserId) {
  172. return '未设置'
  173. }
  174. for (const user of userOption.value) {
  175. if (row.leaderUserId === user.id) {
  176. return user.nickname
  177. }
  178. }
  179. return '未知【' + row.leaderUserId + '】'
  180. }
  181. // ========== 初始化 ==========
  182. onMounted(async () => {
  183. await getUserList()
  184. await getList()
  185. })
  186. </script>