app-base-web
Version:
web development common base package.
449 lines (428 loc) • 12.1 kB
JavaScript
import React from 'react';
import {Layout, Tree, Table, Form, Row, Col, Button, Input, InputNumber, message} from 'antd'
import UtilModal from '../../../library/util-modal';
import UtilString from '../../../library/util-string';
import api from '../../../library/util-axios';
import Jquery from '../../../library/jquery';
// 系统管理-权限管理
const title = "权限管理"
const url = "SysRole/"
class SysRoleList extends React.Component {
constructor(props) {
super(props)
this.state = {
params: {},
data: [],
pagination: {},
loading: false,
selected: [],//Table checked
//显藏控制
showView: false,
showEdit: false,
//values
values: {},
}
this.columns = [{
title: "序号",
width: 50,
fixed: "left",
render: (text, record, index) => {
return index + 1
}
}, {
title: "角色名称",
dataIndex: "name",
render: (text, record) => (<a className="btn-detail" onClick={() => this.onView(record)}>{text}</a>)
},
{
title: "顺序号",
dataIndex: "ord",
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,
params,
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,
values: rs.data
})
})
}
onSave = (values) => {
this.onLoad(this.state.params)
}
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)
}
})
}
})
}
render() {
var tableCfg = {
scroll: {y: this.props.height - 250},
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" style={{paddingRight: "20px"}}>
<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 className="text-left" xs={16}>
<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="text-right" xs={8}>
<Input.Search placeholder="请输入名称" onSearch={value => this.onSearch(value)} enterButton/>
</Col>
</Row>
<Table {...tableCfg} onRow={record => {
return {
onClick: event => {
this.props.onSetRoleId(record.id)
}
}
}}/>
</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})
}}
/> : ""}
</div>
}
}
class MenuTree extends React.Component {
constructor(props) {
super(props)
this.state = {
defaultCheckedKeys:[],
checkedKeys: [],
treeData: [],
}
this.initData()
}
async initData() {
let rs = await api.post("SysRoleMenu/listTree", {
roleId: this.props.roleId
})
this.setState({
treeData: rs.data.treeData,
defaultExpandedKeys: rs.data.defaultExpandedKeys,
checkedKeys: rs.data.checkedKeys
})
}
onCheck = (checkedKeys) => {
this.setState({
checkedKeys
})
};
onExpand = (expandedKeys) => {
this.setState({
expandedKeys,
autoExpandParent:false
})
};
onSelect = (selectedKeys, info) => {
this.setState({
selectedKeys
})
};
onAuth = (e) => {
if (this.props.roleId.length == 0) {
message.error("请选择一条角色记录!")
return false
}
let params = {
roleId: this.props.roleId,
menuIds: JSON.stringify(this.state.checkedKeys)
}
api.post("SysRoleMenu/save", params).then(function (rs) {
if (rs.success) {
message.info(rs.msg)
} else {
message.error(rs.msg)
}
})
}
componentDidMount() {
let timer = setInterval(function () {
let el = Jquery(".ant-tree-iconEle")
if (el.length > 0) {
el.remove()
clearInterval(timer)
}
}, 100)
}
render() {
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>角色权限</Col>
</Row>
<Row className="main-toolbar">
<Col className="text-left" xs={12}>
<Button className="btn-auth" onClick={this.onAuth}><span
className="iconfont icon-shouquan"></span><i>授权</i></Button>
</Col>
</Row>
<Tree
checkable
onExpand={this.onExpand}
expandedKeys={this.state.defaultExpandedKeys}
autoExpandParent={this.state.autoExpandParent}
onCheck={this.onCheck}
checkedKeys={this.state.checkedKeys}
onSelect={this.onSelect}
selectedKeys={this.state.selectedKeys}
treeData={this.state.treeData}
/>
</div>
</div>
)
}
}
export default class SysRoleUser extends React.Component {
constructor(props) {
super(props)
this.state = {
roleId: "-1",
menuKey: UtilString.uuid()
}
}
onSetRoleId = (roleId) => {
// console.log("roleId:")
// console.log(roleId)
this.setState({
roleId,
menuKey: UtilString.uuid()
})
}
render() {
return <Layout style={{background: "#f0f2f5", height: "100%"}}>
<Layout.Sider theme="light" width="61.8%" style={{marginRight: "10px", paddingRight: "10px"}}>
<SysRoleList onSetRoleId={this.onSetRoleId}/>
</Layout.Sider>
<Layout.Content style={{background: "#fff", padding: "0 20px"}}>
<MenuTree key={this.state.menuKey} roleId={this.state.roleId}/>
</Layout.Content>
</Layout>
}
}
class FormView extends React.Component {
constructor(props) {
super(props)
this.state = {
...props.values
}
}
componentDidMount() {
let me = this
api.get(url + "getModel?id=" + this.state.id, {}, function (rs) {
me.setState({
...rs.data
})
})
}
render() {
return (
<Form className="form-view">
<div className="form-title">
<i>{title} - 详情</i>
<span><Button className="btn-edit" onClick={(e) => {
this.props.onEdit(this.state)
}}><i className="iconfont icon-edit"></i>编辑</Button></span>
</div>
<div className="form-content" style={{height: this.props.height}}>
<Row>
<Col xs={24}><label>角色名称</label><span>{this.state.name}</span></Col>
</Row>
<Row>
<Col xs={24}><label>顺序号</label><span>{this.state.ord}</span></Col>
</Row>
</div>
<div className="form-toolbar">
<Button className="btn-return" onClick={this.props.onReturn}><i
className="iconfont icon-return"></i>返回</Button>
</div>
</Form>
)
}
}
class FormEdit extends React.Component {
constructor(props) {
super(props)
this.state = {
...props.values
}
this.formRef = React.createRef()
}
onSave = (values) => {
let me = this
values.id = this.state.id
api.post(url + "save", values).then(function (rs) {
if (rs.success) {
message.info(rs.msg)
me.props.onSave(values)
} else {
message.error(rs.msg)
}
})
}
render() {
return (
<Form ref={this.formRef} className="form-edit" layout="vertical" onFinish={this.onSave}>
<div className="form-title">
<i> {title} - {this.state.id ? "编辑" : "录入"}</i>
{this.state.id ? <span><Button className="btn-return" onClick={this.props.onEditReturn}><i
className="iconfont icon-return"></i>返回</Button></span> : ""}
</div>
<div className="form-content" style={{height: this.props.height}}>
<Row>
<Col xs={24}>
<Form.Item
name="name"
label="角色角色名称"
initialValue={this.state.name}
rules={[{
required: true,
message: '请输入角色名称'
}]}
>
<Input/>
</Form.Item>
</Col>
</Row>
<Row>
<Col xs={24}>
<Form.Item
name="ord"
label="角色顺序号"
initialValue={this.state.ord}
rules={[{
required: true,
message: '请输入顺序号'
}]}
>
<InputNumber style={{width: "100%"}}/>
</Form.Item>
</Col>
</Row>
</div>
<div className="form-toolbar">
<Button className="btn-cancel" onClick={this.props.onCancel}><i
className="iconfont icon-cancel"></i>取消</Button>
<Button className="btn-submit" htmlType="submit"><i className="iconfont icon-submit"></i>提交</Button>
</div>
</Form>
)
}
}