import { useEffect, useRef, useState } from 'react' import { Input } from '@tarojs/components' import Taro from '@tarojs/taro' import { H5El } from './H5El' import { fetchSocialJoinStatus, paySocialJoin, } from '@/utils/socialJoinPay' import './JoinPaySheet.scss' const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ const EMAIL_KEY = 'userEmail' type Props = { open: boolean onClose: () => void onPaid?: () => void } /** * 加群收费底栏 — 微信虚拟支付(social-join),不是 VIP / community。 * rem + 全宽底栏,避免 designWidth:750 缩水。 */ export function JoinPaySheet({ open, onClose, onPaid }: Props) { const [email, setEmail] = useState('') const [busy, setBusy] = useState(false) const [err, setErr] = useState('') const [priceLabel, setPriceLabel] = useState('¥49') const [already, setAlready] = useState(false) const [checking, setChecking] = useState(true) const [enabled, setEnabled] = useState(false) const submitLock = useRef(false) useEffect(() => { if (!open) return try { setEmail(String(Taro.getStorageSync(EMAIL_KEY) || '')) } catch { setEmail('') } setErr('') let cancelled = false setChecking(true) setEnabled(false) setBusy(false) setAlready(false) void fetchSocialJoinStatus().then((s) => { if (cancelled) return setChecking(false) if (!s?.ok) { setErr('暂时无法确认价格与资格,请重新打开后重试'); return } setAlready(s.isMember) setEnabled(s.enabled) if (!s.enabled && !s.isMember) setErr('暂未开放加群支付') setPriceLabel(`¥${(s.priceFen / 100).toFixed(2).replace(/\.00$/, '')}`) }) return () => { cancelled = true } }, [open]) if (!open) return null const submit = async () => { if (submitLock.current || checking || !enabled) return if (already) { onClose() return } const value = email.trim().toLowerCase() if (!EMAIL_RE.test(value)) { setErr('请输入有效邮箱') return } setErr('') submitLock.current = true setBusy(true) try { Taro.setStorageSync(EMAIL_KEY, value) Taro.setStorageSync('userName', value.split('@')[0] || '游民') await paySocialJoin() setAlready(true) Taro.showToast({ title: '已加入游民社区', icon: 'success' }) onPaid?.() onClose() } catch (e) { const msg = String((e as Error)?.message || e || '') if (/取消|cancel/i.test(msg)) { setErr('') } else { setErr(msg || '支付唤起失败,请稍后重试') } } finally { submitLock.current = false setBusy(false) } } return ( !busy && onClose()}> { e?.stopPropagation?.() }} > ✨ 游民社区 · 加群 !busy && onClose()} > ✕ {already ? '你已在群里' : '加入游民社区'} {already ? '认识同频 · 开视频 · 找搭子,直接去逛就好。' : `认识同频的人 · 开视频聊聊 · 出来见一面 · 一起做点事`} {!already ? ( <> {priceLabel} 一次性加群 · 非 VIP 会员 邮箱 setEmail(e.detail.value)} /> {err ? ( {err} ) : null} void submit()} > {checking ? '正在确认…' : busy ? '正在唤起支付…' : `确认并支付 ${priceLabel}`} 🔒 邮箱用于账号识别 · 微信小程序虚拟支付 ) : ( 好的,继续逛 )} ) }