import { View, RefreshControl, FlatList } from "react-native"; import React, { useState, useEffect } from "react"; import Empty from "../../../components/Empty"; import { useTailwind } from "tailwind-rn"; import baseRequest from "../../../utils/baseRequest"; import Toast from "react-native-toast-message"; import { get } from "../../../utils/storeInfo"; import { generateSignature } from "../../../utils/crypto"; import Post from "../../../components/Post"; export default function FollowPosts() { //获取关注的主播mid const [followMids, setFollowMids] = useState([]); const [currentTime, setCurrentTime] = useState(); const getFollowMids = async () => { setCurrentTime(Math.floor(new Date().getTime() / 1000)); const apiUrl = process.env.EXPO_PUBLIC_API_URL; try { const base = await baseRequest(); const account = await get("account"); const signature = await generateSignature({ mid: account.mid, offset: 0, limit: 100, ...base, }); //查关注mid const response = await fetch( `${apiUrl}/api/account_relation/list_follow?signature=${signature}`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ mid: account.mid, offset: 0, limit: 100, ...base, }), } ); const temData = await response.json(); if (temData.ret === -1) { Toast.show({ type: "error", text1: temData.msg, topOffset: 60, }); return; } const temFollowsMids = temData.data.list.map((item) => item.obj_mid); setFollowMids(temFollowsMids); } catch (error) { console.error(error); } }; //获取主播动态 const [data, setData] = useState([]); const [offset, setOffset] = useState(0); const [more, setMore] = useState(1); const getData = async (type) => { if (!currentTime) return; if (followMids.length === 0) { setData((prev) => []); return; } //防止重复加载 if (data.length === 0 && type === "bottom") return; if (type === "bottom" && offset === 0) return; if (!more) return; try { const apiUrl = process.env.EXPO_PUBLIC_API_URL; const base = await baseRequest(); const signature = await generateSignature({ mids: followMids, offset: offset, limit: 4, ...base, }); //查关注主播展示资料 const followsResponse = await fetch( `${apiUrl}/api/moment/list_by_mids?signature=${signature}`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ mids: followMids, ct_upper_bound: currentTime, offset: offset, limit: 4, ...base, }), } ); const followsData = await followsResponse.json(); if (followsData.ret === -1) { Toast.show({ type: "error", text1: followsData.msg, topOffset: 60, }); return; } if (type === "top") { setData((prev) => followsData.data.list); } else { setData((prev) => [...prev, ...followsData.data.list]); } setOffset(followsData.data.offset); setMore(followsData.data.more); } catch (error) { console.error(error); } }; //获取关注的主播mids useEffect(() => { getFollowMids(); }, []); //当关注的主播mids改变,获取新数据 useEffect(() => { getData("top"); }, [followMids, currentTime]); const tailwind = useTailwind(); const renderItem = ({ item }) => ; const [refreshing, setRefreshing] = useState(false); //下拉刷新 const handleRefresh = async () => { setRefreshing(true); setOffset(0); setMore(1); await getFollowMids(); setRefreshing(false); }; return ( handleRefresh()} /> } onEndReached={() => getData("bottom")} ListEmptyComponent={} keyExtractor={(item) => item.id} /> ); }