Drawer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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.about);
  315. }}>
  316. <Image
  317. style={styles.icon2}
  318. source={require('../../images/icon/draw-about.png')}/>
  319. <Text style={styles.label}>About Us</Text>
  320. </Button>
  321. <View style={styles.divided}></View>
  322. { isLogin
  323. ? <View style={styles.walletView}>
  324. <Pressable
  325. style={styles.balanceView}
  326. android_ripple={ripple}
  327. onPress={() => {
  328. startPage(PageList.topup);
  329. }}>
  330. <Image
  331. style={styles.icon}
  332. source={require('../../images/icon/draw-wallet.png')}/>
  333. <Text style={styles.balanceText}>{currency}{userInfo.credit}</Text>
  334. </Pressable>
  335. <Pressable
  336. style={styles.balanceView}
  337. android_ripple={ripple}
  338. onPress={() => {
  339. startPage(PageList.referral);
  340. }}>
  341. <Image
  342. style={styles.icon}
  343. source={require('../../images/icon/draw-gift.png')}/>
  344. <Text style={styles.referText}>Refer your friends to get $5 credit!</Text>
  345. </Pressable>
  346. </View>
  347. : <View style={ui.flex1}></View>
  348. }
  349. <View style={styles.bottomView}>
  350. <Text style={{ color: '#333', fontSize: 10 }}>{app.displayName + ' ' + app.versionName}</Text>
  351. </View>
  352. { isLogin &&
  353. <Button
  354. style={styles.logoutView}
  355. text='Logout'
  356. borderRadius={0}
  357. onClick={() => logout()}/>
  358. }
  359. </View>
  360. );
  361. }
  362. const styles = StyleSheet.create({
  363. drawerTop: {
  364. top: 0,
  365. left: 0,
  366. right: 0,
  367. height: statusHeight,
  368. position: 'absolute',
  369. fontSize: 10,
  370. backgroundColor: drawerBackgroundColor
  371. },
  372. drawerView: {
  373. paddingTop: 16,
  374. minHeight: $vhs(100)
  375. },
  376. guessView: {
  377. padding: 16
  378. },
  379. loginView: {
  380. padding: 16
  381. },
  382. nickname: {
  383. color: '#333',
  384. fontSize:21,
  385. fontWeight: 'bold',
  386. paddingTop: 8,
  387. paddingBottom: 16
  388. },
  389. itemButton: {
  390. borderRadius: 0,
  391. backgroundColor: drawerBackgroundColor
  392. },
  393. itemView: {
  394. flex: 1,
  395. height: 56,
  396. paddingLeft: 16,
  397. marginBottom: 6,
  398. alignItems: 'center',
  399. flexDirection: 'row'
  400. },
  401. disableItem: {
  402. height: 56,
  403. paddingLeft: 16,
  404. marginBottom: 6,
  405. alignItems: 'center',
  406. flexDirection: 'row'
  407. },
  408. icon: {
  409. width: 26,
  410. height: 26,
  411. marginRight: 16
  412. },
  413. icon2: {
  414. width: 24,
  415. height: 24,
  416. marginLeft: 1,
  417. marginRight: 17
  418. },
  419. label: {
  420. color: '#333',
  421. fontSize: 16.5
  422. },
  423. disable: {
  424. color: '#999',
  425. fontSize: 18
  426. },
  427. divided: {
  428. height: 1,
  429. marginTop: 24,
  430. marginLeft: 16,
  431. backgroundColor: colorAccent
  432. },
  433. walletView: {
  434. paddingTop: 16,
  435. paddingLeft: 48,
  436. paddingRight: 48
  437. },
  438. balanceView: {
  439. marginTop: 16,
  440. borderRadius: 8,
  441. ...$padding(13, 22),
  442. alignItems: 'center',
  443. flexDirection: 'row',
  444. justifyContent: 'center',
  445. backgroundColor: 'rgba(255, 204, 44, 0.6)'
  446. },
  447. balanceText: {
  448. color: '#333',
  449. fontSize: 22,
  450. fontWeight: 'bold'
  451. },
  452. referText: {
  453. flex: 1,
  454. color: '#333',
  455. fontSize: 13
  456. },
  457. bottomView: {
  458. flex: 1,
  459. paddingTop: 48,
  460. paddingBottom: 8,
  461. alignItems: 'center',
  462. justifyContent: 'flex-end'
  463. },
  464. logoutView: {
  465. backgroundColor: colorAccent
  466. }
  467. });