import React, { Component } from 'react'; import Cropper from 'react-cropper'; import 'cropperjs/dist/cropper.css'; import './index.less'; import FileUpload from '@src/components/FileUpload'; import Assets from '@src/components/Assets'; import scale from '@src/services/Scale'; import { asyncSMessage } from '@src/services/AsyncTools'; import { SelectInput, VerificationInput, Input } from '../Login'; import { MobileArea } from '../../../Constant'; import Invite from '../Invite'; import Modal from '../Modal'; import { Common } from '../../stores/common'; import { User } from '../../stores/user'; import { My } from '../../stores/my'; export class BindPhone extends Component { constructor(props) { super(props); this.props.data = this.props.data || {}; this.state = Object.assign({ step: 0, data: {} }, this.initState(this.props)); this.stepProp = { 0: { title: '绑定手机', onConfirm: props.onConfirm, }, 1: { title: '绑定手机', onConfirm: () => { this.submit(); }, onCancel: props.onCancel, confirmText: '提交', }, }; } initState(props) { if (!props.show || this.props.show) return {}; const data = Object.assign({}, props.data); if (!data.area) data.area = MobileArea[0].value; return { step: props.data.mobile ? 0 : 1, data }; } componentWillReceiveProps(nextProps) { this.setState(this.initState(nextProps)); } onNext() { this.setState({ step: 1 }); } changeData(field, value) { let { data } = this.state; data = data || {}; data[field] = value; this.setState({ data, error: null }); } validMobile() { const { data } = this.state; const { area, mobile } = data; if (!area || !mobile) return; this.validNumber += 1; const number = this.validNumber; User.validMobile(area, mobile) .then(result => { if (number !== this.validNumber) return Promise.resolve(); return result ? Promise.resolve() : Promise.reject(new Error('该手机已绑定其他账号,请更换手机号码')); }) .catch(err => { this.setState({ mobileError: err.message }); }); } sendValid() { const { data, error } = this.state; const { area, mobile } = data; if (!area || !mobile || error) return Promise.reject(); return Common.sendSms(area, mobile) .then(result => { if (result) { asyncSMessage('发送成功'); this.setState({ error: '', validError: '' }); } else { throw new Error('发送失败'); } }) .catch(err => { this.setState({ error: err.message }); throw err; }); } submit() { const { data, error } = this.state; if (!data.mobile || !data.area || error) return; const { area, mobile } = data; My.bindMobile(area, mobile) .then(() => { asyncSMessage('操作成功'); this.setState({ step: 0 }); User.infoHandle(Object.assign(this.props.data, { area, mobile })); this.props.onConfirm(); }) .catch(e => { this.setState({ error: e.message }); }); } render() { const { show } = this.props; const { step = 0 } = this.state; return (
{this[`renderStep${step}`]()}
); } renderStep0() { const { data } = this.state; return (
已绑定手机 {data.mobile} this.onNext()}>修改
); } renderStep1() { return (
手机号
{ this.changeData('area', value); this.validMobile(); }} onChange={e => { this.changeData('mobile', e.target.value); this.validMobile(); }} /> { return this.sendValid(); }} onChange={e => { this.changeData('mobileVerifyCode', e.target.value); }} />
); } } export class BindEmail extends Component { constructor(props) { super(props); this.props.data = this.props.data || {}; this.state = Object.assign({ step: 0, data: {} }, this.initState(this.props)); this.stepProp = { 0: { title: '绑定邮箱', onConfirm: props.onConfirm, }, 1: { title: '绑定邮箱', onConfirm: () => { this.submit(); }, onCancel: props.onCancel, confirmText: '提交', }, }; } initState(props) { if (!props.show || this.props.show) return {}; return { step: props.data.email ? 0 : 1, data: Object.assign({}, props.data) }; } componentWillReceiveProps(nextProps) { this.setState(this.initState(nextProps)); } onNext() { this.setState({ step: 1 }); } changeData(field, value) { let { data } = this.state; data = data || {}; data[field] = value; this.setState({ data, error: null }); } submit() { const { data, error } = this.state; if (!data.email || error) return; const { email } = data; My.bindEmail(email) .then(() => { asyncSMessage('操作成功'); this.setState({ step: 0 }); User.infoHandle(Object.assign(this.props.data, { email })); this.props.onConfirm(); }) .catch(e => { this.setState({ error: e.message }); }); } render() { const { show } = this.props; const { step = 0 } = this.state; return (
{this[`renderStep${step}`]()}
); } renderStep0() { const { data } = this.state; return (
已绑定邮箱 {data.email} this.onNext()}>修改
); } renderStep1() { return (
邮箱地址
{ this.changeData('email', e.target.value); }} />
); } } export class EditInfo extends Component { constructor(props) { super(props); this.props.data = this.props.data || {}; this.state = Object.assign({ data: {} }, this.initState(this.props)); } initState(props) { if (props.image && this.waitImage) { const { state } = this; Common.upload(props.image).then(result => { const { data } = this.state; data.avatar = result.url; this.setState({ data, uploading: false }); }).catch((e) => { this.setState({ imageError: e.message }); }); state.uploading = true; this.waitImage = false; return state; } if (!props.show || this.props.show) return {}; return { data: Object.assign({}, props.data) }; } componentWillReceiveProps(nextProps) { this.setState(this.initState(nextProps)); } changeData(field, value) { let { data } = this.state; data = data || {}; data[field] = value; this.setState({ data, error: null }); } submit() { const { data, error } = this.state; if (!data.nickname || error) return; const { nickname, avatar } = data; My.editInfo({ nickname, avatar }) .then(() => { asyncSMessage('操作成功'); User.infoHandle(Object.assign(this.props.data, { nickname, avatar })); this.props.onConfirm(); }) .catch(e => { this.setState({ error: e.message }); }); } render() { const { show, onCancel, onSelectImage } = this.props; return ( { this.submit(); }} >
昵称
{ this.changeData('nickname', e.target.value); }} />
头像
{ this.waitImage = true; onSelectImage(file); return Promise.reject(); }} /> {this.state.imageError || ''}
); } } export class RealAuth extends Component { constructor(props) { super(props); this.state = { data: {} }; } render() { const { show, onConfirm } = this.props; return (
完成实名认证即可领取:
6个月VIP权限 和 5折机经优惠劵。
扫码关注公众号,点击“我的-实名认证”
); } } export class EditAvatar extends Component { constructor(props) { super(props); this.state = Object.assign({ data: {} }, this.initState(this.props)); } initState(props) { if (props.image) this.loadImage(props.image); if (!props.show || this.props.show) return {}; return { data: {} }; } componentWillReceiveProps(nextProps) { this.setState(this.initState(nextProps)); } loadImage(file) { this.defaultZoomValue = 1; if (window.FileReader) { const reader = new FileReader(); reader.readAsDataURL(file); // 渲染文件 reader.onload = (arg) => { this.setState({ image: arg.target.result, fileName: file.name, zoomValue: 1 }); }; } else { const img = new Image(); img.onload = function () { const canvas = document.createElement('canvas'); canvas.height = img.height; canvas.width = img.width; const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0); this.setState({ image: canvas.toDataURL() }); }; img.src = '1.gif'; } } computerZoom() { const data = this.cropRef.getImageData(); this.defaultZoomValue = 200 / parseFloat(Math.max(data.naturalWidth, data.naturalHeight)); } crop() { // image in dataUrl // console.log(this.cropRef.getCroppedCanvas().toDataURL()) // const canvas = this.cropRef.getCroppedCanvas(); // const avatar = scale(this.props.crop, canvas, 'png-src'); // let scaleCanvas = this.cropRef.getCroppedCanvas(this.props.crop) // this.setState({ avatar }); } select() { const { fileName } = this.state; const { onConfirm } = this.props; const canvas = this.cropRef.getCroppedCanvas(); const scaleCanvas = scale(this.props.crop, canvas); // let scaleCanvas = this.cropRef.getCroppedCanvas(this.props.crop) scaleCanvas.toBlob((blob) => { const file = new File([blob], fileName); onConfirm(file); }); } render() { const { show, onCancel } = this.props; const { image } = this.state; return ( { this.select(); }} onCancel={onCancel} >
{ this.cropRef = ref; }} src={image} ready={() => { this.computerZoom(); }} style={{ height: 360, width: 360 }} // Cropper.js options aspectRatio={1} viewMode={0} // autoCropArea={0.8} preview=".img-preview" // dragMode='move' guides={false} movable={false} rotatable={false} scalable={false} // zoom={(value) => { // console.log('zoom', value); // const zoomValue = value * this.defaultZoomValue / 50; // this.cropRef.zoomTo(zoomValue); // }} cropmove={() => { this.crop(); }} toggleDragModeOnDblclick={false} cropBoxResizable />
头像预览
); } } export class InviteModal extends Component { constructor(props) { super(props); this.state = { data: {} }; } render() { const { show, onClose, data } = this.props; return (
每邀请一位小伙伴加入“千行GMAT”, 您的VIP权限会延长7天,可累加 无上限!赶紧行动吧~
); } }