import DeleteSvg from '@/assets/icons/svg/delete.svg'; import LogoutSvg from '@/assets/icons/svg/logout.svg'; import ReturnArrowSvg from '@/assets/icons/svg/returnArrow.svg'; import RightArrowSvg from '@/assets/icons/svg/rightArrow.svg'; import DeleteModal from '@/components/owner/delete'; import LcensesModal from '@/components/owner/qualification/lcenses'; import PrivacyModal from '@/components/owner/qualification/privacy'; import CustomSwitch from '@/components/owner/switch'; import UserInfo from '@/components/owner/userInfo'; import { checkNotificationPermission, getLocationPermission, getPermissions, requestLocationPermission, requestMediaLibraryPermission, requestNotificationPermission, reverseGeocode } from '@/components/owner/utils'; import { ThemedText } from '@/components/ThemedText'; import { useAuth } from '@/contexts/auth-context'; import { fetchApi } from '@/lib/server-api-util'; import { Address, User } from '@/types/user'; import * as Location from 'expo-location'; import { useRouter } from 'expo-router'; import React, { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Linking, Pressable, ScrollView, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { useSafeAreaInsets } from "react-native-safe-area-context"; const Setting = (props: { modalVisible: boolean, setModalVisible: (visible: boolean) => void, userInfo: User }) => { const { modalVisible, setModalVisible, userInfo } = props; const insets = useSafeAreaInsets(); const { t } = useTranslation(); const [modalType, setModalType] = useState<'ai' | 'terms' | 'privacy' | 'user'>('ai'); // 协议弹窗 const [privacyModalVisible, setPrivacyModalVisible] = useState(false); // 许可证弹窗 const [lcensesModalVisible, setLcensesModalVisible] = useState(false); // 删除弹窗 const [deleteModalVisible, setDeleteModalVisible] = useState(false); const { logout } = useAuth(); const router = useRouter(); // 打开设置 const openAppSettings = () => { Linking.openSettings(); }; // 通知消息权限开关 const [notificationsEnabled, setNotificationsEnabled] = useState(false); const toggleNotifications = async () => { if (notificationsEnabled) { // 引导去设置关闭权限 openAppSettings() } else { requestNotificationPermission() .then((granted: boolean | ((prevState: boolean) => boolean)) => { setNotificationsEnabled(granted); }); setModalVisible(false); } }; // 相册权限 const [albumEnabled, setAlbumEnabled] = useState(false); const toggleAlbum = async () => { if (albumEnabled) { // 引导去设置关闭权限 openAppSettings() } else { requestMediaLibraryPermission() .then((granted: boolean | ((prevState: boolean) => boolean)) => { setAlbumEnabled(granted); }); setModalVisible(false); } } // 位置权限 const [locationEnabled, setLocationEnabled] = useState(false); // 位置权限更改 const toggleLocation = async () => { if (locationEnabled) { // 如果权限已开启,点击则引导用户去设置关闭 openAppSettings(); } else { requestLocationPermission() .then((granted: boolean | ((prevState: boolean) => boolean)) => { setLocationEnabled(granted); }); setModalVisible(false); } }; // 正在获取位置信息 const [isLoading, setIsLoading] = useState(false); // 动画开启 const [isRefreshing, setIsRefreshing] = useState(false); // 当前位置状态 const [currentLocation, setCurrentLocation] = useState
({} as Address); // 获取当前位置 const getCurrentLocation = async () => { setIsLoading(true); setIsRefreshing(true); try { // 1. 首先检查当前权限状态 -- 获取当前的位置权限 let currentStatus = await getLocationPermission(); console.log('当前权限状态:', currentStatus); // 2. 如果没有权限,则跳过获取位置 if (!currentStatus) { console.log('没有权限,跳过获取位置') return; // const newStatus = await requestLocationPermission(); // setLocationEnabled(newStatus); // currentStatus = newStatus; // if (!currentStatus) { // // alert('需要位置权限才能继续'); // return; // } } // 3. 确保位置服务已启用 const isEnabled = await Location.hasServicesEnabledAsync(); if (!isEnabled) { alert('请先启用位置服务'); return; } console.log('位置服务已启用'); // 4. 获取当前位置 const location = await Location.getCurrentPositionAsync({ accuracy: Location.Accuracy.High, // 使用高精度 timeInterval: 10000, // 可选:最大等待时间(毫秒) }); console.log('位置:', location); // 地理位置逆编码 const address = await reverseGeocode(location.coords.latitude, location.coords.longitude); // 5. 更新位置状态 if (address) { setCurrentLocation(address); } return location; } catch (error: any) { if (error.code === 'PERMISSION_DENIED' || error.code === 'PERMISSION_DENIED_ERROR') { alert('位置权限被拒绝,请在设置中启用位置服务'); } else if (error.code === 'TIMEOUT') { alert('获取位置超时,请检查网络和位置服务'); } else { alert(`无法获取您的位置: ${error.message || '未知错误'}`); } throw error; // 重新抛出错误以便上层处理 } finally { setIsLoading(false); setIsRefreshing(false); } }; // 退出登录 const handleLogout = () => { fetchApi("/iam/logout", { method: "POST", headers: { "Content-Type": "application/json", }, }) .then(async (res) => { await logout(); setModalVisible(false); router.replace('/login'); }) .catch(() => { console.error("jwt has expired."); }); }; // 检查是否有权限 useEffect(() => { if (modalVisible) { // 位置权限 getLocationPermission().then((res: boolean | ((prevState: boolean) => boolean)) => { console.log('位置权限:', res); setLocationEnabled(res); }) // 媒体库权限 getPermissions().then((res: boolean | ((prevState: boolean) => boolean)) => { console.log('媒体库权限:', res); setAlbumEnabled(res); }) // 通知权限 checkNotificationPermission().then((res: boolean | ((prevState: boolean) => boolean)) => { console.log('通知权限:', res); setNotificationsEnabled(res); }) } }, [modalVisible]) return (