"use client"; import React, { useEffect, useRef, useState, useImperativeHandle, forwardRef, } from "react"; import { Tabs, Swiper, PullToRefresh, Toast, InfiniteScroll, List, Image, } from "antd-mobile"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faRefresh } from "@fortawesome/free-solid-svg-icons"; import PostItem from "../components/PostItem"; import { sleep } from "antd-mobile/es/utils/sleep"; import "./index.css"; import PostItemSkeleton from "@/components/skeletons/PostItemSkeleton"; import Link from "next/link"; import require from "@/utils/require"; import Empty from "@/components/Empty"; const variables = { "@active-line-color": "#f00", // 将主题色改为红色 }; const tabItems = [ { key: "commend", title: "推荐" }, { key: "follow", title: "关注" }, ]; // const scrollHeight = 700; // const scrollHeight = window.innerHeight-126 export default function Home() { const recommPostRef = useRef(); const followPostRef = useRef(); const swiperRef = useRef(null); const [activeIndex, setActiveIndex] = useState(0); const [data, setData] = useState([]); const [scrollHeight, setScrollHeight] = useState(0); // 获取屏幕高度 // const scrollHeight = 600; useEffect(() => { setScrollHeight(window.innerHeight - 57); // getData(0) }, []); const childrenFunc = () => { if (!activeIndex) { recommPostRef.current?.doRefresh(); } else { followPostRef.current?.doRefresh(); } }; return (
{ const index = tabItems.findIndex((item) => item.key === key); setActiveIndex(index); swiperRef.current?.swipeTo(index); }} > {tabItems.map((item) => ( ))}
null} ref={swiperRef} defaultIndex={activeIndex} onIndexChange={(index) => { setActiveIndex(index); }} > {!activeIndex && ( )} {activeIndex && ( )}
); } const RecommPostList = forwardRef(({ scrollHeight }, ref) => { const [loading, setLoading] = useState(true); const [hasMore, setHasMore] = useState(true); const [commenPostList, setCommenPostList] = useState([]); useEffect(() => { getRecommPostList(2).then((res) => { setCommenPostList(res); }); }, []); useImperativeHandle( ref, () => { return { doRefresh }; }, [] ); async function doRefresh() { // await sleep(1000); // Toast.show({ // icon: "fail", // content: "刷新失败", // }); // throw new Error("刷新失败"); const list = await getRecommPostList(1); setCommenPostList(list); setHasMore(true) } async function loadMore() { const list = await getRecommPostList(0); if (list.length == 0) { setHasMore(false); } setCommenPostList([...commenPostList, ...list]); } const getRecommPostList = async (type = 2) => { setLoading(true); const data = await require("POST", "/api/moment/recomm_list", { body: { op_type: type }, }); setLoading(false); if (data.ret == -1) { Toast.show({ icon: "fail", content: data.msg, position: "top", }); } else { return data.data.recomm_list; } }; return (
{loading && (
)} {commenPostList.map((item) => ( ))} {commenPostList.length == 0 && !loading && (
)}
{!!commenPostList.length && ( )}
); }); const FollowPostList = forwardRef(({ scrollHeight }, ref) => { const [loading, setLoading] = useState(false); const [hasMore, setHasMore] = useState(false); const [followPostList, setFollowPostList] = useState([]); const [currentTime, setCurrentTime] = useState(); const [offset, setOffset] = useState(0); useEffect(() => { getFollowPostList().then((res) => { setFollowPostList(res); }); }, []); useImperativeHandle( ref, () => { return { doRefresh }; }, [] ); async function doRefresh() { // await sleep(1000); // Toast.show({ // icon: "fail", // content: "刷新失败", // }); // throw new Error("刷新失败"); // getRecommPostList(1); const list = await getFollowPostList(1); setFollowPostList(list); } async function loadMore() { const list = await getFollowPostList(); const newList = [...followPostList, ...list]; setOffset(newList.length / 4); setFollowPostList(newList); } const getFollowPostList = async () => { setLoading(true); setCurrentTime(Math.floor(new Date().getTime() / 1000)); const data = await require("POST", "/api/account_relation/list_follow", { body: { offset, limit: 4 }, }, true); setHasMore(data.data.list.length > 0); if (data.data.list.length > 0) { //查关注主播展示资料 const followsResponse = await require("POST", "/api/moment/list_by_mids", { body: { offset, limit: 4, ct_upper_bound: currentTime, mids: data.data.list.map((item) => item.obj_mid), }, }); setLoading(false); if (data.ret == -1) { Toast.show({ icon: "fail", content: data.msg, position: "top", }); } else { return followsResponse.data.list; } } else { setLoading(false); } }; return (
{/* */} {loading && (
)} {followPostList.map((item, index) => ( ))} {followPostList.length == 0 && (
)}
{!!followPostList.length && ( )} {/*
*/}
); });