Index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. import DrawerViewV3 from './DrawerV3.js';
  19. import DrawerViewV4 from './DrawerV4.js';
  20. import crashlytics from '@react-native-firebase/crashlytics';
  21. const Drawer = createDrawerNavigator();
  22. export default class Home extends Component {
  23. constructor(props) {
  24. super(props);
  25. this.state = {
  26. isLogin: false,
  27. userInfo: {},
  28. notificationCount: 0,
  29. sideCountInfo: {}
  30. }
  31. }
  32. componentDidMount() {
  33. AutoLogin(() => {
  34. this.setState({
  35. userInfo: userInfo
  36. });
  37. });
  38. this.props.navigation.addListener('focus', () => {
  39. //console.log('drawer focus');
  40. getUserInfo(info => {
  41. this.setState({
  42. userInfo: info
  43. });
  44. crashlytics().setUserId("" + info.userPk)
  45. }, true);
  46. if (app.notifications.enable && this.state.isLogin) {
  47. this.getNotificationTotal();
  48. }
  49. });
  50. /*BackHandler.addEventListener('hardwareBackPress', (e) => {
  51. if (global.dialogId !== 0) {
  52. Dialog.dismissLoading();
  53. return true;
  54. }
  55. return false;
  56. })*/
  57. }
  58. componentDidUpdate() {
  59. const status = isLogin();
  60. if (this.state.isLogin != status) {
  61. this.setState({
  62. isLogin: status
  63. }, () => {
  64. getUserInfo(info => {
  65. this.setState({
  66. userInfo: info
  67. });
  68. if (info.firebaseToken) {
  69. let token = isIOS ? info.firebaseToken?.ios : info.firebaseToken?.android
  70. if (notifyToken.token) {
  71. utils.registerFirebaseToken(token ?? "");
  72. }
  73. }
  74. }, true);
  75. if (app.notifications.enable && status) {
  76. this.getNotificationTotal();
  77. }
  78. });
  79. }
  80. }
  81. async requestLogout() {
  82. const data = await getStorageJsonSync('loginData');
  83. if (data && data.email) {
  84. delete data.password
  85. setStorageJson('loginData', data);
  86. setStorage('RegisterTokenDate', "");
  87. }
  88. global.userInfo = {}
  89. this.setState({
  90. isLogin: false,
  91. userInfo: {}
  92. });
  93. setAccessToken('');
  94. Dialog.dismissLoading();
  95. }
  96. getNotificationTotal() {
  97. apiNotification.getUnreadTotal().then(res => {
  98. if (res.data) {
  99. this.setState({
  100. sideCountInfo: res.data,
  101. notificationCount: res.data?.toBeReadCount ?? 0
  102. })
  103. } else {
  104. this.setState({
  105. sideCountInfo: {},
  106. notificationCount: 0
  107. })
  108. }
  109. }).catch(err => {
  110. this.setState({
  111. sideCountInfo: {},
  112. notificationCount: 0
  113. })
  114. })
  115. }
  116. render () {
  117. return (
  118. <Drawer.Navigator
  119. initialRouteName='maps'
  120. drawerContent={props =>
  121. <CustomerDrawerContent
  122. {...props}
  123. isLogin={this.state.isLogin}
  124. userInfo={this.state.userInfo}
  125. onLogout={() => this.requestLogout()}
  126. sideCountInfo={this.state.sideCountInfo}
  127. notificationCount={this.state.notificationCount}
  128. />
  129. }
  130. screenOptions={{
  131. headerShown: false,
  132. drawerType: global.$width >= 768 ? 'back' : 'front',
  133. drawerStyle: {
  134. width: (app.v3.drawer && !app.isLumiWhitelabel) ? $vw(100) : ($vw(75) > 320 ? 320 : $vw(75)),
  135. backgroundColor: (app.v3.drawer && !app.isLumiWhitelabel) ? 'rgba(0,0,0,0.1)' : colorLight
  136. },
  137. swipeEnabled: (!app.v3.drawer || app.isLumiWhitelabel), //启用侧滑打开抽屉
  138. }}>
  139. <Drawer.Screen name="maps" component={Maps} />
  140. </Drawer.Navigator>
  141. );
  142. }
  143. }
  144. const CustomerDrawerContent = (props) => {
  145. return (
  146. <DrawerContentScrollView
  147. {...props}
  148. canCancelContentTouches={true}
  149. style={(app.v3.drawer && !app.isLumiWhitelabel) ? styles.contentV2 : styles.content}
  150. contentContainerStyle={{paddingStart: 0, paddingEnd: 0}}>
  151. { app.v4.drawer
  152. ? <DrawerViewV4 {...props}/>
  153. : app.v3.drawer
  154. ? app.isLumiWhitelabel
  155. ? <DrawerViewV3 {...props}/>
  156. : <DrawerViewV2 {...props}/>
  157. : <DrawerView {...props}/>
  158. }
  159. </DrawerContentScrollView>
  160. );
  161. }
  162. const styles = StyleSheet.create({
  163. content: {
  164. backgroundColor: colorLight
  165. },
  166. contentV2: {
  167. width: $vw(75) > 320 ? 320 : $vw(75),
  168. backgroundColor: colorLight
  169. }
  170. })