Drawer.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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('No charging session found');
  133. }
  134. }).catch(err => {
  135. Dialog.dismissLoading();
  136. toastShort('No charging session found');
  137. })
  138. }
  139. const logout = () => {
  140. Dialog.showDialog({
  141. title: 'Sign out',
  142. message: 'Are you sure you want to sign out?',
  143. callback: btn => {
  144. if (btn == 'ok') {
  145. Dialog.showProgressDialog();
  146. setTimeout(() => {
  147. onLogout();
  148. }, 500);
  149. }
  150. }
  151. })
  152. }
  153. return (
  154. <View style={styles.drawerView}>
  155. <View style={styles.loginView}>
  156. { (isLogin && userInfo.photoUrl)
  157. ? <TouchableWithoutFeedback onPress={() => startPage(PageList.profile)}>
  158. <Image
  159. style={styles.avatar}
  160. source={{uri: host + userInfo.photoUrl}}/>
  161. </TouchableWithoutFeedback>
  162. : <TouchableWithoutFeedback onPress={() => startPage(PageList.login)}>
  163. <Image
  164. style={styles.avatar}
  165. source={require('../../images/user/ic-avatar-default.png')}/>
  166. </TouchableWithoutFeedback>
  167. }
  168. <Pressable
  169. style={styles.nickView}
  170. android_ripple={ripple}
  171. onPress={() => startPage(isLogin ? PageList.profile : PageList.login)}>
  172. <Text
  173. style={styles.nickname}
  174. ellipsizeMode='tail'
  175. numberOfLines={1}>
  176. { isLogin
  177. ? userInfo.nickName
  178. ? userInfo.nickName
  179. : 'Logging...'
  180. : "Sign In"
  181. }
  182. </Text>
  183. <FontAwesome
  184. size={24}
  185. color='#999'
  186. name='angle-right'/>
  187. </Pressable>
  188. </View>
  189. <View style={styles.divideLogin}></View>
  190. {/* isLogin
  191. ? <Button
  192. style={styles.itemButton}
  193. viewStyle={styles.itemView}
  194. onClick={() => {
  195. startPage(PageList.profile)
  196. }}>
  197. <Image
  198. style={styles.icon}
  199. source={require('../../images/icon/draw-user.png')}/>
  200. <Text style={styles.label}>Profile Settings</Text>
  201. </Button>
  202. : <Button
  203. style={styles.itemButton}
  204. viewStyle={styles.itemView}
  205. onClick={() => {
  206. startPage(PageList.login);
  207. }}>
  208. <Image
  209. style={styles.icon}
  210. source={require('../../images/icon/draw-user.png')}/>
  211. <Text style={styles.label}>Sign In</Text>
  212. </Button>
  213. */}
  214. {/* <Button
  215. style={styles.itemButton}
  216. viewStyle={styles.itemView}
  217. onClick={() => {
  218. navigation.toggleDrawer();
  219. }}>
  220. <Image
  221. style={styles.icon}
  222. source={require('../../images/icon/draw-home.png')}/>
  223. <Text style={styles.label}>Home</Text>
  224. </Button> */}
  225. { isLogin
  226. ? <Button
  227. style={styles.itemButton}
  228. viewStyle={styles.itemView}
  229. onClick={() => getCharging()}>
  230. <MaterialCommunityIcons
  231. style={styles.icon}
  232. name="car-electric-outline"
  233. color="#333"
  234. size={26}
  235. />
  236. {/* <Image
  237. style={styles.icon}
  238. source={require('../../images/icon/draw-charge.png')}/> */}
  239. <Text style={styles.label}>Charging</Text>
  240. </Button>
  241. : <View
  242. style={styles.disableItem}>
  243. <MaterialCommunityIcons
  244. style={styles.icon}
  245. name="car-electric-outline"
  246. color="#999"
  247. size={26}
  248. />
  249. {/* <Image
  250. style={styles.icon}
  251. source={require('../../images/icon/draw-charge-no.png')}/> */}
  252. <Text style={styles.disable}>Charging</Text>
  253. </View>
  254. }
  255. {/* isLogin
  256. ? <Button
  257. style={styles.itemButton}
  258. viewStyle={styles.itemView}
  259. onClick={() => {
  260. startPage(PageList.wallet);
  261. }}>
  262. <Image
  263. style={styles.icon}
  264. source={require('../../images/icon/draw-transaction.png')}/>
  265. <Text style={styles.label}>Transactions</Text>
  266. </Button>
  267. : <View
  268. style={styles.disableItem}>
  269. <Image
  270. style={styles.icon}
  271. source={require('../../images/icon/draw-transaction-no.png')}/>
  272. <Text style={styles.disable}>Transactions</Text>
  273. </View>
  274. */}
  275. {
  276. isLogin && <>
  277. <Button
  278. style={styles.itemButton}
  279. viewStyle={styles.itemView}
  280. onClick={() => {
  281. startPage(PageList.wallet);
  282. }}>
  283. <Image
  284. style={styles.icon}
  285. source={require('../../images/icon/draw-transaction.png')}/>
  286. <Text style={styles.label}>Transactions</Text>
  287. </Button>
  288. <Button
  289. style={styles.itemButton}
  290. viewStyle={styles.itemView}
  291. onClick={() => toTopupPage()}>
  292. <MaterialCommunityIcons
  293. style={styles.icon}
  294. size={26}
  295. name={"wallet-outline"}
  296. color={colorDark}/>
  297. <Text style={styles.label}>Wallet</Text>
  298. <Text style={styles.balanceText2}>{currency}{userInfo.credit}</Text>
  299. </Button></>
  300. }
  301. <Button
  302. style={styles.itemButton}
  303. viewStyle={styles.itemView}
  304. onClick={() => {
  305. startPage(PageList.feedback);
  306. }}>
  307. <MaterialCommunityIcons
  308. style={styles.icon2}
  309. name="message-alert-outline"
  310. color="#222"
  311. size={24}/>
  312. <Text style={styles.label}>Feedback</Text>
  313. </Button>
  314. {/* <Button
  315. style={styles.itemButton}
  316. viewStyle={styles.itemView}
  317. onClick={() => {
  318. Linking.openURL(host+"juicePicture/pdf/FAQ.v1.0.pdf")
  319. }}>
  320. <Feather
  321. style={styles.icon2}
  322. name="download"
  323. color={colorDark}
  324. size={24}
  325. />
  326. <Text style={styles.label}>FAQs</Text>
  327. </Button> */}
  328. <Button
  329. style={styles.itemButton}
  330. viewStyle={styles.itemView}
  331. onClick={() => {
  332. startPage(PageList.settings);
  333. }}>
  334. <Ionicons
  335. style={styles.icon}
  336. name="md-settings-outline"
  337. color="#222"
  338. size={25}
  339. />
  340. <Text style={styles.label}>Settings</Text>
  341. </Button>
  342. <Button
  343. style={styles.itemButton}
  344. viewStyle={styles.itemView}
  345. onClick={() => {
  346. startPage(PageList.about);
  347. }}>
  348. <MaterialCommunityIcons
  349. style={styles.icon}
  350. name="information-outline"
  351. color="#333"
  352. size={26}
  353. />
  354. <Text style={styles.label}>About Us</Text>
  355. </Button>
  356. {/* <View style={styles.divided}></View> */}
  357. { DEBUG &&
  358. <>
  359. <View style={styles.divideLogin}></View>
  360. <Text style={{color: "#ccc", paddingLeft: 16, paddingBottom: 8, fontSize: 12}}>Debug Mode Options Only</Text>
  361. <Button
  362. style={styles.itemButton}
  363. viewStyle={styles.itemView}
  364. onClick={() => {
  365. startPage(PageList.privacy);
  366. }}>
  367. <MaterialCommunityIcons
  368. style={styles.icon}
  369. name="file-eye-outline"
  370. color="#222"
  371. size={26}/>
  372. <Text style={styles.label}>Privacy Policy</Text>
  373. </Button>
  374. <Button
  375. style={styles.itemButton}
  376. viewStyle={styles.itemView}
  377. onClick={() => {
  378. startPage(PageList.condition);
  379. }}>
  380. <MaterialCommunityIcons
  381. style={styles.icon}
  382. name="file-eye-outline"
  383. color="#222"
  384. size={26}/>
  385. <Text style={styles.label}>Terms of Use</Text>
  386. </Button>
  387. <Button
  388. style={styles.itemButton}
  389. viewStyle={styles.itemView}
  390. onClick={() => {
  391. startPage(PageList.notify);
  392. }}>
  393. {/* <Image
  394. style={styles.icon2}
  395. source={require('../../images/icon/draw-terms.png')}/> */}
  396. <MaterialIcons
  397. style={styles.icon}
  398. name="notifications-none"
  399. color="#222"
  400. size={26}/>
  401. <Text style={styles.label}>Notification Test</Text>
  402. </Button>
  403. <Button
  404. style={styles.itemButton}
  405. viewStyle={styles.itemView}
  406. onClick={() => {
  407. startPage(isIOS ? PageList.notify : PageList.mapTest);
  408. }}>
  409. <MaterialCommunityIcons
  410. style={styles.icon}
  411. name="map-legend"
  412. color="#222"
  413. size={26}/>
  414. <Text style={styles.label}>Maps Test</Text>
  415. </Button>
  416. </>}
  417. {/* isLogin
  418. ? <View style={styles.walletView}>
  419. <Pressable
  420. style={styles.balanceView}
  421. android_ripple={ripple}
  422. onPress={() => toTopupPage()}>
  423. <Image
  424. style={styles.icon}
  425. source={require('../../images/icon/draw-wallet.png')}/>
  426. <Text style={styles.balanceText}>{currency}{userInfo.credit}</Text>
  427. </Pressable>
  428. {/* <Pressable
  429. style={styles.balanceView}
  430. android_ripple={ripple}
  431. onPress={() => {
  432. startPage(PageList.referral);
  433. }}>
  434. <Image
  435. style={styles.icon}
  436. source={require('../../images/icon/draw-gift.png')}/>
  437. <Text style={styles.referText}>Refer your friends to get $5 credit!</Text>
  438. </Pressable> *}
  439. </View>
  440. : <View style={ui.flex1}></View>s
  441. */}
  442. <View style={styles.bottomView}>
  443. <Image
  444. style={Styles.logo}
  445. resizeMode='contain'
  446. source={require('../../images/app-logo.png')}
  447. />
  448. <Text style={styles.versionText}>{app.displayName + ' ' + app.versionName}</Text>
  449. </View>
  450. {/* isLogin &&
  451. <Button
  452. style={styles.logoutView}
  453. text='Logout'
  454. textColor={textButton}
  455. borderRadius={0}
  456. onClick={() => logout()}/>
  457. */}
  458. </View>
  459. );
  460. }
  461. const styles = StyleSheet.create({
  462. drawerTop: {
  463. top: 0,
  464. left: 0,
  465. right: 0,
  466. height: statusHeight,
  467. position: 'absolute',
  468. fontSize: 10,
  469. backgroundColor: pageBackground
  470. },
  471. drawerView: {
  472. paddingTop: 16,
  473. paddingBottom: 8,
  474. minHeight: $vhs(101)
  475. },
  476. guessView: {
  477. padding: 16
  478. },
  479. loginView: {
  480. paddingTop: 16,
  481. paddingBottom: 8
  482. },
  483. avatar: {
  484. width: 66,
  485. height: 66,
  486. marginLeft: 24,
  487. borderWidth: 2,
  488. borderRadius: 80,
  489. borderColor: colorLight,
  490. },
  491. nickView: {
  492. marginTop: 4,
  493. flexDirection: 'row',
  494. ...$padding(12, 16)
  495. },
  496. nickname: {
  497. flex: 1,
  498. color: textPrimary,
  499. fontSize: 20,
  500. fontWeight: 'bold',
  501. paddingLeft: 16,
  502. },
  503. divideLogin: {
  504. height: 1,
  505. marginTop: 4,
  506. marginRight: 32,
  507. marginBottom: 12,
  508. backgroundColor: '#E5E5E5'
  509. },
  510. itemButton: {
  511. borderRadius: 0,
  512. backgroundColor: colorLight
  513. },
  514. itemView: {
  515. flex: 1,
  516. height: 56,
  517. paddingLeft: 16,
  518. marginBottom: 6,
  519. alignItems: 'center',
  520. flexDirection: 'row'
  521. },
  522. disableItem: {
  523. height: 56,
  524. paddingLeft: 16,
  525. marginBottom: 6,
  526. alignItems: 'center',
  527. flexDirection: 'row'
  528. },
  529. icon: {
  530. width: 26,
  531. height: 26,
  532. marginRight: 16
  533. },
  534. icon2: {
  535. width: 24,
  536. height: 24,
  537. marginLeft: 1,
  538. marginRight: 17
  539. },
  540. label: {
  541. flex: 1,
  542. color: textPrimary,
  543. fontSize: 16.5
  544. },
  545. disable: {
  546. color: '#999',
  547. fontSize: 18
  548. },
  549. divided: {
  550. height: 1,
  551. marginTop: 24,
  552. marginLeft: 16,
  553. backgroundColor: colorAccent
  554. },
  555. walletView: {
  556. paddingTop: 16,
  557. paddingLeft: 48,
  558. paddingRight: 48
  559. },
  560. balanceView: {
  561. marginTop: 16,
  562. borderRadius: 8,
  563. ...$padding(13, 22),
  564. alignItems: 'center',
  565. flexDirection: 'row',
  566. justifyContent: 'center',
  567. backgroundColor: 'rgba(0, 20, 137, 0.2)'
  568. },
  569. balanceText: {
  570. color: textPrimary,
  571. fontSize: 22,
  572. fontWeight: 'bold'
  573. },
  574. referText: {
  575. flex: 1,
  576. color: textPrimary,
  577. fontSize: 13
  578. },
  579. bottomView: {
  580. flex: 1,
  581. paddingTop: 48,
  582. paddingBottom: 0,
  583. alignItems: 'center',
  584. justifyContent: 'flex-end'
  585. },
  586. versionText: {
  587. color: textPrimary,
  588. fontSize: 10,
  589. paddingTop: 16
  590. },
  591. logoutView: {
  592. backgroundColor: colorPrimary
  593. },
  594. balanceText2: {
  595. color: textCancel,
  596. fontSize: 14,
  597. marginRight: 32
  598. }
  599. });