Drawer.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /**
  2. * 首页抽屉菜单
  3. * @邠心vbe on 2021/03/23
  4. */
  5. import React, { Component } from 'react';
  6. import {View, Text, StyleSheet, Image, Pressable, BackHandler, Linking, Touchable} from 'react-native';
  7. import { createDrawerNavigator, DrawerContentScrollView } from '@react-navigation/drawer';
  8. import { Styles } from '../../components/Toolbar';
  9. import app from '../../../app.json';
  10. import Maps from './Home';
  11. import { PageList } from '../Router';
  12. import Dialog from '../../components/Dialog';
  13. import { host, setAccessToken } from '../../api/http';
  14. import { getStorageJsonSync, setStorage, setStorageJson } from '../../utils/storage';
  15. import Button from '../../components/Button';
  16. import { AutoLogin } from '../sign/Login';
  17. import apiCharge from '../../api/apiCharge';
  18. import { TouchableWithoutFeedback } from 'react-native-gesture-handler';
  19. import { toTopupPage } from '../payment/PaymentConfig';
  20. import utils from '../../utils/utils';
  21. const Drawer = createDrawerNavigator();
  22. const DEBUG = app.debug && !app.product;
  23. export default class Home extends Component {
  24. constructor(props) {
  25. super(props);
  26. this.state = {
  27. isLogin: false,
  28. userInfo: {},
  29. }
  30. }
  31. componentDidMount() {
  32. AutoLogin(() => {
  33. this.setState({
  34. userInfo: userInfo
  35. });
  36. });
  37. this.props.navigation.addListener('focus', () => {
  38. //console.log('drawer focus');
  39. getUserInfo(info => {
  40. this.setState({
  41. userInfo: info
  42. });
  43. }, true);
  44. });
  45. /*BackHandler.addEventListener('hardwareBackPress', (e) => {
  46. if (global.dialogId !== 0) {
  47. Dialog.dismissLoading();
  48. return true;
  49. }
  50. return false;
  51. })*/
  52. }
  53. componentDidUpdate() {
  54. const status = isLogin();
  55. if (this.state.isLogin != status) {
  56. this.setState({
  57. isLogin: status
  58. }, () => {
  59. getUserInfo(info => {
  60. this.setState({
  61. userInfo: info
  62. });
  63. if (info.firebaseToken) {
  64. let token = isIOS ? info.firebaseToken?.ios : info.firebaseToken?.android
  65. if (notifyToken.token) {
  66. utils.registerFirebaseToken(token ?? "");
  67. }
  68. }
  69. }, true);
  70. });
  71. }
  72. }
  73. async requestLogout() {
  74. const data = await getStorageJsonSync('loginData');
  75. if (data && data.email) {
  76. delete data.password
  77. setStorageJson('loginData', data);
  78. setStorage('RegisterTokenDate', "");
  79. }
  80. global.userInfo = {}
  81. this.setState({
  82. isLogin: false,
  83. userInfo: {}
  84. });
  85. setAccessToken('');
  86. Dialog.dismissLoading();
  87. }
  88. render () {
  89. return (
  90. <Drawer.Navigator
  91. initialRouteName='maps'
  92. drawerContent={props =>
  93. <CustomerDrawerContent
  94. {...props}
  95. isLogin={this.state.isLogin}
  96. userInfo={this.state.userInfo}
  97. onLogout={() => this.requestLogout()}
  98. />
  99. }
  100. drawerType={
  101. global.$width >= 768 ? 'back' : 'front'
  102. }
  103. drawerStyle={{
  104. width: $vw(75) > 320 ? 320 : $vw(75),
  105. backgroundColor: colorLight
  106. }}>
  107. <Drawer.Screen name="maps" component={Maps} />
  108. </Drawer.Navigator>
  109. );
  110. }
  111. };
  112. const CustomerDrawerContent = (props) => {
  113. return (
  114. <DrawerContentScrollView
  115. {...props}
  116. canCancelContentTouches={true}>
  117. <DrawerContent {...props}/>
  118. </DrawerContentScrollView>
  119. );
  120. }
  121. const DrawerContent = ({isLogin, userInfo, onLogout, navigation}) => {
  122. const getCharging = () => {
  123. Dialog.showProgressDialog();
  124. apiCharge.getUserCharging().then(res => {
  125. Dialog.dismissLoading();
  126. if (res.data.sitePk) {
  127. startPage(PageList.chargeDetailPage, {stationInfo: {id: res.data.sitePk}, action: 'view', from: PageList.home});
  128. //startPage(PageList.chargeDetail, { stationInfo: {id: res.data.sitePk}, action: 'view'});
  129. } else if (res.msg) {
  130. toastShort(res.msg);
  131. } else {
  132. toastShort($t("drawer.noChargingSession"));
  133. }
  134. }).catch((err) => {
  135. if (app.debug)
  136. console.log(err);
  137. Dialog.dismissLoading();
  138. toastShort($t("drawer.noChargingSession"));
  139. })
  140. }
  141. const logout = () => {
  142. Dialog.showDialog({
  143. title: 'Sign out',
  144. message: 'Are you sure you want to sign out?',
  145. callback: btn => {
  146. if (btn == 'ok') {
  147. Dialog.showProgressDialog();
  148. setTimeout(() => {
  149. onLogout();
  150. }, 500);
  151. }
  152. }
  153. })
  154. }
  155. return (
  156. <View style={styles.drawerView}>
  157. <View style={styles.loginView}>
  158. { (isLogin && userInfo.photoUrl)
  159. ? <TouchableWithoutFeedback onPress={() => startPage(PageList.profile)}>
  160. <Image
  161. style={styles.avatar}
  162. source={{uri: host + userInfo.photoUrl}}/>
  163. </TouchableWithoutFeedback>
  164. : <TouchableWithoutFeedback onPress={() => startPage(PageList.login)}>
  165. <Image
  166. style={styles.avatar}
  167. source={require('../../images/user/ic-avatar-default.png')}/>
  168. </TouchableWithoutFeedback>
  169. }
  170. <Pressable
  171. style={styles.nickView}
  172. android_ripple={ripple}
  173. onPress={() => startPage(isLogin ? PageList.profile : PageList.login)}>
  174. <Text
  175. style={styles.nickname}
  176. ellipsizeMode='tail'
  177. numberOfLines={1}>
  178. { isLogin
  179. ? userInfo.nickName
  180. ? userInfo.nickName
  181. : $t('drawer.logging')
  182. : $t('drawer.sign')
  183. }
  184. </Text>
  185. <FontAwesome
  186. size={24}
  187. color='#999'
  188. name='angle-right'/>
  189. </Pressable>
  190. </View>
  191. <View style={styles.divideLogin}></View>
  192. {/* isLogin
  193. ? <Button
  194. style={styles.itemButton}
  195. viewStyle={styles.itemView}
  196. onClick={() => {
  197. startPage(PageList.profile)
  198. }}>
  199. <Image
  200. style={styles.icon}
  201. source={require('../../images/icon/draw-user.png')}/>
  202. <Text style={styles.label}>Profile Settings</Text>
  203. </Button>
  204. : <Button
  205. style={styles.itemButton}
  206. viewStyle={styles.itemView}
  207. onClick={() => {
  208. startPage(PageList.login);
  209. }}>
  210. <Image
  211. style={styles.icon}
  212. source={require('../../images/icon/draw-user.png')}/>
  213. <Text style={styles.label}>Sign In</Text>
  214. </Button>
  215. */}
  216. {/* <Button
  217. style={styles.itemButton}
  218. viewStyle={styles.itemView}
  219. onClick={() => {
  220. navigation.toggleDrawer();
  221. }}>
  222. <Image
  223. style={styles.icon}
  224. source={require('../../images/icon/draw-home.png')}/>
  225. <Text style={styles.label}>Home</Text>
  226. </Button> */}
  227. { isLogin
  228. ? <Button
  229. style={styles.itemButton}
  230. viewStyle={styles.itemView}
  231. onClick={() => getCharging()}>
  232. <MaterialCommunityIcons
  233. style={styles.icon}
  234. name="car-electric-outline"
  235. color="#333"
  236. size={26}
  237. />
  238. {/* <Image
  239. style={styles.icon}
  240. source={require('../../images/icon/draw-charge.png')}/> */}
  241. <Text style={styles.label}>{$t('drawer.charging')}</Text>
  242. </Button>
  243. : <View
  244. style={styles.disableItem}>
  245. <MaterialCommunityIcons
  246. style={styles.icon}
  247. name="car-electric-outline"
  248. color="#999"
  249. size={26}
  250. />
  251. {/* <Image
  252. style={styles.icon}
  253. source={require('../../images/icon/draw-charge-no.png')}/> */}
  254. <Text style={styles.disable}>{$t('drawer.charging')}</Text>
  255. </View>
  256. }
  257. {/* isLogin
  258. ? <Button
  259. style={styles.itemButton}
  260. viewStyle={styles.itemView}
  261. onClick={() => {
  262. startPage(PageList.wallet);
  263. }}>
  264. <Image
  265. style={styles.icon}
  266. source={require('../../images/icon/draw-transaction.png')}/>
  267. <Text style={styles.label}>{$t('drawer.wallet')}</Text>
  268. </Button>
  269. : <View
  270. style={styles.disableItem}>
  271. <Image
  272. style={styles.icon}
  273. source={require('../../images/icon/draw-transaction-no.png')}/>
  274. <Text style={styles.disable}>{$t('drawer.wallet')}</Text>
  275. </View>
  276. */}
  277. {
  278. isLogin && <>
  279. <Button
  280. style={styles.itemButton}
  281. viewStyle={styles.itemView}
  282. onClick={() => {
  283. startPage(PageList.wallet);
  284. }}>
  285. <Image
  286. style={styles.icon}
  287. source={require('../../images/icon/draw-transaction.png')}/>
  288. <Text style={styles.label}>{$t('drawer.wallet')}</Text>
  289. </Button>
  290. <Button
  291. style={styles.itemButton}
  292. viewStyle={styles.itemView}
  293. onClick={() => toTopupPage()}>
  294. <MaterialCommunityIcons
  295. style={styles.icon}
  296. size={26}
  297. name={"wallet-outline"}
  298. color={colorDark}/>
  299. <Text style={styles.label}>{$t('drawer.topup')}</Text>
  300. <Text style={styles.balanceText2}>{currency}{userInfo.credit}</Text>
  301. </Button></>
  302. }
  303. {/*附加功能-开始*/}
  304. { (app.modules.notifications && isLogin) &&
  305. <Button
  306. style={styles.itemButton}
  307. viewStyle={styles.itemView}
  308. onClick={() => {
  309. startPage(PageList.bookmarks);
  310. }}>
  311. <MaterialIcons
  312. style={styles.icon}
  313. name="stars"
  314. color="#222"
  315. size={26}/>
  316. <Text style={styles.label}>{$t('route.bookmarks')}</Text>
  317. </Button>
  318. }
  319. { (app.modules.bookmarks && isLogin) &&
  320. <Button
  321. style={styles.itemButton}
  322. viewStyle={styles.itemView}
  323. onClick={() => {
  324. startPage(PageList.bookmarks);
  325. }}>
  326. <MaterialIcons
  327. style={styles.icon}
  328. name="stars"
  329. color="#222"
  330. size={26}/>
  331. <Text style={styles.label}>{$t('route.bookmarks')}</Text>
  332. </Button>
  333. }
  334. {/*附加功能-结束*/}
  335. { isLogin &&
  336. <Button
  337. style={styles.itemButton}
  338. viewStyle={styles.itemView}
  339. onClick={() => {
  340. startPage(PageList.membersList);
  341. }}>
  342. <MaterialIcons
  343. style={styles.icon}
  344. name="card-membership"
  345. color="#222"
  346. size={26}/>
  347. <Text style={styles.label}>{$t('drawer.members')}</Text>
  348. </Button>
  349. }
  350. <Button
  351. style={styles.itemButton}
  352. viewStyle={styles.itemView}
  353. onClick={() => {
  354. startPage(PageList.feedback);
  355. }}>
  356. <MaterialCommunityIcons
  357. style={styles.icon2}
  358. name="message-alert-outline"
  359. color="#222"
  360. size={24}/>
  361. <Text style={styles.label}>{$t('drawer.feedback')}</Text>
  362. </Button>
  363. {/* <Button
  364. style={styles.itemButton}
  365. viewStyle={styles.itemView}
  366. onClick={() => {
  367. Linking.openURL(host+"juicePicture/pdf/FAQ.v1.0.pdf")
  368. }}>
  369. <Feather
  370. style={styles.icon2}
  371. name="download"
  372. color={colorDark}
  373. size={24}
  374. />
  375. <Text style={styles.label}>FAQs</Text>
  376. </Button> */}
  377. <Button
  378. style={styles.itemButton}
  379. viewStyle={styles.itemView}
  380. onClick={() => {
  381. startPage(PageList.settings);
  382. }}>
  383. <Ionicons
  384. style={styles.icon}
  385. name="md-settings-outline"
  386. color="#222"
  387. size={25}
  388. />
  389. <Text style={styles.label}>{$t('drawer.settings')}</Text>
  390. </Button>
  391. { app.modules.support &&
  392. <Button
  393. style={styles.itemButton}
  394. viewStyle={styles.itemView}
  395. onClick={() => {
  396. startPage(PageList.supportContact);
  397. }}>
  398. <MaterialCommunityIcons
  399. style={styles.icon}
  400. name="face-agent"
  401. color="#333"
  402. size={26}
  403. />
  404. <Text style={styles.label}>{$t('drawer.contactSupport')}</Text>
  405. </Button>
  406. }
  407. <Button
  408. style={styles.itemButton}
  409. viewStyle={styles.itemView}
  410. onClick={() => {
  411. startPage(PageList.about);
  412. }}>
  413. <MaterialCommunityIcons
  414. style={styles.icon}
  415. name="information-outline"
  416. color="#333"
  417. size={26}
  418. />
  419. <Text style={styles.label}>{$t('drawer.about')}</Text>
  420. </Button>
  421. {/* <View style={styles.divided}></View> */}
  422. { DEBUG &&
  423. <>
  424. <View style={styles.divideLogin}></View>
  425. <Text style={{color: "#ccc", paddingLeft: 16, paddingBottom: 8, fontSize: 12}}>{$t('drawer.debugOnly')}</Text>
  426. <Button
  427. style={styles.itemButton}
  428. viewStyle={styles.itemView}
  429. onClick={() => {
  430. startPage(PageList.privacy);
  431. }}>
  432. <MaterialCommunityIcons
  433. style={styles.icon}
  434. name="file-eye-outline"
  435. color="#222"
  436. size={26}/>
  437. <Text style={styles.label}>{$t('drawer.privacyPolicy')}</Text>
  438. </Button>
  439. <Button
  440. style={styles.itemButton}
  441. viewStyle={styles.itemView}
  442. onClick={() => {
  443. startPage(PageList.condition);
  444. }}>
  445. <MaterialCommunityIcons
  446. style={styles.icon}
  447. name="file-eye-outline"
  448. color="#222"
  449. size={26}/>
  450. <Text style={styles.label}>{$t('drawer.termsOfUse')}</Text>
  451. </Button>
  452. <Button
  453. style={styles.itemButton}
  454. viewStyle={styles.itemView}
  455. onClick={() => {
  456. startPage(PageList.notify);
  457. }}>
  458. {/* <Image
  459. style={styles.icon2}
  460. source={require('../../images/icon/draw-terms.png')}/> */}
  461. <MaterialIcons
  462. style={styles.icon}
  463. name="notifications-none"
  464. color="#222"
  465. size={26}/>
  466. <Text style={styles.label}>{$t('route.notificationTest')}</Text>
  467. </Button>
  468. <Button
  469. style={styles.itemButton}
  470. viewStyle={styles.itemView}
  471. onClick={() => {
  472. startPage(isIOS ? PageList.notify : PageList.mapTest);
  473. }}>
  474. <MaterialCommunityIcons
  475. style={styles.icon}
  476. name="map-legend"
  477. color="#222"
  478. size={26}/>
  479. <Text style={styles.label}>{$t('drawer.mapsTest')}</Text>
  480. </Button>
  481. </>}
  482. {/* isLogin
  483. ? <View style={styles.walletView}>
  484. <Pressable
  485. style={styles.balanceView}
  486. android_ripple={ripple}
  487. onPress={() => toTopupPage()}>
  488. <Image
  489. style={styles.icon}
  490. source={require('../../images/icon/draw-wallet.png')}/>
  491. <Text style={styles.balanceText}>{currency}{userInfo.credit}</Text>
  492. </Pressable>
  493. {/* <Pressable
  494. style={styles.balanceView}
  495. android_ripple={ripple}
  496. onPress={() => {
  497. startPage(PageList.referral);
  498. }}>
  499. <Image
  500. style={styles.icon}
  501. source={require('../../images/icon/draw-gift.png')}/>
  502. <Text style={styles.referText}>Refer your friends to get $5 credit!</Text>
  503. </Pressable> *}
  504. </View>
  505. : <View style={ui.flex1}></View>s
  506. */}
  507. <View style={styles.bottomView}>
  508. <Image
  509. style={Styles.logo}
  510. resizeMode='contain'
  511. source={require('../../images/app-logo.png')}
  512. />
  513. <Text style={styles.versionText}>{app.displayName + ' ' + app.versionName}</Text>
  514. </View>
  515. {/* isLogin &&
  516. <Button
  517. style={styles.logoutView}
  518. text='Logout'
  519. textColor={textButton}
  520. borderRadius={0}
  521. onClick={() => logout()}/>
  522. */}
  523. </View>
  524. );
  525. }
  526. const styles = StyleSheet.create({
  527. drawerTop: {
  528. top: 0,
  529. left: 0,
  530. right: 0,
  531. height: statusHeight,
  532. position: 'absolute',
  533. fontSize: 10,
  534. backgroundColor: pageBackground
  535. },
  536. drawerView: {
  537. paddingTop: 16,
  538. paddingBottom: 8,
  539. minHeight: $vhs(101)
  540. },
  541. guessView: {
  542. padding: 16
  543. },
  544. loginView: {
  545. paddingTop: 16,
  546. paddingBottom: 8
  547. },
  548. avatar: {
  549. width: 66,
  550. height: 66,
  551. marginLeft: 24,
  552. borderWidth: 2,
  553. borderRadius: 80,
  554. borderColor: colorLight,
  555. },
  556. nickView: {
  557. marginTop: 4,
  558. flexDirection: 'row',
  559. ...$padding(12, 16)
  560. },
  561. nickname: {
  562. flex: 1,
  563. color: textPrimary,
  564. fontSize: 20,
  565. fontWeight: 'bold',
  566. paddingLeft: 16,
  567. },
  568. divideLogin: {
  569. height: 1,
  570. marginTop: 4,
  571. marginRight: 32,
  572. marginBottom: 12,
  573. backgroundColor: '#E5E5E5'
  574. },
  575. itemButton: {
  576. borderRadius: 0,
  577. backgroundColor: colorLight
  578. },
  579. itemView: {
  580. flex: 1,
  581. height: 56,
  582. paddingLeft: 16,
  583. marginBottom: 6,
  584. alignItems: 'center',
  585. flexDirection: 'row'
  586. },
  587. disableItem: {
  588. height: 56,
  589. paddingLeft: 16,
  590. marginBottom: 6,
  591. alignItems: 'center',
  592. flexDirection: 'row'
  593. },
  594. icon: {
  595. width: 26,
  596. height: 26,
  597. marginRight: 16
  598. },
  599. icon2: {
  600. width: 24,
  601. height: 24,
  602. marginLeft: 1,
  603. marginRight: 17
  604. },
  605. label: {
  606. flex: 1,
  607. color: textPrimary,
  608. fontSize: 16.5
  609. },
  610. disable: {
  611. color: '#999',
  612. fontSize: 18
  613. },
  614. divided: {
  615. height: 1,
  616. marginTop: 24,
  617. marginLeft: 16,
  618. backgroundColor: colorAccent
  619. },
  620. walletView: {
  621. paddingTop: 16,
  622. paddingLeft: 48,
  623. paddingRight: 48
  624. },
  625. balanceView: {
  626. marginTop: 16,
  627. borderRadius: 8,
  628. ...$padding(13, 22),
  629. alignItems: 'center',
  630. flexDirection: 'row',
  631. justifyContent: 'center',
  632. backgroundColor: 'rgba(0, 20, 137, 0.2)'
  633. },
  634. balanceText: {
  635. color: textPrimary,
  636. fontSize: 22,
  637. fontWeight: 'bold'
  638. },
  639. referText: {
  640. flex: 1,
  641. color: textPrimary,
  642. fontSize: 13
  643. },
  644. bottomView: {
  645. flex: 1,
  646. paddingTop: 48,
  647. paddingBottom: 0,
  648. alignItems: 'center',
  649. justifyContent: 'flex-end'
  650. },
  651. versionText: {
  652. color: textPrimary,
  653. fontSize: 10,
  654. paddingTop: 16
  655. },
  656. logoutView: {
  657. backgroundColor: colorPrimary
  658. },
  659. balanceText2: {
  660. color: textCancel,
  661. fontSize: 14,
  662. marginRight: 32
  663. }
  664. });