index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <template>
  2. <div class="h-full flex flex-col teamstyle overflow-hidden">
  3. <!-- 头部 -->
  4. <div class="bg-white flex justify-between team-header">
  5. <div class="flex items-center">
  6. <el-link type="primary" class="text-nowrap mr-20" :icon="CirclePlusFilled"
  7. @click="dialogFromCli('addDeptDialogVisible')">创建部门</el-link>
  8. <el-link class="text-nowrap textFont textFont mr-10" type="primary" :icon="Edit"
  9. @click="updateDepartment('addDeptDialogVisible')">{{ deptListItem.label || '全部人员' }}</el-link>
  10. <span class="textSpan">共 0 人</span>
  11. </div>
  12. <div class="teamForm flex items-center">
  13. <el-input v-model="teamForm.keyword" style="max-width: 650px" size="default" placeholder="请输入姓名搜索" class="mr-6"
  14. clearable @clear="getTableData()" @keyup.enter.native="getTableData()">
  15. <template #prepend>
  16. <el-select v-model="teamForm.matchingType" style="width: 80px">
  17. <el-option label="姓名" :value="0" />
  18. <el-option label="电话" :value="1" />
  19. <el-option label="工号" :value="2" />
  20. </el-select>
  21. </template>
  22. <template #append>
  23. <el-button :icon="Search" @click="getTableData()" />
  24. </template>
  25. </el-input>
  26. <div class="formItem mr-6 flex items-center">
  27. <div class="text-nowrap">状态:</div>
  28. <el-select v-model="teamForm.status" placeholder="请选择" size="default" style="width: 100px"
  29. @change="getTableData()">
  30. <el-option v-for="item in stateOptions" :key="item.value" :label="item.label" :value="item.value" />
  31. </el-select>
  32. </div>
  33. <div class="formItem mr-6 flex items-center">
  34. <div class="text-nowrap">角色:</div>
  35. <el-select v-model="teamForm.roleId" placeholder="请选择" size="default" style="width: 150px" clearable
  36. @change="getTableData()">
  37. <el-option v-for="item in roleList" :key="item.id" :label="item.rolename" :value="item.id" />
  38. </el-select>
  39. </div>
  40. <el-dropdown>
  41. <el-button type="primary">
  42. 更多操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
  43. </el-button>
  44. <template #dropdown>
  45. <el-dropdown-menu>
  46. <el-dropdown-item @click="addPersone(false)" v-permission="['teamAdd']">添加人员</el-dropdown-item>
  47. <el-dropdown-item v-permission="['teamExport']">导出人员</el-dropdown-item>
  48. <el-dropdown-item v-permission="['teamImport']">批量导入</el-dropdown-item>
  49. </el-dropdown-menu>
  50. </template>
  51. </el-dropdown>
  52. </div>
  53. </div>
  54. <!-- 内容 -->
  55. <div class="flex-1 flex">
  56. <div class="p-4 w-80 pr-0">
  57. <div class="bg-white w-full h-full shadow-md rounded-md flex flex-col">
  58. <div class="flex-1 overflow-y-auto const-left">
  59. <el-tree style="max-width: 600px" :data="deptList" :props="treeProps" @node-click="treeNode">
  60. <template #default="{ node, data }">
  61. <div class="flex justify-between treeContent">
  62. <div class="custom-tree-node">
  63. <div class="treeLabel">{{ node.label }}</div>
  64. <div class="treeIcon" v-if="data.id > 0">
  65. <el-link type="primary" :icon="CirclePlus" :underline="false"
  66. @click.stop="dialogFromCli('addDeptDialogVisible', data, true)"></el-link>
  67. <el-link type="primary" :icon="Delete" :underline="false" style="margin-left: 6px;"
  68. @click.stop="deteleDept(data)"></el-link>
  69. </div>
  70. </div>
  71. </div>
  72. </template>
  73. </el-tree>
  74. </div>
  75. </div>
  76. </div>
  77. <div class="flex-1 p-4 overflow-auto">
  78. <div class="bg-white w-full h-full shadow-md rounded-md flex flex-col overflow-hidden pt-2 pl-2 pr-2">
  79. <div class="flex-1">
  80. <el-table ref="multipleTableRef" :data="tableData" v-loading="loadingFrom.tableLoading"
  81. @selection-change="changeBatch" style="width: 100%;height: 100%;">
  82. <el-table-column type="selection" width="55" />
  83. <el-table-column label="姓名" property="name" width="150"></el-table-column>
  84. <el-table-column label="手机" property="phone"></el-table-column>
  85. <el-table-column label="工号" property="jobNumber"></el-table-column>
  86. <el-table-column label="部门" property="departmentName"></el-table-column>
  87. <el-table-column label="角色" property="roleName"></el-table-column>
  88. <el-table-column label="创建时间" property="createTime"></el-table-column>
  89. <el-table-column label="操作" width="200" fixed="right">
  90. <template #default="scope">
  91. <el-button :size="'small'">重置</el-button>
  92. <el-button type="primary" :size="'small'" @click="addPersone(scope.row)">编辑</el-button>
  93. <el-button :size="'small'" v-if="scope.row.isActive == 1">停用</el-button>
  94. <el-button :size="'small'" v-if="scope.row.isActive == 0">启用</el-button>
  95. </template>
  96. </el-table-column>
  97. </el-table>
  98. </div>
  99. <div class="flex justify-between pb-2 pt-2 pl-3 pr-3">
  100. <div class="flex">
  101. <el-button size="default" @click="changeBatch(false)"
  102. :disabled="batchTableData.length == 0">取消</el-button>
  103. <el-dropdown class="ml-3">
  104. <el-button type="primary">
  105. 更多操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
  106. </el-button>
  107. <template #dropdown>
  108. <el-dropdown-menu>
  109. <el-dropdown-item @click="batchItem('批量修改部门', 'dept', deptList)"
  110. :disabled="batchTableData.length == 0">批量修改部门</el-dropdown-item>
  111. <el-dropdown-item @click="batchItem('批量修改角色', 'role', roleList)"
  112. :disabled="batchTableData.length == 0">批量修改角色</el-dropdown-item>
  113. <el-dropdown-item @click="batchEnableItem"
  114. :disabled="batchTableData.length == 0">批量启用员工</el-dropdown-item>
  115. </el-dropdown-menu>
  116. </template>
  117. </el-dropdown>
  118. </div>
  119. <div class="pr-4">
  120. <el-pagination layout="total, prev, pager, next, sizes" :total="totalTable" :page-size="teamForm.pageSize"
  121. @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  122. </div>
  123. </div>
  124. </div>
  125. </div>
  126. </div>
  127. <!-- 新增部门 -->
  128. <el-dialog v-model="dialogFrom.addDeptDialogVisible" :title="deptListItem.label || '创建部门'" width="500"
  129. :before-close="handleClose">
  130. <div class="pt-2">
  131. <el-form ref="deptRuleFormRef" style="max-width: 500px" :model="deptForm" :rules="deptRules" label-width="auto"
  132. size="large" status-icon>
  133. <el-form-item label="部门名称" prop="name">
  134. <el-input v-model="deptForm.name" placeholder="请输入部门名称" clearable />
  135. </el-form-item>
  136. <el-form-item label="主要负责人">
  137. <el-select v-model="deptForm.managerId" placeholder="请选择" style="width: 100%" clearable>
  138. <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.id" />
  139. </el-select>
  140. </el-form-item>
  141. <el-form-item label="其他负责人">
  142. <el-select v-model="deptForm.otherManagerIds" placeholder="请选择" style="width: 100%" multiple clearable>
  143. <el-option v-for="item in userList" :key="item.id" :label="item.name" :value="item.id" />
  144. </el-select>
  145. </el-form-item>
  146. </el-form>
  147. </div>
  148. <template #footer>
  149. <div class="dialog-footer">
  150. <el-button @click="dialogFrom.addDeptDialogVisible = false">取消</el-button>
  151. <el-button type="primary" @click="createDepartment(deptRuleFormRef)"
  152. v-bind:loading="loadingFrom.deptDialogVisibleLoading">
  153. 确定
  154. </el-button>
  155. </div>
  156. </template>
  157. </el-dialog>
  158. <!-- 新增人员 -->
  159. <AddPersonnelModal :data="{
  160. addPersonnelDialogVisible: dialogFrom.addPersonnelDialogVisible,
  161. deptList: deptListUntreated,
  162. roleList: roleList,
  163. personnelFromData: personnelFromData
  164. }" @closeModal="closeModal" @personnelModalConfirm="personnelModalConfirm" />
  165. <!-- 批量操作 -->
  166. <BatchOperation :batchData="visibleData" :batchNode="batchTableData" :visibleText="allText.batchText"
  167. :popup="visibleType" :batchOperationVisible="dialogFrom.batchOperationVisible" @close="closeModal" />
  168. </div>
  169. </template>
  170. <script lang="ts" setup>
  171. import { ref, reactive, onMounted, onBeforeMount, inject } from 'vue';
  172. import { dayjs } from 'element-plus'
  173. import { Search, CirclePlusFilled, Edit, CirclePlus, Delete } from '@element-plus/icons-vue'
  174. import { FormInstance, FormRules, ElMessageBox } from 'element-plus'
  175. import { useStore } from '@/store/index'
  176. import { GET_DATA_LIST, DETELE_DEPT, MOD, GET_USERINFO, GET_ROUTELIST, GET_DEPTLIST, BACTHSERROLE, GET_USERLIST, GET_ADDDEPT, ADD_USER } from './api'
  177. import { post } from "@/utils/request";
  178. import { getFromValue, updateDepTreeData, resetFromValue } from '@/utils/tools'
  179. // 导入页面
  180. import AddPersonnelModal from './module/AddPersonnelModal.vue'
  181. import BatchOperation from './module/BatchOperation.vue'
  182. const { getFunctionList, getUserInfoVal } = useStore()
  183. const globalPopup = inject<GlobalPopup>('globalPopup')
  184. // 定义类型
  185. interface deptRuleForm { // 部门表单类型
  186. name: string,
  187. id: string | number,
  188. parentId: string | number,
  189. managerId: string | number,
  190. otherManagerIds: string[] | number[],
  191. }
  192. // 固定数据
  193. const stateOptions = [{ value: '3', label: '全部' }, { value: '1', label: '在职' }, { value: '0', label: '停用' }]
  194. // 定义变量
  195. const pagePermission: any = ref([]) // 功能权限
  196. const loadingFrom = reactive({ // 所有加载状态
  197. tableLoading: false,
  198. deptDialogVisibleLoading: false
  199. })
  200. const dialogFrom: any = reactive({ // 所有弹窗状态
  201. addDeptDialogVisible: false,
  202. addPersonnelDialogVisible: false,
  203. batchOperationVisible: false
  204. });
  205. const allText = reactive({
  206. batchText: '批量操作'
  207. })
  208. const visibleType = ref<batchOperationType>('dept') // 弹窗类型
  209. const visibleData = ref<any>([]) // 批量弹窗数据源
  210. const totalTable = ref(0) // 表格总数
  211. const tableData: any = ref([]) // 表格数据
  212. const roleList: any = ref([]) // 角色列表
  213. const userList: any = ref([]) // 用户列表
  214. const deptList: any = ref([]) // 部门数据
  215. const batchTableData: any = ref([]) // 批量数据
  216. const multipleTableRef: any = ref()
  217. const deptListUntreated: any = ref([]) // 部门数据(未处理)
  218. const deptListItem: any = ref({}) // 选中的部门数据
  219. const personnelFromData = ref({}) // 人员表单数据
  220. const teamForm = reactive({ // 筛选条件表单
  221. matchingType: 0,
  222. keyword: '',
  223. status: '3',
  224. pageIndex: 1,
  225. pageSize: 20,
  226. roleId: '',
  227. onlyDirect: '',
  228. departmentId: '-1',
  229. });
  230. const deptRuleFormRef = ref<FormInstance>() // 表单实例
  231. const deptForm = reactive<deptRuleForm>({ // 部门表单
  232. name: '',
  233. id: '',
  234. parentId: '',
  235. managerId: '',
  236. otherManagerIds: [],
  237. })
  238. const treeProps = { // 部门树配置
  239. children: 'children',
  240. label: 'label',
  241. }
  242. // 定义校验规则
  243. const deptRules = reactive<FormRules<typeof deptForm>>({ // 部门表单校验规则
  244. name: [{ required: true, trigger: 'blur', message: '请输入部门名称' }]
  245. })
  246. // 定义方法
  247. function batchEnableItem() {
  248. const userIds = batchTableData.value.map((item: any) => item.id)
  249. post(BACTHSERROLE, { ids: JSON.stringify(userIds), isActive: 1 }).then(() => {
  250. globalPopup?.showSuccess('操作成功')
  251. changeBatch(false)
  252. getTableData()
  253. }).catch((err) => {
  254. globalPopup?.showError(err.msg)
  255. })
  256. }
  257. function changeBatch(flag: boolean = true) {
  258. if (flag) {
  259. batchTableData.value = multipleTableRef.value && multipleTableRef.value.getSelectionRows()
  260. } else {
  261. batchTableData.value = []
  262. multipleTableRef.value && multipleTableRef.value.clearSelection()
  263. }
  264. }
  265. function addPersone(item: any) {
  266. console.log(item)
  267. if (!item) {
  268. personnelFromData.value = {}
  269. dialogFrom.addPersonnelDialogVisible = true
  270. return
  271. }
  272. post(GET_USERINFO, { userId: item.id }).then(res => {
  273. const { id, name, phone, jobNumber, roleId, departmentCascade, inductionDate } = res.data
  274. let newData = {
  275. id, name, phone, jobNumber, roleId, departmentId:
  276. departmentCascade && departmentCascade.split(',').map(Number).reverse(),
  277. inductionDate
  278. }
  279. personnelFromData.value = newData
  280. dialogFrom.addPersonnelDialogVisible = true
  281. })
  282. }
  283. async function personnelModalConfirm(data: any, modelType: string) {
  284. post(ADD_USER, { ...data }).then(res => {
  285. if (res.code != 'ok') {
  286. dialogFrom[modelType] = false
  287. globalPopup?.showError(res.msg)
  288. return
  289. }
  290. dialogFrom[modelType] = false
  291. globalPopup?.showSuccess('添加成功')
  292. getTableData()
  293. }).catch(_err => {
  294. dialogFrom[modelType] = false
  295. })
  296. }
  297. function createDepartment(formEl: FormInstance | undefined) {
  298. if (!formEl) return
  299. let data = getFromValue(deptForm)
  300. loadingFrom.deptDialogVisibleLoading = true
  301. post(GET_ADDDEPT, { ...deptForm, otherManagerIds: data.otherManagerIds && data.otherManagerIds.join(',') }).then(res => {
  302. if (res.code != 'ok') {
  303. loadingFrom.deptDialogVisibleLoading = false
  304. globalPopup?.showError(res.msg)
  305. return
  306. }
  307. loadingFrom.deptDialogVisibleLoading = false
  308. globalPopup?.showSuccess('创建成功')
  309. getDeptList()
  310. dialogFrom.addDeptDialogVisible = false
  311. }).catch(_err => {
  312. loadingFrom.deptDialogVisibleLoading = false
  313. })
  314. }
  315. function updateDepartment(type: string) {
  316. if (!deptListItem.value.id || deptListItem.value.id <= 0) return
  317. const { id, label, parentId, managerId, otherManagerIds } = deptListItem.value
  318. console.log(deptListItem.value)
  319. let data = { id, name: label, parentId, managerId, otherManagerIds }
  320. Object.assign(deptForm, data)
  321. dialogFrom[type] = true
  322. }
  323. function treeNode(item: any) {
  324. deptListItem.value = item
  325. teamForm.departmentId = item.id
  326. getTableData()
  327. }
  328. function deteleDept(data: any) {
  329. console.log(data)
  330. ElMessageBox.confirm(
  331. `确定删除【${data.label}】部门吗?`, '',
  332. {
  333. confirmButtonText: '确定',
  334. cancelButtonText: '取消',
  335. type: 'warning',
  336. }
  337. )
  338. .then(() => {
  339. post(DETELE_DEPT, { id: data.id }).then(res => {
  340. if (res.code != 'ok') {
  341. globalPopup?.showError(res.msg)
  342. return
  343. }
  344. globalPopup?.showSuccess('删除成功')
  345. getDeptList()
  346. })
  347. })
  348. }
  349. function getTableData() {
  350. loadingFrom.tableLoading = true
  351. post(GET_DATA_LIST, { ...teamForm }).then(res => {
  352. if (res.code != 'ok') {
  353. loadingFrom.tableLoading = false
  354. globalPopup?.showError(res.msg)
  355. return
  356. }
  357. loadingFrom.tableLoading = false
  358. totalTable.value = res.data.total
  359. tableData.value = res.data.records
  360. }).catch(_err => {
  361. loadingFrom.tableLoading = false
  362. })
  363. }
  364. function getRoleList() {
  365. const companyId = getUserInfoVal('companyId') || ''
  366. post(GET_ROUTELIST, { companyId }).then(res => {
  367. if (res.code != 'ok') {
  368. globalPopup?.showError(res.msg)
  369. return
  370. }
  371. roleList.value = res.data
  372. })
  373. }
  374. function getDeptList() {
  375. post(GET_DEPTLIST, {}).then(res => {
  376. if (res.code != 'ok') {
  377. globalPopup?.showError(res.msg)
  378. return
  379. }
  380. deptListUntreated.value = updateDepTreeData(res.data, false)
  381. deptList.value = updateDepTreeData(res.data, true)
  382. })
  383. }
  384. function getUserList() {
  385. post(GET_USERLIST, {}).then(res => {
  386. if (res.code != 'ok') {
  387. globalPopup?.showError(res.msg)
  388. return
  389. }
  390. userList.value = res.data
  391. })
  392. }
  393. function dialogFromCli(type: string, data: any = {}, flag: boolean = false) {
  394. resetDialog()
  395. if (flag) {
  396. const { id } = data
  397. deptForm.parentId = id
  398. }
  399. dialogFrom[type] = true
  400. }
  401. function handleSizeChange(val: number) {
  402. teamForm.pageIndex = 1
  403. teamForm.pageSize = val
  404. getTableData()
  405. }
  406. function handleCurrentChange(val: number) {
  407. teamForm.pageIndex = val
  408. getTableData()
  409. }
  410. function resetDialog() {
  411. let newDeptForm = resetFromValue(deptForm)
  412. Object.assign(deptForm, newDeptForm)
  413. }
  414. function batchItem(text: string, type: batchOperationType, data: any) {
  415. allText.batchText = text,
  416. visibleType.value = type
  417. visibleData.value = data
  418. dialogFrom.batchOperationVisible = true
  419. }
  420. function handleClose(done: any) {
  421. done()
  422. }
  423. function closeModal(modelType: string, flag: boolean = false) {
  424. dialogFrom[modelType] = false
  425. if (flag) {
  426. changeBatch(false)
  427. getTableData()
  428. }
  429. }
  430. onBeforeMount(() => {
  431. pagePermission.value = getFunctionList(MOD)
  432. })
  433. onMounted(() => {
  434. getRoleList()
  435. getUserList()
  436. getTableData()
  437. getDeptList()
  438. });
  439. </script>
  440. <style lang="scss" scoped>
  441. .teamstyle {
  442. .team-header {
  443. padding: 0.75rem 1.25rem;
  444. box-sizing: border-box
  445. }
  446. .textFont {
  447. font-size: 20px;
  448. }
  449. .textSpan {
  450. color: $fontGray;
  451. }
  452. }
  453. .const-left {
  454. padding: 0.75rem 0;
  455. .treeContent {
  456. width: 87%;
  457. .custom-tree-node {
  458. display: flex;
  459. align-items: center;
  460. justify-content: space-between;
  461. width: 100%;
  462. }
  463. .treeLabel {
  464. width: 80%;
  465. white-space: nowrap;
  466. overflow: hidden;
  467. text-overflow: ellipsis;
  468. }
  469. .treeIcon {
  470. display: flex;
  471. align-items: center;
  472. justify-self: flex-end;
  473. }
  474. }
  475. }
  476. .operation {
  477. cursor: pointer;
  478. }
  479. </style>