Drawer.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. <Button
  392. style={styles.itemButton}
  393. viewStyle={styles.itemView}
  394. onClick={() => {
  395. startPage(PageList.about);
  396. }}>
  397. <MaterialCommunityIcons
  398. style={styles.icon}
  399. name="information-outline"
  400. color="#333"
  401. size={26}
  402. />
  403. <Text style={styles.label}>{$t('drawer.about')}</Text>
  404. </Button>
  405. {/* <View style={styles.divided}></View> */}
  406. { DEBUG &&
  407. <>
  408. <View style={styles.divideLogin}></View>
  409. <Text style={{color: "#ccc", paddingLeft: 16, paddingBottom: 8, fontSize: 12}}>{$t('drawer.debugOnly')}</Text>
  410. <Button
  411. style={styles.itemButton}
  412. viewStyle={styles.itemView}
  413. onClick={() => {
  414. startPage(PageList.privacy);
  415. }}>
  416. <MaterialCommunityIcons
  417. style={styles.icon}
  418. name="file-eye-outline"
  419. color="#222"
  420. size={26}/>
  421. <Text style={styles.label}>{$t('drawer.privacyPolicy')}</Text>
  422. </Button>
  423. <Button
  424. style={styles.itemButton}
  425. viewStyle={styles.itemView}
  426. onClick={() => {
  427. startPage(PageList.condition);
  428. }}>
  429. <MaterialCommunityIcons
  430. style={styles.icon}
  431. name="file-eye-outline"
  432. color="#222"
  433. size={26}/>
  434. <Text style={styles.label}>{$t('drawer.termsOfUse')}</Text>
  435. </Button>
  436. <Button
  437. style={styles.itemButton}
  438. viewStyle={styles.itemView}
  439. onClick={() => {
  440. startPage(PageList.notify);
  441. }}>
  442. {/* <Image
  443. style={styles.icon2}
  444. source={require('../../images/icon/draw-terms.png')}/> */}
  445. <MaterialIcons
  446. style={styles.icon}
  447. name="notifications-none"
  448. color="#222"
  449. size={26}/>
  450. <Text style={styles.label}>{$t('route.notificationTest')}</Text>
  451. </Button>
  452. <Button
  453. style={styles.itemButton}
  454. viewStyle={styles.itemView}
  455. onClick={() => {
  456. startPage(isIOS ? PageList.notify : PageList.mapTest);
  457. }}>
  458. <MaterialCommunityIcons
  459. style={styles.icon}
  460. name="map-legend"
  461. color="#222"
  462. size={26}/>
  463. <Text style={styles.label}>{$t('drawer.mapsTest')}</Text>
  464. </Button>
  465. </>}
  466. {/* isLogin
  467. ? <View style={styles.walletView}>
  468. <Pressable
  469. style={styles.balanceView}
  470. android_ripple={ripple}
  471. onPress={() => toTopupPage()}>
  472. <Image
  473. style={styles.icon}
  474. source={require('../../images/icon/draw-wallet.png')}/>
  475. <Text style={styles.balanceText}>{currency}{userInfo.credit}</Text>
  476. </Pressable>
  477. {/* <Pressable
  478. style={styles.balanceView}
  479. android_ripple={ripple}
  480. onPress={() => {
  481. startPage(PageList.referral);
  482. }}>
  483. <Image
  484. style={styles.icon}
  485. source={require('../../images/icon/draw-gift.png')}/>
  486. <Text style={styles.referText}>Refer your friends to get $5 credit!</Text>
  487. </Pressable> *}
  488. </View>
  489. : <View style={ui.flex1}></View>s
  490. */}
  491. <View style={styles.bottomView}>
  492. <Image
  493. style={Styles.logo}
  494. resizeMode='contain'
  495. source={require('../../images/app-logo.png')}
  496. />
  497. <Text style={styles.versionText}>{app.displayName + ' ' + app.versionName}</Text>
  498. </View>
  499. {/* isLogin &&
  500. <Button
  501. style={styles.logoutView}
  502. text='Logout'
  503. textColor={textButton}
  504. borderRadius={0}
  505. onClick={() => logout()}/>
  506. */}
  507. </View>
  508. );
  509. }
  510. const styles = StyleSheet.create({
  511. drawerTop: {
  512. top: 0,
  513. left: 0,
  514. right: 0,
  515. height: statusHeight,
  516. position: 'absolute',
  517. fontSize: 10,
  518. backgroundColor: pageBackground
  519. },
  520. drawerView: {
  521. paddingTop: 16,
  522. paddingBottom: 8,
  523. minHeight: $vhs(101)
  524. },
  525. guessView: {
  526. padding: 16
  527. },
  528. loginView: {
  529. paddingTop: 16,
  530. paddingBottom: 8
  531. },
  532. avatar: {
  533. width: 66,
  534. height: 66,
  535. marginLeft: 24,
  536. borderWidth: 2,
  537. borderRadius: 80,
  538. borderColor: colorLight,
  539. },
  540. nickView: {
  541. marginTop: 4,
  542. flexDirection: 'row',
  543. ...$padding(12, 16)
  544. },
  545. nickname: {
  546. flex: 1,
  547. color: textPrimary,
  548. fontSize: 20,
  549. fontWeight: 'bold',
  550. paddingLeft: 16,
  551. },
  552. divideLogin: {
  553. height: 1,
  554. marginTop: 4,
  555. marginRight: 32,
  556. marginBottom: 12,
  557. backgroundColor: '#E5E5E5'
  558. },
  559. itemButton: {
  560. borderRadius: 0,
  561. backgroundColor: colorLight
  562. },
  563. itemView: {
  564. flex: 1,
  565. height: 56,
  566. paddingLeft: 16,
  567. marginBottom: 6,
  568. alignItems: 'center',
  569. flexDirection: 'row'
  570. },
  571. disableItem: {
  572. height: 56,
  573. paddingLeft: 16,
  574. marginBottom: 6,
  575. alignItems: 'center',
  576. flexDirection: 'row'
  577. },
  578. icon: {
  579. width: 26,
  580. height: 26,
  581. marginRight: 16
  582. },
  583. icon2: {
  584. width: 24,
  585. height: 24,
  586. marginLeft: 1,
  587. marginRight: 17
  588. },
  589. label: {
  590. flex: 1,
  591. color: textPrimary,
  592. fontSize: 16.5
  593. },
  594. disable: {
  595. color: '#999',
  596. fontSize: 18
  597. },
  598. divided: {
  599. height: 1,
  600. marginTop: 24,
  601. marginLeft: 16,
  602. backgroundColor: colorAccent
  603. },
  604. walletView: {
  605. paddingTop: 16,
  606. paddingLeft: 48,
  607. paddingRight: 48
  608. },
  609. balanceView: {
  610. marginTop: 16,
  611. borderRadius: 8,
  612. ...$padding(13, 22),
  613. alignItems: 'center',
  614. flexDirection: 'row',
  615. justifyContent: 'center',
  616. backgroundColor: 'rgba(0, 20, 137, 0.2)'
  617. },
  618. balanceText: {
  619. color: textPrimary,
  620. fontSize: 22,
  621. fontWeight: 'bold'
  622. },
  623. referText: {
  624. flex: 1,
  625. color: textPrimary,
  626. fontSize: 13
  627. },
  628. bottomView: {
  629. flex: 1,
  630. paddingTop: 48,
  631. paddingBottom: 0,
  632. alignItems: 'center',
  633. justifyContent: 'flex-end'
  634. },
  635. versionText: {
  636. color: textPrimary,
  637. fontSize: 10,
  638. paddingTop: 16
  639. },
  640. logoutView: {
  641. backgroundColor: colorPrimary
  642. },
  643. balanceText2: {
  644. color: textCancel,
  645. fontSize: 14,
  646. marginRight: 32
  647. }
  648. });