index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <div class="system-organize-container layout-padding">
  3. <div class="layout-padding-auto layout-padding-view pd20">
  4. <div class="flex-center-between mb20">
  5. <el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent>
  6. <el-form-item label="关键字" prop="keyword" class="mb0">
  7. <el-input v-model="state.queryParams.keyword" placeholder="部门名称" clearable @keyup.enter="handleQuery" style="width: 250px" />
  8. </el-form-item>
  9. <el-form-item class="mb0">
  10. <el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
  11. <el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
  12. </el-form-item>
  13. </el-form>
  14. <div>
  15. <el-button type="primary" @click="expand"
  16. ><SvgIcon
  17. name="ele-ArrowDownBold"
  18. style="transition: transform var(--el-transition-duration)"
  19. :style="state.isExpand ? 'transform: none' : 'transform: rotateZ(180deg)'"
  20. class="mr5"
  21. />
  22. {{ state.isExpand ? '收起' : '展开' }}</el-button
  23. >
  24. <el-button type="primary" @click="onOpenAddOrg('')" v-auth="'system:organize:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
  25. </div>
  26. </div>
  27. <!-- 表格 -->
  28. <el-auto-resizer class="table" v-loading="state.loading">
  29. <template #default="{ height, width }">
  30. <el-table-v2
  31. v-model:expanded-row-keys="state.expandedRowKeys"
  32. :columns="state.columns"
  33. :data="state.orgTableData"
  34. expand-column-key="orgName"
  35. fixed
  36. :width="width"
  37. :height="height"
  38. >
  39. <template #empty>
  40. <Empty />
  41. </template>
  42. </el-table-v2>
  43. </template>
  44. </el-auto-resizer>
  45. </div>
  46. <AddOrg ref="addOrgRef" @updateList="getOrgListApi" />
  47. <EditOrg ref="editOrgRef" @updateList="getOrgListApi" />
  48. </div>
  49. </template>
  50. <script lang="ts" setup name="organize">
  51. import { defineAsyncComponent, ref, h, reactive, onMounted, watch } from 'vue';
  52. import { ElButton, ElTag, ElMessage } from 'element-plus';
  53. import type { FormInstance } from 'element-plus';
  54. import { getOrgList, baseData } from '/@/api/system/organize';
  55. import { formatDate } from '/@/utils/formatTime';
  56. import { auth } from '/@/utils/authFunction';
  57. import { throttle } from '/@/utils/tools';
  58. // 引入组件
  59. const AddOrg = defineAsyncComponent(() => import('/@/views/system/organize/component/addOrg.vue'));
  60. const EditOrg = defineAsyncComponent(() => import('/@/views/system/organize/component/editOrg.vue'));
  61. // 定义变量内容
  62. const addOrgRef = ref();
  63. const editOrgRef = ref();
  64. const state = reactive({
  65. orgTableData: <any>[], // 获取所有菜单
  66. staticArr: <any>[],
  67. loading: false,
  68. queryParams: {
  69. keyword: '',
  70. },
  71. expandedRowKeys: <any>[],
  72. columns: [
  73. {
  74. key: 'orgName',
  75. dataKey: 'orgName',
  76. title: '部门名称',
  77. width: 400,
  78. },
  79. {
  80. key: 'orgShortName',
  81. dataKey: 'orgShortName',
  82. title: '部门简称',
  83. width: 400,
  84. },
  85. {
  86. key: 'parentName',
  87. dataKey: 'parentName',
  88. title: '上级部门',
  89. width: 200,
  90. },
  91. {
  92. key: 'orgTypeText',
  93. dataKey: 'orgTypeText',
  94. title: '部门类别',
  95. width: 100,
  96. },
  97. {
  98. key: 'orgCode',
  99. dataKey: 'orgCode',
  100. title: '部门编码',
  101. width: 200,
  102. },
  103. {
  104. key: 'isEnable',
  105. dataKey: 'isEnable',
  106. title: '状态',
  107. width: 100,
  108. cellRenderer: (data: any) => {
  109. return h(ElTag, { type: data.rowData.isEnable ? 'success' : 'info' }, { default: () => (data.rowData.isEnable ? '启用' : '禁用') });
  110. },
  111. },
  112. {
  113. key: 'creationTime',
  114. dataKey: 'creationTime',
  115. title: '更新时间',
  116. width: 200,
  117. cellRenderer: (data: any) => h('span', {}, { default: () => formatDate(new Date(data.rowData.creationTime), 'YYYY-mm-dd HH:MM:SS') }),
  118. },
  119. {
  120. key: 'handle',
  121. title: '操作',
  122. width: 100,
  123. fixed: 'right',
  124. align: 'center',
  125. cellRenderer: ({ rowData }: any) => {
  126. return h('span', { class: 'flex' }, [
  127. auth('system:organize:edit')
  128. ? h(
  129. ElButton,
  130. {
  131. onClick: () => onOpenEditOrg(rowData),
  132. type: 'primary',
  133. title: '修改',
  134. link: true,
  135. },
  136. { default: () => '修改' }
  137. )
  138. : '',
  139. auth('system:organize:add')
  140. ? h(
  141. ElButton,
  142. {
  143. onClick: () => onOpenAddOrg(rowData),
  144. type: 'primary',
  145. title: '新增',
  146. link: true,
  147. },
  148. { default: () => '新增' }
  149. )
  150. : '',
  151. ]);
  152. },
  153. },
  154. ],
  155. isExpand: true,
  156. orgType: [],
  157. });
  158. const ruleFormRef = ref();
  159. // 搜索
  160. const formatTable = (list: any[], keyword: string) => {
  161. if (!list.length || !Array.isArray(list)) return [];
  162. let emptyArr: any[] = [];
  163. list.map((item) => {
  164. if (item.orgName.includes(keyword)) {
  165. if (item.children && Array.isArray(item.children) && item.children.length > 0) {
  166. item.children = formatTable(item.children, keyword);
  167. }
  168. emptyArr.push(item);
  169. } else if (item.children && Array.isArray(item.children) && item.children.length > 0) {
  170. item.children = formatTable(item.children, keyword);
  171. if (item.children.length) {
  172. emptyArr.push(item);
  173. }
  174. }
  175. });
  176. return emptyArr;
  177. };
  178. let emptyArr: any[] = [];
  179. const getExpand = (list: any[]) => {
  180. if (!list.length || !Array.isArray(list)) return [];
  181. list.map((item) => {
  182. if (item.children && Array.isArray(item.children) && item.children.length > 0) {
  183. getExpand(item.children);
  184. }
  185. emptyArr.push(item.id);
  186. });
  187. return emptyArr;
  188. };
  189. /** 搜索按钮操作 节流操作 */
  190. const handleQuery = throttle(() => {
  191. if (state.queryParams.keyword) {
  192. state.loading = true;
  193. state.expandedRowKeys = [];
  194. emptyArr = [];
  195. state.orgTableData = formatTable(JSON.parse(JSON.stringify(state.staticArr)), state.queryParams.keyword);
  196. state.expandedRowKeys = getExpand(state.orgTableData);
  197. state.loading = false;
  198. } else {
  199. getOrgListApi();
  200. }
  201. }, 500);
  202. /** 重置按钮操作 */
  203. const resetQuery = throttle((formEl: FormInstance | undefined) => {
  204. if (!formEl) return;
  205. formEl.resetFields();
  206. getOrgListApi();
  207. state.expandedRowKeys = [];
  208. emptyArr = [];
  209. }, 500);
  210. // 打开新增菜单弹窗
  211. const onOpenAddOrg = (row?: any) => {
  212. addOrgRef.value.openDialog(state.orgTableData, state.orgType, row);
  213. };
  214. // 打开编辑菜单弹窗
  215. const onOpenEditOrg = (row: any) => {
  216. editOrgRef.value.openDialog(row, state.orgTableData, state.orgType);
  217. };
  218. const expand = () => {
  219. state.isExpand = !state.isExpand;
  220. };
  221. watch(
  222. () => state.isExpand,
  223. (old: Boolean) => {
  224. if (old) getAllIds(state.orgTableData);
  225. else state.expandedRowKeys = [];
  226. }
  227. );
  228. const getAllIds = (arr: any) => {
  229. if (!arr) return [];
  230. arr.forEach((v: any) => {
  231. if (v.children?.length) {
  232. getAllIds(v.children);
  233. state.expandedRowKeys.push(v.id);
  234. }
  235. });
  236. };
  237. // 删除当前行
  238. // const onTabelRowDel = (row: any) => {
  239. // ElMessageBox.confirm(`此操作将永久删除部门结构:${row.orgName}, 是否继续?`, '提示', {
  240. // confirmButtonText: '确定',
  241. // cancelButtonText: '取消',
  242. // type: 'warning',
  243. // }).then(() => {
  244. // removeOrg(row.id).then(() => {
  245. // ElMessage.success('删除成功');
  246. // getOrgListApi();
  247. // })
  248. // }).catch(() => { });
  249. // };
  250. // 获取所有部门结构
  251. const getOrgListApi = async () => {
  252. if (!auth('system:organize:query')) ElMessage.error('抱歉,您没有权限获取组织架构列表!');
  253. else {
  254. state.loading = true;
  255. try {
  256. const res: any = await Promise.all([getOrgList(), baseData()]);
  257. state.orgTableData = res[0].result ?? [];
  258. state.staticArr = res[0].result ?? [];
  259. state.expandedRowKeys = state.orgTableData.map((v: any) => {
  260. return v.children.map((i: any) => {
  261. return i.id;
  262. });
  263. });
  264. state.expandedRowKeys.push(state.orgTableData[0].id); //默认展开一级部门
  265. state.orgType = res[1].orgType;
  266. state.loading = false;
  267. } catch (error) {
  268. state.loading = false;
  269. }
  270. }
  271. };
  272. // 页面加载时
  273. onMounted(() => {
  274. getOrgListApi();
  275. });
  276. </script>
  277. <style lang="scss" scoped>
  278. .system-organize-container {
  279. .table {
  280. flex: 1;
  281. }
  282. }
  283. </style>