Index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * 首页抽屉菜单
  3. * @邠心vbe on 2021/03/23
  4. */
  5. import React, { Component } from 'react';
  6. import {View, Text, StyleSheet, Image} from 'react-native';
  7. import { createDrawerNavigator, DrawerContentScrollView } from '@react-navigation/drawer';
  8. import app from '../../../app.json';
  9. import Maps from './Home';
  10. import Dialog from '../../components/Dialog';
  11. import { setAccessToken } from '../../api/http';
  12. import { getStorageJsonSync, setStorage, setStorageJson } from '../../utils/storage';
  13. import { AutoLogin } from '../sign/Login';
  14. import utils from '../../utils/utils';
  15. import apiNotification from '../../api/apiNotification';
  16. import DrawerView from './Drawer.js';
  17. import DrawerViewV2 from './DrawerV2.js';
  18. const Drawer = createDrawerNavigator();
  19. export default class Home extends Component {
  20. constructor(props) {
  21. super(props);
  22. this.state = {
  23. isLogin: false,
  24. userInfo: {},
  25. notificationCount: 0,
  26. sideCountInfo: {}
  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. if (app.notifications.enable && this.state.isLogin) {
  43. this.getNotificationTotal();
  44. }
  45. });
  46. /*BackHandler.addEventListener('hardwareBackPress', (e) => {
  47. if (global.dialogId !== 0) {
  48. Dialog.dismissLoading();
  49. return true;
  50. }
  51. return false;
  52. })*/
  53. }
  54. componentDidUpdate() {
  55. const status = isLogin();
  56. if (this.state.isLogin != status) {
  57. this.setState({
  58. isLogin: status
  59. }, () => {
  60. getUserInfo(info => {
  61. this.setState({
  62. userInfo: info
  63. });
  64. if (info.firebaseToken) {
  65. let token = isIOS ? info.firebaseToken?.ios : info.firebaseToken?.android
  66. if (notifyToken.token) {
  67. utils.registerFirebaseToken(token ?? "");
  68. }
  69. }
  70. }, true);
  71. if (app.notifications.enable && status) {
  72. this.getNotificationTotal();
  73. }
  74. });
  75. }
  76. }
  77. async requestLogout() {
  78. const data = await getStorageJsonSync('loginData');
  79. if (data && data.email) {
  80. delete data.password
  81. setStorageJson('loginData', data);
  82. setStorage('RegisterTokenDate', "");
  83. }
  84. global.userInfo = {}
  85. this.setState({
  86. isLogin: false,
  87. userInfo: {}
  88. });
  89. setAccessToken('');
  90. Dialog.dismissLoading();
  91. }
  92. getNotificationTotal() {
  93. apiNotification.getUnreadTotal().then(res => {
  94. if (res.data) {
  95. this.setState({
  96. sideCountInfo: res.data,
  97. notificationCount: res.data?.toBeReadCount ?? 0
  98. })
  99. } else {
  100. this.setState({
  101. sideCountInfo: {},
  102. notificationCount: 0
  103. })
  104. }
  105. }).catch(err => {
  106. this.setState({
  107. sideCountInfo: {},
  108. notificationCount: 0
  109. })
  110. })
  111. }
  112. render () {
  113. return (
  114. <Drawer.Navigator
  115. initialRouteName='maps'
  116. drawerContent={props =>
  117. <CustomerDrawerContent
  118. {...props}
  119. isLogin={this.state.isLogin}
  120. userInfo={this.state.userInfo}
  121. onLogout={() => this.requestLogout()}
  122. sideCountInfo={this.state.sideCountInfo}
  123. notificationCount={this.state.notificationCount}
  124. />
  125. }
  126. screenOptions={{
  127. headerShown: false,
  128. drawerType: global.$width >= 768 ? 'back' : 'front',
  129. drawerStyle: {
  130. width: app.v3.drawer ? $vw(100) : ($vw(75) > 320 ? 320 : $vw(75)),
  131. backgroundColor: app.v3.drawer ? 'rgba(0,0,0,0.1)' : colorLight
  132. },
  133. swipeEnabled: !app.v3.drawer, //启用侧滑打开抽屉
  134. }}>
  135. <Drawer.Screen name="maps" component={Maps} />
  136. </Drawer.Navigator>
  137. );
  138. }
  139. };
  140. const CustomerDrawerContent = (props) => {
  141. return (
  142. <DrawerContentScrollView
  143. {...props}
  144. canCancelContentTouches={true}
  145. style={app.v3.drawer ? styles.contentV2 : {}}>
  146. { app.v3.drawer
  147. ? <DrawerViewV2 {...props}/>
  148. : <DrawerView {...props}/>
  149. }
  150. </DrawerContentScrollView>
  151. );
  152. }
  153. const styles = StyleSheet.create({
  154. contentV2: {
  155. width: $vw(75) > 320 ? 320 : $vw(75),
  156. backgroundColor: colorLight
  157. }
  158. })