mock平台

List.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import React, { PureComponent as Component } from 'react';
  2. import { formatTime } from '../../common.js';
  3. import { Link } from 'react-router-dom';
  4. import { setBreadcrumb } from '../../reducer/modules/user';
  5. //import PropTypes from 'prop-types'
  6. import { connect } from 'react-redux';
  7. import PropTypes from 'prop-types';
  8. import { Table, Popconfirm, message, Input } from 'antd';
  9. import axios from 'axios';
  10. const Search = Input.Search;
  11. const limit = 20;
  12. @connect(
  13. state => {
  14. return {
  15. curUserRole: state.user.role
  16. };
  17. },
  18. {
  19. setBreadcrumb
  20. }
  21. )
  22. class List extends Component {
  23. constructor(props) {
  24. super(props);
  25. this.state = {
  26. data: [],
  27. total: null,
  28. current: 1,
  29. backups: [],
  30. isSearch: false
  31. };
  32. }
  33. static propTypes = {
  34. setBreadcrumb: PropTypes.func,
  35. curUserRole: PropTypes.string
  36. };
  37. changePage = current => {
  38. this.setState(
  39. {
  40. current: current
  41. },
  42. this.getUserList
  43. );
  44. };
  45. getUserList() {
  46. axios.get('/api/user/list?page=' + this.state.current + '&limit=' + limit).then(res => {
  47. let result = res.data;
  48. if (result.errcode === 0) {
  49. let list = result.data.list;
  50. let total = result.data.count;
  51. list.map((item, index) => {
  52. item.key = index;
  53. item.up_time = formatTime(item.up_time);
  54. });
  55. this.setState({
  56. data: list,
  57. total: total,
  58. backups: list
  59. });
  60. }
  61. });
  62. }
  63. componentDidMount() {
  64. this.getUserList();
  65. }
  66. confirm = uid => {
  67. axios
  68. .post('/api/user/del', {
  69. id: uid
  70. })
  71. .then(
  72. res => {
  73. if (res.data.errcode === 0) {
  74. message.success('已删除此用户');
  75. let userlist = this.state.data;
  76. userlist = userlist.filter(item => {
  77. return item._id != uid;
  78. });
  79. this.setState({
  80. data: userlist
  81. });
  82. } else {
  83. message.error(res.data.errmsg);
  84. }
  85. },
  86. err => {
  87. message.error(err.message);
  88. }
  89. );
  90. };
  91. async componentWillMount() {
  92. this.props.setBreadcrumb([{ name: '用户管理' }]);
  93. }
  94. handleSearch = value => {
  95. let params = { q: value };
  96. if (params.q !== '') {
  97. axios.get('/api/user/search', { params }).then(data => {
  98. let userList = [];
  99. data = data.data.data;
  100. if (data) {
  101. data.forEach(v =>
  102. userList.push({
  103. ...v,
  104. _id: v.uid
  105. })
  106. );
  107. }
  108. this.setState({
  109. data: userList,
  110. isSearch: true
  111. });
  112. });
  113. } else {
  114. this.setState({
  115. data: this.state.backups,
  116. isSearch: false
  117. });
  118. }
  119. };
  120. render() {
  121. const role = this.props.curUserRole;
  122. let data = [];
  123. if (role === 'admin') {
  124. data = this.state.data;
  125. }
  126. let columns = [
  127. {
  128. title: '用户名',
  129. dataIndex: 'username',
  130. key: 'username',
  131. width: 180,
  132. render: (username, item) => {
  133. return <Link to={'/user/profile/' + item._id}>{item.username}</Link>;
  134. }
  135. },
  136. {
  137. title: 'Email',
  138. dataIndex: 'email',
  139. key: 'email'
  140. },
  141. {
  142. title: '用户角色',
  143. dataIndex: 'role',
  144. key: 'role',
  145. width: 150
  146. },
  147. {
  148. title: '更新日期',
  149. dataIndex: 'up_time',
  150. key: 'up_time',
  151. width: 160
  152. },
  153. {
  154. title: '功能',
  155. key: 'action',
  156. width: '90px',
  157. render: item => {
  158. return (
  159. <span>
  160. {/* <span className="ant-divider" /> */}
  161. <Popconfirm
  162. title="确认删除此用户?"
  163. onConfirm={() => {
  164. this.confirm(item._id);
  165. }}
  166. okText="确定"
  167. cancelText="取消"
  168. >
  169. <a style={{ display: 'block', textAlign: 'center' }} href="#">
  170. 删除
  171. </a>
  172. </Popconfirm>
  173. </span>
  174. );
  175. }
  176. }
  177. ];
  178. columns = columns.filter(item => {
  179. if (item.key === 'action' && role !== 'admin') {
  180. return false;
  181. }
  182. return true;
  183. });
  184. const pageConfig = {
  185. total: this.state.total,
  186. pageSize: limit,
  187. current: this.state.current,
  188. onChange: this.changePage
  189. };
  190. const defaultPageConfig = {
  191. total: this.state.data.length,
  192. pageSize: limit,
  193. current: 1
  194. };
  195. return (
  196. <section className="user-table">
  197. <div className="user-search-wrapper">
  198. <h2 style={{ marginBottom: '10px' }}>用户总数:{this.state.total}位</h2>
  199. <Search
  200. onChange={e => this.handleSearch(e.target.value)}
  201. onSearch={this.handleSearch}
  202. placeholder="请输入用户名"
  203. />
  204. </div>
  205. <Table
  206. bordered={true}
  207. rowKey={record => record._id}
  208. columns={columns}
  209. pagination={this.state.isSearch ? defaultPageConfig : pageConfig}
  210. dataSource={data}
  211. />
  212. </section>
  213. );
  214. }
  215. }
  216. export default List;