import React, { PureComponent as Component } from 'react'; import { Row, Col, Input, Button, Select, message, Upload, Tooltip } from 'antd'; import axios from 'axios'; import { formatTime } from '../../common.js'; import PropTypes from 'prop-types'; import { setBreadcrumb, setImageUrl } from '../../reducer/modules/user'; import { connect } from 'react-redux'; const EditButton = props => { const { isAdmin, isOwner, onClick, name, admin } = props; if (isOwner) { // 本人 if (admin) { return null; } return ( ); } else if (isAdmin) { // 管理员 return ( ); } else { return null; } }; EditButton.propTypes = { isAdmin: PropTypes.bool, isOwner: PropTypes.bool, onClick: PropTypes.func, name: PropTypes.string, admin: PropTypes.bool }; @connect( state => { return { curUid: state.user.uid, userType: state.user.type, curRole: state.user.role }; }, { setBreadcrumb } ) class Profile extends Component { static propTypes = { match: PropTypes.object, curUid: PropTypes.number, userType: PropTypes.string, setBreadcrumb: PropTypes.func, curRole: PropTypes.string, upload: PropTypes.bool }; constructor(props) { super(props); this.state = { usernameEdit: false, emailEdit: false, secureEdit: false, roleEdit: false, userinfo: {} }; } componentDidMount() { this._uid = this.props.match.params.uid; this.handleUserinfo(this.props); } UNSAFE_componentWillReceiveProps(nextProps) { if (!nextProps.match.params.uid) { return; } if (this._uid !== nextProps.match.params.uid) { this.handleUserinfo(nextProps); } } handleUserinfo(props) { const uid = props.match.params.uid; this.getUserInfo(uid); } handleEdit = (key, val) => { var s = {}; s[key] = val; this.setState(s); }; getUserInfo = id => { var _this = this; const { curUid } = this.props; axios.get('/api/user/find?id=' + id).then(res => { _this.setState({ userinfo: res.data.data, _userinfo: res.data.data }); if (curUid === +id) { this.props.setBreadcrumb([{ name: res.data.data.username }]); } else { this.props.setBreadcrumb([{ name: '管理: ' + res.data.data.username }]); } }); }; updateUserinfo = name => { var state = this.state; let value = this.state._userinfo[name]; let params = { uid: state.userinfo.uid }; params[name] = value; axios.post('/api/user/update', params).then( res => { let data = res.data; if (data.errcode === 0) { let userinfo = this.state.userinfo; userinfo[name] = value; this.setState({ userinfo: userinfo }); this.handleEdit(name + 'Edit', false); message.success('更新用户信息成功'); } else { message.error(data.errmsg); } }, err => { message.error(err.message); } ); }; changeUserinfo = e => { let dom = e.target; let name = dom.getAttribute('name'); let value = dom.value; this.setState({ _userinfo: { ...this.state._userinfo, [name]: value } }); }; changeRole = val => { let userinfo = this.state.userinfo; userinfo.role = val; this.setState({ _userinfo: userinfo }); this.updateUserinfo('role'); }; updatePassword = () => { let old_password = document.getElementById('old_password').value; let password = document.getElementById('password').value; let verify_pass = document.getElementById('verify_pass').value; if (password != verify_pass) { return message.error('两次输入的密码不一样'); } let params = { uid: this.state.userinfo.uid, password: password, old_password: old_password }; axios.post('/api/user/change_password', params).then( res => { let data = res.data; if (data.errcode === 0) { this.handleEdit('secureEdit', false); message.success('修改密码成功'); if (this.props.curUid === this.state.userinfo.uid) { location.reload(); } } else { message.error(data.errmsg); } }, err => { message.error(err.message); } ); }; render() { let ButtonGroup = Button.Group; let userNameEditHtml, emailEditHtml, secureEditHtml, roleEditHtml; const Option = Select.Option; let userinfo = this.state.userinfo; let _userinfo = this.state._userinfo; let roles = { admin: '管理员', member: '会员' }; let userType = ''; if (this.props.userType === 'third') { userType = false; } else if (this.props.userType === 'site') { userType = true; } else { userType = false; } // 用户名信息修改 if (this.state.usernameEdit === false) { userNameEditHtml = (