import React, { Component } from 'react' import { Row, Col, Input, Button, Select, message, Upload} from 'antd' import axios from 'axios'; import {formatTime} from '../../common.js' import PropTypes from 'prop-types' import { Link } from 'react-router-dom' import { connect } from 'react-redux' @connect(state=>{ return { curUid: state.user.uid, userType: state.user.type, curRole: state.user.role } },{ }) class Profile extends Component { static propTypes = { match: PropTypes.object, curUid: PropTypes.number, userType: PropTypes.string, curRole: PropTypes.string } constructor(props) { super(props) this.state = { usernameEdit: false, emailEdit: false, secureEdit: false, roleEdit: false, userinfo: { } } } componentDidMount(){ const uid = this.props.match.params.uid; this.getUserInfo(uid) } handleEdit = (key, val) => { var s = {}; s[key] = val; this.setState(s) } getUserInfo = (id) => { var _this = this; axios.get('/api/user/find?id=' + id).then((res) => { _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; 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; let userinfo = this.state._userinfo; userinfo[name] = value; this.setState({ _userinfo: userinfo }) } 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('修改密码成功'); }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) { let btn = ""; if(userType){ if(userinfo.uid === this.props.curUid){//本人 btn = ; }else{ if(this.props.curRole === "admin"){ btn = ; }else{ btn = ""; } } }else{ // if(userinfo.uid === this.props.curUid){//本人 // btn = ; // }else{ btn = ""; // } } userNameEditHtml =
{userinfo.username}   {/* { this.handleEdit('usernameEdit', true) }}>修改*/} { btn }
} else { userNameEditHtml =
} if (this.state.emailEdit === false) { let btn = ""; if(userType){ if(userinfo.uid === this.props.curUid){//本人 btn = if(userinfo.role === 'admin'){ btn = ""; } }else{ if(this.props.curRole === "admin"){ btn = }else{ btn = ""; } } }else{ if(userinfo.uid === this.props.curUid){//本人 // btn = }else{ btn = ""; } } emailEditHtml =
{userinfo.email}   {/* { this.handleEdit('emailEdit', true) }} >修改*/} {btn}
} else { emailEditHtml =
} if (this.state.roleEdit === false) { let btn = ""; roleEditHtml =
{roles[userinfo.role]}   {btn}
} else { roleEditHtml = } if (this.state.secureEdit === false) { let btn = ""; if(userType){ btn = } secureEditHtml = btn; } else { secureEditHtml =
} let bacToPer = ""; if(userinfo.uid != this.props.curUid){ bacToPer =
} return
{ bacToPer }
{userinfo.uid === this.props.curUid?点击上传头像:
}
{/*
*/}
用户id {userinfo.uid}
用户名 {userNameEditHtml}
Email {emailEditHtml}
角色 {roleEditHtml}
创建账号时间 {formatTime(userinfo.add_time)}
更新账号时间 {formatTime(userinfo.up_time)}
{(userType)?
密码 {secureEditHtml}
:""}
} } class AvatarUpload extends Component { constructor(props) { super(props); this.state = { imageUrl: "" } } static propTypes = { uid: PropTypes.number } uploadAvatar(basecode){ axios.post("/api/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 = this.state.imageUrl?this.state.imageUrl:`/api/user/avatar?uid=${this.props.uid}`; // console.log(this.props.uid); return
{/**/} 点击头像更换

只支持jpg、png格式且大小不超过200kb的图片
} } 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 < 0.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); } export default Profile