Index.js 5.0 KB

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