2026-05-18 20:10:08 +08:00
|
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
|
|
import { useState } from "react";
|
2026-05-20 20:56:21 +08:00
|
|
|
|
import { ScrollText, Coins, Minus } from "lucide-react";
|
2026-05-18 20:10:08 +08:00
|
|
|
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
2026-05-20 21:08:54 +08:00
|
|
|
|
import {
|
|
|
|
|
|
getSupabaseBrowserClient,
|
|
|
|
|
|
hasSupabasePublicEnv,
|
|
|
|
|
|
} from "@/lib/supabase/client";
|
|
|
|
|
|
|
|
|
|
|
|
async function buildOpsAuthHeaders() {
|
|
|
|
|
|
const headers: Record<string, string> = {
|
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
|
};
|
|
|
|
|
|
if (!hasSupabasePublicEnv()) return headers;
|
|
|
|
|
|
try {
|
2026-05-20 21:18:59 +08:00
|
|
|
|
const supabase = getSupabaseBrowserClient();
|
|
|
|
|
|
let {
|
2026-05-20 21:08:54 +08:00
|
|
|
|
data: { session },
|
2026-05-20 21:18:59 +08:00
|
|
|
|
} = await supabase.auth.getSession();
|
|
|
|
|
|
const expiresAtMs = Number(session?.expires_at || 0) * 1000;
|
|
|
|
|
|
if (session && expiresAtMs > 0 && expiresAtMs - Date.now() < 60_000) {
|
|
|
|
|
|
const refreshed = await supabase.auth.refreshSession();
|
|
|
|
|
|
session = refreshed.data.session || session;
|
|
|
|
|
|
}
|
2026-05-20 21:08:54 +08:00
|
|
|
|
const token = String(session?.access_token || "").trim();
|
|
|
|
|
|
if (token) headers.Authorization = `Bearer ${token}`;
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// Route cookies may still authenticate the request.
|
|
|
|
|
|
}
|
|
|
|
|
|
return headers;
|
|
|
|
|
|
}
|
2026-05-18 20:10:08 +08:00
|
|
|
|
|
|
|
|
|
|
export function SubscriptionsPageClient() {
|
|
|
|
|
|
const [email, setEmail] = useState("");
|
|
|
|
|
|
const [planCode, setPlanCode] = useState("pro_monthly");
|
|
|
|
|
|
const [days, setDays] = useState(30);
|
2026-05-20 20:56:21 +08:00
|
|
|
|
const [deductPoints, setDeductPoints] = useState(0);
|
2026-05-18 20:10:08 +08:00
|
|
|
|
const [extendDays, setExtendDays] = useState(30);
|
|
|
|
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
|
|
const [result, setResult] = useState("");
|
|
|
|
|
|
|
|
|
|
|
|
const handleGrant = async () => {
|
|
|
|
|
|
if (!email.trim()) return;
|
|
|
|
|
|
setBusy(true);
|
|
|
|
|
|
setResult("");
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await fetch("/api/ops/subscriptions/grant", {
|
|
|
|
|
|
method: "POST",
|
2026-05-20 21:08:54 +08:00
|
|
|
|
headers: await buildOpsAuthHeaders(),
|
2026-05-20 20:56:21 +08:00
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
|
email: email.trim(),
|
|
|
|
|
|
plan_code: planCode,
|
|
|
|
|
|
days,
|
|
|
|
|
|
deduct_points: deductPoints,
|
|
|
|
|
|
}),
|
2026-05-18 20:10:08 +08:00
|
|
|
|
});
|
2026-05-20 20:56:21 +08:00
|
|
|
|
const text = await res.text();
|
2026-05-18 20:10:08 +08:00
|
|
|
|
if (res.ok) {
|
2026-05-20 20:56:21 +08:00
|
|
|
|
let msg = `已为 ${email} 开通 ${planCode},${days} 天`;
|
|
|
|
|
|
if (deductPoints > 0) msg += `,扣除 ${deductPoints} 积分`;
|
|
|
|
|
|
try {
|
|
|
|
|
|
const data = JSON.parse(text);
|
|
|
|
|
|
if (data.points_result && !data.points_result.ok) {
|
|
|
|
|
|
msg += ` (⚠ 扣分失败: ${data.points_result.reason})`;
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch {}
|
|
|
|
|
|
setResult(msg);
|
2026-05-18 20:10:08 +08:00
|
|
|
|
} else {
|
2026-05-20 20:56:21 +08:00
|
|
|
|
setResult(`失败: ${text.slice(0, 200)}`);
|
2026-05-18 20:10:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
setResult(`错误: ${String(e).slice(0, 100)}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
setBusy(false);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleExtend = async () => {
|
|
|
|
|
|
if (!email.trim()) return;
|
|
|
|
|
|
setBusy(true);
|
|
|
|
|
|
setResult("");
|
|
|
|
|
|
try {
|
|
|
|
|
|
const res = await fetch("/api/ops/subscriptions/extend", {
|
|
|
|
|
|
method: "POST",
|
2026-05-20 21:08:54 +08:00
|
|
|
|
headers: await buildOpsAuthHeaders(),
|
2026-05-18 20:10:08 +08:00
|
|
|
|
body: JSON.stringify({ email: email.trim(), additional_days: extendDays }),
|
|
|
|
|
|
});
|
|
|
|
|
|
if (res.ok) {
|
|
|
|
|
|
setResult(`已为 ${email} 延期 ${extendDays} 天`);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setResult(`失败: ${await res.text().catch(() => "")}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
setResult(`错误: ${String(e).slice(0, 100)}`);
|
|
|
|
|
|
}
|
|
|
|
|
|
setBusy(false);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="space-y-6">
|
|
|
|
|
|
<h1 className="text-2xl font-bold text-white">订阅操作</h1>
|
2026-05-20 20:56:21 +08:00
|
|
|
|
<p className="text-sm text-slate-500">手动为用户开通或延期订阅</p>
|
2026-05-18 20:10:08 +08:00
|
|
|
|
|
|
|
|
|
|
<Card>
|
|
|
|
|
|
<CardHeader><CardTitle>手动开通</CardTitle></CardHeader>
|
2026-05-20 20:56:21 +08:00
|
|
|
|
<CardContent className="space-y-3">
|
2026-05-18 20:10:08 +08:00
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
|
|
|
|
<input
|
|
|
|
|
|
value={email}
|
|
|
|
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
|
|
|
|
placeholder="用户 Supabase 邮箱"
|
|
|
|
|
|
className="flex-1 min-w-[200px] rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white placeholder:text-slate-600 outline-none focus:border-cyan-400/50"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<select
|
|
|
|
|
|
value={planCode}
|
|
|
|
|
|
onChange={(e) => setPlanCode(e.target.value)}
|
|
|
|
|
|
className="rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white outline-none"
|
|
|
|
|
|
>
|
|
|
|
|
|
<option value="pro_monthly">Pro 月付</option>
|
|
|
|
|
|
</select>
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
value={days}
|
|
|
|
|
|
onChange={(e) => setDays(Math.max(1, Math.min(365, Number(e.target.value) || 30)))}
|
|
|
|
|
|
className="w-20 rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white outline-none focus:border-cyan-400/50"
|
|
|
|
|
|
/>
|
2026-05-20 20:56:21 +08:00
|
|
|
|
<span className="text-slate-400 text-sm self-center">天</span>
|
2026-05-18 20:10:08 +08:00
|
|
|
|
<Button onClick={handleGrant} disabled={busy} size="sm" className="gap-1.5">
|
|
|
|
|
|
<Coins className="h-3.5 w-3.5" /> 开通
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
2026-05-20 20:56:21 +08:00
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
|
<Minus className="h-3.5 w-3.5 text-slate-500" />
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
value={deductPoints}
|
|
|
|
|
|
onChange={(e) => setDeductPoints(Math.max(0, Number(e.target.value) || 0))}
|
|
|
|
|
|
placeholder="0"
|
|
|
|
|
|
className="w-24 rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white placeholder:text-slate-600 outline-none focus:border-cyan-400/50"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<span className="text-slate-400 text-xs">扣除积分(0=不扣)</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{result && (
|
|
|
|
|
|
<p className={`text-sm ${result.startsWith("失败") || result.startsWith("错误") || result.includes("⚠") ? "text-amber-400" : "text-emerald-400"}`}>
|
|
|
|
|
|
{result}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
)}
|
2026-05-18 20:10:08 +08:00
|
|
|
|
</CardContent>
|
|
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
|
|
|
|
<Card>
|
|
|
|
|
|
<CardHeader><CardTitle>手动延期</CardTitle></CardHeader>
|
|
|
|
|
|
<CardContent>
|
|
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
|
|
|
|
<input
|
|
|
|
|
|
value={email}
|
|
|
|
|
|
onChange={(e) => setEmail(e.target.value)}
|
|
|
|
|
|
placeholder="用户 Supabase 邮箱"
|
|
|
|
|
|
className="flex-1 min-w-[200px] rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white placeholder:text-slate-600 outline-none focus:border-cyan-400/50"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="number"
|
|
|
|
|
|
value={extendDays}
|
|
|
|
|
|
onChange={(e) => setExtendDays(Math.max(1, Math.min(365, Number(e.target.value) || 30)))}
|
|
|
|
|
|
className="w-20 rounded-lg border border-white/10 bg-black/30 px-3 py-2 text-sm text-white outline-none focus:border-cyan-400/50"
|
|
|
|
|
|
/>
|
|
|
|
|
|
<span className="text-slate-400 text-sm self-center">天</span>
|
|
|
|
|
|
<Button onClick={handleExtend} disabled={busy} size="sm" className="gap-1.5">
|
|
|
|
|
|
<ScrollText className="h-3.5 w-3.5" /> 延期
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</CardContent>
|
|
|
|
|
|
</Card>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|