Drawer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /**
  2. * 首页抽屉菜单
  3. * @邠心vbe on 2021/03/23
  4. */
  5. import React, { Component } from 'react';
  6. import {View, Text, StyleSheet, Image, Pressable, BackHandler, Linking} 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. const Drawer = createDrawerNavigator();
  20. const drawerBackgroundColor = '#FFFCF8';
  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: drawerBackgroundColor
  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.chargeDetail, { stationInfo: {id: res.data.sitePk}, action: 'view'});
  122. } else if (res.msg) {
  123. toastShort(res.msg);
  124. } else {
  125. toastShort('No charging session found');
  126. }
  127. }).catch(err => {
  128. Dialog.dismissLoading();
  129. toastShort('No charging session found');
  130. })
  131. }
  132. const logout = () => {
  133. Dialog.showDialog({
  134. title: 'Sign out',
  135. message: 'Are you sure you want to sign out?',
  136. callback: btn => {
  137. if (btn == 'ok') {
  138. Dialog.showProgressDialog();
  139. setTimeout(() => {
  140. onLogout();
  141. }, 500);
  142. }
  143. }
  144. })
  145. }
  146. return (
  147. <View style={styles.drawerView}>
  148. { isLogin
  149. ? <View style={styles.loginView}>
  150. <Text
  151. style={styles.nickname}
  152. ellipsizeMode='tail'
  153. numberOfLines={1}>
  154. { userInfo.nickName
  155. ? userInfo.nickName
  156. : 'Logging...'
  157. }
  158. </Text>
  159. </View>
  160. : <View style={styles.guessView}>
  161. <Image
  162. style={Styles.logo}
  163. source={require('../../images/app-logo.png')}/>
  164. </View>
  165. }
  166. { isLogin
  167. ? <Button
  168. style={styles.itemButton}
  169. viewStyle={styles.itemView}
  170. onClick={() => {
  171. startPage(PageList.profile)
  172. }}>
  173. <MaterialCommunityIcons
  174. style={styles.icon}
  175. name="account-outline"
  176. color="#333"
  177. size={26}/>
  178. <Text style={styles.label}>Profile Settings</Text>
  179. </Button>
  180. : <Button
  181. style={styles.itemButton}
  182. viewStyle={styles.itemView}
  183. onClick={() => {
  184. startPage(PageList.login);
  185. }}>
  186. <MaterialCommunityIcons
  187. style={styles.icon2}
  188. name="login-variant"
  189. color="#333"
  190. size={24}/>
  191. <Text style={styles.label}>Sign In</Text>
  192. </Button>
  193. }
  194. <Button
  195. style={styles.itemButton}
  196. viewStyle={styles.itemView}
  197. onClick={() => {
  198. navigation.toggleDrawer();
  199. }}>
  200. <MaterialCommunityIcons
  201. style={styles.icon}
  202. name="home-outline"
  203. color="#333"
  204. size={26}/>
  205. <Text style={styles.label}>Home</Text>
  206. </Button>
  207. { isLogin
  208. ? <Button
  209. style={styles.itemButton}
  210. viewStyle={styles.itemView}
  211. onClick={() => getCharging()}>
  212. {/* <Image
  213. style={styles.icon}
  214. source={require('../../images/icon/draw-charge.png')}/> */}
  215. <MaterialCommunityIcons
  216. style={styles.icon}
  217. name="car-electric-outline"
  218. color="#333"
  219. size={26}
  220. />
  221. <Text style={styles.label}>Charging</Text>
  222. </Button>
  223. : <View
  224. style={styles.disableItem}>
  225. <MaterialCommunityIcons
  226. style={styles.icon}
  227. name="car-electric-outline"
  228. color="#999"
  229. size={26}
  230. />
  231. {/* <Image
  232. style={styles.icon}
  233. source={require('../../images/icon/draw-charge-no.png')}/> */}
  234. <Text style={styles.disable}>Charging</Text>
  235. </View>
  236. }
  237. { isLogin &&
  238. <Button
  239. style={styles.itemButton}
  240. viewStyle={styles.itemView}
  241. onClick={() => {
  242. startPage(PageList.wallet);
  243. }}>
  244. <Image
  245. style={styles.icon}
  246. source={require('../../images/icon/draw-transaction.png')}/>
  247. <Text style={styles.label}>Transactions</Text>
  248. </Button>
  249. }
  250. {/* <View
  251. style={styles.disableItem}>
  252. <Image
  253. style={styles.icon}
  254. source={require('../../images/icon/draw-transaction-no.png')}/>
  255. <Text style={styles.disable}>Transactions</Text>
  256. </View> */
  257. }
  258. <Button
  259. style={styles.itemButton}
  260. viewStyle={styles.itemView}
  261. onClick={() => {
  262. startPage(PageList.feedback);
  263. }}>
  264. <MaterialCommunityIcons
  265. style={styles.icon2}
  266. name="message-alert-outline"
  267. color="#222"
  268. size={24}/>
  269. <Text style={styles.label}>Feedback</Text>
  270. </Button>
  271. <Button
  272. style={styles.itemButton}
  273. viewStyle={styles.itemView}
  274. onClick={() => {
  275. startPage(PageList.privacy);
  276. }}>
  277. <Image
  278. style={styles.icon}
  279. source={require('../../images/icon/draw-privacy.png')}/>
  280. <Text style={styles.label}>Privacy Policy</Text>
  281. </Button>
  282. <Button
  283. style={styles.itemButton}
  284. viewStyle={styles.itemView}
  285. onClick={() => {
  286. startPage(PageList.condition);
  287. }}>
  288. <MaterialCommunityIcons
  289. style={styles.icon}
  290. name="file-eye-outline"
  291. color="#222"
  292. size={26}/>
  293. <Text style={styles.label}>Terms of Use</Text>
  294. </Button>
  295. { DEBUG &&
  296. <>
  297. <Button
  298. style={styles.itemButton}
  299. viewStyle={styles.itemView}
  300. onClick={() => {
  301. startPage(PageList.notify);
  302. }}>
  303. <Image
  304. style={styles.icon2}
  305. source={require('../../images/icon/draw-terms.png')}/>
  306. <Text style={styles.label}>Notification</Text>
  307. </Button>
  308. <Button
  309. style={styles.itemButton}
  310. viewStyle={styles.itemView}
  311. onClick={() => {
  312. startPage(isIOS ? PageList.notify : PageList.mapTest);
  313. }}>
  314. <Image
  315. style={styles.icon2}
  316. source={require('../../images/icon/draw-terms.png')}/>
  317. <Text style={styles.label}>Maps Test</Text>
  318. </Button>
  319. </>}
  320. {/* <Button
  321. style={styles.itemButton}
  322. viewStyle={styles.itemView}
  323. onClick={() => {
  324. Linking.openURL(host+"juicePicture/pdf/FAQ.v1.0.pdf")
  325. }}>
  326. <Feather
  327. style={styles.icon2}
  328. name="download"
  329. color="#333"
  330. size={24}
  331. />
  332. <Text style={styles.label}>FAQs</Text>
  333. </Button> */}
  334. <Button
  335. style={styles.itemButton}
  336. viewStyle={styles.itemView}
  337. onClick={() => {
  338. startPage(PageList.settings);
  339. }}>
  340. <Ionicons
  341. style={styles.icon}
  342. name="md-settings-outline"
  343. color="#222"
  344. size={25}
  345. />
  346. <Text style={styles.label}>Settings</Text>
  347. </Button>
  348. <Button
  349. style={styles.itemButton}
  350. viewStyle={styles.itemView}
  351. onClick={() => {
  352. startPage(PageList.about);
  353. }}>
  354. <MaterialCommunityIcons
  355. style={styles.icon}
  356. name="information-outline"
  357. color="#333"
  358. size={26}
  359. />
  360. <Text style={styles.label}>About Us</Text>
  361. </Button>
  362. <View style={styles.divided}></View>
  363. { isLogin
  364. ? <View style={styles.walletView}>
  365. <Pressable
  366. style={styles.balanceView}
  367. android_ripple={ripple}
  368. onPress={() => toTopupPage()}>
  369. <Image
  370. style={styles.icon}
  371. source={require('../../images/icon/draw-wallet.png')}/>
  372. <Text style={styles.balanceText}>{currency}{userInfo.credit}</Text>
  373. </Pressable>
  374. {/* <Pressable
  375. style={styles.balanceView}
  376. android_ripple={ripple}
  377. onPress={() => {
  378. startPage(PageList.referral);
  379. }}>
  380. <Image
  381. style={styles.icon}
  382. source={require('../../images/icon/draw-gift.png')}/>
  383. <Text style={styles.referText}>Refer your friends to get $5 credit!</Text>
  384. </Pressable> */}
  385. </View>
  386. : <View style={ui.flex1}></View>
  387. }
  388. <View style={styles.bottomView}>
  389. <Text style={{ color: '#333', fontSize: 10 }}>{app.displayName + ' ' + app.versionName}</Text>
  390. </View>
  391. { isLogin &&
  392. <Button
  393. style={styles.logoutView}
  394. text='Logout'
  395. textColor='#fff'
  396. borderRadius={0}
  397. onClick={() => logout()}/>
  398. }
  399. </View>
  400. );
  401. }
  402. const styles = StyleSheet.create({
  403. drawerTop: {
  404. top: 0,
  405. left: 0,
  406. right: 0,
  407. height: statusHeight,
  408. position: 'absolute',
  409. fontSize: 10,
  410. backgroundColor: drawerBackgroundColor
  411. },
  412. drawerView: {
  413. paddingTop: 16,
  414. minHeight: $vhs(100)
  415. },
  416. guessView: {
  417. padding: 16
  418. },
  419. loginView: {
  420. padding: 16
  421. },
  422. nickname: {
  423. color: '#333',
  424. fontSize:21,
  425. fontWeight: 'bold',
  426. paddingTop: 8,
  427. paddingBottom: 16
  428. },
  429. itemButton: {
  430. borderRadius: 0,
  431. backgroundColor: drawerBackgroundColor
  432. },
  433. itemView: {
  434. flex: 1,
  435. height: 56,
  436. paddingLeft: 16,
  437. marginBottom: 6,
  438. alignItems: 'center',
  439. flexDirection: 'row'
  440. },
  441. disableItem: {
  442. height: 56,
  443. paddingLeft: 16,
  444. marginBottom: 6,
  445. alignItems: 'center',
  446. flexDirection: 'row'
  447. },
  448. icon: {
  449. width: 26,
  450. height: 26,
  451. marginRight: 16
  452. },
  453. icon2: {
  454. width: 24,
  455. height: 24,
  456. marginLeft: 1,
  457. marginRight: 17
  458. },
  459. label: {
  460. color: '#333',
  461. fontSize: 16.5
  462. },
  463. disable: {
  464. color: '#999',
  465. fontSize: 18
  466. },
  467. divided: {
  468. height: 1,
  469. marginTop: 24,
  470. marginLeft: 16,
  471. backgroundColor: colorAccent
  472. },
  473. walletView: {
  474. paddingTop: 16,
  475. paddingLeft: 48,
  476. paddingRight: 48
  477. },
  478. balanceView: {
  479. marginTop: 16,
  480. borderRadius: 8,
  481. ...$padding(13, 22),
  482. alignItems: 'center',
  483. flexDirection: 'row',
  484. justifyContent: 'center',
  485. backgroundColor: 'rgba(0, 20, 137, 0.2)'
  486. },
  487. balanceText: {
  488. color: '#333',
  489. fontSize: 22,
  490. fontWeight: 'bold'
  491. },
  492. referText: {
  493. flex: 1,
  494. color: '#333',
  495. fontSize: 13
  496. },
  497. bottomView: {
  498. flex: 1,
  499. paddingTop: 48,
  500. paddingBottom: 8,
  501. alignItems: 'center',
  502. justifyContent: 'flex-end'
  503. },
  504. logoutView: {
  505. backgroundColor: colorPrimary
  506. }
  507. });