Drawer.js 13 KB

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