tiefen_space_app/components/SubmitWechatModal/index.jsx

381 lines
12 KiB
React
Raw Normal View History

2023-12-29 00:27:44 +08:00
import {
View,
Text,
Modal,
TouchableOpacity,
TextInput,
Keyboard,
Alert,
2024-04-24 16:33:47 +08:00
KeyboardAvoidingView,
2023-12-29 00:27:44 +08:00
} from "react-native";
import React, { useState, useEffect } from "react";
import { useTailwind } from "tailwind-rn";
import { Image } from "expo-image";
import { useNavigation } from "@react-navigation/native";
import { get } from "../../utils/storeInfo";
import baseRequest from "../../utils/baseRequest";
import Toast from "react-native-toast-message";
import { generateSignature } from "../../utils/crypto";
export default function SubmitWechatModal({
visible,
setVisible,
streamerMid,
}) {
const tailwind = useTailwind();
const blurhash = "LcKUTa%gOYWBYRt6xuoJo~s8V@fk";
const navigation = useNavigation();
//保存用户数据
const [userData, setUserData] = useState({});
//保存主播数据
const [streamerData, setStreamerData] = useState({});
//是否已经解锁微信
const [isWechatUnlocked, setIsWechatUnlocked] = useState(false);
//是否展示余额不足内容
const [isMoneyEnough, setIsMoneyEnough] = useState(true);
//保存用户微信号
const [userWechat, setUserWechat] = useState("");
//保存用户备注
const [remarks, setRemarks] = useState("");
useEffect(() => {
const getInitData = async () => {
const account = await get("account");
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
const base = await baseRequest();
const signature = await generateSignature({
mid: streamerMid,
...base,
});
try {
//获取主播数据
const streamerResponse = await fetch(
`${apiUrl}/api/streamer/list_ext_by_mid?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
mid: streamerMid,
...base,
}),
}
);
const temStreamerData = await streamerResponse.json();
if (temStreamerData.ret === -1) {
Toast.show({
type: "error",
text1: temStreamerData.msg,
topOffset: 60,
});
return;
}
setStreamerData({
...temStreamerData.data.streamer_ext,
wechat_coin_price:
temStreamerData.data.streamer_ext.wechat_coin_price,
2024-04-18 22:58:59 +08:00
wechat_order_id: temStreamerData.data.wechat_order_id,
wechat_order_status: temStreamerData.data.wechat_order_status,
2023-12-29 00:27:44 +08:00
});
//是否已经解锁微信
2024-04-18 22:58:59 +08:00
setIsWechatUnlocked(
temStreamerData.data.is_unlock_wechat === 1 ? true : false
);
2023-12-29 00:27:44 +08:00
//获取用户数据
const signature2 = await generateSignature({
...base,
mid: account.mid,
});
const userResponse = await fetch(
`${apiUrl}/api/account/list_by_mid?signature=${signature2}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
...base,
mid: account.mid,
}),
}
);
const temUserData = await userResponse.json();
if (temUserData.ret === -1) {
Toast.show({
type: "error",
text1: temUserData.msg,
topOffset: 60,
});
return;
}
setUserData(temUserData.data.account);
} catch (error) {
console.error(error);
}
};
if (!streamerMid) return;
2024-04-24 16:33:47 +08:00
if (!visible) return;
2023-12-29 00:27:44 +08:00
getInitData();
2024-04-24 16:33:47 +08:00
}, [streamerMid, visible]);
2023-12-29 00:27:44 +08:00
//点击解锁微信按钮
const unlockWechat = async () => {
2024-04-18 22:58:59 +08:00
if (
userData?.gold_num >= streamerData?.wechat_coin_price ||
(isWechatUnlocked && streamerData?.wechat_order_status === 2)
) {
2023-12-29 00:27:44 +08:00
if (!userWechat) {
Alert.alert(null, "请填写您的微信");
return;
}
2024-04-18 22:58:59 +08:00
//付款函数
const payCoin = async () => {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
const base = await baseRequest();
const signature = await generateSignature({
contact_product_id: "contact_wechat",
uid: streamerMid,
...base,
});
try {
//支付金币解锁微信
const unlockResponse = await fetch(
`${apiUrl}/api/vas/one_step_unlock?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
contact_product_id: "contact_wechat",
uid: streamerMid,
...base,
}),
}
);
const unlockData = await unlockResponse.json();
if (unlockData.ret === -1) {
Toast.show({
type: "error",
text1: unlockData.msg,
topOffset: 60,
});
return;
2023-12-29 00:27:44 +08:00
}
2024-04-18 22:58:59 +08:00
return unlockData.data.order_id;
} catch (error) {
console.error(error);
2023-12-29 00:27:44 +08:00
}
2024-04-18 22:58:59 +08:00
};
//提交微信函数
const submitWechat = async (order_id) => {
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
const base = await baseRequest();
2023-12-29 00:27:44 +08:00
//提交用户微信和备注
2024-04-24 16:33:47 +08:00
const body = {
2024-04-18 22:58:59 +08:00
order_id: order_id,
2023-12-29 00:27:44 +08:00
wechat: userWechat,
note: remarks,
...base,
2024-04-24 16:33:47 +08:00
};
const signature2 = await generateSignature(body);
2024-04-18 22:58:59 +08:00
try {
const submitWechatResponse = await fetch(
`${apiUrl}/api/vas/consumer_fill_contact?signature=${signature2}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
2024-04-24 16:33:47 +08:00
body: JSON.stringify(body),
2024-04-18 22:58:59 +08:00
}
);
const submitWechatData = await submitWechatResponse.json();
if (submitWechatData.ret === -1) {
Toast.show({
type: "error",
text1: submitWechatData.msg,
topOffset: 60,
});
return;
2023-12-29 00:27:44 +08:00
}
2024-04-18 22:58:59 +08:00
} catch (error) {
console.error(error);
2023-12-29 00:27:44 +08:00
}
2024-04-18 22:58:59 +08:00
};
//用户未解锁微信情况:先支付,再提交用户微信和备注
if (!isWechatUnlocked) {
const order_id = await payCoin();
if (!order_id) return;
await submitWechat(order_id);
2023-12-29 00:27:44 +08:00
//展示解锁成功界面
setIsWechatUnlocked(true);
2024-04-18 22:58:59 +08:00
return;
}
//用户已解锁微信情况:直接提交用户微信和备注
if (isWechatUnlocked) {
await submitWechat(streamerData.wechat_order_id);
setStreamerData({
...streamerData,
wechat_order_status: 3,
});
2023-12-29 00:27:44 +08:00
}
} else {
setIsMoneyEnough(false);
}
};
return (
<Modal
visible={visible}
transparent={true}
statusBarTranslucent
animationType="fade"
>
<TouchableOpacity
activeOpacity={1}
onPress={() => {
setVisible(false);
setIsMoneyEnough(true);
}}
2023-12-29 00:27:44 +08:00
style={{
backgroundColor: "#00000080",
2024-04-24 16:33:47 +08:00
...tailwind("flex flex-1 justify-center items-center"),
2023-12-29 00:27:44 +08:00
}}
>
2024-04-24 16:33:47 +08:00
<KeyboardAvoidingView
style={tailwind("flex flex-col items-center w-full")}
behavior="padding"
2023-12-29 00:27:44 +08:00
>
2024-04-24 16:33:47 +08:00
<TouchableOpacity
activeOpacity={1}
onPress={() => Keyboard.dismiss()}
style={tailwind(
"flex flex-col p-4 rounded-2xl bg-[#1E1C29] items-center w-3/4"
)}
>
{isMoneyEnough ? (
<>
<View style={tailwind("items-center")}>
<Image
style={tailwind("w-16 h-16 rounded-full absolute -top-12")}
source={streamerData?.avatar?.images[0]?.urls[0]}
placeholder={blurhash}
contentFit="cover"
transition={1000}
cachePolicy="disk"
/>
2023-12-29 00:27:44 +08:00
</View>
2024-04-24 16:33:47 +08:00
<Text style={tailwind("text-2xl text-white font-medium mt-4")}>
{streamerData?.name}
</Text>
{/* 提交微信区 */}
{isWechatUnlocked && streamerData?.wechat_order_status !== 2 ? (
<View>
<Text style={tailwind("text-sm text-white mt-2")}>
{(streamerData?.wechat_order_status === 0 ||
streamerData?.wechat_order_status === 3) &&
"已成功提交您的微信,请耐心等待"}
{(streamerData?.wechat_order_status === 4 ||
streamerData?.wechat_order_status === 5) &&
"已确认添加,订单完成"}
</Text>
</View>
) : (
<View style={tailwind("flex flex-col items-start w-full")}>
<Text style={tailwind("text-base text-white font-medium")}>
<Text style={tailwind("text-[#F53030]")}>*</Text>您的微信
</Text>
<TextInput
placeholder="填写您的微信以便Ta主动联系您"
placeholderTextColor="#FFFFFF80"
style={tailwind(
"text-white border border-[#2c2b2f] rounded-xl p-2 my-1 w-full"
)}
underlineColorAndroid="transparent"
onChangeText={(value) => setUserWechat(value)}
value={userWechat}
/>
<Text style={tailwind("text-base text-white font-medium")}>
备注
</Text>
<TextInput
placeholder="如添加好友需填写验证信息,请将相应答案填写在此处"
placeholderTextColor="#FFFFFF80"
multiline
textAlignVertical="top"
style={tailwind(
"text-white border border-[#2c2b2f] rounded-xl p-2 my-1 w-full"
)}
underlineColorAndroid="transparent"
onChangeText={(value) => setRemarks(value)}
value={remarks}
/>
</View>
)}
<Text
style={tailwind("text-xs text-[#F53030] font-medium my-2")}
>
若解锁后72小时未通过好友请联系客服
</Text>
{(!isWechatUnlocked ||
streamerData?.wechat_order_status === 2) && (
<TouchableOpacity
onPress={unlockWechat}
2023-12-29 00:27:44 +08:00
style={tailwind(
2024-04-24 16:33:47 +08:00
"px-4 py-2 bg-[#FF669E] rounded-full items-center justify-center"
2023-12-29 00:27:44 +08:00
)}
2024-04-24 16:33:47 +08:00
>
{!isWechatUnlocked && (
<Text
style={tailwind(
"text-white text-base font-medium px-4"
)}
>
提交并支付{streamerData?.wechat_coin_price}金币
</Text>
2023-12-29 00:27:44 +08:00
)}
2024-04-24 16:33:47 +08:00
{streamerData?.wechat_order_status === 2 && (
<Text
style={tailwind(
"text-white text-base font-medium px-4"
)}
>
提交微信
</Text>
)}
</TouchableOpacity>
)}
</>
) : (
<>
<Text style={tailwind("text-2xl text-white font-medium mb-4")}>
余额不足
</Text>
2023-12-29 00:27:44 +08:00
<TouchableOpacity
2024-04-24 16:33:47 +08:00
onPress={() => {
navigation.navigate("Wallet");
setVisible(false);
setIsMoneyEnough(true);
2024-04-24 16:33:47 +08:00
}}
2023-12-29 00:27:44 +08:00
style={tailwind(
"px-4 py-2 bg-[#FF669E] rounded-full items-center justify-center"
)}
>
2024-04-24 16:33:47 +08:00
<Text style={tailwind("text-white text-base")}>前往充值</Text>
2023-12-29 00:27:44 +08:00
</TouchableOpacity>
2024-04-24 16:33:47 +08:00
</>
)}
</TouchableOpacity>
</KeyboardAvoidingView>
2023-12-29 00:27:44 +08:00
</TouchableOpacity>
</Modal>
);
}