yapi/client/containers/User/List.js

120 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-07-14 19:47:12 +08:00
import React, { Component } from 'react'
2017-07-18 19:47:38 +08:00
import { formatTime } from '../../common.js'
2017-07-18 17:15:29 +08:00
import { Link } from 'react-router-dom'
2017-07-17 11:01:05 +08:00
//import PropTypes from 'prop-types'
import {
Table,
2017-07-18 19:47:38 +08:00
Button,
Popconfirm,
message
2017-07-17 11:01:05 +08:00
} from 'antd'
import axios from 'axios';
2017-07-17 11:01:05 +08:00
class List extends Component {
2017-07-14 19:47:12 +08:00
constructor(props) {
super(props)
this.state = {
data: []
}
}
2017-07-17 11:01:05 +08:00
getUserList() {
axios.get('/user/list').then((res) => {
2017-07-18 17:15:29 +08:00
let result = res.data;
2017-07-18 19:47:38 +08:00
if (result.errcode === 0) {
2017-07-18 17:15:29 +08:00
let list = result.data.list;
2017-07-18 19:47:38 +08:00
list.map((item, index) => {
2017-07-18 17:15:29 +08:00
item.key = index;
item.up_time = formatTime(item.up_time)
2017-07-18 19:47:38 +08:00
})
2017-07-18 17:15:29 +08:00
this.setState({
data: list
});
}
})
2017-07-14 19:47:12 +08:00
}
componentDidMount() {
this.getUserList()
}
2017-07-17 11:01:05 +08:00
2017-07-18 19:47:38 +08:00
confirm = (uid) =>{
axios.post('/user/del', {
id: uid
}).then( (res)=>{
if(res.data.errcode === 0){
message.success('已删除此用户');
let userlist = this.state.data;
userlist = userlist.filter( (item)=>{
return item._id != uid
} )
this.setState({
data: userlist
})
}else{
message.error(res.data.errmsg);
}
}, (err) => {
message.error(err.message);
} )
}
2017-07-14 19:47:12 +08:00
2017-07-18 19:47:38 +08:00
render() {
const role = 'admin'
const data = this.state.data;
2017-07-18 19:47:38 +08:00
let columns = [{
title: 'UID',
dataIndex: '_id',
key: '_id'
}, {
title: '用户名',
dataIndex: 'username',
key: 'username'
}, {
title: 'Email',
dataIndex: 'email',
key: 'email'
}, {
title: '用户角色',
dataIndex: 'role',
key: 'role'
}, {
title: '更新日期',
dataIndex: 'up_time',
key: 'up_time'
}, {
title: '功能',
key: 'action',
render: (item) => {
return (
<span>
<Button type="primary"><Link to={"/user/profile/" + item._id} > 查看 </Link></Button>
2017-07-19 13:58:12 +08:00
<Popconfirm placement="leftTop" title="确认删除此用户?" onConfirm={() => {this.confirm(item._id)}} okText="Yes" cancelText="No">
2017-07-18 19:47:38 +08:00
<Button type="danger">删除</Button>
</Popconfirm>
</span>
)
}
}]
columns = columns.filter( (item)=>{
if(item.key === 'action' && role !== 'admin'){
return false;
}
return true;
} )
2017-07-14 19:47:12 +08:00
return (
2017-07-17 11:01:05 +08:00
<section className="user-table">
2017-07-14 19:47:12 +08:00
<Table columns={columns} dataSource={data} />
2017-07-17 11:01:05 +08:00
2017-07-14 19:47:12 +08:00
</section>
)
}
}
2017-07-17 11:01:05 +08:00
export default List