Drawer.js 18 KB

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