tiefen_space_h5/app/page.js

314 lines
8.9 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-06-25 20:18:37 +08:00
import {
Tabs,
Swiper,
Toast,
InfiniteScroll,
List,
2024-06-25 22:47:18 +08:00
Image,
2024-06-25 20:18:37 +08:00
} 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-02 22:12:54 +08:00
import {get} from "@/utils/storeInfo"
import {useSearchParams} from "next/navigation"
import StreamerNavigator from "@/components/StreamerNavigator";
import { checkAuth } from "@/utils/auth";
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-25 20:18:37 +08:00
// const scrollHeight = 700;
// const scrollHeight = window.innerHeight-126
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-08-02 22:12:54 +08:00
const searchParams = useSearchParams();
2024-06-25 20:18:37 +08:00
const swiperRef = useRef(null);
const [activeIndex, setActiveIndex] = useState(0);
2024-08-02 22:12:54 +08:00
const [visible, setVisible] = useState(false);
2024-07-06 11:05:19 +08:00
2024-06-25 20:18:37 +08:00
const [scrollHeight, setScrollHeight] = useState(0);
// 获取屏幕高度
// const scrollHeight = 600;
useEffect(() => {
2024-07-19 14:14:40 +08:00
setScrollHeight(window.innerHeight);
2024-07-03 19:59:39 +08:00
// getData(0)
2024-08-02 22:12:54 +08:00
checkAuth().then(res=>{
if(res && get("inviter")){
setVisible(true)
}
})
}, []);
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 (
2024-07-08 20:07:36 +08:00
<div
className="overflow-x-hidden"
style={{ maxHeight: `${scrollHeight}px` }}
>
<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-07-24 19:08:22 +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"
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-[999] w-10 h-10 flex items-center justify-center bg-[#1d1d1d71] rounded-full text-white
}`}
onClick={childrenFunc}
>
<FontAwesomeIcon icon={faRefresh} size="xl" />
</div>
2024-08-05 15:48:59 +08:00
<StreamerNavigator visible={visible} setVisible={setVisible} userId={Number(get("inviter"))}/>
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-07-06 11:05:19 +08:00
const [loading, setLoading] = useState(true);
const [hasMore, setHasMore] = useState(true);
const [commenPostList, setCommenPostList] = useState([]);
useEffect(() => {
getRecommPostList(2).then((res) => {
setCommenPostList(res);
});
}, []);
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-07-10 16:50:53 +08:00
setHasMore(true)
2024-07-06 11:05:19 +08:00
}
async function loadMore() {
const list = await getRecommPostList(0);
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) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
} else {
return data.data.recomm_list;
}
};
return (
2024-07-25 19:52:30 +08:00
<div className="px-4 pb-20">
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-07-12 22:52:48 +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
{commenPostList?.length == 0 && !loading && (
2024-07-06 11:05:19 +08:00
<div
className={`flex flex-col items-center justify-center`}
style={{ height: `${scrollHeight}px` }}
>
<Empty type="nodata" />
</div>
)}
</List>
2024-07-17 20:30:33 +08:00
{!!commenPostList?.length && (
2024-07-06 11:05:19 +08:00
<InfiniteScroll loadMore={loadMore} hasMore={hasMore} />
)}
</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);
useEffect(() => {
getFollowPostList().then((res) => {
setFollowPostList(res);
});
}, []);
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);
2024-07-08 20:07:36 +08:00
const list = await getFollowPostList(1);
setFollowPostList(list);
2024-07-06 11:05:19 +08:00
}
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));
2024-07-22 16:07:41 +08:00
const data = await requireAPI("POST", "/api/account_relation/list_follow", {
2024-07-06 11:05:19 +08:00
body: { offset, limit: 4 },
}, true);
setHasMore(data.data.list.length > 0);
if (data.data.list.length > 0) {
//查关注主播展示资料
const followsResponse =
2024-07-22 16:07:41 +08:00
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
},
});
setLoading(false);
if (data.ret == -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
} else {
return followsResponse.data.list;
}
} else {
setLoading(false);
}
};
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-07-12 22:52:48 +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`}
2024-07-17 20:30:33 +08:00
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-02 22:12:54 +08:00
});