import React, { Component } from 'react'; import { SnackbarHOC, getImageUrl, WordsBtn } from 'educoder'; import { Form, Button, Input } from 'antd' import '../../courses/css/Courses.css' import './common.css' import axios from 'axios' const PHONE = 'PHONE' const EMAIL = 'EMAIL' const PASSWORD = 'PASSWORD' function regPhoneAndEmail(value){ if(value.indexOf("@")>-1){ // 加密邮箱 let beforeStr=value.split("@")[0]; let afterStr=value.split("@")[1]; return beforeStr.substr(0,3)+"**"+beforeStr.substr(beforeStr.length-1,1)+"@"+afterStr; }else{ // 加密手机号码 return value.substr(0,3)+"****"+value.substr(7,4); } } class AccountSecure extends Component { constructor (props) { super(props) this.state = { updating: '', secondsFlag:false, seconds:60 } } // 获取验证码倒计时 remainTime=()=>{ this.setState({ seconds: 60 }) this.timer = setInterval(() => { let { seconds } =this.state; let s = parseInt(seconds)-1; if(s > -1){ this.setState({ seconds:s }) }else{ this.setState({ secondsFlag:false }) clearInterval(this.timer); } },1000) } // 获取验证码 getCode=(index)=>{ let url=`/accounts/get_verification_code.json` let login = ''; let values=this.props.form.getFieldsValue(); if(index == 3){ //绑定手机号码 login=values.phone; let reg=/^1\d{10}$/; if(reg.test(login)==false){ this.props.showNotification(`请先输入正确的手机号码`); return; } }else if(index == 4){ // 绑定邮箱 login=values.email; let reg=/^[a-zA-Z0-9]+([.\-_\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/; if(reg.test(login)==false){ this.props.showNotification(`请先输入正确的邮箱地址`); return; } } let type = index; if(!login){ this.props.showNotification(`请先输入${index==3?"手机号码":"邮箱地址"}`); return; } axios.get((url),{params:{ login,type }}).then((result)=>{ if(result){ // 倒计时 this.setState({ secondsFlag:true }) this.remainTime(); } }).catch((error)=>{ console.log(error); }) } // 绑定手机 onPhoneSubmit = () => { this.props.form.validateFieldsAndScroll((err, values) => { if (!err) { let {login}=this.props.current_user; let reg=/^1\d{10}$/; if(reg.test(values.phone)){ let url=`/users/accounts/${login}/phone_bind.json` axios.post((url),{ phone:values.phone, code:values.phoneValidateCode }).then((result)=>{ if(result){ this.props.showNotification("手机号码绑定成功!"); this.setState({ updating:'' }) this.props.getBasicInfo(); } }).catch((error)=>{ console.log(error); }) }else{ this.props.showNotification("请输入有效的11位手机号码"); } } }) } // 绑定邮箱 onEmailSubmit = () => { this.props.form.validateFieldsAndScroll((err, values) => { if (!err) { let {login}=this.props.current_user; let reg=/^[a-zA-Z0-9]+([.\-_\\]*[a-zA-Z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/; if(reg.test(values.email)){ let url=`/users/accounts/${login}/email_bind.json` axios.post((url),{ email:values.email, code:values.emailValidateCode }).then((result)=>{ if(result){ this.props.showNotification("邮箱地址绑定成功!"); this.setState({ updating:'' }) this.props.getBasicInfo(); } }).catch((error)=>{ console.log(error); }) }else{ this.props.showNotification("请输入正确的邮箱地址"); } } }) } onPasswordSubmit = () => { this.props.form.validateFieldsAndScroll((err, values) => { if (!err) { if(values.p_old == values.p_new){ this.props.showNotification("新密码不能与旧密码相同!"); return; } if(values.p_again != values.p_new){ this.props.showNotification("两次输入的新密码必须一致!"); return; } let {login}=this.props.current_user; let url=`/users/accounts/${login}/password.json`; axios.put((url),{ old_password:values.p_old, password:values.p_new }).then((result)=>{ if(result){ this.props.showNotification("密码更新成功!"); this.setState({ updating:'' }) this.props.getBasicInfo(); } }).catch((error)=>{ console.log(error); }) } }) } //取消编辑 hideUpdating=()=>{ this.setState({ updating:"" }) } // 修改密码-判断规则 comfirmNewPassword=(rule,value,callback)=>{ const { form } = this.props; if (value != form.getFieldsValue().p_new) { callback("两次输入的新密码必须一致!"); }else { callback(); } } InputNewPassword=(rule,value,callback)=>{ const { form } = this.props; if (!value) { callback("请输入8-16位字符的新密码,区分大小写!"); } else if (value.length<8 || value.length>16) { callback("请输入8-16位字符的新密码,区分大小写!"); } else { callback(); } } render() { let {basicInfo}=this.props; const { getFieldDecorator } = this.props.form; const { updating,seconds,secondsFlag } = this.state return (
安全设置
手机
{ basicInfo && basicInfo.phone ? { regPhoneAndEmail(basicInfo.phone) } : 未绑定 } {basicInfo.phone ? '仅自己可见,可用手机号码登录EduCoder' : '绑定手机号码,将获得500金币的奖励哟~,手机号码仅自己可见~'}
{ updating != PHONE &&
this.setState({updating: PHONE})} >{basicInfo && basicInfo.phone ? "更换" : "立即绑定" }
}
{ updating == PHONE && {getFieldDecorator('phone', { rules: [{ // initialValue: this.state.cityDefaultValue, required: true, message: `请输入要${basicInfo.phone ? '更换' : '绑定'}的手机号码`, }], })( )} {getFieldDecorator('phoneValidateCode', { rules: [{ // initialValue: this.state.cityDefaultValue, required: true, message: '请输入手机获取的验证码', }], })( )}
}
邮箱
{ basicInfo && basicInfo.mail ? { regPhoneAndEmail(basicInfo.mail) } : 未绑定 } 邮箱账号仅自己可见,可用于邮箱账号登录EduCoder
{ updating != EMAIL &&
this.setState({updating: EMAIL})} >{basicInfo && basicInfo.mail ?"更换":"立即绑定"}
}
{ updating == EMAIL && {getFieldDecorator('email', { rules: [{ // initialValue: this.state.cityDefaultValue, required: true, message: basicInfo && basicInfo.mail ?'请输入要更换的新邮箱地址':'请输入邮箱地址', }], })( )} {getFieldDecorator('emailValidateCode', { rules: [{ // initialValue: this.state.cityDefaultValue, required: true, message: '请输入邮箱收到的验证码', }], })( )}
}
密码
********** 用于保护账户信息和登录安全
{ updating != PASSWORD &&
this.setState({updating: PASSWORD})} >修改
}
{ updating == PASSWORD && {getFieldDecorator('p_old', { rules: [{ // initialValue: this.state.cityDefaultValue, required: true, message: '请设置8~16位密码,区分大小写', }], })( )} {getFieldDecorator('p_new', { rules: [{ validator:this.InputNewPassword }], })( )} {getFieldDecorator('p_again', { rules: [{ // initialValue: this.state.cityDefaultValue, required: true, message: '请再次输入新密码', },{ validator:this.comfirmNewPassword }], })( )}
}
* 我们确保你所提供的信息均处于严格保密状态,不会泄露
); } } const WrappedAccountSecure = Form.create({ name: 'AccountSecure' })(AccountSecure); export default WrappedAccountSecure;