Index.js 5.2 KB

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