feat: 添加教练功能和更新用户界面
- 新增教练页面,用户可以与教练进行互动和咨询 - 更新首页,切换到教练 tab 并传递名称参数 - 优化个人信息页面,添加注销帐号和退出登录功能 - 更新隐私政策和用户协议的链接,确保用户在使用前同意相关条款 - 修改今日训练页面标题为“开始训练”,提升用户体验 - 删除不再使用的进度条组件,简化代码结构
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { usePathname, useRouter } from 'expo-router';
|
||||
import { useCallback } from 'react';
|
||||
import { Alert } from 'react-native';
|
||||
|
||||
import { useAppSelector } from '@/hooks/redux';
|
||||
import { useAppDispatch, useAppSelector } from '@/hooks/redux';
|
||||
import { api } from '@/services/api';
|
||||
import { logout as logoutAction } from '@/store/userSlice';
|
||||
|
||||
type RedirectParams = Record<string, string | number | boolean | undefined>;
|
||||
|
||||
@@ -12,6 +16,7 @@ type EnsureOptions = {
|
||||
|
||||
export function useAuthGuard() {
|
||||
const router = useRouter();
|
||||
const dispatch = useAppDispatch();
|
||||
const currentPath = usePathname();
|
||||
const token = useAppSelector((s) => (s as any)?.user?.token as string | null);
|
||||
const isLoggedIn = !!token;
|
||||
@@ -52,11 +57,93 @@ export function useAuthGuard() {
|
||||
[ensureLoggedIn]
|
||||
);
|
||||
|
||||
// 退出登录功能
|
||||
const handleLogout = useCallback(async () => {
|
||||
try {
|
||||
// 调用 Redux action 清除本地状态和缓存
|
||||
await dispatch(logoutAction()).unwrap();
|
||||
|
||||
// 跳转到登录页面
|
||||
router.replace('/auth/login');
|
||||
} catch (error) {
|
||||
console.error('退出登录失败:', error);
|
||||
Alert.alert('错误', '退出登录失败,请稍后重试');
|
||||
}
|
||||
}, [dispatch, router]);
|
||||
|
||||
// 带确认对话框的退出登录
|
||||
const confirmLogout = useCallback(() => {
|
||||
Alert.alert(
|
||||
'确认退出',
|
||||
'确定要退出当前账号吗?',
|
||||
[
|
||||
{
|
||||
text: '取消',
|
||||
style: 'cancel',
|
||||
},
|
||||
{
|
||||
text: '确定',
|
||||
style: 'default',
|
||||
onPress: handleLogout,
|
||||
},
|
||||
]
|
||||
);
|
||||
}, [handleLogout]);
|
||||
|
||||
// 注销账号功能
|
||||
const handleDeleteAccount = useCallback(async () => {
|
||||
try {
|
||||
// 调用注销账号API
|
||||
await api.delete('/api/users/delete-account');
|
||||
|
||||
// 清除额外的本地数据
|
||||
await AsyncStorage.multiRemove(['@user_personal_info', '@onboarding_completed']);
|
||||
|
||||
// 执行退出登录逻辑
|
||||
await dispatch(logoutAction()).unwrap();
|
||||
|
||||
Alert.alert('账号已注销', '您的账号已成功注销', [
|
||||
{
|
||||
text: '确定',
|
||||
onPress: () => router.replace('/auth/login'),
|
||||
},
|
||||
]);
|
||||
} catch (error: any) {
|
||||
console.error('注销账号失败:', error);
|
||||
const message = error?.message || '注销失败,请稍后重试';
|
||||
Alert.alert('注销失败', message);
|
||||
}
|
||||
}, [dispatch, router]);
|
||||
|
||||
// 带确认对话框的注销账号
|
||||
const confirmDeleteAccount = useCallback(() => {
|
||||
Alert.alert(
|
||||
'确认注销账号',
|
||||
'此操作不可恢复,将删除您的账号及相关数据。确定继续吗?',
|
||||
[
|
||||
{
|
||||
text: '取消',
|
||||
style: 'cancel',
|
||||
},
|
||||
{
|
||||
text: '确认注销',
|
||||
style: 'destructive',
|
||||
onPress: handleDeleteAccount,
|
||||
},
|
||||
],
|
||||
{ cancelable: true }
|
||||
);
|
||||
}, [handleDeleteAccount]);
|
||||
|
||||
return {
|
||||
isLoggedIn,
|
||||
ensureLoggedIn,
|
||||
pushIfAuthedElseLogin,
|
||||
guardHandler,
|
||||
handleLogout,
|
||||
confirmLogout,
|
||||
handleDeleteAccount,
|
||||
confirmDeleteAccount,
|
||||
} as const;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user