修复实名认证后的状态更新 & 使用sse方式解决支付轮询两次完成问题 & 修复账户总览图标展示 & 白名单新增获取当前用户IP地址功能 & 购买套餐添加白名单链接

This commit is contained in:
Eamon-meng
2025-12-04 14:43:13 +08:00
parent 591177e7a1
commit a1c80ba588
8 changed files with 128 additions and 77 deletions

View File

@@ -3,7 +3,6 @@ import {API_BASE_URL, ApiResponse, CLIENT_ID, CLIENT_SECRET} from '@/lib/api'
import {cookies, headers} from 'next/headers' import {cookies, headers} from 'next/headers'
import {cache} from 'react' import {cache} from 'react'
import {redirect} from 'next/navigation' import {redirect} from 'next/navigation'
import {userAgent} from 'next/server'
// ====================== // ======================
// public // public
@@ -20,14 +19,7 @@ const _callPublic = cache(async <R = undefined>(
endpoint: string, endpoint: string,
data?: string, data?: string,
): Promise<ApiResponse<R>> => { ): Promise<ApiResponse<R>> => {
return call(`${API_BASE_URL}${endpoint}`, { return call(`${API_BASE_URL}${endpoint}`, data)
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: data,
},
)
}) })
// ====================== // ======================
@@ -56,14 +48,7 @@ const _callByDevice = cache(async <R = undefined>(
const token = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64url') const token = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64url')
// 发起请求 // 发起请求
return call(`${API_BASE_URL}${endpoint}`, { return call(`${API_BASE_URL}${endpoint}`, data, `Basic ${token}`)
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${token}`,
},
body: data,
})
}) })
// ====================== // ======================
@@ -81,8 +66,6 @@ const _callByUser = cache(async <R = undefined>(
endpoint: string, endpoint: string,
data?: string, data?: string,
): Promise<ApiResponse<R>> => { ): Promise<ApiResponse<R>> => {
const header = await headers()
// 获取用户令牌 // 获取用户令牌
const cookie = await cookies() const cookie = await cookies()
const token = cookie.get('auth_token')?.value const token = cookie.get('auth_token')?.value
@@ -95,35 +78,30 @@ const _callByUser = cache(async <R = undefined>(
} }
// 发起请求 // 发起请求
return await call<R>(`${API_BASE_URL}${endpoint}`, { return await call<R>(`${API_BASE_URL}${endpoint}`, data, `Bearer ${token}`)
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'X-Forwarded-For': header.get('x-forwarded-for') || '[web]unknown',
'User-Agent': header.get('user-agent') || '[web]unknown',
} as Record<string, string>,
body: data,
})
}) })
// ====================== // ======================
// call // call
// ====================== // ======================
async function call<R = undefined>(url: string, request: RequestInit): Promise<ApiResponse<R>> { async function call<R = undefined>(url: string, body: RequestInit['body'], auth?: string): Promise<ApiResponse<R>> {
let response: Response let response: Response
try { try {
const userHeaders = await headers() const reqHeaders = await headers()
// request.headers['x-data-ip'] = header.get('x-forwarded-for') const reqIP = reqHeaders.get('x-forwarded-for')
// request.headers['x-data-ua'] = header.get('user-agent') const reqUA = reqHeaders.get('user-agent')
const callHeaders: RequestInit['headers'] = {
'Content-Type': 'application/json',
}
if (auth) callHeaders['Authorization'] = auth
if (reqIP) callHeaders['X-Forwarded-For'] = reqIP
if (reqUA) callHeaders['User-Agent'] = reqUA
response = await fetch(url, { response = await fetch(url, {
...request, method: 'POST',
headers: { headers: callHeaders,
...request.headers, body,
'x-data-ip': userHeaders.get('x-forwarded-for') || '',
'x-data-ua': userHeaders.get('user-agent') || '',
},
}) })
} }
catch (e) { catch (e) {

35
src/actions/ip.ts Normal file
View File

@@ -0,0 +1,35 @@
'use server'
import {headers} from 'next/headers'
export async function getClientIp(): Promise<{ip?: string, error?: string}> {
try {
// 1. 从headers获取
const headersList = await headers()
// 尝试常见header
const forwardedFor = headersList.get('x-forwarded-for')
if (forwardedFor) {
const ip = forwardedFor.split(',')[0].trim()
if (ip) return {ip}
}
const realIp = headersList.get('x-real-ip')
if (realIp) return {ip: realIp}
// 回退到ipify
const response = await fetch('https://api.ipify.org?format=json', {
cache: 'no-store',
})
if (response.ok) {
const data = await response.json()
return {ip: data.ip}
}
return {error: '无法获取IP'}
}
catch (error) {
return {error: error instanceof Error ? error.message : '获取失败'}
}
}

View File

@@ -50,6 +50,8 @@ export default function Charts({initialData}: ChartsProps) {
resource_no: value.resource_no ?? '', resource_no: value.resource_no ?? '',
create_after: value.create_after ?? sevenDaysAgo, create_after: value.create_after ?? sevenDaysAgo,
create_before: value.create_before ?? today, create_before: value.create_before ?? today,
// create_after: value.create_after ? format(value.create_after, 'yyyy-MM-dd') : format(sevenDaysAgo, 'yyyy-MM-dd'),
// create_before: value.create_before ? format(value.create_before, 'yyyy-MM-dd') : format(today, 'yyyy-MM-dd'),
} }
const resp = await statisticsResourceUsage(res) const resp = await statisticsResourceUsage(res)
@@ -67,6 +69,7 @@ export default function Charts({initialData}: ChartsProps) {
date: item.date, date: item.date,
count: item.count, count: item.count,
})) }))
formattedData.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime())
setSubmittedData(formattedData) setSubmittedData(formattedData)
}, },
@@ -147,7 +150,11 @@ type DashboardChartProps = {
} }
function DashboardChart(props: DashboardChartProps) { function DashboardChart(props: DashboardChartProps) {
const chartData = props.data.map((item) => { const sortedData = [...props.data].sort((a, b) => {
return new Date(a.date).getTime() - new Date(b.date).getTime()
})
const chartData = sortedData.map((item) => {
const date = new Date(item.date.split('T')[0]) const date = new Date(item.date.split('T')[0])
return { return {
...item, ...item,

View File

@@ -169,12 +169,12 @@ export default function IdentifyPage(props: IdentifyPageProps) {
<div className="flex flex-col gap-4 items-center"> <div className="flex flex-col gap-4 items-center">
<canvas ref={canvas} width={256} height={256}/> <canvas ref={canvas} width={256} height={256}/>
<p className="text-sm text-gray-600"></p> <p className="text-sm text-gray-600"></p>
{/* <Button onClick={async () => { <Button onClick={async () => {
await refreshProfile() await refreshProfile()
setOpenDialog(false) setOpenDialog(false)
}}> }}>
</Button> */} </Button>
</div> </div>
)} )}
</DialogContent> </DialogContent>

View File

@@ -23,7 +23,7 @@ import {
import Page from '@/components/page' import Page from '@/components/page'
import DataTable from '@/components/data-table' import DataTable from '@/components/data-table'
import {format, parseISO} from 'date-fns' import {format, parseISO} from 'date-fns'
import {getClientIp} from '@/actions/ip'
const schema = z.object({ const schema = z.object({
host: z.string().min(1, {message: 'IP地址不能为空'}), host: z.string().min(1, {message: 'IP地址不能为空'}),
remark: z.string().optional(), remark: z.string().optional(),
@@ -226,6 +226,27 @@ export default function WhitelistPage(props: WhitelistPageProps) {
return <div className="flex items-center justify-center h-full"></div> return <div className="flex items-center justify-center h-full"></div>
} }
const getCurrentIP = async () => {
setWait(true)
try {
const result = await getClientIp()
if (result.ip) {
form.setValue('host', result.ip)
toast.success('已获取当前IP地址')
}
else {
toast.error('获取失败', {
description: result.error || '请手动输入IP地址',
})
}
}
finally {
setWait(false)
}
}
return ( return (
<Page> <Page>
@@ -236,14 +257,6 @@ export default function WhitelistPage(props: WhitelistPageProps) {
{data.total >= MAX_WHITELIST_COUNT && '(已达上限)'} {data.total >= MAX_WHITELIST_COUNT && '(已达上限)'}
</Button> </Button>
{/* <Button
theme="fail"
className="ml-2"
disabled={selection.size === 0 || wait}
onClick={() => confirmRemove()}>
<Trash2/>
删除选中
</Button> */}
</section> </section>
{/* 数据表 */} {/* 数据表 */}
@@ -306,7 +319,23 @@ export default function WhitelistPage(props: WhitelistPageProps) {
onSubmit={onSubmit}> onSubmit={onSubmit}>
<FormField name="host" label="IP地址"> <FormField name="host" label="IP地址">
{({id, field}) => ( {({id, field}) => (
<Input {...field} id={id} placeholder="输入IP地址"/> <div className="flex gap-2">
<Input
{...field}
id={id}
placeholder="输入IP地址"
className="flex-1"
/>
<Button
type="button"
onClick={getCurrentIP}
disabled={wait}
className="shrink-0"
theme="outline"
>
{wait ? <Loader2 className="w-4 h-4 animate-spin"/> : '获取当前IP'}
</Button>
</div>
)} )}
</FormField> </FormField>
<FormField name="remark" label="备注"> <FormField name="remark" label="备注">

View File

@@ -75,8 +75,11 @@ export default function Extract(props: ExtractProps) {
<Alert variant="warn" className="flex items-center"> <Alert variant="warn" className="flex items-center">
<CircleAlert/> <CircleAlert/>
<AlertTitle className="flex">IP前需要将本机IP添加到白名单后才可使用</AlertTitle> <AlertTitle className="flex">IP前需要将本机IP添加到白名单后才可使用</AlertTitle>
<Link href="/admin/whitelist"> <Link
<Button ><Plus/></Button> href="/admin/whitelist"
className="text-blue-600 hover:text-blue-800 hover:underline font-medium ml-2"
>
</Link> </Link>
</Alert> </Alert>
@@ -526,12 +529,16 @@ function ApplyLink() {
const handler = form.handleSubmit( const handler = form.handleSubmit(
// eslint-disable-next-line react-hooks/refs // eslint-disable-next-line react-hooks/refs
async (values: z.infer<typeof schema>) => { async (values: z.infer<typeof schema>) => {
console.log(values, 'values')
const params = link(values) const params = link(values)
console.log(params, 'paramsparams')
switch (type.current) { switch (type.current) {
case 'copy': case 'copy':
const url = new URL(window.location.href).origin const url = new URL(window.location.href).origin
const text = `${url}${params}` const text = `${url}${params}`
console.log(text, 'text')
// 使用 clipboard API 复制链接 // 使用 clipboard API 复制链接
let copied = false let copied = false

View File

@@ -33,33 +33,28 @@ export function PaymentModal(props: PaymentModalProps) {
} }
} }
// 轮询检查支付状态 // SSE处理方式检查支付状态
useEffect(() => { useEffect(() => {
const pollInterval = 2000 const eventSource = new EventSource(
const maxRetries = 30 `${process.env.NEXT_PUBLIC_API_BASE_URL}/api/trade/check?trade_no=${props.inner_no}&method=${props.method}`,
let retries = 0 )
eventSource.onmessage = async (event) => {
const interval = setInterval(async () => { console.log(event, 'eventeventevent')
try { switch (event.data) {
await props.onConfirm(false) case '1':
return props.onConfirm?.(true)
case '2':
props.onClose?.()
} }
catch (error) { }
console.error('支付状态检查失败:', error) eventSource.onerror = (error) => {
} console.error('SSE 连接错误:', error)
finally { }
console.log('进入轮询支付状态')
retries++
if (retries >= maxRetries) {
clearInterval(interval)
}
}
}, pollInterval)
return () => { return () => {
clearInterval(interval) eventSource.close()
} }
}, [props]) }, [props.inner_no, props.method, props.onConfirm])
return ( return (
<Dialog <Dialog

View File

@@ -1,5 +1,5 @@
// 定义后端服务URL和OAuth2配置 // 定义后端服务URL和OAuth2配置
const API_BASE_URL = process.env.API_BASE_URL const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL
const CLIENT_ID = process.env.CLIENT_ID const CLIENT_ID = process.env.CLIENT_ID
const CLIENT_SECRET = process.env.CLIENT_SECRET const CLIENT_SECRET = process.env.CLIENT_SECRET