Register.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /**
  2. * 注册页面
  3. * @邠心vbe on 2021/03/23
  4. */
  5. import React from 'react';
  6. import { View, Text, ScrollView, StyleSheet, TextInput, Pressable, Image } from 'react-native';
  7. import { BackIcon } from '../../components/Toolbar';
  8. import apiUser from '../../api/apiUser';
  9. import Button from '../../components/Button';
  10. import { PageList } from '../Router';
  11. import Dialog from '../../components/Dialog';
  12. import Modal from 'react-native-modal';
  13. import { RegisterDialog } from '../charge/InfoDialog';
  14. import Dropdown from '../../components/Dropdown';
  15. import ImagePicker from 'react-native-image-crop-picker';
  16. import apiUpload from '../../api/apiUpload';
  17. import CheckBox from '@react-native-community/checkbox';
  18. import { ModalProps } from '../../components/BottomModal';
  19. import { CountryDropNum, GetCountryList } from '../../components/CountryIcon';
  20. import StrengthView from './StrengthView';
  21. import { UploadThemes } from '../../components/MyRefreshControl';
  22. import utils from '../../utils/utils';
  23. const options = {
  24. width: 300,
  25. height: 200,
  26. cropping: true,
  27. multiple: false,
  28. mediaType: 'photo',
  29. writeTempFile: false,
  30. compressImageQuality: 0.8,
  31. compressImageMaxWidth: 720,
  32. compressImageMaxHeight: 1280,
  33. ...UploadThemes
  34. }
  35. export default class Register extends React.Component {
  36. constructor(props) {
  37. super(props);
  38. this.state = {
  39. agree: true,
  40. strength: 0,
  41. countryNum: '65',
  42. countryShow: false,
  43. userInfo: {},
  44. countryNums: [],
  45. wrongCount: 0,
  46. params: {...this.props.route.params},
  47. email: '',
  48. password: '',
  49. fleetCompanyId: '',
  50. visible: false,
  51. isFleetDriver: false,
  52. pdvImages: ['', ''],
  53. companyList: []
  54. };
  55. }
  56. componentDidMount() {
  57. //console.log(this.state.params);
  58. this.getCountryList();
  59. this.getCompanyList();
  60. }
  61. applyStrength(text) {
  62. StrengthView.V1.apply(text, strength => {
  63. if (this.state.strength != strength) {
  64. this.setState({
  65. password: text,
  66. strength: strength
  67. });
  68. } else {
  69. this.setState({
  70. password: text
  71. });
  72. }
  73. });
  74. }
  75. changeInfo(key, value) {
  76. var info = this.state.userInfo;
  77. info[key] = value;
  78. this.setState({
  79. 'userInfo': info
  80. });
  81. }
  82. changeTab(type) {
  83. this.setState({
  84. isFleetDriver: type
  85. })
  86. }
  87. getCountryList() {
  88. GetCountryList(list => {
  89. this.setState({
  90. countryNums: list
  91. })
  92. })
  93. }
  94. getCompanyList() {
  95. apiUser.getConmpany().then(res => {
  96. if (res.data) {
  97. this.setState({
  98. companyList: res.data
  99. })
  100. }
  101. }).catch(err => [
  102. toastShort(err)
  103. ])
  104. }
  105. onRegister() {
  106. //console.log('sign up', this.state);
  107. var info = this.state.userInfo;
  108. if (!info.nickName) {
  109. toastShort('Please enter display name');
  110. return;
  111. }
  112. if (!info.email) {
  113. toastShort('Please enter email address');
  114. return;
  115. }
  116. if (!/^[a-zA-Z0-9]+[\S]+@[a-zA-Z0-9_-]+[\.][\Sa-zA-Z]+$/.test(info.email)) {
  117. toastShort('Email is incorrect format');
  118. return;
  119. }
  120. if (!info.phone) {
  121. toastShort('Please enter contact number');
  122. return;
  123. }
  124. if (!/^\d{6,15}$/.test(info.phone)) {
  125. toastShort('Phone Number is incorrect format');
  126. return;
  127. }
  128. if (!this.state.password) {
  129. toastShort('Please enter password');
  130. return;
  131. }
  132. if (this.state.strength < StrengthView.V1.maxStrength) {
  133. toastShort('Password is not strong');
  134. return;
  135. }
  136. if (!info.password) {
  137. toastShort('Please enter confirm password');
  138. return;
  139. }
  140. if (info.password != this.state.password) {
  141. toastShort('The twice passwords are inconsistent');
  142. if (this.state.wrongCount < 3) {
  143. this.setState({
  144. wrongCount: this.state.wrongCount + 1
  145. })
  146. }
  147. return;
  148. }
  149. if (this.state.isFleetDriver) {
  150. if (!info.pdvLicence) {
  151. toastShort('Please enter PDV Licence');
  152. return;
  153. }
  154. if (this.state.pdvImages[0] == '' || this.state.pdvImages[1] == '') {
  155. toastShort('Please upload PDV Licence Photos');
  156. return;
  157. }
  158. }
  159. let param = Object.assign({}, info);
  160. //param.phone = this.state.countryNum + info.phone
  161. param.callingCode = this.state.countryNum;
  162. if (this.state.isFleetDriver) {
  163. param.userType = 'Driver';
  164. param.pdvLicencePictures = this.state.pdvImages;
  165. param.fleetCompanyId = this.state.fleetCompanyId;
  166. } else {
  167. param.userType = 'Public';
  168. }
  169. console.log('params', param);
  170. Dialog.showProgressDialog();
  171. apiUser.register(param).then(res => {
  172. Dialog.dismissLoading();
  173. //toastShort('Sign up successfully!');
  174. this.setState({
  175. email: param.email,
  176. visible: true
  177. });
  178. //this.backToLogin();
  179. }).catch(err => {
  180. toastShort(err);
  181. Dialog.dismissLoading();
  182. });
  183. }
  184. getBackTopPosition() {
  185. return isIOS ? statusHeight - 16 : 8;
  186. }
  187. backToLogin() {
  188. if (this.state.params.actionLogin) {
  189. goBack()
  190. startPage(PageList.login);
  191. } else {
  192. goBack();
  193. }
  194. }
  195. uploadImage(index) {
  196. ImagePicker.openPicker({
  197. ...options,
  198. cropperToolbarTitle: $t('common.cropperTitle')
  199. }).then(image => {
  200. if (image.path) {
  201. apiUpload.uploadImage(image.path, image.mime, 'PDVL').then(res => {
  202. if (res.success && res.data.picturePath) {
  203. let imageUrl = this.state.pdvImages;
  204. imageUrl[index] = res.data.picturePath;
  205. this.setState({
  206. pdvImages: imageUrl
  207. });
  208. } else {
  209. toastShort('Upload failed, please retry');
  210. }
  211. }).catch(err => {
  212. toastShort(err);
  213. });
  214. }
  215. }).catch(err1 => {
  216. //console.log(err1);
  217. });
  218. }
  219. changeAgree(ag) {
  220. this.setState({
  221. agree: ag
  222. })
  223. }
  224. hideDialog() {
  225. this.setState({
  226. visible: false
  227. });
  228. this.backToLogin();
  229. }
  230. render() {
  231. return (
  232. <View style={StyleSheet.absoluteFillObject}>
  233. <ScrollView
  234. style={styles.scollView}
  235. keyboardShouldPersistTaps={isIOS ? 'never' : 'handled'}>
  236. <View style={styles.header}>
  237. <View style={styles.logoView}>
  238. <Image
  239. style={styles.logoImg}
  240. source={require('../../images/app-logo.png')}/>
  241. </View>
  242. </View>
  243. <View style={{...styles.backView, top: this.getBackTopPosition()}}>
  244. <Button
  245. style={styles.backButton}
  246. viewStyle={styles.backButtonView}
  247. onClick={() => goBack()}>
  248. <BackIcon/>
  249. <Text style={{color: textPrimary,fontSize: 16,paddingLeft: 8}}>Back to Login</Text>
  250. </Button>
  251. </View>
  252. <View style={styles.signView}>
  253. <View style={styles.tabView}>
  254. <Text
  255. style={[
  256. styles.tabText,
  257. this.state.isFleetDriver ? {} : styles.tabActive
  258. ]}
  259. onPress={() => this.changeTab(false)}
  260. >Public Users</Text>
  261. <Text
  262. style={[
  263. styles.tabText,
  264. this.state.isFleetDriver ? styles.tabActive: {}
  265. ]}
  266. onPress={() => this.changeTab(true)}
  267. >Fleet/PH Drivers</Text>
  268. </View>
  269. <Text style={styles.title}>Registration</Text>
  270. <View style={styles.signInput}>
  271. <Text style={styles.inputLabel}>Display Name</Text>
  272. <TextInput
  273. style={styles.inputView}
  274. placeholder='Display Name'
  275. placeholderTextColor={textPlacehoder}
  276. maxLength={50}
  277. onChangeText={v => this.changeInfo('nickName', v)}
  278. />
  279. </View>
  280. <View style={styles.signInput}>
  281. <Text style={styles.inputLabel}>Email Address</Text>
  282. <TextInput
  283. style={styles.inputView}
  284. placeholder='Email Address'
  285. placeholderTextColor={textPlacehoder}
  286. maxLength={50}
  287. onChangeText={v => this.changeInfo('email', v)}
  288. />
  289. </View>
  290. <View style={styles.signInput}>
  291. <Text style={styles.inputLabel}>Phone Number</Text>
  292. <View style={styles.mobileView}>
  293. <View style={styles.dropView}>
  294. <TextInput style={styles.dropInput} editable={false}/>
  295. <Text style={styles.countryText}>{"+" + this.state.countryNum}</Text>
  296. <MaterialIcons name={'arrow-drop-down'} size={24} color={colorDark}/>
  297. <Dropdown
  298. style={styles.dropLayer}
  299. title='Country'
  300. prefixText="+"
  301. list={this.state.countryNums}
  302. value={this.state.countryNum}
  303. nameKey='countryNum'
  304. valueKey='countryNum'
  305. onChange={(value, index)=> {
  306. this.setState({
  307. countryNum: value
  308. })
  309. }}
  310. customerItemView={
  311. (item, index, onClick) =>
  312. <CountryDropNum
  313. key={index}
  314. country={item}
  315. value={this.state.countryNum}
  316. onClick={onClick}/>
  317. }/>
  318. </View>
  319. <TextInput
  320. style={styles.contactView}
  321. placeholder='Mobile Number'
  322. placeholderTextColor={textPlacehoder}
  323. keyboardType='phone-pad'
  324. maxLength={15}
  325. onChangeText={v => this.changeInfo('phone', v)}
  326. />
  327. </View>
  328. </View>
  329. <View style={styles.signInput}>
  330. <Text style={styles.inputLabel}>Create Password</Text>
  331. <TextInput
  332. secureTextEntry={this.state.wrongCount < 3}
  333. style={styles.inputView}
  334. placeholder='Password'
  335. placeholderTextColor={textPlacehoder}
  336. maxLength={20}
  337. onChangeText={(value) => {
  338. this.applyStrength(value);
  339. }}
  340. />
  341. </View>
  342. <View style={styles.signInput}>
  343. <Text style={styles.inputLabel}></Text>
  344. <View style={styles.passwordView}>
  345. <StrengthView.V1.VIEW {...this.state}/>
  346. </View>
  347. </View>
  348. <View style={styles.signInput}>
  349. <Text style={styles.inputLabel}>Confirm Password</Text>
  350. <TextInput
  351. secureTextEntry={this.state.wrongCount < 3}
  352. style={styles.inputView}
  353. placeholder='Password'
  354. placeholderTextColor={textPlacehoder}
  355. maxLength={20}
  356. onChangeText={v => this.changeInfo('password', v)}
  357. />
  358. </View>
  359. { this.state.isFleetDriver &&
  360. <>
  361. <View style={styles.signInput}>
  362. <Text style={styles.inputLabel}>Your Company</Text>
  363. <Dropdown
  364. style={[styles.inputView, ui.flexc]}
  365. title='Company'
  366. list={this.state.companyList}
  367. value={this.state.fleetCompanyId}
  368. valueKey='fleetCompanyId'
  369. nameKey='fleetCompanyName'
  370. onChange={(value, index)=> {
  371. this.setState({
  372. fleetCompanyId: value
  373. })
  374. }}/>
  375. </View>
  376. <View style={styles.signInput}>
  377. <Text style={styles.inputLabel}>{'PDV Licence'}</Text>
  378. <TextInput
  379. style={styles.inputView}
  380. placeholder='PH Driver Vocational Licence'
  381. placeholderTextColor={textPlacehoder}
  382. maxLength={20}
  383. onChangeText={v => this.changeInfo('pdvLicence', v)}
  384. />
  385. </View>
  386. <View style={styles.signInput}>
  387. <Text style={styles.inputLabel}>{' PDV Photos\n(Front & Back)'}</Text>
  388. <View style={styles.uploadGroup}>
  389. { this.state.pdvImages.map((item, index) => (
  390. <UploadView
  391. key={index}
  392. onPress={() => this.uploadImage(index)}
  393. url={item}/>
  394. ))
  395. }
  396. </View>
  397. </View>
  398. </>
  399. }
  400. <View style={styles.referView}>
  401. <Text style={styles.referTitle}>Have a referral code?</Text>
  402. <View style={styles.referText}>
  403. <Text>You'll get</Text>
  404. <Text style={styles.weight}>$5</Text>
  405. <Text>Credit as registration bonus!</Text>
  406. </View>
  407. <View style={styles.codeView}>
  408. <Text style={{ color: textPrimary, fontSize: 16 }}>Referral Code</Text>
  409. <TextInput
  410. style={styles.codeText}
  411. maxLength={6}
  412. placeholder='Referral Code'
  413. placeholderTextColor={textPlacehoder}
  414. onChangeText={v => this.changeInfo('referralCode', v)}
  415. />
  416. </View>
  417. </View>
  418. <View style={styles.agreeView}>
  419. <CheckBox
  420. value={this.state.agree}
  421. onTintColor={colorAccent}
  422. onCheckColor={colorAccent}
  423. onValueChange={v => this.changeAgree(v)}
  424. />
  425. <View style={styles.agreeTextRow}>
  426. <Text style={styles.agreeText} onPress={() => this.changeAgree(!this.state.agree)}>
  427. {'I have read and I agree with the '}
  428. </Text>
  429. <Text style={styles.agreeLink} onPress={() => startPage(PageList.condition)}>Terms of Use</Text>
  430. <Text style={styles.agreeText}>{' '}</Text>
  431. <Text style={styles.agreeText}>{'and '}</Text>
  432. <Text style={styles.agreeLink} onPress={() => startPage(PageList.privacy)}>Privacy Policy</Text>
  433. <Text style={styles.agreeText}>.</Text>
  434. </View>
  435. </View>
  436. <Button
  437. style={styles.signButton}
  438. elevation={1.5}
  439. disabled={!this.state.agree}
  440. text='SIGN UP'
  441. fontSize={14}
  442. onClick={() => {
  443. this.onRegister();
  444. }}
  445. />
  446. </View>
  447. </ScrollView>
  448. <Modal
  449. isVisible={this.state.visible}
  450. onBackButtonPress={() => this.hideDialog()}
  451. {...ModalProps}>
  452. <RegisterDialog
  453. address={this.state.email}
  454. onClose={() => this.hideDialog()}
  455. />
  456. </Modal>
  457. </View>
  458. );
  459. }
  460. }
  461. const UploadView = ({url, onPress}) => (
  462. <Pressable
  463. style={styles.uploadView}
  464. onPress={onPress}>
  465. { url == ''
  466. ? <Image
  467. style={styles.uploadIcon}
  468. source={require('../../images/icon/ic-add-photo.png')}/>
  469. : <Image
  470. style={styles.uploadIcon}
  471. defaultSource={require('../../images/icon/icon-upload-default.png')}
  472. source={{uri: utils.getImageUrl(url)}}/>
  473. }
  474. </Pressable>
  475. )
  476. const styles = StyleSheet.create({
  477. header: {
  478. paddingTop: 56,
  479. backgroundColor: colorAccent
  480. },
  481. backView: {
  482. top: 12,
  483. zIndex: 5,
  484. padding: 16,
  485. position: 'absolute',
  486. flexDirection: 'row'
  487. },
  488. backButton: {
  489. borderRadius: 60,
  490. backgroundColor: colorLight
  491. },
  492. backButtonView: {
  493. height: 43,
  494. paddingLeft: 16,
  495. paddingRight: 16,
  496. alignItems: 'center',
  497. flexDirection: 'row'
  498. },
  499. scollView: {
  500. flex: 1
  501. },
  502. logoView: {
  503. paddingTop: 32,
  504. paddingBottom: 56,
  505. alignItems: 'center'
  506. },
  507. logoImg: {
  508. width:165.09,
  509. height: 51.94,
  510. },
  511. signView: {
  512. flex: 1,
  513. padding: 16,
  514. marginTop: -30,
  515. borderTopLeftRadius: 20,
  516. borderTopRightRadius: 20,
  517. backgroundColor: colorLight
  518. },
  519. tabView: {
  520. marginBottom: 4,
  521. borderWidth: 1,
  522. borderRadius: 6,
  523. overflow: 'hidden',
  524. alignItems: 'center',
  525. flexDirection: 'row',
  526. borderColor: colorAccent,
  527. },
  528. tabText: {
  529. flex: 1,
  530. color: textPrimary,
  531. fontSize: 15,
  532. textAlign: 'center',
  533. ...$padding(10, 0),
  534. },
  535. tabActive: {
  536. fontWeight: 'bold',
  537. backgroundColor: colorAccent
  538. },
  539. title: {
  540. color: textPrimary,
  541. fontSize: 18,
  542. fontWeight: '700',
  543. paddingTop: 4,
  544. paddingBottom: 6,
  545. },
  546. signInput: {
  547. marginTop: 16,
  548. alignItems: 'center',
  549. flexDirection: 'row'
  550. },
  551. inputLabel: {
  552. flex: 1,
  553. color: textPrimary,
  554. fontSize: 14,
  555. marginRight: 4
  556. },
  557. inputView: {
  558. flex: 2,
  559. color: textPrimary,
  560. fontSize: 14,
  561. borderRadius: 5,
  562. minHeight: 40,
  563. paddingTop: 6,
  564. paddingLeft: 12,
  565. paddingRight: 12,
  566. paddingBottom: 6,
  567. backgroundColor: '#F5F5F5'
  568. },
  569. mobileView: {
  570. flex: 2,
  571. marginLeft: -12,
  572. alignItems: 'center',
  573. flexDirection: 'row'
  574. },
  575. dropView: {
  576. fontSize: 16,
  577. borderRadius: 4,
  578. paddingRight: 4,
  579. flexDirection: 'row',
  580. alignItems: 'center',
  581. backgroundColor: '#F5F5F5'
  582. },
  583. dropInput: {
  584. width: 12,
  585. padding: 6,
  586. color: textPrimary,
  587. minHeight: 40,
  588. },
  589. countryText: {
  590. color: textPrimary,
  591. fontSize: 14,
  592. paddingRight: 4
  593. },
  594. dropLayer: {
  595. left: 0,
  596. right: 0,
  597. opacity: 0,
  598. position: 'absolute'
  599. },
  600. contactView: {
  601. flex: 1,
  602. color: textPrimary,
  603. fontSize: 15,
  604. borderRadius: 4,
  605. minHeight: 40,
  606. paddingTop: 6,
  607. paddingLeft: 12,
  608. paddingRight: 12,
  609. paddingBottom: 6,
  610. marginLeft: 10,
  611. backgroundColor: '#F5F5F5'
  612. },
  613. passwordView: {
  614. flex: 2,
  615. marginTop: -8,
  616. },
  617. strengthView: {
  618. marginTop: -4,
  619. alignItems: 'center',
  620. paddingBottom: 4,
  621. flexDirection: 'row'
  622. },
  623. passwordRole: {
  624. color: '#999',
  625. fontSize: 10,
  626. lineHeight: 13
  627. },
  628. strengthItem: {
  629. width: 14,
  630. height: 2.5,
  631. marginLeft: 8,
  632. borderRadius: 3,
  633. backgroundColor: '#CCC'
  634. },
  635. referView: {
  636. padding: 16,
  637. marginTop: 24,
  638. borderRadius: 6,
  639. alignItems: 'center',
  640. backgroundColor: '#F5F5F5'
  641. },
  642. referTitle: {
  643. color: textPrimary,
  644. fontSize: 18,
  645. fontWeight: 'bold'
  646. },
  647. referText: {
  648. color: textPrimary,
  649. paddingTop: 4,
  650. alignItems: 'flex-end',
  651. flexDirection: 'row'
  652. },
  653. weight: {
  654. fontSize: 18,
  655. paddingLeft: 4,
  656. paddingRight: 4,
  657. color: colorAccent,
  658. fontWeight: 'bold'
  659. },
  660. codeView: {
  661. paddingTop: 16,
  662. alignItems: 'center',
  663. flexDirection: 'row'
  664. },
  665. codeText: {
  666. color: textPrimary,
  667. fontSize: 16,
  668. paddingTop: 6,
  669. paddingLeft: 12,
  670. paddingRight: 12,
  671. paddingBottom: 6,
  672. minHeight: 40,
  673. marginLeft: 16,
  674. borderRadius: 6,
  675. textAlign: 'center',
  676. backgroundColor: colorLight
  677. },
  678. signButton: {
  679. marginTop: 24,
  680. marginBottom: 8,
  681. },
  682. uploadGroup: {
  683. flex: 2,
  684. alignItems: 'center',
  685. flexDirection: 'row',
  686. justifyContent: 'center'
  687. },
  688. uploadView: {
  689. flex: 1,
  690. alignItems: 'center'
  691. },
  692. uploadIcon: {
  693. width: $vw(28),
  694. height: $vw(28),
  695. borderRadius: 6
  696. },
  697. agreeView: {
  698. marginTop: 24,
  699. marginBottom: 16,
  700. flexDirection: 'row',
  701. alignItems: 'flex-start',
  702. },
  703. agreeTextRow: {
  704. flex: 1,
  705. paddingTop: 4,
  706. paddingLeft: 8,
  707. flexWrap: 'wrap',
  708. flexDirection: 'row'
  709. },
  710. agreeText: {
  711. color: textPrimary,
  712. fontSize: 14,
  713. paddingTop: 2,
  714. paddingBottom: 2
  715. },
  716. agreeLink: {
  717. ...ui.link,
  718. fontSize: 14,
  719. paddingTop: 2,
  720. paddingBottom: 2,
  721. textDecorationLine: 'underline'
  722. }
  723. });