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. 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. <Image
  174. style={styles.icon}
  175. source={require('../../images/icon/draw-user.png')}/>
  176. <Text style={styles.label}>Profile Settings</Text>
  177. </Button>
  178. : <Button
  179. style={styles.itemButton}
  180. viewStyle={styles.itemView}
  181. onClick={() => {
  182. startPage(PageList.login);
  183. }}>
  184. <Image
  185. style={styles.icon}
  186. source={require('../../images/icon/draw-user.png')}/>
  187. <Text style={styles.label}>Sign In</Text>
  188. </Button>
  189. }
  190. <Button
  191. style={styles.itemButton}
  192. viewStyle={styles.itemView}
  193. onClick={() => {
  194. navigation.toggleDrawer();
  195. }}>
  196. <Image
  197. style={styles.icon}
  198. source={require('../../images/icon/draw-home.png')}/>
  199. <Text style={styles.label}>Home</Text>
  200. </Button>
  201. { isLogin
  202. ? <Button
  203. style={styles.itemButton}
  204. viewStyle={styles.itemView}
  205. onClick={() => getCharging()}>
  206. <Image
  207. style={styles.icon}
  208. source={require('../../images/icon/draw-charge.png')}/>
  209. <Text style={styles.label}>Charging</Text>
  210. </Button>
  211. : <View
  212. style={styles.disableItem}>
  213. <Image
  214. style={styles.icon}
  215. source={require('../../images/icon/draw-charge-no.png')}/>
  216. <Text style={styles.disable}>Charging</Text>
  217. </View>
  218. }
  219. { isLogin
  220. ? <Button
  221. style={styles.itemButton}
  222. viewStyle={styles.itemView}
  223. onClick={() => {
  224. startPage(PageList.wallet);
  225. }}>
  226. <Image
  227. style={styles.icon}
  228. source={require('../../images/icon/draw-transaction.png')}/>
  229. <Text style={styles.label}>Transactions</Text>
  230. </Button>
  231. : <View
  232. style={styles.disableItem}>
  233. <Image
  234. style={styles.icon}
  235. source={require('../../images/icon/draw-transaction-no.png')}/>
  236. <Text style={styles.disable}>Transactions</Text>
  237. </View>
  238. }
  239. <Button
  240. style={styles.itemButton}
  241. viewStyle={styles.itemView}
  242. onClick={() => {
  243. startPage(PageList.feedback);
  244. }}>
  245. <Image
  246. style={styles.icon2}
  247. source={require('../../images/icon/draw-feedback.png')}/>
  248. <Text style={styles.label}>Feedback</Text>
  249. </Button>
  250. <Button
  251. style={styles.itemButton}
  252. viewStyle={styles.itemView}
  253. onClick={() => {
  254. startPage(PageList.privacy);
  255. }}>
  256. <Image
  257. style={styles.icon2}
  258. source={require('../../images/icon/draw-privacy.png')}/>
  259. <Text style={styles.label}>Privacy Policy</Text>
  260. </Button>
  261. { DEBUG &&
  262. <>
  263. <Button
  264. style={styles.itemButton}
  265. viewStyle={styles.itemView}
  266. onClick={() => {
  267. startPage(PageList.condition);
  268. }}>
  269. <Image
  270. style={styles.icon2}
  271. source={require('../../images/icon/draw-terms.png')}/>
  272. <Text style={styles.label}>Terms and Conditions</Text>
  273. </Button>
  274. <Button
  275. style={styles.itemButton}
  276. viewStyle={styles.itemView}
  277. onClick={() => {
  278. startPage(PageList.notify);
  279. }}>
  280. <Image
  281. style={styles.icon2}
  282. source={require('../../images/icon/draw-terms.png')}/>
  283. <Text style={styles.label}>Notification</Text>
  284. </Button>
  285. <Button
  286. style={styles.itemButton}
  287. viewStyle={styles.itemView}
  288. onClick={() => {
  289. startPage(isIOS ? PageList.notify : PageList.mapTest);
  290. }}>
  291. <Image
  292. style={styles.icon2}
  293. source={require('../../images/icon/draw-terms.png')}/>
  294. <Text style={styles.label}>Maps Test</Text>
  295. </Button>
  296. </>}
  297. {/* <Button
  298. style={styles.itemButton}
  299. viewStyle={styles.itemView}
  300. onClick={() => {
  301. Linking.openURL(host+"juicePicture/pdf/FAQ.v1.0.pdf")
  302. }}>
  303. <Feather
  304. style={styles.icon2}
  305. name="download"
  306. color="#333"
  307. size={24}
  308. />
  309. <Text style={styles.label}>FAQs</Text>
  310. </Button> */}
  311. <Button
  312. style={styles.itemButton}
  313. viewStyle={styles.itemView}
  314. onClick={() => {
  315. startPage(PageList.settings);
  316. }}>
  317. <Ionicons
  318. style={styles.icon2}
  319. name="settings-outline"
  320. color="#333"
  321. size={24}/>
  322. <Text style={styles.label}>Settings</Text>
  323. </Button>
  324. <Button
  325. style={styles.itemButton}
  326. viewStyle={styles.itemView}
  327. onClick={() => {
  328. startPage(PageList.about);
  329. }}>
  330. <Image
  331. style={styles.icon2}
  332. source={require('../../images/icon/draw-about.png')}/>
  333. <Text style={styles.label}>About Us</Text>
  334. </Button>
  335. <View style={styles.divided}></View>
  336. { isLogin
  337. ? <View style={styles.walletView}>
  338. <Pressable
  339. style={styles.balanceView}
  340. android_ripple={ripple}
  341. onPress={() => toTopupPage()}>
  342. <Image
  343. style={styles.icon}
  344. source={require('../../images/icon/draw-wallet.png')}/>
  345. <Text style={styles.balanceText}>{currency}{userInfo.credit}</Text>
  346. </Pressable>
  347. {/* <Pressable
  348. style={styles.balanceView}
  349. android_ripple={ripple}
  350. onPress={() => {
  351. startPage(PageList.referral);
  352. }}>
  353. <Image
  354. style={styles.icon}
  355. source={require('../../images/icon/draw-gift.png')}/>
  356. <Text style={styles.referText}>Refer your friends to get $5 credit!</Text>
  357. </Pressable> */}
  358. </View>
  359. : <View style={ui.flex1}></View>
  360. }
  361. <View style={styles.bottomView}>
  362. <Text style={{ color: '#333', fontSize: 10 }}>{app.displayName + ' ' + app.versionName}</Text>
  363. </View>
  364. { isLogin &&
  365. <Button
  366. style={styles.logoutView}
  367. text='Logout'
  368. textColor='#fff'
  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(0, 20, 137, 0.2)'
  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: colorPrimary
  479. }
  480. });