Index.js 4.6 KB

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