2017-07-17 11:01:05 +08:00
|
|
|
|
import React, { Component } from 'react'
|
2017-08-10 21:20:23 +08:00
|
|
|
|
import { Row, Col, Input, Button, Select, message, Upload, Icon } from 'antd'
|
2017-07-17 16:24:00 +08:00
|
|
|
|
import axios from 'axios';
|
2017-07-18 17:15:29 +08:00
|
|
|
|
import {formatTime} from '../../common.js'
|
2017-07-18 15:35:32 +08:00
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
|
2017-07-17 11:01:05 +08:00
|
|
|
|
class Profile extends Component {
|
|
|
|
|
|
2017-07-18 15:35:32 +08:00
|
|
|
|
static propTypes = {
|
|
|
|
|
match: PropTypes.object
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 11:01:05 +08:00
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props)
|
|
|
|
|
this.state = {
|
|
|
|
|
usernameEdit: false,
|
|
|
|
|
emailEdit: false,
|
|
|
|
|
secureEdit: false,
|
2017-07-18 15:35:32 +08:00
|
|
|
|
roleEdit: false,
|
|
|
|
|
userinfo: {
|
|
|
|
|
|
|
|
|
|
}
|
2017-07-17 11:01:05 +08:00
|
|
|
|
}
|
2017-07-27 19:08:40 +08:00
|
|
|
|
|
2017-07-18 15:35:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount(){
|
|
|
|
|
const uid = this.props.match.params.uid;
|
|
|
|
|
this.getUserInfo(uid)
|
2017-07-17 11:01:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 15:35:32 +08:00
|
|
|
|
handleEdit = (key, val) => {
|
2017-07-17 11:01:05 +08:00
|
|
|
|
var s = {};
|
2017-07-18 15:35:32 +08:00
|
|
|
|
s[key] = val;
|
2017-07-17 11:01:05 +08:00
|
|
|
|
this.setState(s)
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 16:24:00 +08:00
|
|
|
|
getUserInfo = (id) => {
|
2017-07-18 15:35:32 +08:00
|
|
|
|
var _this = this;
|
2017-08-11 10:25:19 +08:00
|
|
|
|
axios.get('/api/user/find?id=' + id).then((res) => {
|
2017-07-18 15:35:32 +08:00
|
|
|
|
_this.setState({
|
|
|
|
|
userinfo: res.data.data,
|
|
|
|
|
_userinfo: res.data.data
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateUserinfo = (name) =>{
|
|
|
|
|
var state = this.state;
|
|
|
|
|
let value = this.state._userinfo[name];
|
|
|
|
|
let params = {uid: state.userinfo.uid}
|
|
|
|
|
params[name] = value;
|
2017-07-27 19:08:40 +08:00
|
|
|
|
|
2017-08-11 10:25:19 +08:00
|
|
|
|
axios.post('/api/user/update', params).then( (res)=>{
|
2017-07-18 15:35:32 +08:00
|
|
|
|
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)
|
2017-07-18 19:47:38 +08:00
|
|
|
|
message.success('更新用户信息成功');
|
2017-07-18 15:35:32 +08:00
|
|
|
|
}else{
|
2017-07-18 19:47:38 +08:00
|
|
|
|
message.error(data.errmsg)
|
2017-07-18 15:35:32 +08:00
|
|
|
|
}
|
2017-07-27 19:08:40 +08:00
|
|
|
|
|
2017-07-18 15:35:32 +08:00
|
|
|
|
}, (err) => {
|
2017-07-18 19:47:38 +08:00
|
|
|
|
message.error(err.message)
|
2017-07-18 15:35:32 +08:00
|
|
|
|
} )
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
changeUserinfo = (e) =>{
|
|
|
|
|
let dom = e.target;
|
|
|
|
|
let name = dom.getAttribute("name");
|
|
|
|
|
let value = dom.value;
|
|
|
|
|
let userinfo = this.state._userinfo;
|
|
|
|
|
userinfo[name] = value;
|
|
|
|
|
this.setState({
|
|
|
|
|
_userinfo: userinfo
|
2017-07-17 16:24:00 +08:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 15:35:32 +08:00
|
|
|
|
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){
|
2017-07-18 19:47:38 +08:00
|
|
|
|
return message.error('两次输入的密码不一样');
|
2017-07-18 15:35:32 +08:00
|
|
|
|
}
|
|
|
|
|
let params = {
|
|
|
|
|
uid: this.state.userinfo.uid,
|
|
|
|
|
password: password,
|
|
|
|
|
old_password: old_password
|
|
|
|
|
}
|
2017-07-27 19:08:40 +08:00
|
|
|
|
|
|
|
|
|
|
2017-08-11 10:25:19 +08:00
|
|
|
|
axios.post('/api/user/change_password', params).then( (res)=>{
|
2017-07-18 15:35:32 +08:00
|
|
|
|
let data = res.data;
|
|
|
|
|
if(data.errcode === 0){
|
|
|
|
|
this.handleEdit('secureEdit', false)
|
2017-07-18 19:47:38 +08:00
|
|
|
|
message.success('修改密码成功');
|
2017-07-18 15:35:32 +08:00
|
|
|
|
}else{
|
2017-07-18 19:47:38 +08:00
|
|
|
|
message.error(data.errmsg)
|
2017-07-18 15:35:32 +08:00
|
|
|
|
}
|
2017-07-27 19:08:40 +08:00
|
|
|
|
|
2017-07-18 15:35:32 +08:00
|
|
|
|
}, (err) => {
|
2017-07-18 19:47:38 +08:00
|
|
|
|
message.error(err.message)
|
2017-07-18 15:35:32 +08:00
|
|
|
|
} )
|
2017-07-27 19:08:40 +08:00
|
|
|
|
|
2017-07-18 15:35:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 11:01:05 +08:00
|
|
|
|
render() {
|
|
|
|
|
let ButtonGroup = Button.Group;
|
2017-07-18 15:35:32 +08:00
|
|
|
|
let userNameEditHtml, emailEditHtml, secureEditHtml, roleEditHtml;
|
2017-07-17 11:01:05 +08:00
|
|
|
|
const Option = Select.Option;
|
2017-07-18 15:35:32 +08:00
|
|
|
|
let userinfo = this.state.userinfo;
|
|
|
|
|
let _userinfo = this.state._userinfo;
|
|
|
|
|
let roles = { admin: '管理员', member: '会员' }
|
|
|
|
|
if (this.state.usernameEdit === false) {
|
2017-07-17 11:01:05 +08:00
|
|
|
|
userNameEditHtml = <div >
|
2017-07-18 15:35:32 +08:00
|
|
|
|
<span className="text">{userinfo.username}</span>
|
2017-07-26 16:37:51 +08:00
|
|
|
|
{/*<span className="text-button" onClick={() => { this.handleEdit('usernameEdit', true) }}><Icon type="edit" />修改</span>*/}
|
2017-07-27 15:50:04 +08:00
|
|
|
|
<Button icon="edit" onClick={() => { this.handleEdit('usernameEdit', true) }}>修改</Button>
|
2017-07-17 11:01:05 +08:00
|
|
|
|
</div>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
} else {
|
2017-07-17 11:01:05 +08:00
|
|
|
|
userNameEditHtml = <div>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
<Input value={_userinfo.username} name="username" onChange={this.changeUserinfo} placeholder="用户名" />
|
2017-07-17 11:01:05 +08:00
|
|
|
|
<ButtonGroup className="edit-buttons" >
|
2017-07-27 19:08:40 +08:00
|
|
|
|
<Button className="edit-button" onClick={() => { this.handleEdit('usernameEdit', false) }} >取消</Button>
|
|
|
|
|
<Button className="edit-button" onClick={ () => { this.updateUserinfo('username')} } type="primary">确定</Button>
|
2017-07-17 11:01:05 +08:00
|
|
|
|
</ButtonGroup>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 15:35:32 +08:00
|
|
|
|
if (this.state.emailEdit === false) {
|
2017-07-17 11:01:05 +08:00
|
|
|
|
emailEditHtml = <div >
|
2017-07-18 15:35:32 +08:00
|
|
|
|
<span className="text">{userinfo.email}</span>
|
2017-07-26 16:37:51 +08:00
|
|
|
|
{/*<span className="text-button" onClick={() => { this.handleEdit('emailEdit', true) }} ><Icon type="edit" />修改</span>*/}
|
2017-07-27 15:50:04 +08:00
|
|
|
|
<Button icon="edit" onClick={() => { this.handleEdit('emailEdit', true) }}>修改</Button>
|
2017-07-17 11:01:05 +08:00
|
|
|
|
</div>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
} else {
|
2017-07-17 11:01:05 +08:00
|
|
|
|
emailEditHtml = <div>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
<Input placeholder="Email" value={_userinfo.email} name="email" onChange={this.changeUserinfo} />
|
2017-07-17 11:01:05 +08:00
|
|
|
|
<ButtonGroup className="edit-buttons" >
|
2017-07-27 19:08:40 +08:00
|
|
|
|
<Button className="edit-button" onClick={() => { this.handleEdit('emailEdit', false) }} >取消</Button>
|
|
|
|
|
<Button className="edit-button" type="primary" onClick={ () => { this.updateUserinfo('email')} }>确定</Button>
|
2017-07-17 11:01:05 +08:00
|
|
|
|
</ButtonGroup>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 15:35:32 +08:00
|
|
|
|
if (this.state.roleEdit === false) {
|
2017-07-17 11:01:05 +08:00
|
|
|
|
roleEditHtml = <div>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
<span className="text">{roles[userinfo.role]}</span>
|
2017-07-26 16:37:51 +08:00
|
|
|
|
{/*<span className="text-button" onClick={() => { this.handleEdit('roleEdit', true) }} ><Icon type="edit" />修改</span>*/}
|
2017-07-27 15:50:04 +08:00
|
|
|
|
<Button icon="edit" onClick={() => { this.handleEdit('roleEdit', true) }}>修改</Button>
|
2017-07-17 11:01:05 +08:00
|
|
|
|
</div>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
} else {
|
|
|
|
|
roleEditHtml = <Select defaultValue={_userinfo.role} onChange={ this.changeRole} style={{ width: 150 }} >
|
2017-07-17 11:01:05 +08:00
|
|
|
|
<Option value="admin">管理员</Option>
|
|
|
|
|
<Option value="member">会员</Option>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
|
2017-07-17 11:01:05 +08:00
|
|
|
|
</Select>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
}
|
2017-07-17 11:01:05 +08:00
|
|
|
|
|
2017-07-18 15:35:32 +08:00
|
|
|
|
if (this.state.secureEdit === false) {
|
2017-07-27 15:50:04 +08:00
|
|
|
|
secureEditHtml = <Button icon="edit" onClick={() => { this.handleEdit('secureEdit', true) }}>修改</Button>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
} else {
|
2017-07-17 11:01:05 +08:00
|
|
|
|
secureEditHtml = <div>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
<Input style={{display: this.state.userinfo.role === 'admin' ? 'none': ''}} placeholder="旧的密码" type="password" name="old_password" id="old_password" />
|
|
|
|
|
<Input placeholder="新的密码" type="password" name="password" id="password" />
|
|
|
|
|
<Input placeholder="确认密码" type="password" name="verify_pass" id="verify_pass" />
|
2017-07-17 11:01:05 +08:00
|
|
|
|
<ButtonGroup className="edit-buttons" >
|
2017-07-27 19:08:40 +08:00
|
|
|
|
<Button className="edit-button" onClick={() => { this.handleEdit('secureEdit', false) }}>取消</Button>
|
|
|
|
|
<Button className="edit-button" onClick={this.updatePassword} type="primary">确定</Button>
|
2017-07-17 11:01:05 +08:00
|
|
|
|
</ButtonGroup>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return <div className="user-profile">
|
2017-07-18 17:15:29 +08:00
|
|
|
|
<Row className="user-item" type="flex" justify="start">
|
2017-08-10 21:20:23 +08:00
|
|
|
|
<Col span={24}><Avatar uid={userinfo.uid}>点击上传头像</Avatar></Col>
|
2017-07-18 17:15:29 +08:00
|
|
|
|
<Col span={4}>用户id</Col>
|
|
|
|
|
<Col span={12}>
|
|
|
|
|
{userinfo.uid}
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
<Row className="user-item" type="flex" justify="start">
|
2017-07-17 11:01:05 +08:00
|
|
|
|
<Col span={4}>用户名</Col>
|
|
|
|
|
<Col span={12}>
|
|
|
|
|
{userNameEditHtml}
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
<Row className="user-item" type="flex" justify="start">
|
2017-07-17 11:01:05 +08:00
|
|
|
|
<Col span={4}>Email</Col>
|
|
|
|
|
<Col span={12}>
|
|
|
|
|
{emailEditHtml}
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
2017-07-18 15:35:32 +08:00
|
|
|
|
<Row className="user-item" style={{display: this.state.userinfo.role === 'admin'? '': 'none'}} type="flex" justify="start">
|
2017-07-17 11:01:05 +08:00
|
|
|
|
<Col span={4}>角色</Col>
|
|
|
|
|
<Col span={12}>
|
|
|
|
|
{roleEditHtml}
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
2017-07-18 17:15:29 +08:00
|
|
|
|
<Row className="user-item" type="flex" justify="start">
|
|
|
|
|
<Col span={4}>创建账号时间</Col>
|
|
|
|
|
<Col span={12}>
|
|
|
|
|
{formatTime(userinfo.add_time)}
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
|
|
|
|
<Row className="user-item" type="flex" justify="start">
|
|
|
|
|
<Col span={4}>更新账号时间</Col>
|
|
|
|
|
<Col span={12}>
|
|
|
|
|
{formatTime(userinfo.up_time)}
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
|
|
|
|
|
2017-07-17 11:01:05 +08:00
|
|
|
|
<Row className="user-item" type="flex" justify="start">
|
2017-07-27 12:01:56 +08:00
|
|
|
|
<Col span={4}>密码</Col>
|
2017-07-17 11:01:05 +08:00
|
|
|
|
<Col span={12}>
|
|
|
|
|
{secureEditHtml}
|
|
|
|
|
</Col>
|
|
|
|
|
</Row>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-10 21:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Avatar extends Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
imageUrl:""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static propTypes = {
|
|
|
|
|
uid: PropTypes.number
|
|
|
|
|
}
|
|
|
|
|
uploadAvatar(basecode){
|
|
|
|
|
axios.post("/user/upload_avatar",{basecode: basecode}).then(()=>{
|
|
|
|
|
this.setState({ imageUrl: basecode })
|
|
|
|
|
}).catch((e)=>{
|
|
|
|
|
console.log(e);
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
handleChange(info){
|
|
|
|
|
if (info.file.status === 'done') {
|
|
|
|
|
// Get this url from response in real world.
|
|
|
|
|
getBase64(info.file.originFileObj, basecode=>{this.uploadAvatar(basecode)});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
let imageUrl = "";
|
|
|
|
|
if(this.props.uid && !this.state.imageUrl){
|
|
|
|
|
imageUrl = `/user/avatar?uid=${this.props.uid}`;
|
|
|
|
|
}else{
|
|
|
|
|
imageUrl = this.state.imageUrl;
|
|
|
|
|
}
|
|
|
|
|
return <div className="avatar-box">
|
|
|
|
|
<Upload
|
|
|
|
|
className="avatar-uploader"
|
|
|
|
|
name="basecode"
|
|
|
|
|
showUploadList={true}
|
|
|
|
|
action="/user/upload_avatar"
|
|
|
|
|
beforeUpload={beforeUpload}
|
|
|
|
|
onChange={this.handleChange.bind(this)} >
|
|
|
|
|
{
|
|
|
|
|
imageUrl ?
|
|
|
|
|
<img src={imageUrl} alt="" className="avatar" /> :
|
|
|
|
|
<Icon type="plus" className="avatar-uploader-trigger" />
|
|
|
|
|
}
|
|
|
|
|
</Upload>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function beforeUpload(file) {
|
|
|
|
|
const isJPG = file.type === 'image/jpeg';
|
|
|
|
|
const isPNG = file.type === 'image/png';
|
|
|
|
|
if (!isJPG && !isPNG) {
|
|
|
|
|
message.error('图片的格式只能为 jpg、png!');
|
|
|
|
|
}
|
|
|
|
|
const isLt2M = file.size / 1024 / 1024 < 2;
|
|
|
|
|
if (!isLt2M) {
|
|
|
|
|
message.error('图片必须小于 200kb!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (isPNG||isJPG) && isLt2M;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getBase64(img, callback) {
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.addEventListener('load', () => callback(reader.result));
|
|
|
|
|
reader.readAsDataURL(img);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-07-17 11:01:05 +08:00
|
|
|
|
export default Profile
|