From a430284aa21e3ae1f0d5654e55b2ad2852519cc2 Mon Sep 17 00:00:00 2001
From: wwf <yearningwang@iqtogether.com>
Date: 星期三, 04 六月 2025 15:17:49 +0800
Subject: [PATCH] 初始化

---
 hooks/use-pay.tsx |   68 +++++++++++++++++++++++++++++++++-
 1 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/hooks/use-pay.tsx b/hooks/use-pay.tsx
index 3ba23b6..344f039 100644
--- a/hooks/use-pay.tsx
+++ b/hooks/use-pay.tsx
@@ -4,8 +4,11 @@
 import { useRouter, useSearchParams } from 'next/navigation'
 import { useTranslation } from 'react-i18next'
 import useSWR from 'swr'
+import { useContext } from 'use-context-selector'
+import I18n from '@/context/i18n'
 import {
   fetchDataSourceNotionBinding,
+  fetchFreeQuotaVerify,
 } from '@/service/common'
 import type { IConfirm } from '@/app/components/base/confirm'
 import Confirm from '@/app/components/base/confirm'
@@ -50,6 +53,66 @@
   return confirm
 }
 
+const QUOTA_RECEIVE_STATUS: Record<string, any> = {
+  spark: {
+    success: {
+      'en': 'Successful collection, the quota will be automatically increased after 5 minutes.',
+      'zh-Hans': '棰嗗彇鎴愬姛锛屽皢鍦� 5 鍒嗛挓鍚庤嚜鍔ㄥ鍔犻厤棰�',
+    },
+    fail: {
+      'en': 'Failure to collect',
+      'zh-Hans': '棰嗗彇澶辫触',
+    },
+  },
+  zhipuai: {
+    success: {
+      'en': 'Successful collection',
+      'zh-Hans': '棰嗗彇鎴愬姛',
+    },
+    fail: {
+      'en': 'Failure to collect',
+      'zh-Hans': '棰嗗彇澶辫触',
+    },
+  },
+}
+
+const FREE_CHECK_PROVIDER = ['spark', 'zhipuai']
+export const useCheckFreeQuota = () => {
+  const { locale } = useContext(I18n)
+  const router = useRouter()
+  const [shouldVerify, setShouldVerify] = useState(false)
+  const searchParams = useSearchParams()
+  const type = searchParams.get('type')
+  const provider = searchParams.get('provider')
+  const result = searchParams.get('result')
+  const token = searchParams.get('token')
+
+  const { data, error } = useSWR(
+    shouldVerify
+      ? `/workspaces/current/model-providers/${provider}/free-quota-qualification-verify?token=${token}`
+      : null,
+    fetchFreeQuotaVerify,
+  )
+
+  useEffect(() => {
+    if (error)
+      router.replace('/')
+  }, [error, router])
+
+  useEffect(() => {
+    if (type === 'provider_apply_callback' && FREE_CHECK_PROVIDER.includes(provider as string) && result === 'success')
+      setShouldVerify(true)
+  }, [type, provider, result])
+
+  return (data && provider)
+    ? {
+      type: data.flag ? 'info' : 'warning',
+      title: data.flag ? QUOTA_RECEIVE_STATUS[provider as string].success[locale] : QUOTA_RECEIVE_STATUS[provider].fail[locale],
+      desc: !data.flag ? data.reason : undefined,
+    }
+    : null
+}
+
 export const useCheckNotion = () => {
   const router = useRouter()
   const [confirm, setConfirm] = useState<ConfirmType | null>(null)
@@ -91,6 +154,7 @@
   const { t } = useTranslation()
   const [showPayStatusModal, setShowPayStatusModal] = useState(true)
   const anthropicConfirmInfo = useAnthropicCheckPay()
+  const freeQuotaConfirmInfo = useCheckFreeQuota()
   const notionConfirmInfo = useCheckNotion()
   const billingConfirmInfo = useBillingPay()
 
@@ -99,7 +163,7 @@
     router.replace('/')
   }, [router])
 
-  const confirmInfo = anthropicConfirmInfo || notionConfirmInfo || billingConfirmInfo
+  const confirmInfo = anthropicConfirmInfo || freeQuotaConfirmInfo || notionConfirmInfo || billingConfirmInfo
 
   if (!confirmInfo || !showPayStatusModal)
     return null
@@ -112,7 +176,7 @@
       showCancel={false}
       type={confirmInfo.type === 'info' ? 'info' : 'warning' }
       title={confirmInfo.title}
-      content={(confirmInfo as unknown as { desc: string }).desc || ''}
+      content={(confirmInfo as { desc: string }).desc || ''}
       confirmText={(confirmInfo.type === 'info' && t('common.operation.ok')) || ''}
     />
   )

--
Gitblit v1.8.0