"use client"; import { useState, useEffect, useRef } from "react"; import { CloudDownload, Copy, LogIn, Loader2, Info, ExternalLink, } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { toast } from "sonner"; interface StatusResponse { status: "LoginSuccess" | "ScanSuccess" | "LoginFailed" | "QRCodeExpired" | "WaitLogin"; access_token: string; refresh_token: string; } export default function AlipanTvToken() { const [hasGenerated, setHasGenerated] = useState(false); const [authUrl, setAuthUrl] = useState(""); const [isLoading, setIsLoading] = useState(true); const [hasAccessToken, setHasAccessToken] = useState(false); const [hasRefreshToken, setHasRefreshToken] = useState(false); const [authorizing, setAuthorizing] = useState(false); const [isNoticeOpen, setIsNoticeOpen] = useState(false); const [accessToken, setAccessToken] = useState(""); const [refreshToken, setRefreshToken] = useState(""); const [currentSid, setCurrentSid] = useState(""); const [currentHost, setCurrentHost] = useState(""); const checkTimer = useRef(null); const getCurrentHost = () => { if (typeof window !== "undefined") { return `${window.location.protocol}//${window.location.host}`; } return ""; }; async function generateAuthUrl() { try { setIsLoading(true); const response = await fetch("/api/alipan-tv-token/generate_qr", { method: "POST", }); const data = await response.json(); setCurrentSid(data.sid); setAuthUrl(`https://www.alipan.com/o/oauth/authorize?sid=${data.sid}`); } catch { toast.error("初始化失败,请检查网络"); } finally { setIsLoading(false); } } function closeNotice() { setIsNoticeOpen(false); } async function checkStatus(sid: string) { try { const response = await fetch(`/api/alipan-tv-token/check_status/${sid}`); const data: StatusResponse = await response.json(); if (data.status === "LoginSuccess") { setAccessToken(data.access_token); setRefreshToken(data.refresh_token); setHasAccessToken(!!data.access_token); setHasRefreshToken(!!data.refresh_token); setAuthorizing(false); toast.success("登录成功"); } else if (data.status === "ScanSuccess") { checkTimer.current = setTimeout(() => checkStatus(sid), 2000); } else if (data.status === "LoginFailed") { setAuthorizing(false); toast.error("登录失败,请刷新页面重试"); } else if (data.status === "QRCodeExpired") { setAuthorizing(false); toast.error("链接过期,请刷新页面重试"); } else { // WaitLogin checkTimer.current = setTimeout(() => checkStatus(sid), 2000); } } catch (error) { console.error("检查状态时出错:", error); toast.error("发生错误,请稍后重试"); } } const copyToClipboard = async (text: string, name: string) => { try { await navigator.clipboard.writeText(text); toast.success(`${name} 已复制`); } catch { toast.error("复制失败"); } }; const handleAuth = (url: string) => { setAuthorizing(true); window.open(url, "_blank"); if (currentSid) { if (checkTimer.current) { clearTimeout(checkTimer.current); } checkTimer.current = setTimeout(() => checkStatus(currentSid), 1000); } }; useEffect(() => { setCurrentHost(getCurrentHost()); setIsNoticeOpen(true); if (!hasGenerated) { generateAuthUrl(); setHasGenerated(true); } return () => { if (checkTimer.current) { clearTimeout(checkTimer.current); } }; }, [hasGenerated]); return (
{/* Page Header */}

阿里云盘TV Token

获取阿里云盘TV端的授权令牌,解锁高速下载

{/* Left: Tokens */}
访问令牌