tiefen_space_h5/app/streamerPosts/createPost/page.jsx

202 lines
5.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import React, { useState, useEffect } from "react";
import { DotLoading, Popup, Toast, TextArea, Switch } from "antd-mobile";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleLeft } from "@fortawesome/free-solid-svg-icons";
import requireAPI from "@/utils/requireAPI";
import { useRouter } from "next/navigation";
import { multiUploadImage } from "@/utils/upload";
import UploadImgs from "@/components/UploadImgs";
export default function CreatePost() {
const [isSubmitting, setIsSubmitting] = useState(false);
const [oldPhotos, setOldPhotos] = useState([]);
//价格
const [formData, setFormData] = useState({
content: "",
imageAssets: [],
videoAssets: [],
});
const router = useRouter();
useEffect(() => {
getPostCountData();
}, []);
const getPostCountData = async () => {
try {
const _data = await requireAPI(
"POST",
"/api/zone_moment/list_statistics_by_creater_mid"
);
if (_data.ret === -1) {
Toast.show({
icon: "fail",
content: _data.msg,
position: "top",
});
return;
}
} catch (error) {
console.error(error);
}
};
//发布内容
const handleSubmit = async () => {
if (formData.content == "") {
Toast.show({
icon: "fail",
content: "动态内容不能为空",
position: "top",
});
return;
}
if (
formData.imageAssets.length === 0 &&
formData.videoAssets.length === 0
) {
Toast.show({
icon: "fail",
content: "请上传图片或视频",
position: "top",
});
return;
}
//提交数据
if (isSubmitting) return;
setIsSubmitting(true);
const { content, imageAssets, videoAssets } = formData;
const newImageMedia = imageAssets.filter((it) => it.id == undefined);
const type = formData.imageAssets.length > 0 ? 1 : 2;
const media = await multiUploadImage(
type == 1 ? newImageMedia : videoAssets,
type
);
// const media = await multiUploadImage(imageAssets);
if (type == 1) {
media.image_ids = [
...imageAssets.filter((it) => it.id != undefined).map((it) => it.id),
...media.image_ids,
];
}
try {
const body = {
text: content,
media_component: media,
status: 2,
};
const data = await requireAPI(
"POST",
"/api/moment/create",
{
body,
},
true,
100000
);
if (data.ret === -1) {
Toast.show({
icon: "fail",
content: data.msg,
position: "top",
});
return;
}
//提交成功后显示Toast并返回上一页
Toast.show({
icon: "success",
content: "发布成功",
position: "top",
});
router.back();
} catch (error) {
console.error(error);
} finally {
setIsSubmitting(false);
}
};
return (
<div className="flex-1">
{/* 头部标题 */}
<div className="p-4 fixed top-0 z-10 w-full flex justify-between items-center bg-black">
<div className="w-9 h-9 flex items-center justify-center bg-[#FFFFFF1A] rounded-full">
<FontAwesomeIcon
icon={faAngleLeft}
style={{ maxWidth: "12px" }}
size="xl"
onClick={() => {
router.back();
}}
/>
</div>
<p className="text-base text-center leading-9">广场动态</p>
{isSubmitting ? (
<DotLoading />
) : (
<span className="text-primary text-lg" onClick={handleSubmit}>
发布
</span>
)}
</div>
{/* 内容 */}
<div className="pt-16 p-4">
<div className="mt-2">
<p className="text-base font-medium text-white">动态内容</p>
<div className="h-32">
<TextArea
placeholder="请遵守平台准则,严禁发布违规内容"
value={formData.content}
autoSize={{ minRows: 6, maxRows: 15 }}
onChange={(value) =>
setFormData((old) => ({ ...old, content: value }))
}
style={{ "--font-size": "16px" }}
className="h-full bg-[#FFFFFF1A] rounded-2xl mt-2 mb-4 p-2"
/>
</div>
</div>
<div className="mt-4">
<p className="text-base font-medium mt-4 mb-1">
图片
<span className="text-[#FFFFFF80] text-sm">
添加图片后不可添加视频
</span>
</p>
<UploadImgs
type={1}
id="uploadAvatarBtn1"
existImages={oldPhotos}
assets={formData.imageAssets}
getImgs={(imgs) => {
setFormData((old) => ({
...old,
imageAssets: imgs,
videoAssets: [],
}));
}}
/>
</div>
<div className="mt-4">
<p className="text-base font-medium mt-4 mb-1">
视频
<span className="text-[#FFFFFF80] text-sm">
添加视频后不可添加图片
</span>
</p>
<UploadImgs
type={2}
accept="video/*"
id="uploadAvatarBtn2"
assets={formData.videoAssets}
getImgs={(imgs) => {
setFormData((old) => ({
...old,
videoAssets: imgs,
imageAssets: [],
}));
}}
/>
</div>
</div>
</div>
);
}