"use client";
import React, { useState, useEffect, useRef, useMemo, Fragment } from "react";
import { Switch, Space, Checkbox, Button, Toast, Radio } from "antd-mobile";
import { useRouter, useSearchParams } from "next/navigation";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleLeft } from "@fortawesome/free-solid-svg-icons";
import OwnInput from "@/components/OwnInput";
import requireAPI from "@/utils/requireAPI";
import { get } from "@/utils/storeInfo";
import { getStreamerInfo } from "@/api/space";
const superSingles = [
{ key: 0, text: "永久" },
{ key: 1, text: "按月生效" },
{ key: 2, text: "按季度生效" },
{ key: 3, text: "按半年生效" },
{ key: 4, text: "按年生效" },
];
// const superSingles = [
// { key: 0, text: "永久" },
// { key: 1, text: "按年生效" },
// { key: 2, text: "按半年生效" },
// { key: 3, text: "按季度生效" },
// { key: 4, text: "按月生效" },
// ];
const ListItemWithCheckbox = ({
superSingle,
formData,
setFormData,
superSingleCheckeds,
}) => {
const checkboxRef = useRef(null);
return (
{
checkboxRef.current?.toggle();
}}
>
e.stopPropagation()}>
{
const newFormData = { ...formData };
newFormData.superSingle.forEach((el) => {
el.enable = 0;
});
newFormData.superSingle[superSingle.key].enable = value;
// newFormData.superSingle[superSingle.key].price = 0;
// newFormData.superSingle[superSingle.key].wechatFree = false;
setFormData(newFormData);
}}
>
{superSingle.text}
{/*
{
const newFormData = { ...formData };
newFormData.superSingle[superSingle.key].enable = value;
newFormData.superSingle[superSingle.key].price = 0;
newFormData.superSingle[superSingle.key].wechatFree = false;
setFormData(newFormData);
}}
>
{superSingle.text}
*/}
¥
{
const newFormData = { ...formData };
newFormData.superSingle[superSingle.key].price = value;
setFormData(newFormData);
}}
/>
|
{
const newFormData = { ...formData };
newFormData.superSingle[superSingle.key].wechatFree = value;
setFormData(newFormData);
}}
>
赠送微信
{/*
{
const newFormData = { ...formData };
newFormData.superSingle[superSingle.key].wechatFree = value;
setFormData(newFormData);
}}
>
赠送微信
*/}
);
};
export default function spacePaymentSetting() {
const router = useRouter();
const searchParams = useSearchParams();
const [formData, setFormData] = useState({
spacePrice: null,
ironFanPrice: null,
openSuper: false,
superSingle: [
{ enable: false, price: 0, wechatFree: false },
{ enable: false, price: 0, wechatFree: false },
{ enable: false, price: 0, wechatFree: false },
{ enable: false, price: 0, wechatFree: false },
{ enable: false, price: 0, wechatFree: false },
],
});
const [spacePriceAble, setSpacePriceAble] = useState(false);
const [tiefenPriceAble, setTiefenPriceAble] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
// const [superSingleCheckeds, setSuperSingleCheckeds] = useState([]);
const [superSingleChecked, setSuperSingleChecked] = useState(null);
useEffect(() => {
const account = get("account");
getStreamerInfo(Number(account.mid)).then((res) => {
// 旧版本
const haveChecked = res.is_superfanship_enabled;
if (haveChecked) {
setSuperSingleChecked(res.superfanship_valid_period);
}
const newFormData = {
spacePrice: res.admission_price / 100,
ironFanPrice: res.ironfanship_price / 100,
openSuper: !!res.is_superfanship_enabled,
superSingle: formData.superSingle.map((it, index) => {
if (index == res.superfanship_valid_period) {
return {
enable: !!res.is_superfanship_enabled,
price: res.superfanship_price / 100,
wechatFree: !!res.is_superfanship_give_wechat,
};
} else {
return it;
}
}),
spaceIntro: res.profile,
};
setFormData(newFormData);
// 新版本
// const haveChecked = res.superfan_price_list
// .map((it, index) => ({ ...it, index }))
// .filter((it) => it.enable);
// if (haveChecked.length > 0) {
// setSuperSingleChecked(haveChecked[0].period);
// }
// setSuperSingleCheckeds(
// res.superfan_price_list
// .map((it, index) => ({ ...it, index }))
// .filter((it) => it.enable)
// .map((it) => superSingles[it.index].key)
// );
// setFormData({
// spacePrice: res.admission_price / 100,
// ironFanPrice: res.ironfanship_price / 100,
// openSuper: !!res.is_superfanship_enabled,
// superSingle: res.superfan_price_list.map((it) => ({
// enable: !!it.enable,
// price: it.price / 100,
// wechatFree: !!it.is_superfanship_give_wechat,
// })),
// spaceIntro: res.profile,
// });
});
}, []);
const listItemWithCheckboxMemo = useMemo(() => {
return superSingles.map((item, index) => (
));
}, [formData, superSingleChecked]);
const handleSubmit = async () => {
const { spacePrice, ironFanPrice, openSuper, superSingle } = formData;
const openSuperEveryFalse = Object.values(superSingle).every(
(it) => !it.enable
);
if (!spacePrice || !ironFanPrice || (openSuper && openSuperEveryFalse)) {
Toast.show({
icon: "fail",
content: "请完善内容后提交",
position: "top",
});
return;
}
const _spacePrice = parseInt(spacePrice * 100, 10);
if (isNaN(_spacePrice) || _spacePrice < 0 || _spacePrice > 388800) {
Toast.show({
icon: "fail",
content: "请输入有效的解锁空间价格",
position: "top",
});
return;
}
const _ironFanPrice = parseInt(ironFanPrice * 100, 10);
if (isNaN(_ironFanPrice) || _ironFanPrice < 100 || _ironFanPrice > 388800) {
Toast.show({
icon: "fail",
content: "请输入有效的铁粉价格",
position: "top",
});
return;
}
let isPrice = false;
if (openSuper) {
// Object.values(superSingle).forEach((it) => {
// if (it.enable) {
// const superFanPrice = it.price;
// if (!superFanPrice) {
// Toast.show({
// icon: "fail",
// content: "请填写超粉价格",
// position: "top",
// });
// isPrice = true;
// return;
// } else {
// const _superFanPrice = parseInt(superFanPrice * 100, 10);
// if (
// openSuper &&
// (isNaN(_superFanPrice) ||
// _superFanPrice < 100 ||
// _superFanPrice > 388800)
// ) {
// isPrice = true;
// Toast.show({
// icon: "fail",
// content: "请输入有效的超粉价格",
// position: "top",
// });
// return;
// }
// if (openSuper && _superFanPrice <= _ironFanPrice) {
// isPrice = true;
// Toast.show({
// icon: "fail",
// content: "请输入大于铁粉价格的超粉价格",
// position: "top",
// });
// return;
// }
// }
// }
// });
if (superSingleChecked == null) {
Toast.show({
icon: "fail",
content: "请选择铁粉类型",
position: "top",
});
} else {
const superChecked = superSingle.filter(
(_, index) => index == superSingleChecked
)[0];
if (superChecked) {
const superFanPrice = superChecked.price;
if (!superFanPrice) {
Toast.show({
icon: "fail",
content: "请填写超粉价格",
position: "top",
});
isPrice = true;
return;
} else {
const _superFanPrice = parseInt(superFanPrice * 100, 10);
if (
openSuper &&
(isNaN(_superFanPrice) ||
_superFanPrice < 100 ||
_superFanPrice > 388800)
) {
isPrice = true;
Toast.show({
icon: "fail",
content: "请输入有效的超粉价格",
position: "top",
});
return;
}
if (openSuper && _superFanPrice <= _ironFanPrice) {
isPrice = true;
Toast.show({
icon: "fail",
content: "请输入大于铁粉价格的超粉价格",
position: "top",
});
return;
}
}
}
}
}
if (isPrice) return;
// if (
// openSuper &&
// superFanExpiration !== 0 &&
// superFanExpiration !== 1 &&
// superFanExpiration !== 2 &&
// superFanExpiration !== 3 &&
// superFanExpiration !== 4
// ) {
// Toast.show({
// icon: "error",
// content: "请选择超粉有效期",
// position: "top",
// });
// return;
// }
if (isSubmitting) return;
// 旧版本
const superfanList = superSingle.map((it, index) => ({
period: index,
enable: it.enable ? 1 : 0,
price: parseInt(it.price * 100, 10),
is_superfanship_give_wechat: it.wechatFree ? 1 : 0,
}));
const superfanObj = superfanList.filter((it) => it.enable)[0];
// 新版本
// const superfan_price_list = superSingle.map((it, index) => ({
// period: index,
// enable: it.enable ? 1 : 0,
// price: parseInt(it.price * 100, 10),
// is_superfanship_give_wechat: it.wechatFree ? 1 : 0,
// }));
setIsSubmitting(true);
try {
const body = {
id: parseInt(searchParams.get("zid"), 10),
admission_price: parseInt(spacePrice * 100, 10),
ironfanship_price: parseInt(ironFanPrice * 100, 10),
is_superfanship_enabled: openSuper ? 1 : 0,
// 旧版本
superfanship_price: openSuper ? superfanObj.price : 0,
superfanship_valid_period: openSuper ? superfanObj.period : null,
is_superfanship_give_wechat: openSuper
? superfanObj.is_superfanship_give_wechat
: 0,
// 新版本
// superfan_price_list,
};
// console.log("body", body);
const _data = await requireAPI(
"POST",
"/api/zone/update",
{
body,
},
true
);
if (_data.ret === -1) {
Toast.show({
icon: "error",
content: _data.msg,
position: "top",
});
return;
}
Toast.show({
icon: "success",
content: "修改成功,请重进空间刷新查看",
position: "top",
});
router.back();
} catch (error) {
console.error(error);
} finally {
setIsSubmitting(false);
}
};
return (
{/* 头部标题 */}
{
router.back();
}}
/>
空间付费设置
{/* 内容 */}
¥
{/* {!spacePriceAble ? (
{formData.spacePrice}
) : (
setFormData((old) => ({ ...old, spacePrice: value }))
}
/>
)} */}
setFormData((old) => ({ ...old, spacePrice: value }))
}
className={!formData.spacePrice ? "pb-1.5" : ""}
inputClassName="placeholder:text-[14px]"
/>
¥
{/* {!tiefenPriceAble ? (
{formData.ironFanPrice}
) : (
)} */}
setFormData((old) => ({ ...old, ironFanPrice: value }))
}
className={!formData.ironFanPrice ? "pb-1.5" : ""}
inputClassName="placeholder:text-[14px]"
/>
超粉功能
*
是否启用
{
setFormData((old) => ({
...old,
openSuper: value,
}));
}}
style={{
"--checked-color": "#FF669E",
"--height": "24px",
"--width": "36px",
}}
/>
{formData.openSuper && (
超粉单次开通类型
*
(付费后解锁对应期限超粉权益)
{
setSuperSingleChecked(values);
// setSuperSingleCheckeds(values);
}}
>
{listItemWithCheckboxMemo}
{/* {
setSuperSingleCheckeds(values);
}}
>
{listItemWithCheckboxMemo}
*/}
)}
);
}