tiefen_space_app/screeens/StreamerPosts/index.jsx

223 lines
6.1 KiB
JavaScript

import {
View,
Text,
RefreshControl,
TouchableOpacity,
Image as NativeImage,
} 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 { generateSignature } from "../../utils/crypto";
import { FlashList } from "@shopify/flash-list";
import Post from "../../components/Post";
import { get } from "../../utils/storeInfo";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { useVipVisibility } from "../../context/VipVisibilityProvider";
export default function StreamerPosts({ navigation, route }) {
const insets = useSafeAreaInsets();
//查看是否展示vip功能
const { isVipVisible } = useVipVisibility();
//获取会员价格
const [vipPrice, setVipPrice] = useState();
const getVipPrice = async () => {
try {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
const base = await baseRequest();
const signature = await generateSignature({
...base,
});
const _response = await fetch(
`${apiUrl}/api/vas/get_membership_product_list?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...base,
}),
}
);
const _data = await _response.json();
if (_data.ret === -1) {
Toast.show({
type: "error",
text1: _data.msg,
topOffset: 60,
});
return;
}
setVipPrice(_data.data.product.real_price / 100);
} catch (error) {
console.error(error);
}
};
//获取当前时间
const [currentTime, setCurrentTime] = useState();
const getCurrentTime = async () => {
setCurrentTime(Math.floor(new Date().getTime() / 1000));
};
//获取截止当前时间的动态数据
const [data, setData] = useState([]);
const [offset, setOffset] = useState(0);
const [more, setMore] = useState(1);
const getData = async (type) => {
if (!currentTime) 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({
mid: route.params.mid,
ct_upper_bound: currentTime,
offset: offset,
limit: 4,
...base,
});
const _response = await fetch(
`${apiUrl}/api/moment/list_by_mid?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
mid: route.params.mid,
ct_upper_bound: currentTime,
offset: offset,
limit: 4,
...base,
}),
}
);
const _data = await _response.json();
if (_data.ret === -1) {
Toast.show({
type: "error",
text1: _data.msg,
topOffset: 60,
});
return;
}
if (type === "top") {
setData((prev) => _data.data.list);
} else {
setData((prev) => [...prev, ..._data.data.list]);
}
setOffset(_data.data.offset);
setMore(_data.data.more);
} catch (error) {
console.error(error);
}
};
//是否需要blur
const [blur, setBlur] = useState(true);
const checkBlur = async () => {
const account = await get("account");
const role = account.role;
const isVip = account.is_a_member;
if (role !== 0 || isVip === 1 || !isVipVisible) {
return setBlur(false);
}
return setBlur(true);
};
//查看是否需要blur并获取当前时间和会员价格
useEffect(() => {
checkBlur();
getVipPrice();
getCurrentTime();
}, []);
//当时间改变,获取新数据
useEffect(() => {
getData("top");
}, [currentTime]);
const tailwind = useTailwind();
const renderItem = ({ item }) => <Post isBlur={blur} data={item} />;
const [refreshing, setRefreshing] = useState(false);
//下拉刷新
const handleRefresh = async () => {
setRefreshing(true);
setOffset(0);
setMore(1);
await getCurrentTime();
setRefreshing(false);
};
return (
<View style={tailwind("flex-1")}>
<FlashList
data={data}
renderItem={renderItem}
estimatedItemSize={310}
initialNumToRender={4}
refreshControl={
<RefreshControl
colors={["#FF669E"]}
tintColor="white"
refreshing={refreshing}
onRefresh={() => handleRefresh()}
/>
}
onEndReached={() => getData("bottom")}
ListEmptyComponent={<Empty type="nodata" />}
keyExtractor={(item) => item.id}
/>
{blur && (
<TouchableOpacity
activeOpacity={1}
onPress={() =>
navigation.navigate("WebWithoutHeader", {
uri: process.env.EXPO_PUBLIC_WEB_URL + "/vip",
})
}
style={{
paddingBottom: insets.bottom,
...tailwind(
"flex flex-row items-center justify-between bg-[#301024] px-4"
),
}}
>
<View style={tailwind("flex flex-row items-center h-12")}>
<NativeImage
source={require("../../assets/icon/others/vipbig.png")}
/>
<Text
numberOfLines={1}
ellipsizeMode="tail"
style={tailwind("text-[#FF669E] text-base font-medium ml-1")}
>
开通会员订阅全部
</Text>
</View>
<View
style={tailwind(
"flex justify-center items-center h-9 bg-[#FF669E] rounded-full px-4"
)}
>
<Text style={tailwind("text-white text-sm font-medium")}>
¥{vipPrice}/永久
</Text>
</View>
</TouchableOpacity>
)}
</View>
);
}