| | |
| | | |
| | | import React, { useState, useRef, useEffect } from 'react'; |
| | | import { Button, message, Modal } from 'antd'; |
| | | import { Button, message, Modal, Row, Col, Card, Tree, Input, Tag } from 'antd'; |
| | | import { |
| | | FooterToolbar, |
| | | PageContainer, |
| | | ProTable, |
| | | LightFilter, |
| | | } from '@ant-design/pro-components'; |
| | | import { PlusOutlined, ExportOutlined } from '@ant-design/icons'; |
| | | import { PlusOutlined, ExportOutlined, DownOutlined } from '@ant-design/icons'; |
| | | import Http from '@/utils/http'; |
| | | import Edit from './components/edit' |
| | | import Pwd from './components/pwd' |
| | | import { TextFilter, SelectFilter, DatetimeRangeFilter, LinkFilter } from '@/components/TableSearch' |
| | | import { transformTreeData, getTreeAllKeys } from '@/utils/tree-util' |
| | | import { statusMap } from '@/utils/enum-util' |
| | | |
| | | const handleSave = async (val) => { |
| | | const hide = message.loading('正在添加'); |
| | |
| | | } |
| | | }; |
| | | |
| | | const handlePwd = async (val) => { |
| | | const hide = message.loading('正在重置'); |
| | | try { |
| | | const resp = await Http.doPost('api/user/reset/pwd', val); |
| | | if (resp.code === 200) { |
| | | message.success('重置成功'); |
| | | return true; |
| | | } else { |
| | | message.error(resp.msg); |
| | | return false; |
| | | } |
| | | } catch (error) { |
| | | message.error('重置失败请重试!'); |
| | | return false; |
| | | } finally { |
| | | hide(); |
| | | } |
| | | }; |
| | | |
| | | |
| | | const Main = () => { |
| | | const formTableRef = useRef(); |
| | |
| | | const [modalVisible, setModalVisible] = useState(false); |
| | | const [currentRow, setCurrentRow] = useState(); |
| | | const [searchParam, setSearchParam] = useState({}); |
| | | |
| | | const [boxHeight, setBoxHeight] = useState(window.innerHeight - 450); |
| | | |
| | | const [boxHeight, setBoxHeight] = useState(); |
| | | const [deptTreeData, setDeptTreeData] = useState([]); |
| | | const [deptExpandedKeys, setDeptExpandedKeys] = useState([]); |
| | | const [pwdModalVisible, setPwdModalVisible] = useState(false); |
| | | |
| | | useEffect(() => { |
| | | const handleResize = () => setBoxHeight(window.innerHeight - 450); |
| | | const handleResize = () => setBoxHeight(window.innerHeight - 368); |
| | | window.addEventListener('resize', handleResize); |
| | | handleResize(); |
| | | |
| | | Http.doPostPromise('/api/dept/tree', {}, (res) => { |
| | | const rootMenu = { id: 0, name: '全部', value: 0, children: [] }; |
| | | rootMenu.children = res.data; |
| | | const treeData = transformTreeData([rootMenu]); |
| | | setDeptTreeData(treeData); |
| | | const treeAllKeys = getTreeAllKeys(treeData); |
| | | setDeptExpandedKeys(treeAllKeys); |
| | | }) |
| | | |
| | | return () => window.removeEventListener('resize', handleResize); |
| | | }, []); |
| | | |
| | |
| | | title: 'No', |
| | | dataIndex: 'index', |
| | | valueType: 'indexBorder', |
| | | width: 48, |
| | | width: 30, |
| | | }, |
| | | { |
| | | title: '昵称', |
| | | title: '名称', |
| | | dataIndex: 'nickname', |
| | | valueType: 'text', |
| | | hidden: false, |
| | | width: 140, |
| | | copyable: true, |
| | | width: 100, |
| | | filterDropdown: (props) => <TextFilter |
| | | name='nickname' |
| | | {...props} |
| | |
| | | />, |
| | | }, |
| | | { |
| | | title: '账号', |
| | | title: '登录账号', |
| | | dataIndex: 'username', |
| | | valueType: 'text', |
| | | hidden: false, |
| | | width: 140, |
| | | width: 100, |
| | | filterDropdown: (props) => <TextFilter |
| | | name='username' |
| | | {...props} |
| | |
| | | dataIndex: 'password', |
| | | valueType: 'password', |
| | | hidden: true, |
| | | width: 140, |
| | | width: 100, |
| | | filterDropdown: (props) => <TextFilter |
| | | name='password' |
| | | {...props} |
| | |
| | | title: '头像', |
| | | dataIndex: 'avatar', |
| | | valueType: 'text', |
| | | hidden: false, |
| | | hidden: true, |
| | | width: 140, |
| | | filterDropdown: (props) => <TextFilter |
| | | name='avatar' |
| | |
| | | dataIndex: 'sex$', |
| | | valueType: 'text', |
| | | hidden: false, |
| | | width: 140, |
| | | width: 80, |
| | | filterDropdown: (props) => <SelectFilter |
| | | name='sex' |
| | | {...props} |
| | |
| | | dataIndex: 'phone', |
| | | valueType: 'text', |
| | | hidden: false, |
| | | width: 140, |
| | | width: 100, |
| | | filterDropdown: (props) => <TextFilter |
| | | name='phone' |
| | | {...props} |
| | |
| | | title: '邮箱', |
| | | dataIndex: 'email', |
| | | valueType: 'text', |
| | | hidden: false, |
| | | hidden: true, |
| | | width: 140, |
| | | filterDropdown: (props) => <TextFilter |
| | | name='email' |
| | |
| | | dataIndex: 'deptId$', |
| | | valueType: 'text', |
| | | hidden: false, |
| | | width: 140, |
| | | filterDropdown: (props) => <LinkFilter |
| | | name='deptId' |
| | | major='dept' |
| | | {...props} |
| | | actionRef={actionRef} |
| | | setSearchParam={setSearchParam} |
| | | />, |
| | | width: 100, |
| | | render: (_, record) => { |
| | | if (_ && _ !== '-') { |
| | | return <Tag color={""}>{_}</Tag> |
| | | } else { |
| | | return '-' |
| | | } |
| | | }, |
| | | // filterDropdown: (props) => <LinkFilter |
| | | // name='deptId' |
| | | // major='dept' |
| | | // {...props} |
| | | // actionRef={actionRef} |
| | | // setSearchParam={setSearchParam} |
| | | // />, |
| | | }, |
| | | { |
| | | title: '真实姓名', |
| | |
| | | dataIndex: 'status$', |
| | | valueType: 'text', |
| | | hidden: false, |
| | | width: 140, |
| | | width: 80, |
| | | render: (_, record) => { |
| | | const status = statusMap[record.status] |
| | | return <Tag color={status.color}>{status.text}</Tag> |
| | | }, |
| | | filterDropdown: (props) => <SelectFilter |
| | | name='status' |
| | | {...props} |
| | |
| | | { |
| | | title: '操作', |
| | | dataIndex: 'option', |
| | | width: 140, |
| | | width: 150, |
| | | valueType: 'option', |
| | | render: (_, record) => [ |
| | | <Button |
| | |
| | | }} |
| | | > |
| | | 编辑 |
| | | </Button>, |
| | | <Button |
| | | type="link" |
| | | key="pwd" |
| | | onClick={() => { |
| | | setPwdModalVisible(true); |
| | | setCurrentRow(record); |
| | | }} |
| | | > |
| | | 重置密码 |
| | | </Button>, |
| | | <Button |
| | | type="link" |
| | |
| | | ]; |
| | | |
| | | return ( |
| | | <PageContainer> |
| | | <div style={{ width: '100%', float: 'right' }}> |
| | | <ProTable |
| | | key="user" |
| | | rowKey="id" |
| | | actionRef={actionRef} |
| | | formRef={formTableRef} |
| | | columns={columns} |
| | | cardBordered |
| | | scroll={{ x: 1300, y: boxHeight }} |
| | | dateFormatter="string" |
| | | pagination={{ pageSize: 20 }} |
| | | search={false} |
| | | toolbar={{ |
| | | search: { |
| | | onSearch: (value) => { |
| | | <PageContainer |
| | | header={{ |
| | | breadcrumb: {}, |
| | | }} |
| | | > |
| | | <Row gutter={[16, 24]}> |
| | | <Col lg={6} md={24}> |
| | | <Card title="部门" style={{ width: '100%', height: 'calc(100vh - 200px)' }}> |
| | | <Input |
| | | style={{ marginBottom: 10 }} |
| | | placeholder="Search" |
| | | onChange={(e) => { |
| | | const { value } = e.target; |
| | | console.log(value); |
| | | }} |
| | | /> |
| | | <Tree |
| | | showLine |
| | | blockNode |
| | | defaultExpandAll // 异步加载失效 |
| | | expandedKeys={deptExpandedKeys} |
| | | onExpand={setDeptExpandedKeys} |
| | | treeData={deptTreeData} |
| | | switcherIcon={<DownOutlined />} |
| | | onSelect={(selectedKeys, info) => { |
| | | const deptId = selectedKeys[0]; |
| | | setSearchParam(prevState => ({ |
| | | ...prevState, |
| | | condition: value |
| | | deptId: deptId |
| | | })); |
| | | actionRef.current?.reload(); |
| | | }, |
| | | }, |
| | | filter: ( |
| | | <LightFilter |
| | | onValuesChange={(val) => { |
| | | }} |
| | | > |
| | | </LightFilter> |
| | | ), |
| | | actions: [ |
| | | <Button |
| | | type="primary" |
| | | key="save" |
| | | onClick={async () => { |
| | | setModalVisible(true) |
| | | }} |
| | | > |
| | | <PlusOutlined /> |
| | | 添加 |
| | | </Button>, |
| | | <Button |
| | | key="export" |
| | | onClick={async () => { |
| | | handleExport(); |
| | | }} |
| | | > |
| | | <ExportOutlined /> |
| | | 导出 |
| | | </Button>, |
| | | ], |
| | | }} |
| | | request={(params, sorter, filter) => |
| | | Http.doPostPromise('/api/user/page', { ...params, ...searchParam }, (res) => { |
| | | return { |
| | | data: res.data.records, |
| | | total: res.data.total, |
| | | success: true, |
| | | } |
| | | }) |
| | | } |
| | | rowSelection={{ |
| | | onChange: (ids, rows) => { |
| | | setSelectedRows(rows); |
| | | } |
| | | }} |
| | | columnsState={{ |
| | | persistenceKey: 'pro-table-user', |
| | | persistenceType: 'localStorage', |
| | | defaultValue: { |
| | | option: { fixed: 'right', disable: true }, |
| | | }, |
| | | onChange(value) { |
| | | }, |
| | | }} |
| | | /> |
| | | </div> |
| | | |
| | | {selectedRows?.length > 0 && ( |
| | | <FooterToolbar |
| | | extra={ |
| | | <div> |
| | | 已选择 |
| | | <a style={{ fontWeight: 600 }}>{selectedRows.length}</a> |
| | | 项 |
| | | </div> |
| | | } |
| | | > |
| | | <Button |
| | | key="remove" |
| | | danger |
| | | onClick={async () => { |
| | | Modal.confirm({ |
| | | title: '删除', |
| | | content: '确定删除该项吗?', |
| | | onOk: async () => { |
| | | const success = await handleRemove(selectedRows); |
| | | if (success) { |
| | | setSelectedRows([]); |
| | | actionRef.current?.reloadAndRest?.(); |
| | | } |
| | | }} |
| | | /> |
| | | </Card> |
| | | </Col> |
| | | <Col lg={18} md={24}> |
| | | <ProTable |
| | | key="user" |
| | | rowKey="id" |
| | | actionRef={actionRef} |
| | | formRef={formTableRef} |
| | | columns={columns} |
| | | cardBordered |
| | | scroll={{ y: boxHeight }} |
| | | dateFormatter="string" |
| | | pagination={{ pageSize: 20 }} |
| | | search={false} |
| | | toolbar={{ |
| | | search: { |
| | | onSearch: (value) => { |
| | | setSearchParam(prevState => ({ |
| | | ...prevState, |
| | | condition: value |
| | | })); |
| | | actionRef.current?.reload(); |
| | | }, |
| | | }); |
| | | }, |
| | | filter: ( |
| | | <LightFilter |
| | | onValuesChange={(val) => { |
| | | }} |
| | | > |
| | | </LightFilter> |
| | | ), |
| | | actions: [ |
| | | <Button |
| | | type="primary" |
| | | key="save" |
| | | onClick={async () => { |
| | | setModalVisible(true) |
| | | }} |
| | | > |
| | | <PlusOutlined /> |
| | | 添加 |
| | | </Button>, |
| | | <Button |
| | | key="export" |
| | | onClick={async () => { |
| | | handleExport(); |
| | | }} |
| | | > |
| | | <ExportOutlined /> |
| | | 导出 |
| | | </Button>, |
| | | ], |
| | | }} |
| | | > |
| | | 批量删除 |
| | | </Button> |
| | | </FooterToolbar> |
| | | )} |
| | | request={(params, sorter, filter) => |
| | | Http.doPostPromise('/api/user/page1', { ...params, ...searchParam }, (res) => { |
| | | return { |
| | | data: res.data.records, |
| | | total: res.data.total, |
| | | success: true, |
| | | } |
| | | }) |
| | | } |
| | | rowSelection={{ |
| | | onChange: (ids, rows) => { |
| | | setSelectedRows(rows); |
| | | } |
| | | }} |
| | | columnsState={{ |
| | | persistenceKey: 'pro-table-user', |
| | | persistenceType: 'localStorage', |
| | | defaultValue: { |
| | | option: { fixed: 'right', disable: true }, |
| | | }, |
| | | onChange(value) { |
| | | }, |
| | | }} |
| | | /> |
| | | </Col> |
| | | |
| | | <Edit |
| | | open={modalVisible} |
| | | values={currentRow || {}} |
| | | onCancel={ |
| | | () => { |
| | | setModalVisible(false); |
| | | setCurrentRow(undefined); |
| | | } |
| | | } |
| | | onSubmit={async (values) => { |
| | | let ok = false; |
| | | if (values.id) { |
| | | ok = await handleUpdate({ ...values }) |
| | | } else { |
| | | ok = await handleSave({ ...values }) |
| | | } |
| | | if (ok) { |
| | | setModalVisible(false); |
| | | setCurrentRow(undefined); |
| | | if (actionRef.current) { |
| | | actionRef.current.reload(); |
| | | {selectedRows?.length > 0 && ( |
| | | <FooterToolbar |
| | | extra={ |
| | | <div> |
| | | 已选择 |
| | | <a style={{ fontWeight: 600 }}>{selectedRows.length}</a> |
| | | 项 |
| | | </div> |
| | | } |
| | | > |
| | | <Button |
| | | key="remove" |
| | | danger |
| | | onClick={async () => { |
| | | Modal.confirm({ |
| | | title: '删除', |
| | | content: '确定删除该项吗?', |
| | | onOk: async () => { |
| | | const success = await handleRemove(selectedRows); |
| | | if (success) { |
| | | setSelectedRows([]); |
| | | actionRef.current?.reloadAndRest?.(); |
| | | } |
| | | }, |
| | | }); |
| | | }} |
| | | > |
| | | 批量删除 |
| | | </Button> |
| | | </FooterToolbar> |
| | | )} |
| | | |
| | | <Edit |
| | | open={modalVisible} |
| | | values={currentRow || {}} |
| | | treeData={deptTreeData} |
| | | onCancel={ |
| | | () => { |
| | | setModalVisible(false); |
| | | setCurrentRow(undefined); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | /> |
| | | onSubmit={async (values) => { |
| | | let ok = false; |
| | | if (values.id) { |
| | | ok = await handleUpdate({ ...values }) |
| | | } else { |
| | | ok = await handleSave({ ...values }) |
| | | } |
| | | if (ok) { |
| | | setModalVisible(false); |
| | | setCurrentRow(undefined); |
| | | if (actionRef.current) { |
| | | actionRef.current.reload(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | /> |
| | | |
| | | <Pwd |
| | | open={pwdModalVisible} |
| | | values={currentRow || {}} |
| | | onCancel={ |
| | | () => { |
| | | setPwdModalVisible(false); |
| | | setCurrentRow(undefined); |
| | | } |
| | | } |
| | | onSubmit={async (values) => { |
| | | let ok = false; |
| | | if (values.id) { |
| | | ok = await handlePwd({ ...values }) |
| | | } |
| | | if (ok) { |
| | | setPwdModalVisible(false); |
| | | setCurrentRow(undefined); |
| | | if (actionRef.current) { |
| | | actionRef.current.reload(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | /> |
| | | </Row> |
| | | </PageContainer> |
| | | ); |
| | | }; |