"use client"; import React, { useState, useEffect } from "react"; import { List, Toast, Image, Space, Picker, TextArea, Modal, Mask, SpinLoading, Button, } from "antd-mobile"; import { DragDropContext, Draggable, Droppable } from "@hello-pangea/dnd"; import { useRouter } from "next/navigation"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faAngleLeft, faAdd, faEdit, faTrashCan, faSortDown, } from "@fortawesome/free-solid-svg-icons"; import OwnInput from "@/components/OwnInput"; import requireAPI from "@/utils/requireAPI"; import { get } from "@/utils/storeInfo"; const reorder = (list, startIndex, endIndex) => { const result = Array.from(list); const [removed] = result.splice(startIndex, 1); result.splice(endIndex, 0, removed); return result; }; const defaultFormData = { nickname: "", url: "", selectedPlatform: [], }; export default function EditPlace() { const router = useRouter(); const [formData, setFormData] = useState(defaultFormData); const [allPlatforms, setAllPlatforms] = useState([]); const [platform, setPlatform] = useState([]); const [currentPlat, setcurrentPlat] = useState(null); //正在提交状态 const [isSubmitting, setIsSubmitting] = useState(false); const [platformEditable, setPlatformEditable] = useState(false); useEffect(() => { getAllPlatforms(); }, []); const getAllPlatforms = async () => { try { const detailData = await requireAPI("POST", "/api/platform/list", {}); if (detailData.ret === -1) { Toast.show({ icon: "fail", content: detailData.msg, position: "top", }); return; } const detailDataValues = Object.entries(detailData.data); const temAllPlatforms = detailDataValues.map((item) => { return { label: item[1].name, value: { link_no: parseInt(item[0], 10), link_name: item[1].name, link_icon: item[1].icon, }, }; }); //获取主播当前所有平台 const streamerPlatformData = await requireAPI( "POST", "/api/streamer_link/list_by_mid", {}, true ); if (streamerPlatformData.ret === -1) { Toast.show({ icon: "error", content: streamerPlatformData.msg, position: "top", }); return; } const streamerPlatList = streamerPlatformData.data.list; setAllPlatforms(temAllPlatforms); const platformsWithIcon = streamerPlatList.map((item) => { return { ...item, link_icon: detailData.data[item.link_no].icon, }; }); setPlatform(platformsWithIcon); } catch (error) { console.error(error); } }; //点击删除按钮 const handleDelete = (index) => { const updatedPlatform = [...platform]; updatedPlatform.splice(index, 1); setPlatform(updatedPlatform); }; const handSubmit = async () => { const account = get("account"); const newPlatforms = [...platform]; newPlatforms.forEach((item, index) => { item.order = index + 1; item.mid = account.mid; }); try { //获取主播当前所有平台 const streamerPlatformData = await requireAPI( "POST", "/api/streamer_link/list_by_mid", {}, true ); if (streamerPlatformData.ret === -1) { Toast.show({ icon: "fail", content: streamerPlatformData.msg, position: "top", }); return; } //删除全部现有平台 const ids = streamerPlatformData.data.list.map((item) => item.id); if (ids.length !== 0) { const deleteData = await requireAPI( "POST", "/api/streamer_link/delete_batch", { body: { ids: ids, }, } ); if (deleteData.ret === -1) { Toast.show({ icon: "fail", content: deleteData.msg, position: "top", }); return; } } //批量创建新的平台 if (newPlatforms.length === 0) { Toast.show({ icon: "success", content: "更改成功", position: "top", }); return; } const data = await requireAPI("POST", "/api/streamer_link/create_batch", { body: { streamer_links: newPlatforms, }, }); if (data.ret === -1) { Toast.show({ icon: "fail", content: data.msg, position: "top", }); return; } Toast.show({ icon: "success", content: "更改成功", position: "top", }); router.back(); } catch (error) { console.error(error); } finally { setPlatformEditable(false); setFormData(defaultFormData); setIsSubmitting(false); } }; const handleEditPlatform = (values) => { if (!values.nickname || !values.selectedPlatform.length || !values.url) { Toast.show({ icon: "fail", content: "请完整填写信息", position: "top", }); return; } if ( values.url.indexOf("http://") == -1 && values.url.indexOf("https://") == -1 ) { Toast.show({ icon: "fail", content: "链接必须以https://或http://开头", position: "top", }); return; } let newPlatforms = []; if (!formData.id) { const currentPlatFormEle = allPlatforms.filter( (it) => it.label == values.selectedPlatform[0] )[0]; const newPlatform = { link_name: values.selectedPlatform[0], link_icon: currentPlatFormEle.value.link_icon, link_no: currentPlatFormEle.value.link_no, nickname: values.nickname, url: values.url, }; newPlatforms = platform.concat(newPlatform); // setIsSubmitting(true); } else { const index = platform.findIndex( (it) => it.link_name == formData.selectedPlatform ); newPlatforms = [...platform]; newPlatforms[index].link_name = formData.selectedPlatform; newPlatforms[index] = { ...newPlatforms[index], link_name: formData.selectedPlatform, nickname: formData.nickname, url: formData.url, }; } setFormData(defaultFormData); setPlatform(newPlatforms); setPlatformEditable(false); }; const onDragEnd = (result) => { if (!result.destination) return; const newList = reorder( platform, result.source.index, result.destination.index ); setPlatform([...newList]); }; return (
{/* 头部标题 */}
{ router.back(); }} />

编辑平台

{/* 内容 */}
setPlatformEditable(true)} > 添加
{(droppableProvided) => (
{platform.map((item, index) => ( {(provided, snapshot) => (
平台:
{item.link_name}

昵称: {item.nickname}

链接: {item.url}

handleDelete(index)}>
{ setPlatformEditable(true); setFormData({ nickname: item.nickname, url: item.url, selectedPlatform: item.link_name, id: item.id, }); }} >
)}
))} {droppableProvided.placeholder}
)}

编辑/添加平台

平台: {!formData.id ? (
{ return { label: it.label, value: it.label, }; }) .filter((it) => { return !platform.some( (el) => el.link_name == it.label ); }), ], ]} onSelect={(value) => { setFormData((old) => ({ ...old, selectedPlatform: value, })); }} value={formData.selectedPlatform} > {(items, { open }) => { return ( {items.every((item) => item === null) ? "未选择" : items .map((item) => item?.label ?? "未选择") .join("")} ); }}
) : ( {formData?.selectedPlatform} )}
昵称: { setFormData((old) => ({ ...old, nickname: value })); }} value={formData.nickname} className="border border-[#2c2b2f] font-medium rounded-lg h-10 px-2" />
链接: