tsrntemp 1.1.3 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,28 +1,156 @@
1
- import React from 'react';
2
- import { NavigationContainer } from '@react-navigation/native';
3
- import { createNativeStackNavigator } from '@react-navigation/native-stack';
4
- import { SafeAreaProvider } from 'react-native-safe-area-context';
5
- import RouteArr from 'config/routeConfig';
6
- import { StackParamList } from 'types/RouteParamList';
1
+ import React, {useReducer, useMemo, useEffect} from 'react';
2
+ import {StyleSheet, View, ActivityIndicator} from 'react-native';
3
+ import {NavigationContainer} from '@react-navigation/native';
4
+ import {createNativeStackNavigator} from '@react-navigation/native-stack';
5
+ import {SafeAreaProvider} from 'react-native-safe-area-context';
6
+ import Login from 'pages/Login';
7
+ import {StackParamList} from 'types/RouteParamList';
8
+ import Storage from 'utils/storage';
9
+ import color from 'config/color';
10
+ import RouteArr from 'config/routeArr';
7
11
 
8
- const Stack = createNativeStackNavigator<StackParamList>()
12
+ const Stack = createNativeStackNavigator<StackParamList>();
13
+
14
+ export const AuthContext = React.createContext<{
15
+ isSignedIn: boolean;
16
+ signIn: (T: any) => void;
17
+ signOut: () => void;
18
+ }>({
19
+ isSignedIn: false,
20
+ signIn: () => {},
21
+ signOut: () => {},
22
+ });
23
+
24
+ type State = {
25
+ isLoading: boolean;
26
+ isSignout: boolean;
27
+ userInfo: undefined | string;
28
+ };
29
+
30
+ type Action =
31
+ | {type: 'RESTORE_TOKEN'; user: undefined | string}
32
+ | {type: 'SIGN_IN'; user: string}
33
+ | {type: 'SIGN_OUT'};
34
+
35
+ // APP初始化加载登录信息
36
+ const SplashScreen = () => {
37
+ return (
38
+ <View style={styles.content}>
39
+ <ActivityIndicator />
40
+ </View>
41
+ );
42
+ };
9
43
 
10
44
  const RouterContainer: React.FC = () => {
45
+ // 初始化登录信息
46
+ const [state, dispatch] = useReducer<React.Reducer<State, Action>>(
47
+ (prevState, action) => {
48
+ switch (action.type) {
49
+ case 'RESTORE_TOKEN':
50
+ return {
51
+ ...prevState,
52
+ userInfo: action.user,
53
+ isLoading: false,
54
+ };
55
+ case 'SIGN_IN':
56
+ return {
57
+ ...prevState,
58
+ isLoading: false,
59
+ isSignout: false,
60
+ userInfo: action.user,
61
+ };
62
+ case 'SIGN_OUT':
63
+ return {
64
+ ...prevState,
65
+ isSignout: true,
66
+ isLoading: false,
67
+ userInfo: undefined,
68
+ };
69
+ }
70
+ },
71
+ {
72
+ isLoading: true,
73
+ isSignout: false,
74
+ userInfo: undefined,
75
+ },
76
+ );
77
+
78
+ // 获取当前登录信息
79
+ useEffect(() => {
80
+ Storage.getItem('userInfo').then(res => {
81
+ if (!res) {
82
+ return dispatch({type: 'SIGN_OUT'});
83
+ }
84
+ return dispatch({type: 'SIGN_IN', user: res});
85
+ });
86
+ }, []);
87
+
88
+ const isSignedIn = state.userInfo !== undefined;
89
+
90
+ const authContext = useMemo(
91
+ () => ({
92
+ isSignedIn,
93
+ signIn: (T: any) => {
94
+ dispatch({type: 'SIGN_IN', user: T});
95
+ Storage.setItem('userInfo', T);
96
+ },
97
+ signOut: () => {
98
+ dispatch({type: 'SIGN_OUT'});
99
+ Storage.clearItem('userInfo');
100
+ },
101
+ }),
102
+ [isSignedIn],
103
+ );
104
+
105
+ if (state.isLoading) {
106
+ return <SplashScreen />;
107
+ }
108
+
11
109
  return (
12
- <SafeAreaProvider>
13
- <NavigationContainer>
14
- <Stack.Navigator
15
- screenOptions={{
16
- headerBackTitleVisible: false,
17
- headerTitleAlign: 'center'
18
- }}
19
- >
20
- {
21
- RouteArr.map(v => <Stack.Screen key={v.name} name={v.name} component={v.component} options={v.options} />)
22
- }
23
- </Stack.Navigator>
24
- </NavigationContainer>
25
- </SafeAreaProvider>
26
- )
27
- }
28
- export default RouterContainer
110
+ <AuthContext.Provider value={authContext}>
111
+ <SafeAreaProvider>
112
+ <NavigationContainer>
113
+ <Stack.Navigator
114
+ screenOptions={{
115
+ headerBackTitleVisible: false,
116
+ headerTitleAlign: 'center',
117
+ headerStyle: {
118
+ backgroundColor: color.primary[400],
119
+ },
120
+ }}>
121
+ {isSignedIn ? (
122
+ <Stack.Group>
123
+ {RouteArr.map(v => (
124
+ <Stack.Screen
125
+ key={v.name}
126
+ name={v.name}
127
+ component={v.component}
128
+ options={v.options ?? {}}
129
+ />
130
+ ))}
131
+ </Stack.Group>
132
+ ) : (
133
+ <Stack.Group>
134
+ <Stack.Screen
135
+ name="Login"
136
+ component={Login}
137
+ options={{headerTitle: '登录'}}
138
+ />
139
+ </Stack.Group>
140
+ )}
141
+ </Stack.Navigator>
142
+ </NavigationContainer>
143
+ </SafeAreaProvider>
144
+ </AuthContext.Provider>
145
+ );
146
+ };
147
+
148
+ const styles = StyleSheet.create({
149
+ content: {
150
+ flex: 1,
151
+ padding: 16,
152
+ justifyContent: 'center',
153
+ },
154
+ });
155
+
156
+ export default RouterContainer;
@@ -2,8 +2,9 @@
2
2
  export type BottomTabParamList = {
3
3
  Home: undefined;
4
4
  Mine: undefined;
5
- }
5
+ };
6
6
 
7
7
  export type StackParamList = {
8
- BottomTab: undefined
9
- }
8
+ BottomTab: undefined;
9
+ Login: undefined;
10
+ };
@@ -2,7 +2,7 @@
2
2
  /**
3
3
  * 使用可参照https://www.npmjs.com/package/umi-request
4
4
  */
5
- import { extend, ResponseError } from 'umi-request';
5
+ import {extend, ResponseError} from 'umi-request';
6
6
 
7
7
  //错误处理
8
8
  const errorHandler = (error: ResponseError) => {
@@ -17,13 +17,12 @@ const errorHandler = (error: ResponseError) => {
17
17
  // The request was made but no response was received or error occurs when setting up the request.
18
18
  console.log(error.message);
19
19
  }
20
- }
20
+ };
21
21
 
22
22
  const request = extend({
23
23
  // prefix:'',
24
24
  timeout: 1000,
25
- errorHandler
26
- })
25
+ errorHandler,
26
+ });
27
27
 
28
-
29
- export default request
28
+ export default request;
@@ -6,64 +6,62 @@
6
6
  import AsyncStorage from '@react-native-async-storage/async-storage';
7
7
 
8
8
  export default class Storage {
9
-
10
9
  //读取数据
11
10
  static async getItem(key: string) {
12
11
  try {
13
- return AsyncStorage.getItem(key)
12
+ return AsyncStorage.getItem(key);
14
13
  } catch (error) {
15
- console.log(error)
14
+ console.log(error);
16
15
  }
17
16
  }
18
17
 
19
18
  //保存数据
20
19
  static async setItem(key: string, val: string) {
21
- return AsyncStorage.setItem(key, val)
20
+ const value = typeof val === 'string' ? val : JSON.stringify(val);
21
+ return AsyncStorage.setItem(key, value);
22
22
  }
23
23
 
24
24
  //更新保存数据
25
25
  static async updateItem(key: string, val: string) {
26
26
  try {
27
- return AsyncStorage.mergeItem(key, val)
27
+ return AsyncStorage.mergeItem(key, val);
28
28
  } catch (error) {
29
- console.log(error)
29
+ console.log(error);
30
30
  }
31
31
  }
32
32
 
33
33
  //清除指缓存
34
34
  static async clearItem(key: string) {
35
- return AsyncStorage.removeItem(key)
35
+ return AsyncStorage.removeItem(key);
36
36
  }
37
37
 
38
38
  // 获取所有已存储的键
39
39
  static async getAllKeys() {
40
- return AsyncStorage.getAllKeys()
40
+ return AsyncStorage.getAllKeys();
41
41
  }
42
42
 
43
43
  // 批量获取数据
44
44
  static async getMultiple(key: readonly string[]) {
45
- return AsyncStorage.multiGet(key)
45
+ return AsyncStorage.multiGet(key);
46
46
  }
47
47
 
48
48
  // 批量设置数据
49
49
  static async multiSet(keyValuePairs: [string, string][]) {
50
- return AsyncStorage.multiSet(keyValuePairs)
50
+ return AsyncStorage.multiSet(keyValuePairs);
51
51
  }
52
52
 
53
53
  // 批量更新数据
54
54
  static async updateMultiple(keyValuePairs: [string, string][]) {
55
- return AsyncStorage.multiMerge(keyValuePairs)
55
+ return AsyncStorage.multiMerge(keyValuePairs);
56
56
  }
57
57
 
58
58
  //批量移除
59
59
  static async multiClear(key: string[]) {
60
- return AsyncStorage.multiRemove(key)
60
+ return AsyncStorage.multiRemove(key);
61
61
  }
62
62
 
63
63
  // 清除所有缓存
64
64
  static async clearAll() {
65
- return AsyncStorage.clear()
65
+ return AsyncStorage.clear();
66
66
  }
67
-
68
67
  }
69
-