Index.js 4.7 KB

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