/** * 带OTP的注册页面 * @邠心vbe on 2023/02/06 */ import React from 'react'; import { View, ScrollView, StyleSheet, TextInput, Pressable, Image, Linking } from 'react-native'; import apiUser from '../../api/apiUser'; import Button from '../../components/Button'; import { PageList } from '../Router'; import Dialog from '../../components/Dialog'; import app from '../../../app.json'; import Dropdown from '../../components/Dropdown'; import ImagePicker from 'react-native-image-crop-picker'; import apiUpload from '../../api/apiUpload'; import { CountryDropCode, CountryDropNum, GetCountryList } from '../../components/CountryIcon'; import StrengthView from './StrengthView'; import CheckBox from '../../components/CheckBox'; import { UploadThemes } from '../../components/ThemesConfig'; import TextView from '../../components/TextView'; import utils from '../../utils/utils'; const options = { width: 300, height: 200, cropping: true, multiple: false, mediaType: 'photo', writeTempFile: false, compressImageQuality: 0.8, compressImageMaxWidth: 720, compressImageMaxHeight: 1280, ...UploadThemes } export default class RegisterV4 extends React.Component { constructor(props) { super(props); this.StrengthView = StrengthView.V4 this.state = { agree: false, agree2: false, strength: 0, countryNum: '65', countryCode: "SG", userInfo: { email: "", verificationCode: "" }, countryList: [], wrongCount: 0, params: {...this.props.route.params}, email: '', password: '', fleetCompanyId: '', sendMinutes: 0, isFleetDriver: false, pdvImages: ['', ''], companyList: [] }; this.canChangeCalling = true; } componentDidMount() { //console.log(this.state.params); if (this.state.params.isFleetUser) { this.setState({ isFleetDriver: true }) this.props.navigation.setOptions({ headerTitle: $t('route.driverRegister') }) } this.getCountryList(); this.getCompanyList(); } applyStrength(text) { this.StrengthView.apply(text, strength => { if (this.state.strength != strength) { this.setState({ password: text, strength: strength }); } else { this.setState({ password: text }); } }); } changeInfo(key, value) { var info = this.state.userInfo; info[key] = value; this.setState({ 'userInfo': info }); } getCountryList() { GetCountryList(list => { this.setState({ countryList: list }) }) } getCompanyList() { apiUser.getConmpany().then(res => { if (res.data) { this.setState({ companyList: res.data }) } }).catch(err => [ toastShort(err) ]) } sendVerification() { var info = this.state.userInfo; if (!info.email) { toastShort($t('sign.plsInputEmail')); return; } if (!utils.isValidEmail(info.email)) { toastShort($t('sign.errEmailFormat')); return; } Dialog.showProgressDialog() apiUser.sendVerificationCodeV2({email: info.email, type: "register"}).then(res => { Dialog.dismissLoading() //this.state.sendMinutes = 60; this.state.sendMinutes = res.data?.resendTime ?? 60; toastShort($t('sign.sendOTPSuccess')); this.contdownTime(); }).catch(err => { Dialog.dismissLoading() toastShort(err); }); } contdownTime() { if (this.state.sendMinutes > 0) { this.setState({ sendMinutes: this.state.sendMinutes - 1 }) setTimeout(() => { this.contdownTime(); }, 1000); } } onRegister() { //console.log('sign up', this.state); var info = this.state.userInfo; if (!info.nickName) { toastShort($t('sign.plsInputDiaplayName')); return; } if (!info.email) { toastShort($t('sign.plsInputEmail')); return; } if (!utils.isValidEmail(info.email)) { toastShort($t('sign.errEmailFormat')); return; } if (!info.verificationCode) { toastShort($t('sign.plsInputOTP')); return; } if (!info.phone) { toastShort($t('sign.plsInputContactNo')); return; } if (!/^\d{6,15}$/.test(info.phone)) { toastShort($t('sign.errContactNoFormat')); return; } if (!this.state.password) { toastShort($t('sign.plsInputPassword')); return; } if (this.state.strength < this.StrengthView.maxStrength) { toastShort($t('sign.errPasswordStrong')); return; } if (!info.password) { toastShort($t('sign.plsInputPassword2')); return; } if (info.password != this.state.password) { toastShort($t('sign.errPasswordConfirm')); if (this.state.wrongCount < 3) { this.setState({ wrongCount: this.state.wrongCount + 1 }) } return; } if (this.state.isFleetDriver) { if (!info.pdvLicence) { toastShort($t('sign.plsInputPDVLicence')); return; } if (this.state.pdvImages[0] == '' || this.state.pdvImages[1] == '') { toastShort($t('sign.plsUploadLicencePhotos')); return; } } let param = Object.assign({}, info); //param.phone = this.state.countryNum + info.phone param.callingCode = this.state.countryNum; param.countryCode = this.state.countryCode; if (this.state.isFleetDriver) { param.userType = 'Driver'; param.pdvLicencePictures = this.state.pdvImages; param.fleetCompanyId = this.state.fleetCompanyId; } else { param.userType = 'Public'; } param.otp = param.verificationCode; console.log('params', param); Dialog.showProgressDialog(); apiUser.register(param).then(res => { Dialog.dismissLoading(); //toastShort('Sign up successfully!'); if (isIOS) { setTimeout(() => { this.showSuccessDialog(); }, 600); } else { this.showSuccessDialog(); } }).catch(err => { toastShort(err); Dialog.dismissLoading(); }); } showSuccessDialog() { Dialog.showResultDialog($t('sign.signUpSuccess'), $t('nav.ok'), () => { this.backToLogin(); } ) } getBackTopPosition() { return isIOS ? statusHeight - 16 : 8; } backToLogin() { if (this.state.params.actionLogin) { goBack() startPage(PageList.login); } else { goBack(); } } uploadImage(index) { ImagePicker.openPicker({ ...options, cropperToolbarTitle: $t('common.cropperTitle') }).then(image => { if (image.path) { apiUpload.uploadImage(image.path, image.mime, 'PDVL').then(res => { if (res.success && res.data.picturePath) { let imageUrl = this.state.pdvImages; imageUrl[index] = res.data.picturePath; this.setState({ pdvImages: imageUrl }); toastShort($t('common.uploadSuccess')); } else { toastShort($t('common.updateFailed')); } }).catch(err => { toastShort(err); }); } }).catch(err1 => { //console.log(err1); }); } changeAgree(ag) { this.setState({ agree: ag }) } changeAgree2(ag) { this.setState({ agree2: ag }) } changeCountry(value, index) { this.setState({ countryCode: value }) if (this.canChangeCalling) { const country = this.state.countryList[index] this.changeCalling(country.countryNum, -1); } } changeCalling(value, index) { this.setState({ countryNum: value }) if (index >= 0) { this.canChangeCalling = false; } } toPrivayPolicy() { Linking.openURL(app.storeUrl.privacyPolicyUrl) } toUserTerms() { Linking.openURL(app.storeUrl.termsUseUrl) } render() { return ( {/* this.changeTab(false)} >Public Users this.changeTab(true)} >Fleet/PH Drivers */} {$t('sign.labelDisplayName')} this.changeInfo('nickName', v)} /> {$t('sign.labelEmail')} this.changeInfo('email', v)} /> {$t('sign.labelValidateEmail')} this.changeInfo('verificationCode', v)} />