-
Notifications
You must be signed in to change notification settings - Fork 21
/
App.js
141 lines (127 loc) · 3.54 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, {Component} from 'react';
import {StatusBar} from 'react-native';
import {SafeAreaView} from 'react-navigation'
import Router from './src/router';
import {colors} from './src/assets/styles/colors-theme';
import {handleNavigationChange} from './src/common/history';
import {Provider as ProviderAntd, Modal} from '@ant-design/react-native'
import LoadingView from './src/common/loading';
import { Provider, observer } from 'mobx-react'
import * as stores from './src/stores/index';
import {handleErrors} from './src/common/global-error-handler';
import Event from './src/common/event';
@observer
export default class App extends Component {
constructor(props) {
super(props)
this.timer = null
this.state = {
loadingCount: 0
}
require('promise/setimmediate/rejection-tracking').enable({
allRejections: true,
onUnhandled: (id, error) => {
handleErrors(error);
}
})
this._handleGlobalError = this.handleGlobalError.bind(this)
this._handleShowLoading = this.handleShowLoading.bind(this)
this._handleHideLoading = this.handleHideLoading.bind(this)
}
componentDidMount() {
// 监听全局报错
Event.listen('GLOBAL_ERROR', this._handleGlobalError, this)
// 显示加载动画
Event.listen('SHOW_LOADING', this._handleShowLoading, this)
// 隐藏加载动画
Event.listen('HIDE_LOADING', this._handleHideLoading, this)
}
//组件卸载之前移除监听
componentWillUnmount() {
Event.remove('GLOBAL_ERROR', this)
Event.remove('SHOW_LOADING', this)
Event.remove('HIDE_LOADING', this)
}
render() {
return (
<Provider rootStore={stores}>
<ProviderAntd>
<SafeAreaView
style={{flex: 1, backgroundColor: colors.statusBarColor}}
forceInset={{
top: 'always',
bottom: 'always'
}}
>
<StatusBar
animated={true}
barStyle={'light-content'}
backgroundColor={colors.statusBarColor}
translucent={true}
/>
<Router
onNavigationStateChange={handleNavigationChange}
/>
<LoadingView visible={this.state.loadingCount > 0} />
</SafeAreaView>
</ProviderAntd>
</Provider>
);
}
/**
* showLoading
*/
handleShowLoading() {
if(this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => {
this.setState({
loadingCount: this.state.loadingCount + 1
})
}, 50)
}
/**
* hideLoading
* @param bForece
*/
handleHideLoading(bForece){
if(this.timer){
clearTimeout(this.timer);
}
this.timer = setTimeout(() => {
if(this.state.loadingCount > 0){
this.setState({
loadingCount: (bForece ? 0 : this.state.loadingCount - 1)
});
}
}, 50)
}
/**
* 全局报错处理
* @param event
*/
handleGlobalError(event) {
// 报错时,取消加载动画
if(this.state.loadingCount > 0){
this.handleHideLoading(true)
}
if(event && event.type){
switch(event.type){
case 'SESSION_TIMEOUT':
Modal.alert('会话超时', '您的会话已超时,请重新登录')
break;
case 'SERVICE_ERROR':
if(event.message) {
Modal.alert('出错了', event.message)
}
break;
default:
if(event.message) {
Modal.alert('温馨提示', '系统未知异常')
}
break;
}
}
}
}