tiefen_space_h5/app/page.js

320 lines
9.2 KiB
JavaScript
Raw Normal View History

2024-06-24 22:12:13 +08:00
"use client";
2024-07-08 20:07:36 +08:00
import React, {
useEffect,
useRef,
useState,
useImperativeHandle,
forwardRef,
} from "react";
2024-08-05 21:10:36 +08:00
import { Tabs, Swiper, Toast, InfiniteScroll, List, Image } from "antd-mobile";
2024-07-06 11:05:19 +08:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faRefresh } from "@fortawesome/free-solid-svg-icons";
2024-06-25 20:18:37 +08:00
import PostItem from "../components/PostItem";
2024-07-16 20:20:12 +08:00
// import { sleep } from "antd-mobile/es/utils/sleep";
2024-06-25 20:18:37 +08:00
import "./index.css";
2024-06-25 22:47:18 +08:00
import PostItemSkeleton from "@/components/skeletons/PostItemSkeleton";
2024-06-29 12:07:37 +08:00
import Link from "next/link";
2024-07-22 16:07:41 +08:00
import requireAPI from "@/utils/requireAPI";
2024-07-06 11:05:19 +08:00
import Empty from "@/components/Empty";
2024-08-05 21:10:36 +08:00
import { get } from "@/utils/storeInfo";
2024-08-02 22:12:54 +08:00
import StreamerNavigator from "@/components/StreamerNavigator";
import AddToHome from "@/components/AddToHome";
2024-08-07 20:41:51 +08:00
import InfiniteScrollContent from "@/components/InfiniteScrollContent";
2024-06-25 20:18:37 +08:00
const variables = {
"@active-line-color": "#f00", // 将主题色改为红色
};
2024-06-24 22:12:13 +08:00
const tabItems = [
2024-06-25 20:18:37 +08:00
{ key: "commend", title: "推荐" },
{ key: "follow", title: "关注" },
];
2024-06-29 12:07:37 +08:00
2024-06-24 22:12:13 +08:00
export default function Home() {
2024-07-08 20:07:36 +08:00
const recommPostRef = useRef();
const followPostRef = useRef();
2024-06-25 20:18:37 +08:00
const swiperRef = useRef(null);
const [activeIndex, setActiveIndex] = useState(0);
2024-07-06 11:05:19 +08:00
2024-06-25 20:18:37 +08:00
const [scrollHeight, setScrollHeight] = useState(0);
2024-07-08 20:07:36 +08:00
const childrenFunc = () => {
if (!activeIndex) {
recommPostRef.current?.doRefresh();
} else {
followPostRef.current?.doRefresh();
}
};
2024-06-24 22:12:13 +08:00
return (
<div>
2024-07-08 20:07:36 +08:00
<div className="flex justify-between items-center px-2 custom-tabs text-gray-400 sticky top-0 z-10 bg-deepBg">
2024-06-25 20:18:37 +08:00
<Tabs
2024-06-24 22:12:13 +08:00
activeKey={tabItems[activeIndex].key}
2024-06-25 20:18:37 +08:00
onChange={(key) => {
const index = tabItems.findIndex((item) => item.key === key);
setActiveIndex(index);
swiperRef.current?.swipeTo(index);
2024-06-24 22:12:13 +08:00
}}
>
2024-06-25 20:18:37 +08:00
{tabItems.map((item) => (
<Tabs.Tab
2024-07-06 11:05:19 +08:00
destroyOnClose={true}
2024-06-25 20:18:37 +08:00
forceRender={false}
title={item.title}
key={item.key}
className="text-left"
/>
2024-06-24 22:12:13 +08:00
))}
</Tabs>
2024-07-03 19:59:39 +08:00
<Link
href="search"
className="w-9 h-9 flex items-center justify-center bg-[#FFFFFF1A] rounded-full"
>
2024-08-05 21:10:36 +08:00
<Image
src={process.env.NEXT_PUBLIC_WEB_ASSETS_URL + "/icons/search.png"}
/>
2024-07-03 19:59:39 +08:00
</Link>
2024-06-25 20:18:37 +08:00
</div>
<Swiper
direction="horizontal"
allowTouchMove={false}
2024-07-06 11:05:19 +08:00
loop={false}
2024-06-25 20:18:37 +08:00
indicator={() => null}
ref={swiperRef}
defaultIndex={activeIndex}
onIndexChange={(index) => {
setActiveIndex(index);
}}
>
<Swiper.Item>
2024-07-08 20:07:36 +08:00
{!activeIndex && (
<RecommPostList scrollHeight={scrollHeight} ref={recommPostRef} />
)}
2024-06-25 20:18:37 +08:00
</Swiper.Item>
<Swiper.Item>
2024-07-31 15:11:13 +08:00
{!!activeIndex && (
2024-07-08 20:07:36 +08:00
<FollowPostList scrollHeight={scrollHeight} ref={followPostRef} />
)}
2024-06-25 20:18:37 +08:00
</Swiper.Item>
</Swiper>
2024-07-08 20:07:36 +08:00
<div
id="rightddd"
className={`fixed bottom-[96px] right-4 z-[50] w-10 h-10 flex items-center justify-center bg-[#1d1d1d71] rounded-full text-white
2024-07-08 20:07:36 +08:00
}`}
onClick={childrenFunc}
>
<FontAwesomeIcon icon={faRefresh} size="xl" />
</div>
2024-08-05 21:10:36 +08:00
<StreamerNavigator userId={Number(get("inviter"))} />
2024-08-07 20:41:51 +08:00
<AddToHome />
2024-07-02 15:09:48 +08:00
</div>
2024-06-25 20:18:37 +08:00
);
2024-06-24 22:12:13 +08:00
}
2024-07-08 20:07:36 +08:00
const RecommPostList = forwardRef(({ scrollHeight }, ref) => {
2024-08-07 20:41:51 +08:00
const [loading, setLoading] = useState(false);
2024-07-06 11:05:19 +08:00
const [hasMore, setHasMore] = useState(true);
2024-08-07 20:41:51 +08:00
const [currentSearchKey, setCurrentSearchKey] = useState(2);
2024-07-06 11:05:19 +08:00
const [commenPostList, setCommenPostList] = useState([]);
useEffect(() => {
2024-08-07 20:41:51 +08:00
// getRecommPostList(2).then((res) => {
// setCommenPostList(res);
// setHasMore(true)
// });
2024-07-06 11:05:19 +08:00
}, []);
2024-07-08 20:07:36 +08:00
useImperativeHandle(
ref,
() => {
return { doRefresh };
},
[]
);
2024-07-06 11:05:19 +08:00
async function doRefresh() {
// await sleep(1000);
// Toast.show({
// icon: "fail",
// content: "刷新失败",
// });
// throw new Error("刷新失败");
const list = await getRecommPostList(1);
setCommenPostList(list);
2024-08-05 21:10:36 +08:00
setHasMore(true);
2024-07-06 11:05:19 +08:00
}
async function loadMore() {
2024-08-07 21:28:17 +08:00
// debugger
2024-08-07 20:41:51 +08:00
const list = await getRecommPostList(!commenPostList.length?2:0);
2024-07-06 11:05:19 +08:00
if (list.length == 0) {
setHasMore(false);
}
setCommenPostList([...commenPostList, ...list]);
}
const getRecommPostList = async (type = 2) => {
setLoading(true);
2024-07-22 16:07:41 +08:00
const data = await requireAPI("POST", "/api/moment/recomm_list", {
2024-07-06 11:05:19 +08:00
body: { op_type: type },
});
setLoading(false);
if (data.ret == -1) {
2024-08-07 19:44:56 +08:00
// Toast.show({
// icon: "fail",
// content: data.msg,
// position: "top",
// });
2024-07-06 11:05:19 +08:00
} else {
return data.data.recomm_list;
}
};
return (
// <div className="px-4 pb-20 max-h-screen overflow-y-auto">
<div className="px-4 pb-20 min-h-screen">
2024-07-06 11:05:19 +08:00
<List>
2024-08-05 15:48:59 +08:00
{loading && !commenPostList.length && (
2024-07-06 11:05:19 +08:00
<div>
<PostItemSkeleton />
<PostItemSkeleton />
<PostItemSkeleton />
<PostItemSkeleton />
<PostItemSkeleton />
</div>
)}
2024-07-17 20:30:33 +08:00
{commenPostList?.map((item) => (
2024-07-06 11:05:19 +08:00
<List.Item key={item.id} className="!p-0">
2024-08-05 21:10:36 +08:00
<PostItem type="post" data={item} date={new Date(item.ct * 1000)} />
2024-07-06 11:05:19 +08:00
</List.Item>
))}
2024-08-07 20:41:51 +08:00
{/* {commenPostList?.length == 0 && !loading && (
2024-07-06 11:05:19 +08:00
<div
className={`flex flex-col items-center justify-center h-screen`}
// style={{ height: `${scrollHeight}px` }}
2024-07-06 11:05:19 +08:00
>
<Empty type="nodata" />
</div>
2024-08-07 20:41:51 +08:00
)} */}
2024-07-06 11:05:19 +08:00
</List>
2024-08-07 20:41:51 +08:00
{/* {!!commenPostList?.length && (
2024-07-06 11:05:19 +08:00
<InfiniteScroll loadMore={loadMore} hasMore={hasMore} />
2024-08-07 20:41:51 +08:00
)} */}
<InfiniteScroll loadMore={loadMore} hasMore={hasMore}>
<InfiniteScrollContent
hasMore={hasMore}
isEmpty={commenPostList.length == 0}
showNoMore={commenPostList.length === 0}
/>
</InfiniteScroll>
2024-07-06 11:05:19 +08:00
</div>
);
2024-07-08 20:07:36 +08:00
});
const FollowPostList = forwardRef(({ scrollHeight }, ref) => {
2024-07-06 11:05:19 +08:00
const [loading, setLoading] = useState(false);
const [hasMore, setHasMore] = useState(false);
const [followPostList, setFollowPostList] = useState([]);
const [currentTime, setCurrentTime] = useState();
const [offset, setOffset] = useState(0);
2024-08-05 21:10:36 +08:00
const [ids, setIds] = useState([]);
2024-07-06 11:05:19 +08:00
useEffect(() => {
2024-08-05 21:10:36 +08:00
getFollowIds().then((res) => {
setIds(res);
getFollowPostList(res, 0);
2024-07-06 11:05:19 +08:00
});
}, []);
2024-07-08 20:07:36 +08:00
useImperativeHandle(
ref,
() => {
return { doRefresh };
},
[]
);
2024-07-06 11:05:19 +08:00
async function doRefresh() {
// await sleep(1000);
// Toast.show({
// icon: "fail",
// content: "刷新失败",
// });
// throw new Error("刷新失败");
// getRecommPostList(1);
await getFollowPostList(ids, 0);
2024-07-06 11:05:19 +08:00
}
async function loadMore() {
await getFollowPostList(ids, offset);
2024-08-05 21:10:36 +08:00
// const newList = [...followPostList, ...list];
// setFollowPostList(newList);
2024-07-06 11:05:19 +08:00
}
2024-08-05 21:10:36 +08:00
const getFollowIds = async () => {
2024-07-06 11:05:19 +08:00
setLoading(true);
setCurrentTime(Math.floor(new Date().getTime() / 1000));
2024-08-05 21:10:36 +08:00
const data = await requireAPI(
"POST",
"/api/account_relation/list_follow",
{
body: { offset, limit: 4 },
},
true
);
return data;
};
const getFollowPostList = async (data, offset) => {
2024-07-06 11:05:19 +08:00
if (data.data.list.length > 0) {
//查关注主播展示资料
2024-08-05 21:10:36 +08:00
const followsResponse = await requireAPI(
"POST",
"/api/moment/list_by_mids",
{
2024-07-06 11:05:19 +08:00
body: {
offset,
limit: 4,
ct_upper_bound: currentTime,
2024-07-17 20:30:33 +08:00
mids: data.data.list?.map((item) => item.obj_mid),
2024-07-06 11:05:19 +08:00
},
2024-08-05 21:10:36 +08:00
}
);
// debugger;
console.log("offset", followsResponse.data.offset);
2024-08-05 21:10:36 +08:00
setOffset(followsResponse.data.offset);
setHasMore(followsResponse.data.more);
2024-07-06 11:05:19 +08:00
setLoading(false);
if (data.ret == -1) {
2024-08-07 19:44:56 +08:00
// Toast.show({
// icon: "fail",
// content: data.msg,
// position: "top",
// });
2024-07-06 11:05:19 +08:00
} else {
setFollowPostList((old) => [...old, ...followsResponse.data.list]);
2024-07-06 11:05:19 +08:00
}
} else {
setLoading(false);
}
};
2024-08-05 21:10:36 +08:00
2024-07-06 11:05:19 +08:00
return (
2024-07-25 19:52:30 +08:00
<div className="px-4 pb-20">
2024-07-06 11:05:19 +08:00
{/* <PullToRefresh onRefresh={doRefresh}> */}
<List>
2024-08-05 15:48:59 +08:00
{loading && !followPostList.length && (
2024-07-06 11:05:19 +08:00
<div className="my-[31px]">
<PostItemSkeleton />
<PostItemSkeleton />
<PostItemSkeleton />
<PostItemSkeleton />
<PostItemSkeleton />
</div>
)}
2024-07-17 20:30:33 +08:00
{followPostList?.map((item, index) => (
2024-07-06 11:05:19 +08:00
<List.Item key={item.id + "_" + index} className="!p-0">
2024-08-05 21:10:36 +08:00
<PostItem type="post" data={item} date={new Date(item.ct * 1000)} />
2024-07-06 11:05:19 +08:00
</List.Item>
))}
2024-07-17 20:30:33 +08:00
{!followPostList?.length && (
2024-07-06 11:05:19 +08:00
<div
className={`flex flex-col items-center justify-center h-screen`}
// style={{ height: `${scrollHeight - 98}px` }}
2024-07-06 11:05:19 +08:00
>
<Empty type="nodata" />
</div>
)}
</List>
2024-07-17 20:30:33 +08:00
{!!followPostList?.length && (
2024-07-06 11:05:19 +08:00
<InfiniteScroll loadMore={loadMore} hasMore={hasMore} />
)}
{/* </PullToRefresh> */}
</div>
);
2024-08-05 21:10:36 +08:00
});