import React, { useEffect, useState } from 'react'; import { connect } from 'react-redux'; import { DeleteOutlined, EllipsisOutlined, FormOutlined } from '@ant-design/icons'; import { Spin, Space, Button, Popconfirm, Row, Col, Tree, Table, Card, Switch, Tooltip, Breadcrumb } from 'antd'; import ProTable from '@ant-design/pro-table'; import { getDepById } from '../actions/user'; import { getDepMessage, getDepUser, createUser, updateUser, delUser, resetPwd, createDep, delDep, updateDep } from '../actions/user' import { postUserRes } from '../actions/authority' import UserModal from '../components/userModal'; import ResetPwd from '../components/resetPwd'; import DepModal from '../components/depModal'; const TreeNode = Tree.TreeNode; const UserManage = (props) => { const { dispatch, loading, depMessage, depUser, clientHeight, user, actions } = props const [modalVisible, setModalVisible] = useState(false); const [modalType, setModalType] = useState(); const [modalRecord, setModalRecord] = useState(); const [pwdModalVisible, setPwdModalVisible] = useState(false); const [depSelectedKeys, setDepSelectedKeys] = useState([]) const [rowSelected, setRowSelected] = useState([]) const [depModalVisible, setDepModalVisible] = useState(false); const [depModalType, setDepModalType] = useState(); const [depModalRecord, setDepModalRecord] = useState(); const [selectedTree, setSelectedTree] = useState(); const [depCrumbs, setDepCrumbs] = useState([]); const [depUserCopy, setDepUserCopy] = useState([]) const [uid, setuid] = useState() const [editAble, setEditAble] = useState(user?.username !== 'SuperAdmin' && user?.userResources?.find(i => i.resourceId === 'USERMANAGE')?.isshow === "true" ? true : '')//控制操作(新增删除等操作,对应权限的'不可编辑')是否可操作 const [depMessagedata, setdepMessagedata] = useState(depMessage) //('depMessagedata', depMessagedata) useEffect(async () => { if (depMessage.length) { //console.log('depMessage', depMessage) //超级管理员展示所有部门 if (user?.username === 'SuperAdmin') { setdepMessagedata(depMessage) setDepSelectedKeys([depMessage[0].id]) dispatch(getDepUser(depMessage[0].id)) } else { //不是超级管理员,展示相应部门的数据 dispatch(getDepUser(user.departmentId)) const res = await dispatch(getDepById({ deptId: parseInt(user.departmentId) })) //('resssss', res) setdepMessagedata(res.payload.data) // setDepSelectedKeys([res.payload.data[0].id]) // dispatch(getDepUser(res.payload.data[0].id)) } } }, [depMessage]) useEffect(() => { let code = ['USERMANAGE', 'AUTHORIMANAGE'] //console.log('你來u盧克嗎', depUser, uid) // console.log(depUser.filter(i=>i.phone===uid)[0]?.id,'当前的用户user') if (depUser?.filter(i => i.phone === uid)[0]?.id) { //console.log('你來u盧克嗎', depUser, uid) dispatch(postUserRes({ userId: depUser.filter(i => i.phone === uid)[0]?.id, resCode: code, isShow: false })) } }, [uid]) useEffect(() => { dispatch(getDepMessage()) }, []) useEffect(() => { //console.log('depuser', depUser, user) // const copy = depUser.filter((item) => { // //console.log('item1', item) // return item.id !== user.id//把自己筛选出去 // }) // //console.log('copy', copy) const copy = [...new Set(depUser)] setDepUserCopy(copy) }, [depUser]) useEffect(() => { if (depMessage.length) { } }, [depMessage]) useEffect(() => { const list = handleCrumbs(depSelectedKeys) setDepCrumbs(list) }, [depSelectedKeys]) //删除自己退出登录 const conFirmHandler = (record) => { delUsers([record.id]) if (user?.id === record.id) { dispatch(actions.auth.logout(user)); history.push(`/signin`); } } const columns = [ { title: '姓名', dataIndex: 'name', }, { title: '手机号(用户名)', dataIndex: 'phone', }, { title: '邮箱', dataIndex: 'email', }, { title: '启用状态', dataIndex: 'enable', render: (_, r) => { return } }, { title: '是否管理员', dataIndex: 'isadmin', render: (_, r) => { return
{r.isAdmin ? '是' : '否'}
} }, { title: '操作', dataIndex: 'action', render: (dom, record) => { // return record.username == 'SuperAdmin' ? '' : [ // , // { // delUsers([record.id]) // }} // > // // , // // ] return [ , { conFirmHandler(record) }} disabled={editAble} > , ] }, }, ]; //弹窗确认 const onConfirm = (values) => { //('values.contract', values.contract) if (modalType == 'edit') { dispatch(updateUser(modalRecord.id, values.contract)).then(res => { if (res.success) { setModalVisible(false); dispatch(getDepUser(depSelectedKeys[0])); } }); } else { dispatch(createUser(values.contract)).then(res => { if (res.success) { setModalVisible(false); dispatch(getDepMessage()) setuid(values.contract.phone) } }); } } //打开弹窗 const openModal = (type, record) => { setModalVisible(true); setModalType(type); if (type == 'edit') { setModalRecord(record); } else { setModalRecord(null); } } //打开部门弹窗 const openDepModal = (type, record) => { setDepModalVisible(true) setDepModalType(type) if (type == 'edit') { setDepModalRecord(record); } else { setModalRecord(null); } } //删除用户 const delUsers = (ids, type) => { dispatch(delUser(ids)).then(res => { dispatch(getDepUser(depSelectedKeys[0])); if (type == 'batch') { setRowSelected([]); } }); } //重置密码 const onPwdConfirm = (values) => { dispatch(resetPwd(modalRecord.id, { password: values.password })).then(res => { if (res.success) { setPwdModalVisible(false); dispatch(getDepUser(depSelectedKeys[0])); } }); } //部门新增及编辑 const onDepConfirm = (data) => { //console.log('depModalType:', depModalType); //console.log('data:', data); if (depModalType == 'edit') { dispatch(updateDep(data)).then(res => { if (res.success) { setDepModalVisible(false); dispatch(getDepMessage()); } }); } else { dispatch(createDep(data)).then(res => { if (res.success) { setDepModalVisible(false); dispatch(getDepMessage()); } }); } } // 删除部门 const delDepartment = (id) => { dispatch(delDep(id)).then(res => { if (res.success) { dispatch(getDepMessage()) } }); } const renderTree = (item, id) => { return
{item.name}
{ selectedTree == id ? <> { setDepModalRecord(item) setDepModalVisible(true) setDepModalType('edit') }} /> {user?.username === 'SuperAdmin' ? { delDepartment(id) }} > : ''} : null }
} const handleCrumbs = (id) => { let crumbsList = [] depMessage?.forEach(d => { if (id == d.id) { crumbsList.push(d.name) } d.subordinate?.forEach(s => { if (id == s.id) { crumbsList.push(d.name) crumbsList.push(s.name) } s.subordinate?.forEach(a => { if (id == a.id) { crumbsList.push(d.name) crumbsList.push(s.name) crumbsList.push(a.name) } }) }) }) return crumbsList } return (
{ depMessage.length ? { // console.log('selectedKeys:',selectedKeys); if (e.selected) { handleCrumbs(selectedKeys) setSelectedTree(selectedKeys) setDepSelectedKeys(selectedKeys) dispatch(getDepUser(selectedKeys[0])) } }} style={{ paddingTop: 20 }} > { depMessagedata?.map((s, index) => { return { s.subordinate.map(k => { return { setIShowIcon(k.id) }} onMouseOut={() => { setIShowIcon(null) }}> { k.subordinate.map(i => { return { setIShowIcon(i.id) }} onMouseOut={() => { setIShowIcon(null) }}> }) } }) } }) } : '' } { depCrumbs?.map((c, index) => { return ({c}) }) } { setRowSelected(selectedRowKeys); }, getCheckboxProps: (record) => { return { disabled: record.username === 'SuperAdmin', } }, }} options={false} search={false} rowKey="id" toolBarRender={() => [ {/* */} { delUsers(rowSelected, 'batch') }} disabled={editAble}> ]} /> { depMessage.length && modalVisible ? : '' } {pwdModalVisible ? : ''} {depModalVisible ? : ''}
) } function mapStateToProps(state) { const { depMessage, depUser, global, auth } = state; // console.log(state); return { clientHeight: global.clientHeight, loading: depMessage.isRequesting, depMessage: depMessage.data || [], depUser: depUser.data || [], user: auth.user, actions: global.actions }; } export default connect(mapStateToProps)(UserManage);