import React, { Component } from 'react'; import Cropper from 'react-cropper'; import 'cropperjs/dist/cropper.css'; import './index.less'; import { Checkbox, Icon } from 'antd'; 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, TextbookFeedbackTarget, TextbookSubject } 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'; import Select from '../Select'; import { formatDate, getMap } from '../../../../src/services/Tools'; const TextbookFeedbackTargetMap = getMap(TextbookFeedbackTarget, 'value', 'tip'); 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天,可累加 无上限!赶紧行动吧~
); } } // 模考选择下载 export class DownloadModal extends Component { constructor(props) { super(props); this.state = { checkMap: {} }; } onConfirm() { const { onConfirm, data } = this.props; if (onConfirm) onConfirm(); const { checkMap } = this.state; Object.keys(checkMap).forEach(key => { if (!checkMap[key]) return; const link = data[`${key}`]; if (link) { openLink(link); } }); this.setState({ checkList: [] }); } render() { const { show, data = {}, onCancel } = this.props; const { checkMap = {} } = this.state; const quantVersion = data.quantVersion || 0; const irVersion = data.irVersion || 0; const rcVersion = data.rcVersion || 0; return ( this.onConfirm()} onCancel={onCancel} >
请选择下载科目
{quantVersion > 0 && (
{ checkMap.quant = !checkMap.quant; this.setState({ checkMap }); }} /> 数学 (版本{quantVersion} 最后更新:{formatDate(data.quantTime, 'YYYY-MM-DD HH:mm:ss')})
)} {irVersion > 0 && (
{ checkMap.ir = !checkMap.ir; this.setState({ checkMap }); }} /> 逻辑 (版本{irVersion} 最后更新:{formatDate(data.irTime, 'YYYY-MM-DD HH:mm:ss')})
)} {rcVersion > 0 && (
{ checkMap.rc = !checkMap.rc; this.setState({ checkMap }); }} /> 阅读 (版本{rcVersion} 最后更新:{formatDate(data.rcTime, 'YYYY-MM-DD HH:mm:ss')})
)}
); } } // 模考开通确认 export class OpenConfirmModal extends Component { render() { const { show, onConfirm, onCancel, data = {} } = this.props; return (
您正在开通「{data.title}」。
模考有效期至:{data.endTime && formatDate(data.endTime, 'YYYY-MM-DD')}
); } } // 模考重置确认 export class RestartConfirmModal extends Component { render() { const { show, onConfirm, onCancel } = this.props; return (
重置后,可进行新一轮的模考。
本轮模考的成绩和报告可在本页面或「我的报告」查看。
只有 1 次重置机会。
); } } export class FeedbackErrorDataModal extends Component { constructor(props) { super(props); this.state = { data: { position: ['', '', ''] } }; } componentWillReceiveProps(nextProps) { if (nextProps.defaultData) { this.setState({ data: Object.assign({}, nextProps.defaultData, this.state.data) }); } } onConfirm() { const { onConfirm } = this.props; const { data } = this.state; if (!data.content || !data.originContent) return; My.addFeedbackErrorData(data.dataId, data.title, data.position.join(','), data.originContent, data.content).then( () => { if (onConfirm) onConfirm(); this.setState({ data: { position: ['', '', ''] } }); }, ); } onCancel() { const { onCancel } = this.props; if (onCancel) onCancel(); this.setState({ data: { position: [] } }); } render() { const { show } = this.props; const { data } = this.state; return ( this.onConfirm()} onCancel={() => this.onCancel()} >
定位: { data.position[0] = e.target.value; this.setState({ data }); }} /> { data.position[1] = e.target.value; this.setState({ data }); }} /> , 题号 { data.position[2] = e.target.value; this.setState({ data }); }} />
错误内容是: