Rebuild admin.nomadro.com around community ops, add configurable free peeks instead of nav locks, humanize user copy, and harden UGC/liveness admin APIs with backup/self-test scripts. Co-authored-by: Cursor <cursoragent@cursor.com>
175 lines
5.3 KiB
TypeScript
175 lines
5.3 KiB
TypeScript
"use client";
|
||
|
||
import { useEffect, useId, useRef, useState } from "react";
|
||
import { ArrowUpRight, LockKeyhole, Sparkles, X } from "lucide-react";
|
||
import { archiveJoinLead } from "@social/lib/joinArchive";
|
||
import { markJoinPayIntent } from "@social/lib/joinFlow";
|
||
import { loginWithEmail } from "@social/lib/socialSession";
|
||
import { useSocialConfig } from "./SocialExperience";
|
||
import { socialTx } from "@social/lib/socialTx";
|
||
|
||
type Props = {
|
||
open: boolean;
|
||
onClose: () => void;
|
||
};
|
||
|
||
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||
|
||
/**
|
||
* 加群:填邮箱 → 调起门户统一支付
|
||
* PC/H5 → zpay;微信内 H5 → xpay(xorpay);小程序 WebView 提示走虚拟支付
|
||
*/
|
||
export default function JoinPayModal({ open, onClose }: Props) {
|
||
return open ? <JoinPaySheet onClose={onClose} /> : null;
|
||
}
|
||
|
||
function JoinPaySheet({ onClose }: Pick<Props, "onClose">) {
|
||
const titleId = useId();
|
||
const descriptionId = useId();
|
||
const errorId = useId();
|
||
const dialogRef = useRef<HTMLDialogElement>(null);
|
||
const inputRef = useRef<HTMLInputElement>(null);
|
||
const config = useSocialConfig();
|
||
const rawFen = Number(config.copy?.vipPriceFen);
|
||
const priceFen =
|
||
Number.isFinite(rawFen) && rawFen >= 0 ? Math.round(rawFen) : 10;
|
||
const [email, setEmail] = useState(() => {
|
||
try {
|
||
return localStorage.getItem("userEmail") || "";
|
||
} catch {
|
||
return "";
|
||
}
|
||
});
|
||
const [busy, setBusy] = useState(false);
|
||
const [err, setErr] = useState("");
|
||
|
||
useEffect(() => {
|
||
const dialog = dialogRef.current;
|
||
const opener = document.activeElement as HTMLElement | null;
|
||
const previousOverflow = document.body.style.overflow;
|
||
dialog?.showModal();
|
||
inputRef.current?.focus();
|
||
document.body.style.overflow = "hidden";
|
||
return () => {
|
||
dialog?.close();
|
||
document.body.style.overflow = previousOverflow;
|
||
opener?.focus();
|
||
};
|
||
}, []);
|
||
|
||
const submit = async () => {
|
||
if (busy) return;
|
||
const value = email.trim().toLowerCase();
|
||
if (!EMAIL_RE.test(value)) {
|
||
setErr("请输入有效邮箱");
|
||
return;
|
||
}
|
||
setErr("");
|
||
setBusy(true);
|
||
try {
|
||
loginWithEmail(value);
|
||
markJoinPayIntent();
|
||
// Must await before pay redirect — otherwise fetch is aborted and admin sees no lead
|
||
await archiveJoinLead("email_submit", {
|
||
priceFen,
|
||
intent: "pay",
|
||
});
|
||
|
||
const inMiniProgram =
|
||
/miniProgram/i.test(navigator.userAgent) ||
|
||
(window as unknown as { __wxjs_environment?: string })
|
||
.__wxjs_environment === "miniprogram";
|
||
|
||
if (inMiniProgram) {
|
||
window.location.href = "/vip";
|
||
return;
|
||
}
|
||
|
||
const { triggerPay } = await import("@vip/app/lib/triggerPay");
|
||
const returnUrl = `${window.location.origin}/paid?join=1`;
|
||
await triggerPay(undefined, { returnUrl });
|
||
void archiveJoinLead("pay_launched", { priceFen });
|
||
onClose();
|
||
} catch (e) {
|
||
setErr(e instanceof Error ? e.message : "支付唤起失败,请稍后重试");
|
||
} finally {
|
||
setBusy(false);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<dialog
|
||
ref={dialogRef}
|
||
className="social-join-overlay social-join-dialog"
|
||
aria-labelledby={titleId}
|
||
aria-describedby={descriptionId}
|
||
onCancel={(event) => {
|
||
event.preventDefault();
|
||
if (!busy) onClose();
|
||
}}
|
||
onClick={(e) => {
|
||
if (e.target === e.currentTarget && !busy) onClose();
|
||
}}
|
||
>
|
||
<div className="social-join-sheet">
|
||
<header className="social-join-sheet-head">
|
||
<span className="social-join-brand">
|
||
<Sparkles size={17} /> {socialTx("游民社区")}
|
||
</span>
|
||
<button
|
||
type="button"
|
||
className="social-join-close"
|
||
aria-label={socialTx("关闭")}
|
||
disabled={busy}
|
||
onClick={onClose}
|
||
>
|
||
<X size={18} />
|
||
</button>
|
||
</header>
|
||
<h2 id={titleId} className="social-join-title">
|
||
{socialTx("加入游民社区")}
|
||
</h2>
|
||
<p id={descriptionId} className="social-muted">
|
||
{socialTx("填写邮箱后继续")}
|
||
</p>
|
||
|
||
<label className="social-join-field">
|
||
{socialTx("邮箱")}
|
||
<input
|
||
ref={inputRef}
|
||
type="email"
|
||
inputMode="email"
|
||
autoComplete="email"
|
||
placeholder={socialTx("你的邮箱")}
|
||
value={email}
|
||
aria-invalid={Boolean(err)}
|
||
aria-describedby={err ? errorId : undefined}
|
||
disabled={busy}
|
||
onChange={(e) => setEmail(e.target.value)}
|
||
onKeyDown={(e) => {
|
||
if (e.key === "Enter" && !e.nativeEvent.isComposing) void submit();
|
||
}}
|
||
/>
|
||
</label>
|
||
{err ? (
|
||
<p id={errorId} className="social-join-err" role="alert">
|
||
{err}
|
||
</p>
|
||
) : null}
|
||
<button
|
||
type="button"
|
||
className="btn btn-primary social-join-submit"
|
||
disabled={busy}
|
||
onClick={() => void submit()}
|
||
>
|
||
{busy ? socialTx("正在前往支付…") : socialTx("确认")}
|
||
<ArrowUpRight size={17} />
|
||
</button>
|
||
<p className="social-join-privacy">
|
||
<LockKeyhole size={11} /> {socialTx("请填写常用邮箱,方便找回账号")}
|
||
</p>
|
||
</div>
|
||
</dialog>
|
||
);
|
||
}
|