Drawer.js 16 KB

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