Skip to content

Commit 78641e1

Browse files
committed
fix: 删除没有用到的Home页面 & 简化登陆请求和验证token请求
1 parent e6bb61d commit 78641e1

8 files changed

Lines changed: 104 additions & 123 deletions

File tree

HelloWorld/src/hooks/users.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import {Alert} from 'react-native';
1+
import { Alert } from 'react-native';
22
import AsyncStorage from '@react-native-async-storage/async-storage';
3-
import {userLogin, userAuth} from '../services/users';
4-
import {useQuery, useMutation} from 'react-query';
3+
import { userLogin, userAuth } from '../services/users';
4+
import { useQuery, useMutation } from 'react-query';
55
import Global from '../global';
66
import conf from '../config';
7+
import { useSelector, useDispatch } from 'react-redux'
78
// 登录
8-
export const login = ({config = {}, update, formData, remember}) => {
9+
export const login = ({ config = {}, update, remember }) => {
910
const mutation = useMutation({
1011
mutationFn: userLogin,
1112
onSuccess: async data => {
@@ -16,7 +17,7 @@ export const login = ({config = {}, update, formData, remember}) => {
1617
await AsyncStorage.setItem('cachPassword', formData.password);
1718
}
1819
await AsyncStorage.setItem('userData', JSON.stringify(data.data));
19-
update({token: data.token, userData: data.data});
20+
update({ token: data.token, userData: data.data });
2021
if (Global.navigation) {
2122
Global.navigation.replace('Tab');
2223
}
@@ -30,14 +31,21 @@ export const login = ({config = {}, update, formData, remember}) => {
3031
};
3132

3233
// 验证token
33-
export const useAuthToken = ({token, update}) => {
34+
export const useAuthToken = () => {
35+
const { token } = useSelector(state => state.global)
36+
const dispatch = useDispatch()
3437
const mutation = useMutation({
3538
mutationFn: userAuth,
3639
onMutate: async () => {
3740
let host = await AsyncStorage.getItem('apihost');
3841
if (!host && conf.hosts[0]) {
3942
await AsyncStorage.setItem('apihost', JSON.stringify(conf.hosts[0]));
40-
await update({apihost: conf.hosts[0]});
43+
dispatch({
44+
type: "global/update",
45+
payload: {
46+
apihost: conf.hosts[0]
47+
}
48+
})
4149
}
4250
if (!token) {
4351
await AsyncStorage.removeItem('userData');
@@ -46,20 +54,39 @@ export const useAuthToken = ({token, update}) => {
4654
},
4755
onSuccess: async data => {
4856
if (data?.token) {
49-
await update({authState: true, token: data.token});
57+
dispatch({
58+
type: "global/update",
59+
payload: {
60+
token: data.token,
61+
authState: true
62+
}
63+
})
5064
} else {
51-
await update({authState: true, token: null});
65+
dispatch({
66+
type: "global/update",
67+
payload: {
68+
authState: true,
69+
token: null
70+
}
71+
})
5272
}
5373
},
5474
});
5575
return mutation;
5676
};
5777

5878
// 退出
59-
export const logout = ({update}) => {
79+
export const logout = () => {
80+
const dispatch = useDispatch()
6081
AsyncStorage.removeItem('token');
6182
AsyncStorage.removeItem('userData');
62-
update({token: null, userData: null});
83+
dispatch({
84+
type: "global/update",
85+
payload: {
86+
token: null,
87+
authState: null
88+
}
89+
})
6390
if (Global.navigation) {
6491
Global.navigation.navigate?.('SignIn');
6592
}

HelloWorld/src/pages/AuthLoading/index.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import React, { useEffect } from 'react';
22
import { Text, StatusBar, StyleSheet, SafeAreaView } from 'react-native';
3-
import { connect } from 'react-redux';
3+
import { useSelector } from 'react-redux';
44
import { Flex, Loader, H3, Icon } from '@uiw/react-native';
55
import Global from '../../global';
66
import { logoLight } from '../../components/icons/signin';
77
import Footer from '../../components/Footer';
88
import { useAuthToken } from '../../hooks/users'
99

10+
1011
const AuthLoadingScreen = ({
1112
navigation,
12-
update,
13-
token,
14-
authState,
1513
children
1614
}) => {
17-
const { mutate, isLoading } = useAuthToken({ update, token })
15+
const { token, authState } = useSelector(state=>state.global)
16+
const { mutate, isLoading } = useAuthToken()
17+
1818
useEffect(() => {
1919
if (navigation && Global) {
2020
Global.navigation = navigation;
@@ -42,15 +42,7 @@ const AuthLoadingScreen = ({
4242
);
4343
}
4444

45-
export default connect(
46-
({ global }) => ({
47-
token: global.token,
48-
authState: global.authState,
49-
}),
50-
({ global }) => ({
51-
update: global.update,
52-
})
53-
)(AuthLoadingScreen);
45+
export default AuthLoadingScreen
5446

5547
const styles = StyleSheet.create({
5648
container: {

HelloWorld/src/pages/Home/index.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

HelloWorld/src/routes/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Home from '../pages/Home';
21
import SignIn from '../pages/SignIn';
32
import DevOptions from '../pages/DevOptions';
43
import MyHomeSetting from '../pages/MyHome/Setting';
Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import { Alert } from "react-native";
2-
import AsyncStorage from "@react-native-async-storage/async-storage";
3-
import { userLogin, userAuth } from "../services/users";
4-
import { useQuery, useMutation } from "react-query";
5-
import Global from "../global";
6-
import conf from "../config";
7-
1+
import { Alert } from 'react-native';
2+
import AsyncStorage from '@react-native-async-storage/async-storage';
3+
import { userLogin, userAuth } from '../services/users';
4+
import { useQuery, useMutation } from 'react-query';
5+
import Global from '../global';
6+
import conf from '../config';
7+
import { useSelector, useDispatch } from 'react-redux'
88
// 登录
9-
export const useLogin = ({ config = {}, update, formData, remember }) => {
9+
export const login = ({ config = {}, update, remember }) => {
1010
const mutation = useMutation({
1111
mutationFn: userLogin,
12-
onSuccess: async (data) => {
12+
onSuccess: async data => {
1313
if (data?.token && data?.data) {
14-
await AsyncStorage.setItem("token", data.token);
14+
await AsyncStorage.setItem('token', data.token);
1515
if (remember) {
16-
await AsyncStorage.setItem("cachLoginName", formData.loginName);
17-
await AsyncStorage.setItem("cachPassword", formData.password);
16+
await AsyncStorage.setItem('cachLoginName', formData.loginName);
17+
await AsyncStorage.setItem('cachPassword', formData.password);
1818
}
19-
await AsyncStorage.setItem("userData", JSON.stringify(data.data));
19+
await AsyncStorage.setItem('userData', JSON.stringify(data.data));
2020
update({ token: data.token, userData: data.data });
2121
if (Global.navigation) {
22-
Global.navigation.replace("Tab");
22+
Global.navigation.replace('Tab');
2323
}
2424
} else if (data && data.message) {
2525
Alert.alert(`Login failed - ${data.error}`, data.message);
@@ -31,37 +31,63 @@ export const useLogin = ({ config = {}, update, formData, remember }) => {
3131
};
3232

3333
// 验证token
34-
export const useAuthToken = ({ token, update }) => {
34+
export const useAuthToken = () => {
35+
const { token } = useSelector(state => state.global)
36+
const dispatch = useDispatch()
3537
const mutation = useMutation({
3638
mutationFn: userAuth,
3739
onMutate: async () => {
38-
let host = await AsyncStorage.getItem("apihost");
40+
let host = await AsyncStorage.getItem('apihost');
3941
if (!host && conf.hosts[0]) {
40-
await AsyncStorage.setItem("apihost", JSON.stringify(conf.hosts[0]));
41-
await update({ apihost: conf.hosts[0] });
42+
await AsyncStorage.setItem('apihost', JSON.stringify(conf.hosts[0]));
43+
dispatch({
44+
type: "global/update",
45+
payload: {
46+
apihost: conf.hosts[0]
47+
}
48+
})
4249
}
4350
if (!token) {
44-
await AsyncStorage.removeItem("userData");
45-
await AsyncStorage.removeItem("token");
51+
await AsyncStorage.removeItem('userData');
52+
await AsyncStorage.removeItem('token');
4653
}
4754
},
48-
onSuccess: async (data) => {
55+
onSuccess: async data => {
4956
if (data?.token) {
50-
await update({ authState: true, token: data.token });
57+
dispatch({
58+
type: "global/update",
59+
payload: {
60+
token: data.token,
61+
authState: true
62+
}
63+
})
5164
} else {
52-
await update({ authState: true, token: null });
65+
dispatch({
66+
type: "global/update",
67+
payload: {
68+
authState: true,
69+
token: null
70+
}
71+
})
5372
}
5473
},
5574
});
5675
return mutation;
5776
};
5877

5978
// 退出
60-
export const logout = ({ update }) => {
61-
AsyncStorage.removeItem("token");
62-
AsyncStorage.removeItem("userData");
63-
update({ token: null, userData: null });
79+
export const logout = () => {
80+
const dispatch = useDispatch()
81+
AsyncStorage.removeItem('token');
82+
AsyncStorage.removeItem('userData');
83+
dispatch({
84+
type: "global/update",
85+
payload: {
86+
token: null,
87+
authState: null
88+
}
89+
})
6490
if (Global.navigation) {
65-
Global.navigation.navigate?.("SignIn");
91+
Global.navigation.navigate?.('SignIn');
6692
}
6793
};

template/template/src/pages/AuthLoading/index.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import React, { useEffect } from 'react';
22
import { Text, StatusBar, StyleSheet, SafeAreaView } from 'react-native';
3-
import { connect } from 'react-redux';
3+
import { useSelector } from 'react-redux';
44
import { Flex, Loader, H3, Icon } from '@uiw/react-native';
55
import Global from '../../global';
66
import { logoLight } from '../../components/icons/signin';
77
import Footer from '../../components/Footer';
88
import { useAuthToken } from '../../hooks/users'
99

10+
1011
const AuthLoadingScreen = ({
1112
navigation,
12-
update,
13-
token,
14-
authState,
1513
children
1614
}) => {
17-
const { mutate, isLoading } = useAuthToken({ update, token })
15+
const { token, authState } = useSelector(state=>state.global)
16+
const { mutate, isLoading } = useAuthToken()
17+
1818
useEffect(() => {
1919
if (navigation && Global) {
2020
Global.navigation = navigation;
@@ -42,15 +42,7 @@ const AuthLoadingScreen = ({
4242
);
4343
}
4444

45-
export default connect(
46-
({ global }) => ({
47-
token: global.token,
48-
authState: global.authState,
49-
}),
50-
({ global }) => ({
51-
update: global.update,
52-
})
53-
)(AuthLoadingScreen);
45+
export default AuthLoadingScreen
5446

5547
const styles = StyleSheet.create({
5648
container: {

template/template/src/pages/Home/index.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

template/template/src/routes/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Home from "../pages/Home";
21
import SignIn from "../pages/SignIn";
32
import DevOptions from "../pages/DevOptions";
43
import MyHomeSetting from "../pages/MyHome/Setting";

0 commit comments

Comments
 (0)