-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
382 lines (346 loc) · 11.9 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import React, { Component } from 'react'
import { Image, View, TextInput, Button } from 'react-native'
//导入container导航栏
import { createAppContainer } from 'react-navigation'
//导入导航栏Stack
import { createStackNavigator } from 'react-navigation-stack';
//导入底部导航栏组件
import { createBottomTabNavigator } from 'react-navigation-tabs'
//LoginPage
import LoginPage from './page/login/loginPage'
//BottomNavigation
import AccountingPage from './page/accounting/accountingPage'
import MinePage from './page/mine/minePage'
//AccountingPage
import SearchPage from './page/accounting/search/searchPage'
import AnalysePage from './page/accounting/analyse/analysePage'
import TypeDetailPage from './page/accounting/analyse/typeDetailPage'
//MinePage
import UserInfoPage from './page/mine/userInfo/userInfoPage'
import TeachingPage from './page/mine/teaching/teachingPage'
import WalletPage from './page/mine/wallet/walletPage'
import TransferAccountPage from './page/mine/wallet/transfer/transferAccountPage'
import WalletAccountPage from './page/mine/wallet/walletAccount/walletAccountPage'
import WalletAccountSettingPage from './page/mine/wallet/walletAccount/walletAccountSettingPage'
import ConsumeDetailPage from './page/mine/wallet/walletAccount/consumeDetailPage'
import NewAccountPage from './page/mine/wallet/newAccount/newAccountPage'
import ChooseAccountTypePage from './page/mine/wallet/newAccount/chooseAccountTypePage'
import ReminderPage from './page/mine/reminder/reminderPage'
import SettingPage from './page/mine/setting/settingPage'
import AboutPage from './page/mine/about/aboutPage'
import UserPasswordPage from './page/mine/setting/userPasswordPage'
import { Heading1, styles_common, Paragraph } from './assets/js/CommonStyles'
import AsyncStorage from '@react-native-community/async-storage'
import { Toast, Provider } from '@ant-design/react-native'
//底部TabBar Navigator
const BottomTabNavigator = createBottomTabNavigator(
{
Accounting: {
screen: AccountingPage,
navigationOptions: {
title: '记账',
tabBarLabel: '记账',
}
},
Mine: {
screen: MinePage,
navigationOptions: {
title: '我的',
tabBarLabel: '我的',
}
},
},
{
defaultNavigationOptions: ({ navigation }) => ({
//taBar图标
tabBarIcon: ({ focused }) => {
const { routeName } = navigation.state;
if (routeName === 'Accounting') {
if (focused) {
return (
<Image style={{width: 30,height: 30,}} source={require('./assets/img/account0.png')}/>
);
}
return (
<Image style={{width: 30,height: 30,}} source={require('./assets/img/account1.png')}/>
);
} else if (routeName === 'Mine') {
if (focused) {
return (
<Image style={{width: 30,height: 30,}} source={require('./assets/img/mine0.png')}/>
);
}
return (
<Image style={{width: 30,height: 30,}} source={require('./assets/img/mine1.png')}/>
);
}
},
}),
//taBar文字两种状态颜色
tabBarOptions: {
activeTintColor: '#EEBF30',
inactiveTintColor: '#BEC2CA',
},
initialRouteName: 'Accounting'
}
)
const RootStack = createStackNavigator(
{
BottomTabNavigator: {screen: BottomTabNavigator},
Login: {screen: LoginPage},
//Accounting 页面
//搜索页面
Search: {screen: SearchPage},
//分析页面
Analyse: {screen: AnalysePage},
//分析类型分类详情页面
TypeDetail: {screen: TypeDetailPage},
//Mine 页面
//个人信息
UserInfo: {screen: UserInfoPage},
//调教页面
Teaching: {screen: TeachingPage},
//我的钱包
Wallet: {screen: WalletPage},
//转账页面
TransferAccount: {screen: TransferAccountPage},
//钱包单个账户页面
WalletAccount: {screen: WalletAccountPage},
//单个账户设置页面(NewAccountPage共用)
WalletAccountSetting: {screen: WalletAccountSettingPage},
//账户每笔消费页面(AccountPage共用) 未书写
ConsumeDetail: {screen: ConsumeDetailPage},
//选择账户类型页面
ChooseAccountType: {screen: ChooseAccountTypePage},
//新建账户页面
NewAccount: {screen: NewAccountPage},
//记账提醒
Reminder: {screen: ReminderPage},
//设置
Setting: {screen: SettingPage},
//修改密码页面
UserPassword: {screen: UserPasswordPage},
//关于APP
About: {screen: AboutPage},
},
{
headerMode: 'none',
}
);
//create AppContainer
//AppContainer里是BottomTabNavigator和MainStack,包含了所有APP登录后的内容
const AppContainer = createAppContainer(RootStack);
// const AuthContext = React.createContext();
// function SplashScreen() {
// return (
// <View style={[styles_common.columnCenterCenter, styles_common.containerWhite]}>
// <Image source={require('./assets/img/MyAccounter.png')} style={{width: 200, height: 200}}/>
// <Heading1>MyAccounter</Heading1>
// <Paragraph>Loading...</Paragraph>
// <View style={{height: 100}}/>
// </View>
// );
// }
// // function HomeScreen() {
// // const { signOut } = React.useContext(AuthContext);
// // return (
// // <View>
// // <Text>Signed in!</Text>
// // <Button title="Sign out" onPress={signOut} />
// // </View>
// // );
// // }
// function SignInScreen() {
// const [username, setUsername] = React.useState('');
// const [password, setPassword] = React.useState('');
// const { signIn } = React.useContext(AuthContext);
// return (
// <View>
// <TextInput
// placeholder="Username"
// value={username}
// onChangeText={setUsername}
// />
// <TextInput
// placeholder="Password"
// value={password}
// onChangeText={setPassword}
// secureTextEntry
// />
// <Button title="Sign in" onPress={() => signIn({ username, password })} />
// </View>
// );
// }
// const Stack = createStackNavigator();
// export default function App({ navigation }) {
// const [state, dispatch] = React.useReducer(
// (prevState, action) => {
// switch (action.type) {
// case 'RESTORE_TOKEN':
// return {
// ...prevState,
// userToken: action.token,
// isLoading: false,
// };
// case 'SIGN_IN':
// return {
// ...prevState,
// isSignout: false,
// userToken: action.token,
// };
// case 'SIGN_OUT':
// return {
// ...prevState,
// isSignout: true,
// userToken: null,
// };
// }
// },
// {
// isLoading: true,
// isSignout: false,
// userToken: null,
// }
// );
// React.useEffect(() => {
// // Fetch the token from storage then navigate to our appropriate place
// const bootstrapAsync = async () => {
// let userToken;
// try {
// userToken = await AsyncStorage.getItem('userToken');
// } catch (e) {
// // Restoring token failed
// }
// // After restoring token, we may need to validate it in production apps
// // This will switch to the App screen or Auth screen and this loading
// // screen will be unmounted and thrown away.
// dispatch({ type: 'RESTORE_TOKEN', token: userToken });
// };
// bootstrapAsync();
// }, []);
// const authContext = React.useMemo(
// () => ({
// signIn: async data => {
// // In a production app, we need to send some data (usually username, password) to server and get a token
// // We will also need to handle errors if sign in failed
// // After getting token, we need to persist the token using `AsyncStorage`
// // In the example, we'll use a dummy token
// dispatch({ type: 'SIGN_IN', token: 'dummy-auth-token' });
// },
// signOut: () => dispatch({ type: 'SIGN_OUT' }),
// signUp: async data => {
// // In a production app, we need to send user data to server and get a token
// // We will also need to handle errors if sign up failed
// // After getting token, we need to persist the token using `AsyncStorage`
// // In the example, we'll use a dummy token
// dispatch({ type: 'SIGN_IN', token: 'dummy-auth-token' });
// },
// }),
// []
// );
// return (
// <AuthContext.Provider value={authContext}>
// <NavigationContainer>
// <Stack.Navigator>
// {state.isLoading ? (
// // We haven't finished checking for the token yet
// <Stack.Screen name="Splash" component={SplashScreen} />
// ) : state.userToken == null ? (
// // No token found, user isn't signed in
// <Stack.Screen
// name="SignIn"
// component={SignInScreen}
// options={{
// title: 'Sign in',
// // When logging out, a pop animation feels intuitive
// animationTypeForReplace: state.isSignout ? 'pop' : 'push',
// }}
// />
// ) : (
// // User is signed in
// <AppContainer/>
// )}
// </Stack.Navigator>
// </NavigationContainer>
// </AuthContext.Provider>
// );
// }
//export App
export default class App extends Component {
constructor(props){
super(props)
this.state = {
isLoading: true,
isSignedOut: true
}
}
//获取服务器数据&更新本地存储数据
getUserData(user) {
fetch('http://192.168.43.2:3000/login', {
method: 'POST',
headers: {
// Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
nickName: user.nickName,
password: user.password
}),
})
.then(res=>res.json())
.then(async (data)=>{
if(data.user=='') {
Toast.fail('请重新登录',0.5);
this.setState({isSignedOut: true, isLoading: false});
} else {
//Toast.success(data.user.nickName+'登录成功',0.1);
this.setState({isSignedOut: false, isLoading: false});
//更新本地存储
await AsyncStorage.setItem('user', JSON.stringify(data.user));
await AsyncStorage.setItem('accounts', JSON.stringify(data.accounts));
await AsyncStorage.setItem('deals', JSON.stringify(data.deals));
}
})
.catch((error) => {
Toast.offline('服务器出错了',0.5,()=>{},true);
console.error(error);
});
}
//检查本地是否存储用户登录信息
checkSignedOut = async () => {
let user = {};
try {
user = await AsyncStorage.getItem('user');
//console.warn(user);
} catch (e) {
console.error(e);
}
if(user==null) {
this.setState({isSignedOut: true, isLoading: false});
} else {
this.getUserData(JSON.parse(user));
}
}
render() {
if(this.state.isLoading){
//await AsyncStorage.clear();
setTimeout(()=>{
this.checkSignedOut();
}, 1000)
return(
<Provider>
<View style={[styles_common.columnCenterCenter, styles_common.containerWhite]}>
<Image source={require('./assets/img/MyAccounter.png')} style={{width: 200, height: 200}}/>
<Heading1>MyAccounter</Heading1>
<View style={{height: 100}}/>
</View>
</Provider>
)
}
return(
<Provider>
{this.state.isSignedOut?<LoginPage/>:<AppContainer />}
</Provider>
)
}
}