tiefen_space_h5/app/my/relationship/page.js

224 lines
7.5 KiB
JavaScript
Raw Normal View History

2024-07-02 15:09:48 +08:00
"use client";
2024-07-23 20:56:25 +08:00
import React, { useEffect, useState } from "react";
import {
JumboTabs,
List,
InfiniteScroll,
Avatar,
Toast,
DotLoading,
} from "antd-mobile";
2024-07-02 15:09:48 +08:00
// import { useRouter } from "next/navigation";
import { useRouter } from "next/navigation";
2024-07-23 20:56:25 +08:00
import { useSearchParams } from "next/navigation";
2024-07-02 15:09:48 +08:00
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleLeft } from "@fortawesome/free-solid-svg-icons";
2024-07-10 00:48:37 +08:00
import styles from "./index.module.scss";
import { handleFollow } from "@/api/public";
2024-07-22 16:07:41 +08:00
import requireAPI from "@/utils/requireAPI";
import InfiniteScrollContent from "@/components/InfiniteScrollContent";
2024-07-30 22:53:41 +08:00
import Empty from "@/components/Empty";
2024-07-02 15:09:48 +08:00
export default function Relationship() {
const [currentKey, setCurrentKey] = useState("");
2024-07-02 15:09:48 +08:00
const [hasMore, setHasMore] = useState(true);
// const router = useRouter();
const router = useRouter();
const searchParams = useSearchParams();
2024-07-10 00:48:37 +08:00
const [data, setData] = useState([]);
const [offset, setOffset] = useState(0);
2024-07-02 15:09:48 +08:00
// 获取屏幕高度
2024-07-30 22:53:41 +08:00
const scrollHeight = 600;
2024-07-02 15:09:48 +08:00
useEffect(() => {
if (currentKey) {
2024-07-10 00:48:37 +08:00
setOffset(0);
setData([]);
2024-07-23 20:56:25 +08:00
getData(currentKey, 0).then((res) => {
2024-07-10 00:48:37 +08:00
setData(res);
});
2024-07-10 00:48:37 +08:00
}
}, [currentKey]);
2024-07-02 15:09:48 +08:00
useEffect(() => {
const key = searchParams.get("key");
// console.log('nnnnn',searchParams.get("key"))
2024-07-10 00:48:37 +08:00
key && setCurrentKey(key);
}, [searchParams]);
async function loadMore(key) {
const append = await getData(key, offset);
2024-07-02 15:09:48 +08:00
setData((val) => [...val, ...append]);
2024-07-23 20:56:25 +08:00
// setHasMore(append.length > 0);
2024-07-02 15:09:48 +08:00
}
const getData = async (key, currentOffset) => {
2024-07-10 00:48:37 +08:00
// api/account_relation/list_is_followed
2024-07-22 16:07:41 +08:00
const followIds = await requireAPI("POST", `/api/account_relation/${
2024-07-10 00:48:37 +08:00
key == "follow" ? "list_follow" : "list_is_followed"
}`, {
body: { offset: currentOffset, limit: 12 },
2024-07-10 00:48:37 +08:00
}, true);
if (followIds.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
if (!followIds.data.list.length) return [];
const followsMids = followIds.data.list.map((item) => item.obj_mid);
2024-07-23 20:56:25 +08:00
const streamers = await requireAPI("POST", `/api${
key == "follow" ? "/streamer/list_ext_by_mids" : "/account/list_others_by_mids"
2024-07-10 00:48:37 +08:00
}`, {
body: { mids: followsMids, offset: 0, limit: 12 },
}, true);
if (streamers.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
const followsDataList = streamers.data.list.map((item) => ({
...item,
isFollowed: true,
}));
setOffset(streamers.data.offset);
2024-07-23 20:56:25 +08:00
setHasMore(streamers.data.more)
return [...followsDataList];
2024-07-10 00:48:37 +08:00
// setMore(temData.data.more);
2024-07-02 15:09:48 +08:00
};
const cancleFollow = async (item, index) => {
await handleFollow(item.isFollowed, item.mid);
const newData = [...data];
newData[index].isFollowed = !item.isFollowed;
setData(newData);
};
2024-07-02 15:09:48 +08:00
return (
<div className={styles.relationshipBox}>
<div className="p-4 fixed top-0 z-10 w-full">
2024-07-02 23:08:38 +08:00
<div className="w-9 h-9 flex items-center justify-center bg-[#FFFFFF1A] rounded-full absolute">
2024-07-02 15:09:48 +08:00
<FontAwesomeIcon
icon={faAngleLeft}
size="xl"
onClick={() => {
router.back();
}}
/>
</div>
<p className="text-base text-center leading-9">关系</p>
</div>
{/* 内容 */}
<div className="pt-16 px-4">
<JumboTabs
onChange={(key) => setCurrentKey(key)}
activeKey={currentKey}
className={`bg-deepBg ${styles.jumboTabs}`}
>
<JumboTabs.Tab
title="关注"
key="follow"
description={
2024-07-30 22:53:41 +08:00
currentKey == "follow" && (
2024-07-02 15:09:48 +08:00
<div className="titlePinkLine relative w-full"></div>
)
}
destroyOnClose={true}
>
<List className="overflow-y-auto scrollbarBox_hidden">
2024-07-10 00:48:37 +08:00
{data.map((item, index) => (
<List.Item
key={item.id + "_" + index}
className={`!p-0 ${styles.listTimeBox}`}
>
<div
className="grid gap-2 items-center"
style={{
gridTemplateColumns: "48px calc(100% - 128px) 64px",
}}
>
<Avatar
src={item?.avatar?.images[0].urls[0]}
style={{ "--border-radius": "50px" }}
/>
<div>
<p>{item.name}</p>
<p className="text-xs truncate">{item.bio}</p>
</div>
<div
className="text-sm leading-9 h-max bg-[#FFFFFF1A] px-2 rounded-full whitespace-nowrap flex items-center justify-center"
onClick={() => cancleFollow(item, index)}
>
2024-07-10 00:48:37 +08:00
{item.isFollowed ? "已关注" : "关注"}
</div>
2024-07-02 15:09:48 +08:00
</div>
2024-07-10 00:48:37 +08:00
</List.Item>
))}
<InfiniteScroll
loadMore={() => loadMore("follow")}
hasMore={hasMore}
>
2024-07-30 22:53:41 +08:00
<InfiniteScrollContent isEmpty={data.length==0} showNoMore={false}/>
</InfiniteScroll>
2024-07-02 15:09:48 +08:00
</List>
2024-07-30 22:53:41 +08:00
{/* {!data.length && <div
className={`flex flex-col items-center mt-20`}
style={{ height: `${scrollHeight}px` }}
>
<Empty type="nodata" />
</div>} */}
2024-07-02 15:09:48 +08:00
</JumboTabs.Tab>
<JumboTabs.Tab
title="粉丝"
key="fans"
description={
2024-07-30 22:53:41 +08:00
currentKey == "fans" && (
2024-07-02 15:09:48 +08:00
<div className="titlePinkLine relative w-full"></div>
)
}
destroyOnClose={true}
>
2024-07-10 00:48:37 +08:00
<List className="overflow-y-auto scrollbarBox_hidden">
{data.map((item, index) => (
<List.Item
key={item.id + "_" + index}
className={`!p-0 ${styles.listTimeBox}`}
>
<div
className="grid gap-2 items-center"
style={{
gridTemplateColumns: "48px calc(100% - 128px) 64px",
}}
>
<Avatar
src={item?.avatar?.images[0].urls[0]}
style={{ "--border-radius": "50px" }}
/>
<div>
<p>{item.name}</p>
<p className="text-xs truncate">{item.bio}</p>
</div>
2024-07-30 22:53:41 +08:00
{/* <div
className="text-sm leading-9 h-max bg-[#FFFFFF1A] px-2 rounded-full whitespace-nowrap flex items-center justify-center"
onClick={() => handleFollow(item.isFollowed, item.mid)}
>
2024-07-10 00:48:37 +08:00
{item.isFollowed ? "已关注" : "关注"}
2024-07-30 22:53:41 +08:00
</div> */}
2024-07-10 00:48:37 +08:00
</div>
</List.Item>
))}
<InfiniteScroll
loadMore={() => loadMore("fans")}
hasMore={hasMore}
>
2024-07-30 22:53:41 +08:00
<InfiniteScrollContent hasMore={hasMore} isEmpty={data.length==0} showNoMore={false}/>
</InfiniteScroll>
2024-07-10 00:48:37 +08:00
</List>
2024-07-02 15:09:48 +08:00
</JumboTabs.Tab>
</JumboTabs>
</div>
</div>
);
}