feat: 计费功能

master
xjs 2025-06-06 15:18:41 +08:00
parent d774630aea
commit 48ed954da0
11 changed files with 311 additions and 34 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
public/icons/receive.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

View File

@ -5,45 +5,45 @@ import AntechamberScore from "@/components/AntechamberScore";
import { useContext, useEffect, useState } from "react"; import { useContext, useEffect, useState } from "react";
import { RealtimeClientContext } from "@/components/Provider/RealtimeClientProvider"; import { RealtimeClientContext } from "@/components/Provider/RealtimeClientProvider";
import { useSearchParams } from "react-router-dom"; import { useSearchParams } from "react-router-dom";
import { fetchUserToken } from "@/apis/user"; import { fetchUserToken } from "@/apis/user";
import { useToast } from "@/hooks/use-toast"; import { useToast } from "@/hooks/use-toast";
import { useAbortController } from "@/hooks/useAbortController"; import { useAbortController } from "@/hooks/useAbortController";
import AntechamberFile from "@/components/AntechamberFile"; import AntechamberFile from "@/components/AntechamberFile";
import AntechamberReport from "@/components/AntechamberReport"; import AntechamberReport from "@/components/AntechamberReport";
import AntechamberWishList from "@/components/AntechamberWishList"; import AntechamberWishList from "@/components/AntechamberWishList";
import { ReportContext } from "@/components/Provider/ReportResolveProvider"; import { ReportContext } from "@/components/Provider/ReportResolveProvider";
import ReceiveTime from "/icons/receive-time.png";
import { ReceiveDialog } from "@/components/ReceiveDialog";
export default function Antechamber() { export default function Antechamber() {
const { handleConnect } = useContext(RealtimeClientContext);
const { handleConnect} = useContext(RealtimeClientContext);
const { hasHandledReport } = useContext(ReportContext); const { hasHandledReport } = useContext(ReportContext);
const [searchParams] = useSearchParams(); const [searchParams] = useSearchParams();
const [disable,setDisable] = useState(true); const [disable, setDisable] = useState(true);
const [isLoading,setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const token = searchParams.get("token") || "";
const token = searchParams.get("token") || '';
const { toast } = useToast(); const { toast } = useToast();
const { getSignal } = useAbortController(); const { getSignal } = useAbortController();
const getUserToken = async () => { const getUserToken = async () => {
try { try {
const { result, message } = await fetchUserToken({ const { result, message } = await fetchUserToken({
options: { options: {
signal: getSignal(), signal: getSignal(),
headers: { headers: {
"Authorization": `Bearer ${encodeURIComponent(token)}` Authorization: `Bearer ${encodeURIComponent(token)}`,
} },
} },
}); });
if (message) { if (message) {
console.log(message); console.log(message);
} else { } else {
const _result = result as {isExpired:boolean;msg:string}; const _result = result as { isExpired: boolean; msg: string };
setDisable(!_result.isExpired); setDisable(!_result.isExpired);
if(!_result.isExpired &&_result.msg){ if (!_result.isExpired && _result.msg) {
toast({ toast({
title: _result.msg, title: _result.msg,
description: "请重新登录", description: "请重新登录",
@ -51,42 +51,60 @@ export default function Antechamber() {
} }
} }
} catch (error: any) { } catch (error: any) {
if (error.name !== 'AbortError') { if (error.name !== "AbortError") {
console.error('获取用户令牌失败:', error); console.error("获取用户令牌失败:", error);
} }
} }
}; };
useEffect(() => { useEffect(() => {
getUserToken(); getUserToken();
}, [token]); }, [token]);
const toRoom = (params: { initMessage?: string; fileUrl?: string }) => {
const toRoom = (params:{initMessage?:string,fileUrl?:string}) => { if (!hasHandledReport && (disable || isLoading)) {
if(!hasHandledReport && (disable || isLoading)){
return; return;
} }
setIsLoading(true) setIsLoading(true);
handleConnect(params).then(() => { handleConnect(params).then(() => {
setIsLoading(false); setIsLoading(false);
}); });
}; };
const [isReceiveDialogOpen, setIsReceiveDialogOpen] = useState(true);
return ( return (
<div className="flex flex-col items-center h-full overflow-y-auto relative"> <div className="flex flex-col items-center h-full overflow-y-auto relative">
<AntechamberHeader /> <AntechamberHeader />
<AntechamberScore /> <AntechamberScore />
<AntechamberWishList handleLoading={setIsLoading}/> <AntechamberWishList handleLoading={setIsLoading} />
<AntechamberFile handleLoading={setIsLoading} /> <AntechamberFile handleLoading={setIsLoading} />
<AntechamberReport handleLoading={setIsLoading} /> <AntechamberReport handleLoading={setIsLoading} />
<InvokeButton disable={disable} onClick={() => toRoom({})} /> <InvokeButton disable={disable} onClick={() => toRoom({})} />
{ <img
isLoading ? <div className="absolute w-full h-full bg-red"><div className="w-[108px] h-[108px] absolute top-[50%] left-[50%] translate-x-[-50%] translate-y-[-50%] bg-black/60 rounded-[20px] flex flex-col items-center justify-center"> src={ReceiveTime}
<img src="/icons/loading.gif" alt="loading" className="w-[68px] h-[68px]" /> alt="receive-item"
<span className="text-[14px] text-[#fff]"></span> className="w-[100px] h-[100px] absolute right-[12px] bottom-[80px]"
</div></div> : <></> onClick={() => setIsReceiveDialogOpen(true)}
} />
{isLoading ? (
<div className="absolute w-full h-full bg-red">
<div className="w-[108px] h-[108px] absolute top-[50%] left-[50%] translate-x-[-50%] translate-y-[-50%] bg-black/60 rounded-[20px] flex flex-col items-center justify-center">
<img
src="/icons/loading.gif"
alt="loading"
className="w-[68px] h-[68px]"
/>
<span className="text-[14px] text-[#fff]"></span>
</div>
</div>
) : (
<></>
)}
<ReceiveDialog
isOpen={isReceiveDialogOpen}
onOpenChange={setIsReceiveDialogOpen}
/>
</div> </div>
); );
} }

View File

@ -8,6 +8,7 @@ import {
import { ReportProvider } from "@/components/Provider/ReportResolveProvider"; import { ReportProvider } from "@/components/Provider/ReportResolveProvider";
// import { RealtimeUtils } from "@coze/realtime-api"; // import { RealtimeUtils } from "@coze/realtime-api";
import { useLocation } from "react-router-dom"; import { useLocation } from "react-router-dom";
import { CountdownProvider } from "@/components/Provider/CountdownProvider";
function MainContent() { function MainContent() {
const { isConnected, handleDisconnect } = useContext(RealtimeClientContext); const { isConnected, handleDisconnect } = useContext(RealtimeClientContext);
@ -28,9 +29,14 @@ function MainContent() {
}, [location.pathname]); }, [location.pathname]);
return ( return (
<ReportProvider> <CountdownProvider>
{isConnected ? <Room /> : <Antechamber />} <ReportProvider>
</ReportProvider> <>
{/* {isConnected ? <Room /> : <Antechamber />} */}
{isConnected ? <Antechamber /> : <Room/>}
</>
</ReportProvider>
</CountdownProvider>
); );
} }

View File

@ -8,7 +8,7 @@ import styles from "./index.module.css";
import { fetchQuestions } from "@/apis/questions"; import { fetchQuestions } from "@/apis/questions";
import { useAbortController } from "@/hooks/useAbortController"; import { useAbortController } from "@/hooks/useAbortController";
import { RealtimeClientContext } from "../Provider/RealtimeClientProvider"; import { RealtimeClientContext } from "../Provider/RealtimeClientProvider";
import Countdown from "../Countdown";
export default function HeaderGroup() { export default function HeaderGroup() {
const [isRotating, setIsRotating] = useState(false); const [isRotating, setIsRotating] = useState(false);
@ -69,6 +69,7 @@ export default function HeaderGroup() {
return ( return (
<div className={styles.headerWrapper}> <div className={styles.headerWrapper}>
<Countdown />
<div className={styles.wrapper}> <div className={styles.wrapper}>
<img className={styles.img} src={HelloGIF} alt="hello" /> <img className={styles.img} src={HelloGIF} alt="hello" />
<div className={styles.text}>Hey,AI</div> <div className={styles.text}>Hey,AI</div>

View File

@ -0,0 +1,45 @@
import { useEffect } from 'react';
import { useCountdown } from '../Provider/CountdownProvider';
/**
* "分钟秒"
* @param totalSeconds
* @returns "10分00秒"
*/
const formatTime = (totalSeconds: number): string => {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
// 确保秒数显示为两位数
const formattedSeconds = seconds < 10 ? `0${seconds}` : `${seconds}`;
return `${minutes}${formattedSeconds}`;
};
interface CountdownProps {
}
export default function Countdown({ }: CountdownProps) {
const { countdown, setCountdown } = useCountdown();
useEffect(() => {
// 只有当秒数大于0时才启动倒计时
if (countdown <= 0) return;
const timer = setInterval(() => {
setCountdown(countdown - 1)
}, 1000);
// 清理定时器
return () => clearInterval(timer);
}, [countdown]);
return (
<div className="w-max h-[23px] rounded-full bg-[#EAF0FE] flex items-center ml-auto px-[10px] py-[3px] mb-[5px]">
<div className="w-[6px] h-[6px] rounded-full bg-[#1580FF] mr-[5px]"></div>
<div className="text-[12px] text-[#000]">{formatTime(countdown)}</div>
</div>
);
}

View File

@ -0,0 +1,25 @@
import { createContext, useContext, useState } from "react"
export const CountdownContext = createContext<{
countdown: number;
setCountdown: (countdown: number) => void;
}>({
countdown: 0,
setCountdown: () => {}
})
export const useCountdown = () => {
const ctx = useContext(CountdownContext);
if (!ctx) throw new Error("useCountdown 必须在 CountdownProvider 内部使用");
return ctx;
}
export const CountdownProvider = ({ children }: { children: React.ReactNode }) => {
const [countdown, setCountdown] = useState(0);
return (
<CountdownContext.Provider value={{ countdown, setCountdown }}>
{children}
</CountdownContext.Provider>
)
}

View File

@ -0,0 +1,3 @@
.custom-bg {
background: linear-gradient(0deg, #FFFFFF 30%,transparent 100%);
}

View File

@ -0,0 +1,56 @@
import {
Dialog,
DialogContent,
DialogTitle,
DialogDescription,
DialogClose
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { X } from "lucide-react";
import "./index.css";
export const ReceiveDialog = ({
isOpen = false,
onOpenChange,
onConfirm
}: {
isOpen?: boolean;
onOpenChange?: (open: boolean) => void;
onConfirm?: () => void;
}) => {
return (
<Dialog open={isOpen} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md border-none bg-transparent p-0 shadow-none">
<DialogTitle className="sr-only"></DialogTitle>
<DialogDescription className="sr-only"></DialogDescription>
<DialogClose className="absolute right-[38px] top-2 z-10 rounded-full w-[28px] h-[28px] bg-[#acacad] flex items-center justify-center bg-[#FFFFFF33]">
<X className="w-[12px] h-[12px] text-white"/>
<span className="sr-only" onClick={() => onOpenChange?.(false)}></span>
</DialogClose>
<div className="relative flex flex-col items-center">
<img
src="/icons/receive.png"
alt="接收"
className="rounded-lg w-full h-[286px] px-[48px]"
/>
<div className="absolute top-1/2 left-0 right-0 flex flex-col items-center px-[20px] py-[17px] text-center mx-[48px] rounded-[40px] custom-bg">
<h3 className="font-[600] text-[18px] text-black mb-[7px]">AI</h3>
<p className="text-sm text-white/90 mb-6 text-[#666]">AI~</p>
<div className="flex space-x-4 w-full justify-center">
<Button
className="bg-[#1580FF] text-white w-full rounded-[50px] text-[16px] py-[11px]"
onClick={() => {
onConfirm?.();
onOpenChange?.(false);
}}
>
</Button>
</div>
</div>
</div>
</DialogContent>
</Dialog>
);
};

View File

@ -96,6 +96,10 @@ export default function RoomConversation() {
<div ref={messagesEndRef} /> <div ref={messagesEndRef} />
</div> </div>
<div className="text-[12px] text-[#999] font-[400] text-center mt-auto mb-[7px]">AI</div> <div className="text-[12px] text-[#999] font-[400] text-center mt-auto mb-[7px]">AI</div>
<div className="mx-[8px] rounded-full bg-[#ffeede] py-[7px] pl-[18px] pr-[3px] flex items-center justify-between">
<span className="text-[#FA8E23] text-[12px]">3~</span>
<button className="rounded-full bg-[#FA8E23] py-[6px] px-[13px] text-[13px] text-white font-[500]"></button>
</div>
</div> </div>
); );
} }

View File

@ -0,0 +1,119 @@
import * as React from "react"
import * as DialogPrimitive from "@radix-ui/react-dialog"
import { cn } from "@/lib/utils"
import { Cross2Icon } from "@radix-ui/react-icons"
const Dialog = DialogPrimitive.Root
const DialogTrigger = DialogPrimitive.Trigger
const DialogPortal = DialogPrimitive.Portal
const DialogClose = DialogPrimitive.Close
const DialogOverlay = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Overlay
ref={ref}
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
/>
))
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName
const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
<DialogPortal>
<DialogOverlay />
<DialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
>
{children}
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
<Cross2Icon className="h-4 w-4" />
<span className="sr-only">Close</span>
</DialogPrimitive.Close>
</DialogPrimitive.Content>
</DialogPortal>
))
DialogContent.displayName = DialogPrimitive.Content.displayName
const DialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-1.5 text-center sm:text-left",
className
)}
{...props}
/>
)
DialogHeader.displayName = "DialogHeader"
const DialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
DialogFooter.displayName = "DialogFooter"
const DialogTitle = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Title
ref={ref}
className={cn(
"text-lg font-semibold leading-none tracking-tight",
className
)}
{...props}
/>
))
DialogTitle.displayName = DialogPrimitive.Title.displayName
const DialogDescription = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
DialogDescription.displayName = DialogPrimitive.Description.displayName
export {
Dialog,
DialogPortal,
DialogOverlay,
DialogTrigger,
DialogClose,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
}