import {
View,
Text,
TextInput,
ScrollView,
ActivityIndicator,
Modal,
KeyboardAvoidingView,
TouchableOpacity,
Platform,
} from "react-native";
import React, { useState, useEffect, useCallback } from "react";
import { useTailwind } from "tailwind-rn";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import MediaPicker from "../../components/MediaPicker";
import Toast from "react-native-toast-message";
import { multiUpload } from "../../utils/upload";
import baseRequest from "../../utils/baseRequest";
import { generateSignature } from "../../utils/crypto";
import { Image } from "expo-image";
import { Icon } from "@rneui/themed";
import { Button, Switch } from "@rneui/themed";
import MediaGrid from "../../components/MediaGrid";
export default function EditSpacePost({ navigation, route }) {
const data = route.params.data;
const tailwind = useTailwind();
const insets = useSafeAreaInsets();
const [content, setContent] = useState(
data?.paid_text
? data.text.slice(0, data.text.length - data.paid_text.length)
: data.text
);
const [isSubmitting, setIsSubmitting] = useState(false);
//付费设置弹窗是否展示
const [isModalVisible, setIsModalVisible] = useState(false);
//价格
const [price, setPrice] = useState((data?.price / 100)?.toString());
//可预览文案行数
const [textVisibleRange, setTextVisibleRange] = useState(
data?.text_visible_range?.toString()
);
//可预览图片张数
const [imageVisibleRange, setImageVisibleRange] = useState(
data?.media_visible_range?.toString()
);
//是否模糊封面
const [blurCover, setBlurCover] = useState(
data?.is_blurring_cover === 1 ? true : false
);
//是否铁粉免费查看
const [isFreeForIronfan, setIsFreeForIronfan] = useState(
data?.is_ironfan_visible === 1 ? true : false
);
//是否添加付费文案
const [isCreatingPaidText, setIsCreatingPaidText] = useState(
data?.is_creating_paid_text === 1 ? true : false
);
//付费文案内容
const [paidText, setPaidText] = useState(
data?.paid_text ? data.paid_text.substring(1) : data?.paid_text
);
//保存付费设置
const handleSavePaymentSetting = () => {
if (parseFloat(price) === 0) {
Toast.show({
type: "error",
text1: "不可改为免费动态",
topOffset: 60,
});
return;
}
if (parseFloat(price) < 0) {
Toast.show({
type: "error",
text1: "请输入正确区间的价格",
topOffset: 60,
});
return;
}
if (parseFloat(price) > 0 && parseInt(parseFloat(price) * 100, 10) < 100) {
Toast.show({
type: "error",
text1: "请输入正确区间的价格",
topOffset: 60,
});
return;
}
if (parseInt(parseFloat(price) * 100, 10) > 300000) {
Toast.show({
type: "error",
text1: "请输入正确区间的价格",
topOffset: 60,
});
return;
}
if (parseInt(textVisibleRange, 10) < 1) {
Toast.show({
type: "error",
text1: "请输入正确区间的可预览文案行数",
topOffset: 60,
});
return;
}
if (
parseInt(imageVisibleRange, 10) < 0 ||
parseInt(imageVisibleRange, 10) > 3
) {
Toast.show({
type: "error",
text1: "请输入正确区间的可预览图片张数",
topOffset: 60,
});
return;
}
setIsModalVisible(false);
};
//保存新添加的媒体
const [newMeidaAssets, setNewMediaAssets] = useState([]);
//展示旧媒体组件
const [oldMediaAssets, setOldMediaAssets] = useState(
data.m_type === 1
? data.media_component.images
: data.media_component.videos
);
//发布更新内容
const handleSubmit = async () => {
if (!content) {
Toast.show({
type: "error",
text1: "动态内容不能为空",
topOffset: 60,
});
return;
}
if (oldMediaAssets.length === 0 && newMeidaAssets.length === 0) {
Toast.show({
type: "error",
text1: "请选择要上传的图片或视频",
topOffset: 60,
});
return;
}
if (
data.m_type === 1 &&
oldMediaAssets.length + newMeidaAssets.length > 30
) {
Toast.show({
type: "error",
text1: "最多只能上传30张照片哦!",
topOffset: 60,
});
return;
}
if (
data.m_type === 2 &&
oldMediaAssets.length + newMeidaAssets.length > 1
) {
Toast.show({
type: "error",
text1: "最多只能上传1个视频哦!",
topOffset: 60,
});
return;
}
if (
data.c_type === 1 &&
data.m_type === 1 &&
oldMediaAssets.length + newMeidaAssets.length <= imageVisibleRange
) {
Toast.show({
type: "error",
text1: "预览图片数不得大于等于上传图片数",
topOffset: 60,
});
return;
}
if (isCreatingPaidText && price > 0 && !paidText) {
Toast.show({
type: "error",
text1: "请填写付费文案",
topOffset: 60,
});
return;
}
//提交数据
if (isSubmitting) return;
setIsSubmitting(true);
const newMediaIds = await multiUpload(newMeidaAssets);
const oldMediaIds = oldMediaAssets.map((item) => item.id);
const media = {
image_ids:
data.m_type === 1 ? [...oldMediaIds, ...newMediaIds?.image_ids] : [],
video_ids:
data.m_type === 2 ? [...oldMediaIds, ...newMediaIds?.video_ids] : [],
};
const apiUrl = process.env.EXPO_PUBLIC_API_URL;
try {
const base = await baseRequest();
const body = {
...base,
id: data.id,
c_type: data.c_type,
m_type: data.m_type,
text: content,
media_component: media,
text_visible_range: textVisibleRange
? parseInt(textVisibleRange, 10)
: 999,
media_visible_range: imageVisibleRange
? parseInt(imageVisibleRange, 10)
: 1,
is_blurring_cover: blurCover ? 1 : 0,
is_ironfan_visible: isFreeForIronfan ? 1 : 0,
price: parseFloat(price) ? parseInt(parseFloat(price) * 100, 10) : null,
is_creating_paid_text: isCreatingPaidText && price > 0 ? 1 : 0,
paid_text: isCreatingPaidText && price > 0 ? paidText : null,
};
const signature = await generateSignature(body);
const _response = await fetch(
`${apiUrl}/api/zone_moment/update?signature=${signature}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
}
);
const _data = await _response.json();
if (_data.ret === -1) {
Toast.show({
type: "error",
text1: _data.msg,
topOffset: 60,
});
return;
}
//提交成功后显示Toast并返回上一页
Toast.show({
type: "info",
text1: "提交成功,等耐心等待审核",
topOffset: 60,
});
navigation.goBack();
} catch (error) {
console.error(error);
} finally {
setIsSubmitting(false);
}
};
//发布按钮
useEffect(() => {
navigation.setOptions({
headerRight: () => (
{isSubmitting ? "" : "发布"}
{isSubmitting && }
),
});
}, [isSubmitting, handleSubmit]);
//是否正在拖动图片,用于禁用ScrollView的滚动
const [dragging, setDragging] = useState(false);
return (
{data?.status === 3 && (
违规详情
{data.text_audit_opinion && (
文案违规原因:
{data.text_audit_opinion}
)}
{data.image_audit_opinion && (
图片/视频违规原因:
{data.image_audit_opinion}
)}
{data.manually_review_opinion && (
运营追加:
{data.manually_review_opinion}
)}
)}
动态内容
setContent(value)}
value={content}
style={tailwind(
"h-32 bg-[#FFFFFF1A] text-white rounded-2xl mt-2 mb-4 p-2"
)}
/>
{isCreatingPaidText && price > 0 && (
<>
付费文案
setPaidText(value)}
value={paidText}
style={tailwind(
"h-32 bg-[#FFFFFF1A] text-white rounded-2xl mt-2 mb-4 p-2"
)}
/>
>
)}
{data.c_type === 1 && (
)}
{data.m_type === 1 && (
价格:¥
setPrice(value)}
value={price}
style={tailwind(
"flex-1 bg-[#FFFFFF1A] text-white rounded-2xl px-4 h-8 mx-2"
)}
/>
setPrice("0")}
style={tailwind(
"text-[#FF669E] text-base font-medium"
)}
>
免费
{parseFloat(price) > 0 && (
<>
可预览图片:
前
setImageVisibleRange(value)
}
value={imageVisibleRange}
style={tailwind(
"bg-[#FFFFFF1A] text-white rounded-2xl px-4 mx-2 h-8"
)}
/>
张
setImageVisibleRange("0")}
style={tailwind(
"text-[#FF669E] text-base font-medium ml-auto"
)}
>
不可预览
添加付费文案:
setIsCreatingPaidText(value)
}
thumbColor="#ffffff"
trackColor={{
true: "#FF669E",
false: "#FFFFFF1A",
}}
/>
铁粉免费查看:
setIsFreeForIronfan(value)
}
thumbColor="#ffffff"
trackColor={{
true: "#FF669E",
false: "#FFFFFF1A",
}}
/>
>
)}
保存
)}
{data.m_type === 2 && (
价格:¥
setPrice(value)}
value={price}
style={tailwind(
"flex-1 bg-[#FFFFFF1A] text-white rounded-2xl px-4 h-8 mx-2"
)}
/>
setPrice("0")}
style={tailwind(
"text-[#FF669E] text-base font-medium"
)}
>
免费
{parseFloat(price) > 0 && (
<>
添加付费文案:
setIsCreatingPaidText(value)
}
thumbColor="#ffffff"
trackColor={{
true: "#FF669E",
false: "#FFFFFF1A",
}}
/>
封面模糊:
setBlurCover(value)}
thumbColor="#ffffff"
trackColor={{
true: "#FF669E",
false: "#FFFFFF1A",
}}
/>
铁粉免费查看:
setIsFreeForIronfan(value)
}
thumbColor="#ffffff"
trackColor={{
true: "#FF669E",
false: "#FFFFFF1A",
}}
/>
>
)}
保存
)}
);
}