tiefen_space_h5/app/my/createProfile/page.jsx

529 lines
17 KiB
React
Raw Normal View History

2024-10-22 17:24:02 +08:00
"use client";
import React, { useState, useRef, useMemo, Fragment } from "react";
2024-11-01 15:46:26 +08:00
import {
Switch,
Space,
Checkbox,
Button,
Toast,
TextArea,
Radio,
} from "antd-mobile";
2024-10-22 17:24:02 +08:00
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";
2024-10-24 21:28:49 +08:00
// getStreamerInfo
2024-10-22 17:24:02 +08:00
const superSingles = [
{ key: 0, text: "永久" },
2024-11-05 17:59:29 +08:00
{ key: 1, text: "按月生效" },
{ key: 2, text: "按季度生效" },
{ key: 3, text: "按半年生效" },
{ key: 4, text: "按年生效" },
2024-10-22 17:24:02 +08:00
];
2024-11-05 17:59:29 +08:00
// const superSingles = [
// { key: 0, text: "永久" },
// { key: 1, text: "按年生效" },
// { key: 2, text: "按半年生效" },
// { key: 3, text: "按季度生效" },
// { key: 4, text: "按月生效" },
// ];
2024-10-22 17:24:02 +08:00
const ListItemWithCheckbox = ({
superSingle,
formData,
setFormData,
superSingleCheckeds,
}) => {
return (
2024-11-05 19:43:11 +08:00
<li className="mt-4" onClick={() => {}}>
2024-10-22 17:24:02 +08:00
<div onClick={(e) => e.stopPropagation()}>
2024-11-01 15:46:26 +08:00
<Radio
value={superSingle.key}
onChange={(value) => {
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}
</Radio>
{/* <Checkbox
2024-10-22 17:24:02 +08:00
value={superSingle.key}
onChange={(value) => {
const newFormData = { ...formData };
newFormData.superSingle[superSingle.key].enable = value;
2024-11-01 15:46:26 +08:00
newFormData.superSingle[superSingle.key].price = 0;
newFormData.superSingle[superSingle.key].wechatFree = false;
2024-10-22 17:24:02 +08:00
setFormData(newFormData);
}}
>
{superSingle.text}
2024-11-01 15:46:26 +08:00
</Checkbox> */}
2024-10-22 17:24:02 +08:00
<div
className={`mt-2 px-4 h-12 py-3 rounded-[0.8rem] bg-[#FFFFFF1a] flex justify-between items-center ${
2024-11-01 15:46:26 +08:00
superSingleCheckeds != superSingle.key
2024-10-22 17:24:02 +08:00
? "mt-0 px-0 py-0 hidden"
: ""
}`}
2024-11-01 15:46:26 +08:00
// className={`mt-2 px-4 h-12 py-3 rounded-[0.8rem] bg-[#FFFFFF1a] flex justify-between items-center ${
// !superSingleCheckeds.includes(superSingle.key)
// ? "mt-0 px-0 py-0 hidden"
// : ""
// }`}
2024-10-22 17:24:02 +08:00
>
<div className="flex items-center">
<span className="mr-1 text-[#ffffffae] text-sm">¥</span>
<OwnInput
type="number"
value={formData.superSingle[superSingle.key].price}
onChange={(value) => {
const newFormData = { ...formData };
newFormData.superSingle[superSingle.key].price = value;
setFormData(newFormData);
}}
/>
</div>
<div className="flex items-center">
2024-10-23 13:52:52 +08:00
<span className="text-[#ffffff26] mr-2">|</span>
2024-11-05 17:53:13 +08:00
<Checkbox
2024-11-01 15:46:26 +08:00
block
checked={formData.superSingle[superSingle.key].wechatFree}
onChange={(value) => {
const newFormData = { ...formData };
newFormData.superSingle[superSingle.key].wechatFree = value;
setFormData(newFormData);
}}
>
<p className="text-sm whitespace-nowrap">赠送微信</p>
2024-11-05 17:53:13 +08:00
</Checkbox>
2024-11-01 15:46:26 +08:00
{/* <Checkbox
2024-10-22 17:24:02 +08:00
block
checked={formData.superSingle[superSingle.key].wechatFree}
onChange={(value) => {
const newFormData = { ...formData };
newFormData.superSingle[superSingle.key].wechatFree = value;
setFormData(newFormData);
}}
>
2024-10-23 13:52:52 +08:00
<p className="text-sm whitespace-nowrap">赠送微信</p>
2024-11-01 15:46:26 +08:00
</Checkbox> */}
2024-10-22 17:24:02 +08:00
</div>
</div>
</div>
</li>
);
};
export default function CreateProfile() {
const router = useRouter();
const searchParams = useSearchParams();
const [formData, setFormData] = useState({
2024-10-30 21:42:23 +08:00
spacePrice: "",
ironFanPrice: "",
2024-11-05 19:43:11 +08:00
openSuper: true,
2024-10-22 17:24:02 +08:00
superSingle: [
2024-11-05 20:07:14 +08:00
{ enable: true, price: 0, wechatFree: true },
{ enable: false, price: 0, wechatFree: true },
{ enable: false, price: 0, wechatFree: true },
{ enable: false, price: 0, wechatFree: true },
{ enable: false, price: 0, wechatFree: true },
2024-10-22 17:24:02 +08:00
],
spaceIntro: "",
});
const [spacePriceAble, setSpacePriceAble] = useState(false);
const [tiefenPriceAble, setTiefenPriceAble] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
2024-11-01 15:46:26 +08:00
// const [superSingleCheckeds, setSuperSingleCheckeds] = useState([]);
2024-11-05 20:07:14 +08:00
const [superSingleChecked, setSuperSingleChecked] = useState(0);
2024-10-22 17:24:02 +08:00
const listItemWithCheckboxMemo = useMemo(() => {
return superSingles.map((item) => (
<Fragment key={item.key}>
<ListItemWithCheckbox
superSingle={item}
formData={formData}
setFormData={setFormData}
2024-11-01 15:46:26 +08:00
superSingleCheckeds={superSingleChecked}
// superSingleCheckeds={superSingleCheckeds}
2024-10-22 17:24:02 +08:00
/>
</Fragment>
));
2024-11-01 15:46:26 +08:00
}, [formData, superSingleChecked]);
2024-10-24 21:28:49 +08:00
2024-10-22 17:24:02 +08:00
const handleSubmit = async () => {
const { spaceIntro, spacePrice, ironFanPrice, openSuper, superSingle } =
formData;
2024-10-30 21:42:23 +08:00
const openSuperEveryFalse = Object.values(superSingle).every(
(it) => !it.enable
);
if (
!spaceIntro ||
!spacePrice ||
!ironFanPrice ||
(openSuper && openSuperEveryFalse)
) {
2024-10-22 17:24:02 +08:00
Toast.show({
icon: "fail",
content: "请完善内容后提交",
position: "top",
});
return;
}
const _spacePrice = parseInt(spacePrice * 100, 10);
2024-10-31 18:16:47 +08:00
if (isNaN(_spacePrice) || _spacePrice < 0 || _spacePrice > 388800) {
2024-10-22 17:24:02 +08:00
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;
}
2024-10-25 19:19:41 +08:00
let isPrice = false;
2024-10-22 17:24:02 +08:00
if (openSuper) {
2024-11-01 15:46:26 +08:00
// 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;
2024-10-22 17:24:02 +08:00
if (!superFanPrice) {
Toast.show({
icon: "fail",
content: "请填写超粉价格",
position: "top",
});
2024-11-01 15:46:26 +08:00
isPrice = true;
return;
2024-10-22 17:24:02 +08:00
} else {
const _superFanPrice = parseInt(superFanPrice * 100, 10);
if (
openSuper &&
(isNaN(_superFanPrice) ||
_superFanPrice < 100 ||
_superFanPrice > 388800)
) {
2024-10-25 19:19:41 +08:00
isPrice = true;
2024-10-22 17:24:02 +08:00
Toast.show({
icon: "fail",
content: "请输入有效的超粉价格",
position: "top",
});
return;
}
if (openSuper && _superFanPrice <= _ironFanPrice) {
2024-10-25 19:19:41 +08:00
isPrice = true;
2024-10-22 17:24:02 +08:00
Toast.show({
icon: "fail",
content: "请输入大于铁粉价格的超粉价格",
position: "top",
});
return;
}
}
}
2024-11-01 15:46:26 +08:00
}
2024-11-04 14:55:23 +08:00
}
if (isPrice) return;
2024-11-05 17:53:13 +08:00
// 旧版本
const superfanList = superSingle.map((it, index) => ({
2024-11-04 14:55:23 +08:00
period: index,
enable: it.enable ? 1 : 0,
price: parseInt(it.price * 100, 10),
is_superfanship_give_wechat: it.wechatFree ? 1 : 0,
}));
2024-11-05 17:53:13 +08:00
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,
// }));
2024-11-04 14:55:23 +08:00
setIsSubmitting(true);
try {
const body = {
profile: spaceIntro,
admission_price: parseInt(spacePrice * 100, 10),
ironfanship_price: parseInt(ironFanPrice * 100, 10),
is_superfanship_enabled: openSuper ? 1 : 0,
2024-11-05 17:53:13 +08:00
// 旧版本
2024-11-05 20:11:10 +08:00
superfanship_price: superfanObj.price,
superfanship_valid_period: superfanObj.period,
is_superfanship_give_wechat: superfanObj.is_superfanship_give_wechat,
2024-11-05 17:53:13 +08:00
// 新版本
// superfan_price_list,
2024-11-04 14:55:23 +08:00
};
const _data = await requireAPI(
"POST",
"/api/zone/create",
{
body,
},
true
);
if (_data.ret === -1) {
2024-10-31 18:16:47 +08:00
Toast.show({
2024-11-04 14:55:23 +08:00
icon: "fail",
content: _data.msg,
2024-10-31 18:16:47 +08:00
position: "top",
});
2024-11-04 14:55:23 +08:00
return;
2024-10-31 18:16:47 +08:00
}
2024-11-04 14:55:23 +08:00
Toast.show({
icon: "success",
content: "开通空间成功!空间简介将在审核完成后生效。",
position: "top",
});
router.back();
} catch (error) {
console.error(error);
} finally {
setIsSubmitting(false);
2024-10-31 18:16:47 +08:00
}
2024-10-22 17:24:02 +08:00
};
return (
<div>
{/* 头部标题 */}
<div className="p-4 fixed top-0 z-10 w-full bg-black">
<div className="w-9 h-9 flex items-center justify-center bg-[#FFFFFF1A] rounded-full absolute">
<FontAwesomeIcon
icon={faAngleLeft}
style={{ maxWidth: "12px" }}
size="xl"
onClick={() => {
router.back();
}}
/>
</div>
2024-10-23 13:52:52 +08:00
<p className="text-base text-center leading-9 whitespace-nowrap">
2024-10-31 18:16:47 +08:00
开通空间
2024-10-23 13:52:52 +08:00
</p>
2024-10-22 17:24:02 +08:00
</div>
{/* 内容 */}
<div className="pt-16 p-4">
<div>
<div className="flex justify-between items-center">
<p className="text-sm">
2024-10-23 13:52:52 +08:00
<span className="whitespace-nowrap">空间介绍</span>
2024-10-22 17:24:02 +08:00
<span className="text-[#f00]">*</span>
</p>
<p className="text-[#ffffffae] text-xs"></p>
</div>
<div className="mt-2 px-4 py-3 rounded-[0.8rem] bg-[#FFFFFF1a] flex justify-between items-center">
<TextArea
style={{ "--font-size": `16px` }}
2024-11-05 19:16:48 +08:00
value={formData.spaceIntro}
2024-10-22 17:24:02 +08:00
placeholder="介绍下你的空间吧~"
onChange={(value) =>
setFormData((old) => ({ ...old, spaceIntro: value }))
}
/>
</div>
</div>
<div className="mt-6">
<div className="flex justify-between items-center">
2024-10-23 13:52:52 +08:00
<p className="text-sm whitespace-nowrap">
2024-10-22 17:24:02 +08:00
<span>解锁空间价格</span>
<span className="text-[#f00]">*</span>
</p>
<p className="text-[#ffffffae] text-xs">
成为空间成员可查看免费帖
</p>
</div>
<div className="mt-2 px-4 py-3 h-12 rounded-[0.8rem] bg-[#FFFFFF1a] flex justify-between items-center">
<div className="flex items-center">
<span className="mr-1 text-[#ffffffae] text-sm">¥</span>
2024-10-23 13:52:52 +08:00
<OwnInput
2024-10-25 19:19:41 +08:00
id="spacePrice"
2024-10-23 13:52:52 +08:00
type="number"
2024-10-31 18:16:47 +08:00
placeholder="0~3888仅支持整数"
2024-10-23 13:52:52 +08:00
value={formData.spacePrice}
onChange={(value) =>
setFormData((old) => ({ ...old, spacePrice: value }))
}
2024-10-30 21:42:23 +08:00
inputClassName="placeholder:text-[14px]"
className={!formData.spacePrice ? "pb-1.5" : ""}
2024-10-23 13:52:52 +08:00
/>
2024-10-22 17:24:02 +08:00
</div>
2024-10-25 19:19:41 +08:00
<label
htmlFor="spacePrice"
2024-10-23 13:52:52 +08:00
className="text-[#ffffffae] text-xs whitespace-nowrap"
2024-10-22 17:24:02 +08:00
onClick={() => setSpacePriceAble(true)}
>
点击编辑
2024-10-25 19:19:41 +08:00
</label>
2024-10-22 17:24:02 +08:00
</div>
</div>
<div className="mt-3">
<div className="flex justify-between items-center">
2024-10-23 13:52:52 +08:00
<p className="text-sm whitespace-nowrap">
2024-10-22 17:24:02 +08:00
<span>铁粉价格</span>
<span className="text-[#f00]">*</span>
</p>
<p className="text-[#ffffffae] text-xs">
2024-10-30 21:42:23 +08:00
累计消费达成后解锁铁粉权益
2024-10-22 17:24:02 +08:00
</p>
</div>
<div className="mt-2 px-4 py-3 h-12 rounded-[0.8rem] bg-[#FFFFFF1a] flex justify-between items-center">
<div className="flex items-center">
<span className="mr-1 text-[#ffffffae] text-sm">¥</span>
2024-10-23 13:52:52 +08:00
<OwnInput
2024-10-25 19:19:41 +08:00
id="ironPrice"
2024-10-23 13:52:52 +08:00
type="number"
2024-10-30 21:42:23 +08:00
placeholder="1~3888仅支持整数"
2024-10-23 13:52:52 +08:00
value={formData.ironFanPrice}
onChange={(value) =>
setFormData((old) => ({ ...old, ironFanPrice: value }))
}
2024-10-30 21:42:23 +08:00
inputClassName="placeholder:text-[14px]"
className={!formData.ironFanPrice ? "pb-1.5" : ""}
2024-10-23 13:52:52 +08:00
/>
2024-10-22 17:24:02 +08:00
</div>
2024-10-25 19:19:41 +08:00
<label
htmlFor="ironPrice"
2024-10-23 13:52:52 +08:00
className="text-[#ffffffae] text-xs whitespace-nowrap"
2024-10-22 17:24:02 +08:00
onClick={() => setTiefenPriceAble(true)}
>
点击编辑
2024-10-25 19:19:41 +08:00
</label>
2024-10-22 17:24:02 +08:00
</div>
</div>
<div className="mt-3">
<div className="flex justify-between items-center">
<p className="text-sm">
<span>超粉功能</span>
<span className="text-[#f00]">*</span>
</p>
<div className="flex items-center">
<p className="text-[#ffffffae] text-xs mr-2">是否启用</p>
<Switch
checked={formData.openSuper}
onChange={(value) => {
setFormData((old) => ({
...old,
openSuper: value,
}));
}}
style={{
"--checked-color": "#FF669E",
"--height": "24px",
"--width": "36px",
}}
/>
</div>
</div>
</div>
2024-10-30 21:42:23 +08:00
{formData.openSuper && (
<div className="mt-3">
<div className="flex justify-between items-center">
<p className="text-sm">
<span>超粉单次开通类型</span>
<span className="text-[#f00]">*</span>
</p>
<p className="text-[#ffffffae] text-xs">
付费后解锁对应期限超粉权益
</p>
</div>
<Space direction="vertical" block>
2024-11-01 15:46:26 +08:00
<Radio.Group
value={superSingleChecked}
// value={superSingleCheckeds}
onChange={(values) => {
setSuperSingleChecked(values);
// setSuperSingleCheckeds(values);
}}
>
<ul>{listItemWithCheckboxMemo}</ul>
</Radio.Group>
{/* <Checkbox.Group
value={superSingleCheckeds}
2024-10-30 21:42:23 +08:00
onChange={(values) => {
setSuperSingleCheckeds(values);
}}
>
<ul>{listItemWithCheckboxMemo}</ul>
2024-11-01 15:46:26 +08:00
</Checkbox.Group> */}
2024-10-30 21:42:23 +08:00
</Space>
2024-10-22 17:24:02 +08:00
</div>
2024-10-30 21:42:23 +08:00
)}
2024-10-22 17:24:02 +08:00
<div className="mt-10 flex justify-center">
<Button
size="middle"
shape="rounded"
style={{
"--background-color": "#FF669E",
paddingLeft: "32px",
paddingRight: "32px",
}}
onClick={handleSubmit}
>
2024-11-05 21:34:45 +08:00
{isSubmitting ? "正在保存..." : "开通空间"}
2024-10-22 17:24:02 +08:00
</Button>
</div>
</div>
</div>
);
}