Register.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 { host } from '../../api/http';
  18. import CheckBox from '@react-native-community/checkbox';
  19. import { ModalProps } from '../../components/BottomModal';
  20. import { CountryDropNum, GetCountryList } from '../../components/CountryIcon';
  21. import StrengthView from './StrengthView';
  22. import { UploadThemes } from '../../components/MyRefreshControl';
  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(options).then(image => {
  197. if (image.path) {
  198. apiUpload.uploadImage(image.path, image.mime, 'PDVL').then(res => {
  199. if (res.success && res.data.picturePath) {
  200. let imageUrl = this.state.pdvImages;
  201. imageUrl[index] = res.data.picturePath;
  202. this.setState({
  203. pdvImages: imageUrl
  204. });
  205. } else {
  206. toastShort('Upload failed, please retry');
  207. }
  208. }).catch(err => {
  209. toastShort(err);
  210. });
  211. }
  212. }).catch(err => {
  213. //console.log(err);
  214. });
  215. }
  216. changeAgree(ag) {
  217. this.setState({
  218. agree: ag
  219. })
  220. }
  221. hideDialog() {
  222. this.setState({
  223. visible: false
  224. });
  225. this.backToLogin();
  226. }
  227. render() {
  228. return (
  229. <View style={StyleSheet.absoluteFillObject}>
  230. <ScrollView
  231. style={styles.scollView}
  232. keyboardShouldPersistTaps={'handled'}>
  233. <View style={styles.header}>
  234. <View style={styles.logoView}>
  235. <Image
  236. style={styles.logoImg}
  237. source={require('../../images/app-logo.png')}/>
  238. </View>
  239. </View>
  240. <View style={{...styles.backView, top: this.getBackTopPosition()}}>
  241. <Button
  242. style={styles.backButton}
  243. viewStyle={styles.backButtonView}
  244. onClick={() => goBack()}>
  245. <BackIcon/>
  246. <Text style={{color: textPrimary,fontSize: 16,paddingLeft: 8}}>Back to Login</Text>
  247. </Button>
  248. </View>
  249. <View style={styles.signView}>
  250. <View style={styles.tabView}>
  251. <Text
  252. style={[
  253. styles.tabText,
  254. this.state.isFleetDriver ? {} : styles.tabActive
  255. ]}
  256. onPress={() => this.changeTab(false)}
  257. >Public Users</Text>
  258. <Text
  259. style={[
  260. styles.tabText,
  261. this.state.isFleetDriver ? styles.tabActive: {}
  262. ]}
  263. onPress={() => this.changeTab(true)}
  264. >Fleet/PH Drivers</Text>
  265. </View>
  266. <Text style={styles.title}>Registration</Text>
  267. <View style={styles.signInput}>
  268. <Text style={styles.inputLabel}>Display Name</Text>
  269. <TextInput
  270. style={styles.inputView}
  271. placeholder='Display Name'
  272. placeholderTextColor={textPlacehoder}
  273. maxLength={50}
  274. onChangeText={v => this.changeInfo('nickName', v)}
  275. />
  276. </View>
  277. <View style={styles.signInput}>
  278. <Text style={styles.inputLabel}>Email Address</Text>
  279. <TextInput
  280. style={styles.inputView}
  281. placeholder='Email Address'
  282. placeholderTextColor={textPlacehoder}
  283. maxLength={50}
  284. onChangeText={v => this.changeInfo('email', v)}
  285. />
  286. </View>
  287. <View style={styles.signInput}>
  288. <Text style={styles.inputLabel}>Phone Number</Text>
  289. <View style={styles.mobileView}>
  290. <View style={styles.dropView}>
  291. <TextInput style={styles.dropInput} editable={false}/>
  292. <Text style={styles.countryText}>{"+" + this.state.countryNum}</Text>
  293. <MaterialIcons name={'arrow-drop-down'} size={24} color={colorDark}/>
  294. <Dropdown
  295. style={styles.dropLayer}
  296. title='Country'
  297. prefixText="+"
  298. list={this.state.countryNums}
  299. value={this.state.countryNum}
  300. nameKey='countryNum'
  301. valueKey='countryNum'
  302. onChange={(value, index)=> {
  303. this.setState({
  304. countryNum: value
  305. })
  306. }}
  307. customerItemView={
  308. (item, index, onClick) =>
  309. <CountryDropNum
  310. key={index}
  311. country={item}
  312. value={this.state.countryNum}
  313. onClick={onClick}/>
  314. }/>
  315. </View>
  316. <TextInput
  317. style={styles.contactView}
  318. placeholder='Mobile Number'
  319. placeholderTextColor={textPlacehoder}
  320. keyboardType='phone-pad'
  321. maxLength={15}
  322. onChangeText={v => this.changeInfo('phone', v)}
  323. />
  324. </View>
  325. </View>
  326. <View style={styles.signInput}>
  327. <Text style={styles.inputLabel}>Create Password</Text>
  328. <TextInput
  329. secureTextEntry={this.state.wrongCount < 3}
  330. style={styles.inputView}
  331. placeholder='Password'
  332. placeholderTextColor={textPlacehoder}
  333. maxLength={20}
  334. onChangeText={(value) => {
  335. this.applyStrength(value);
  336. }}
  337. />
  338. </View>
  339. <View style={styles.signInput}>
  340. <Text style={styles.inputLabel}></Text>
  341. <View style={styles.passwordView}>
  342. <StrengthView.V1.VIEW {...this.state}/>
  343. </View>
  344. </View>
  345. <View style={styles.signInput}>
  346. <Text style={styles.inputLabel}>Confirm Password</Text>
  347. <TextInput
  348. secureTextEntry={this.state.wrongCount < 3}
  349. style={styles.inputView}
  350. placeholder='Password'
  351. placeholderTextColor={textPlacehoder}
  352. maxLength={20}
  353. onChangeText={v => this.changeInfo('password', v)}
  354. />
  355. </View>
  356. { this.state.isFleetDriver &&
  357. <>
  358. <View style={styles.signInput}>
  359. <Text style={styles.inputLabel}>Your Company</Text>
  360. <Dropdown
  361. style={[styles.inputView, ui.flexc]}
  362. title='Company'
  363. list={this.state.companyList}
  364. value={this.state.fleetCompanyId}
  365. valueKey='fleetCompanyId'
  366. nameKey='fleetCompanyName'
  367. onChange={(value, index)=> {
  368. this.setState({
  369. fleetCompanyId: value
  370. })
  371. }}/>
  372. </View>
  373. <View style={styles.signInput}>
  374. <Text style={styles.inputLabel}>{'PDV Licence'}</Text>
  375. <TextInput
  376. style={styles.inputView}
  377. placeholder='PH Driver Vocational Licence'
  378. placeholderTextColor={textPlacehoder}
  379. maxLength={20}
  380. onChangeText={v => this.changeInfo('pdvLicence', v)}
  381. />
  382. </View>
  383. <View style={styles.signInput}>
  384. <Text style={styles.inputLabel}>{' PDV Photos\n(Front & Back)'}</Text>
  385. <View style={styles.uploadGroup}>
  386. { this.state.pdvImages.map((item, index) => (
  387. <UploadView
  388. key={index}
  389. onPress={() => this.uploadImage(index)}
  390. url={item}/>
  391. ))
  392. }
  393. </View>
  394. </View>
  395. </>
  396. }
  397. <View style={styles.referView}>
  398. <Text style={styles.referTitle}>Have a referral code?</Text>
  399. <View style={styles.referText}>
  400. <Text>You'll get</Text>
  401. <Text style={styles.weight}>$5</Text>
  402. <Text>Credit as registration bonus!</Text>
  403. </View>
  404. <View style={styles.codeView}>
  405. <Text style={{ color: textPrimary, fontSize: 16 }}>Referral Code</Text>
  406. <TextInput
  407. style={styles.codeText}
  408. maxLength={6}
  409. placeholder='Referral Code'
  410. placeholderTextColor={textPlacehoder}
  411. onChangeText={v => this.changeInfo('referralCode', v)}
  412. />
  413. </View>
  414. </View>
  415. <View style={styles.agreeView}>
  416. <CheckBox
  417. value={this.state.agree}
  418. onTintColor={colorAccent}
  419. onCheckColor={colorAccent}
  420. onValueChange={v => this.changeAgree(v)}
  421. />
  422. <View style={styles.agreeTextRow}>
  423. <Text style={styles.agreeText} onPress={() => this.changeAgree(!this.state.agree)}>
  424. {'I have read and I agree with the '}
  425. </Text>
  426. <Text style={styles.agreeLink} onPress={() => startPage(PageList.condition)}>Terms of Use</Text>
  427. <Text style={styles.agreeText}>{' '}</Text>
  428. <Text style={styles.agreeText}>{'and '}</Text>
  429. <Text style={styles.agreeLink} onPress={() => startPage(PageList.privacy)}>Privacy Policy</Text>
  430. <Text style={styles.agreeText}>.</Text>
  431. </View>
  432. </View>
  433. <Button
  434. style={styles.signButton}
  435. elevation={1.5}
  436. disabled={!this.state.agree}
  437. text='SIGN UP'
  438. fontSize={14}
  439. onClick={() => {
  440. this.onRegister();
  441. }}
  442. />
  443. </View>
  444. </ScrollView>
  445. <Modal
  446. isVisible={this.state.visible}
  447. onBackButtonPress={() => this.hideDialog()}
  448. {...ModalProps}>
  449. <RegisterDialog
  450. address={this.state.email}
  451. onClose={() => this.hideDialog()}
  452. />
  453. </Modal>
  454. </View>
  455. );
  456. }
  457. }
  458. const UploadView = ({url, onPress}) => (
  459. <Pressable
  460. style={styles.uploadView}
  461. onPress={onPress}>
  462. { url == ''
  463. ? <Image
  464. style={styles.uploadIcon}
  465. source={require('../../images/icon/ic-add-photo.png')}/>
  466. : <Image
  467. style={styles.uploadIcon}
  468. defaultSource={require('../../images/icon/icon-upload-default.png')}
  469. source={{uri: host + url}}/>
  470. }
  471. </Pressable>
  472. )
  473. const styles = StyleSheet.create({
  474. header: {
  475. paddingTop: 56,
  476. backgroundColor: colorAccent
  477. },
  478. backView: {
  479. top: 12,
  480. zIndex: 5,
  481. padding: 16,
  482. position: 'absolute',
  483. flexDirection: 'row'
  484. },
  485. backButton: {
  486. borderRadius: 60,
  487. backgroundColor: colorLight
  488. },
  489. backButtonView: {
  490. height: 43,
  491. paddingLeft: 16,
  492. paddingRight: 16,
  493. alignItems: 'center',
  494. flexDirection: 'row'
  495. },
  496. scollView: {
  497. flex: 1
  498. },
  499. logoView: {
  500. paddingTop: 32,
  501. paddingBottom: 56,
  502. alignItems: 'center'
  503. },
  504. logoImg: {
  505. width:165.09,
  506. height: 51.94,
  507. },
  508. signView: {
  509. flex: 1,
  510. padding: 16,
  511. marginTop: -30,
  512. borderTopLeftRadius: 20,
  513. borderTopRightRadius: 20,
  514. backgroundColor: colorLight
  515. },
  516. tabView: {
  517. marginBottom: 4,
  518. borderWidth: 1,
  519. borderRadius: 6,
  520. overflow: 'hidden',
  521. alignItems: 'center',
  522. flexDirection: 'row',
  523. borderColor: colorAccent,
  524. },
  525. tabText: {
  526. flex: 1,
  527. color: textPrimary,
  528. fontSize: 15,
  529. textAlign: 'center',
  530. ...$padding(10, 0),
  531. },
  532. tabActive: {
  533. fontWeight: 'bold',
  534. backgroundColor: colorAccent
  535. },
  536. title: {
  537. color: textPrimary,
  538. fontSize: 18,
  539. fontWeight: '700',
  540. paddingTop: 4,
  541. paddingBottom: 6,
  542. },
  543. signInput: {
  544. marginTop: 16,
  545. alignItems: 'center',
  546. flexDirection: 'row'
  547. },
  548. inputLabel: {
  549. flex: 1,
  550. color: textPrimary,
  551. fontSize: 14,
  552. marginRight: 4
  553. },
  554. inputView: {
  555. flex: 2,
  556. color: textPrimary,
  557. fontSize: 14,
  558. borderRadius: 5,
  559. minHeight: 40,
  560. paddingTop: 6,
  561. paddingLeft: 12,
  562. paddingRight: 12,
  563. paddingBottom: 6,
  564. backgroundColor: '#F5F5F5'
  565. },
  566. mobileView: {
  567. flex: 2,
  568. marginLeft: -12,
  569. alignItems: 'center',
  570. flexDirection: 'row'
  571. },
  572. dropView: {
  573. fontSize: 16,
  574. borderRadius: 4,
  575. paddingRight: 4,
  576. flexDirection: 'row',
  577. alignItems: 'center',
  578. backgroundColor: '#F5F5F5'
  579. },
  580. dropInput: {
  581. width: 12,
  582. padding: 6,
  583. color: textPrimary,
  584. minHeight: 40,
  585. },
  586. countryText: {
  587. color: textPrimary,
  588. fontSize: 14,
  589. paddingRight: 4
  590. },
  591. dropLayer: {
  592. left: 0,
  593. right: 0,
  594. opacity: 0,
  595. position: 'absolute'
  596. },
  597. contactView: {
  598. flex: 1,
  599. color: textPrimary,
  600. fontSize: 15,
  601. borderRadius: 4,
  602. minHeight: 40,
  603. paddingTop: 6,
  604. paddingLeft: 12,
  605. paddingRight: 12,
  606. paddingBottom: 6,
  607. marginLeft: 10,
  608. backgroundColor: '#F5F5F5'
  609. },
  610. passwordView: {
  611. flex: 2,
  612. marginTop: -8,
  613. },
  614. strengthView: {
  615. marginTop: -4,
  616. alignItems: 'center',
  617. paddingBottom: 4,
  618. flexDirection: 'row'
  619. },
  620. passwordRole: {
  621. color: '#999',
  622. fontSize: 10,
  623. lineHeight: 13
  624. },
  625. strengthItem: {
  626. width: 14,
  627. height: 2.5,
  628. marginLeft: 8,
  629. borderRadius: 3,
  630. backgroundColor: '#CCC'
  631. },
  632. referView: {
  633. padding: 16,
  634. marginTop: 24,
  635. borderRadius: 6,
  636. alignItems: 'center',
  637. backgroundColor: '#F5F5F5'
  638. },
  639. referTitle: {
  640. color: textPrimary,
  641. fontSize: 18,
  642. fontWeight: 'bold'
  643. },
  644. referText: {
  645. color: textPrimary,
  646. paddingTop: 4,
  647. alignItems: 'flex-end',
  648. flexDirection: 'row'
  649. },
  650. weight: {
  651. fontSize: 18,
  652. paddingLeft: 4,
  653. paddingRight: 4,
  654. color: colorAccent,
  655. fontWeight: 'bold'
  656. },
  657. codeView: {
  658. paddingTop: 16,
  659. alignItems: 'center',
  660. flexDirection: 'row'
  661. },
  662. codeText: {
  663. color: textPrimary,
  664. fontSize: 16,
  665. paddingTop: 6,
  666. paddingLeft: 12,
  667. paddingRight: 12,
  668. paddingBottom: 6,
  669. minHeight: 40,
  670. marginLeft: 16,
  671. borderRadius: 6,
  672. textAlign: 'center',
  673. backgroundColor: colorLight
  674. },
  675. signButton: {
  676. marginTop: 24,
  677. marginBottom: 8,
  678. },
  679. uploadGroup: {
  680. flex: 2,
  681. alignItems: 'center',
  682. flexDirection: 'row',
  683. justifyContent: 'center'
  684. },
  685. uploadView: {
  686. flex: 1,
  687. alignItems: 'center'
  688. },
  689. uploadIcon: {
  690. width: $vw(28),
  691. height: $vw(28),
  692. borderRadius: 6
  693. },
  694. agreeView: {
  695. marginTop: 24,
  696. marginBottom: 16,
  697. flexDirection: 'row',
  698. alignItems: 'flex-start',
  699. },
  700. agreeTextRow: {
  701. flex: 1,
  702. paddingTop: 4,
  703. paddingLeft: 8,
  704. flexWrap: 'wrap',
  705. flexDirection: 'row'
  706. },
  707. agreeText: {
  708. color: textPrimary,
  709. fontSize: 14,
  710. paddingTop: 2,
  711. paddingBottom: 2
  712. },
  713. agreeLink: {
  714. ...ui.link,
  715. fontSize: 14,
  716. paddingTop: 2,
  717. paddingBottom: 2,
  718. textDecorationLine: 'underline'
  719. }
  720. });