import { useState, useEffect } from "react"; import { View, Text, ScrollView, Image } from "@tarojs/components"; import { withAuth, EmptyState, GeneralNavbar } from "@/components"; import { useGlobalState } from "@/store/global"; import FollowService from "@/services/followService"; import messageService from "@/services/messageService"; import { formatShortRelativeTime } from "@/utils/timeUtils"; import Taro from "@tarojs/taro"; import "./index.scss"; // 关注项类型定义 interface FollowItem { id: number; user_id: number; user_avatar: string; user_nickname: string; user_signature?: string; city?: string; ntrp_level?: string; time: string; is_mutual: boolean; // 是否互相关注 } const NewFollow = () => { const [followList, setFollowList] = useState([]); const [loading, setLoading] = useState(false); const [refreshing, setRefreshing] = useState(false); const { statusNavbarHeightInfo } = useGlobalState() || {}; const { totalHeight = 98 } = statusNavbarHeightInfo || {}; useEffect(() => { getFollowList(); }, []); // 获取新增关注列表 const getFollowList = async () => { if (loading) return; setLoading(true); try { const res = await FollowService.get_new_fans_list(1, 20); if (res.list && res.list.length > 0) { // 映射数据 const mappedList = res.list.map((item: any) => ({ id: item.id, user_id: item.id, user_avatar: item.avatar_url || "", user_nickname: item.nickname || "匿名用户", user_signature: item.personal_profile || "", city: item.city || "", ntrp_level: item.ntrp_level || "", time: item.follow_time, is_mutual: item.is_mutual || false, })); setFollowList(mappedList); // 获取所有关注者ID列表并标记已读(传入所有ID,包括已读和未读) const allFanIds = res.list.map((item: any) => item.id); if (allFanIds.length > 0) { // 使用统一接口标记已读,传入所有关注者ID messageService.markAsRead('follow', allFanIds).catch(e => { console.error("标记关注已读失败:", e); }); } } else { // 如果没有数据,设置为空数组以显示空状态 setFollowList([]); } } catch (e) { Taro.showToast({ title: "获取列表失败", icon: "none", duration: 2000, }); } finally { setLoading(false); } }; // 处理回关/取消关注 const handleFollowBack = async (item: FollowItem) => { try { if (item.is_mutual) { // 已经互相关注,点击取消关注 await FollowService.unfollow_user(item.user_id); Taro.showToast({ title: "取消关注成功", icon: "success", duration: 2000, }); // 更新列表 setFollowList(prevList => prevList.map(followItem => followItem.id === item.id ? { ...followItem, is_mutual: false } : followItem ) ); } else { // 未互关,点击回关 await FollowService.follow_back(item.user_id); Taro.showToast({ title: "关注成功", icon: "success", duration: 2000, }); // 更新列表 setFollowList(prevList => prevList.map(followItem => followItem.id === item.id ? { ...followItem, is_mutual: true } : followItem ) ); } } catch (e) { Taro.showToast({ title: item.is_mutual ? "取消关注失败" : "关注失败", icon: "none", duration: 2000, }); } }; // 处理返回 const handleBack = () => { Taro.navigateBack(); }; // 处理点击用户 const handleUserClick = (userId: number) => { Taro.navigateTo({ url: `/user_pages/other/index?userid=${userId}`, }); }; // 处理下拉刷新 const handleRefresh = async () => { setRefreshing(true); try { const res = await FollowService.get_new_fans_list(1, 20); if (res.list && res.list.length > 0) { const mappedList = res.list.map((item: any) => ({ id: item.id, user_id: item.id, user_avatar: item.avatar_url || "", user_nickname: item.nickname || "匿名用户", user_signature: item.personal_profile || "", city: item.city || "", ntrp_level: item.ntrp_level || "", time: item.follow_time, is_mutual: item.is_mutual || false, })); setFollowList(mappedList); // 获取所有关注者ID列表并标记已读(传入所有ID) const allFanIds = res.list.map((item: any) => item.id); if (allFanIds.length > 0) { messageService.markAsRead('follow', allFanIds).catch(e => { console.error("标记关注已读失败:", e); }); } } else { // 如果没有数据,设置为空数组以显示空状态 setFollowList([]); } } catch (e) { Taro.showToast({ title: "刷新失败", icon: "none", duration: 2000, }); } finally { setRefreshing(false); } }; // 渲染关注项 const renderFollowItem = (item: FollowItem) => { return ( handleUserClick(item.user_id)}> {item.user_nickname} {item.user_signature ? ( {item.user_signature} ) : ( {formatShortRelativeTime(item.time)}关注了你 )} handleFollowBack(item)} > {item.is_mutual ? "互相关注" : "回关"} ); }; return ( {/* 顶部导航栏 */} {/* 关注列表 */} {!loading && followList.length > 0 ? ( {followList.map(renderFollowItem)} {/* 到底了提示 */} 到底了 ) : !loading ? ( ) : null} ); }; export default withAuth(NewFollow);