UNPKG

app-base-web

Version:
292 lines (287 loc) 7.6 kB
import React, { Suspense, lazy } from 'react'; import { Switch, Table, Form, Modal, Row, Col, Button, Input, message } from 'antd'; import api from '../../../library/util-axios'; import CheckGroup from '../../../library/check-group'; import dic from '../../../library/dic'; import UtilModal from "../../../library/util-modal" // 用户管理-用户/专家 const title = "用户/专家"; const url = "User/"; const FormView = lazy(() => import('./UserView')); const FormEdit = lazy(() => import('./UserEdit')); export default class UserList extends React.Component { constructor(props) { super(props); this.state = { params: {}, data: [], pagination: {}, loading: false, selected: [],//Table checked height: 0, //显藏控制 showView: false, showEdit: false, //values values: {}, }; this.columns = [ { title: "用户名称", dataIndex: "name", width: 100, render: (text, record) => (<a className="btn-detail" onClick={() => this.onView(record)}>{text}</a>) }, { title: "在线状态", dataIndex: "isOnline", width: 100, render: (text, record) => { return text == 1 ? "是" : "否" } }, { title: "头像", dataIndex: "photoUrl", width: 100, render: (text, record) => { return <img style={{ height: "40px", width: "40px" }} src={text} /> } }, { title: "是否为专家", dataIndex: "isExpert", width: 100, render: (text, record) => { return <Switch onChange={(checked) => { this.setExpert(record.id, checked); }} checkedChildren="" unCheckedChildren="" checked={Boolean(text)} /> } }, { title: "专家类型", dataIndex: "type", width: 160, render: (text, record) => { return <CheckGroup id="type" value={text} options={["机械", "电学", "化学", "其他"]} /> } }, { title: "性别", dataIndex: "sex", width: 100, render: (text, record) => { return text==0?'女':'男' } }, { title: "职务", dataIndex: "position", width: 100 }, { title: "手机", dataIndex: "phone", width: 100 }, { title: "邮箱", dataIndex: "email", width: 100 }, { title: "公司名称", dataIndex: "companyName", width: 100 }]; } componentDidMount() { this.onLoad(this.state.params); } /*** Table ***/ async onLoad(params) { this.setState({ loading: true }); let rs = await api.get(url + "getList", params); let pagination = { pageSizeOptions: ["20", "100", "200", "500", "1000"], pageSize: params.pageSize || 20, defaultPageSize: params.pageSize || 20, showSizeChanger: true, showQuickJumper: true, total: rs.total, showTotal: (total) => { return `总记录 ${total} ` } }; this.setState({ loading: false, showEdit: false, data: rs.data, pagination }); } onChange = (pagination, filters, sorter) => { let params = this.state.params; params.pageSize = pagination.pageSize; params.pageIndex = pagination.current; this.onLoad(params); } onSearch = (value) => { let params = this.state.params; params.name = value; this.onLoad(params); } onAdd = () => { let values = {}; Object.keys(this.state.values).forEach(key => values[key] = undefined); this.setState({ showView: false, showEdit: true, values }); } onView = (values) => { this.setState({ showView: true, loading: false, values }); } onEdit = (values) => { let me = this; api.get(url + "getModel?id=" + values.id, {}, function (rs) { me.setState({ showEdit: true, showView: false, showEditBatch: false, values: rs.data }); }); } onDel = () => { let me = this; if (me.state.selected.length == 0) { message.error('请选择记录!'); return; } UtilModal.confirm({ content: '确定删除?', onOk: function () { api.delete(url + "delete?id=" + me.state.selected).then(function (rs) { if (rs.success) { message.info(rs.msg); me.onLoad(me.state.params || {}); } else { message.error(rs.msg); } }); } }) } onSave = (values) => { this.onLoad(this.state.params); } onExport = () => { let me = this; let result = []; let titleArr = []; this.columns.map(function (model) { if (model.title == "操作") return true; titleArr.push(model.title); }); result.push(titleArr); this.state.data.map(function (rec, index) { let record = []; me.columns.map(function (model) { let text = rec[model.dataIndex]; if (model.title == "操作") { return true; } else if (model.render) { record.push(string.delTag(model.render(text, rec, index))); } else { record.push(text) } }); result.push(record); }); xlsx.write(result, title + date.getDateTime() + '.xlsx'); } setExpert = (id, checked) => { let me = this; Modal.confirm({ title: '警告', content: '确定设置' + (checked ? "专家" : "普通会员"), okText: '确认', cancelText: '取消', onOk: function () { api.post(url + "setExpert", { "id": id, "checked": checked }).then(function (rs) { if (rs.success) { message.info(rs.msg); me.onLoad(me.state.params || {}); } else { message.error(rs.msg); } }); } }); } render() { var tableCfg = { scroll: { y: this.props.height - 210 }, size: "middle", rowKey: "id", columns: this.columns, rowSelection: { selectedRowKeys: this.state.selected, onChange: (selectedRowKeys, selectedRows) => { this.setState({ selected: selectedRowKeys }); } }, rowClassName: (record) => { return record.id === this.state.selectedRowId ? 'row-selected' : ''; }, onRow: (record) => { return { onClick: event => { this.setState({ selectedRowId: record.id }); } } }, dataSource: this.state.data, pagination: this.state.pagination, loading: this.state.loading, onChange: this.onChange } return ( <div className="app-admin"> <div className={this.state.showView || this.state.showEdit ? "hide" : ""}> <Row> <Col className="main-title" ><i className="iconfont icon-title"></i>{title}</Col> </Row> <Row className="main-toolbar"> <Col xs={12}> <Button className="btn-add" onClick={this.onAdd}><i className="iconfont icon-add"></i>录入</Button> <Button className="btn-del" onClick={this.onDel}><i className="iconfont icon-del"></i>删除</Button> </Col> <Col className="query-filed" xs={12}> <Input.Search placeholder="请输入会员名称" onSearch={value => this.onSearch(value.trim())} enterButton /> </Col> </Row> <Table {...tableCfg} /> </div> <Suspense fallback={<div></div>}> {this.state.showView ? <FormView height={this.props.height - 140} values={this.state.values} onEdit={this.onEdit} onReturn={() => { this.setState({ showView: false }) }} /> : ""} {this.state.showEdit ? <FormEdit height={this.props.height - 140} values={this.state.values} onSave={this.onSave} onCancel={() => { this.setState({ showEdit: false }) }} onEditReturn={() => { this.setState({ showEdit: false, showView: true }) }} /> : ""} </Suspense> </div> ); } }