修复实名认证后的状态更新 & 使用sse方式解决支付轮询两次完成问题 & 修复账户总览图标展示 & 白名单新增获取当前用户IP地址功能 & 购买套餐添加白名单链接
This commit is contained in:
@@ -3,7 +3,6 @@ import {API_BASE_URL, ApiResponse, CLIENT_ID, CLIENT_SECRET} from '@/lib/api'
|
||||
import {cookies, headers} from 'next/headers'
|
||||
import {cache} from 'react'
|
||||
import {redirect} from 'next/navigation'
|
||||
import {userAgent} from 'next/server'
|
||||
|
||||
// ======================
|
||||
// public
|
||||
@@ -20,14 +19,7 @@ const _callPublic = cache(async <R = undefined>(
|
||||
endpoint: string,
|
||||
data?: string,
|
||||
): Promise<ApiResponse<R>> => {
|
||||
return call(`${API_BASE_URL}${endpoint}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: data,
|
||||
},
|
||||
)
|
||||
return call(`${API_BASE_URL}${endpoint}`, data)
|
||||
})
|
||||
|
||||
// ======================
|
||||
@@ -56,14 +48,7 @@ const _callByDevice = cache(async <R = undefined>(
|
||||
const token = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64url')
|
||||
|
||||
// 发起请求
|
||||
return call(`${API_BASE_URL}${endpoint}`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Basic ${token}`,
|
||||
},
|
||||
body: data,
|
||||
})
|
||||
return call(`${API_BASE_URL}${endpoint}`, data, `Basic ${token}`)
|
||||
})
|
||||
|
||||
// ======================
|
||||
@@ -81,8 +66,6 @@ const _callByUser = cache(async <R = undefined>(
|
||||
endpoint: string,
|
||||
data?: string,
|
||||
): Promise<ApiResponse<R>> => {
|
||||
const header = await headers()
|
||||
|
||||
// 获取用户令牌
|
||||
const cookie = await cookies()
|
||||
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}`, {
|
||||
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,
|
||||
})
|
||||
return await call<R>(`${API_BASE_URL}${endpoint}`, data, `Bearer ${token}`)
|
||||
})
|
||||
|
||||
// ======================
|
||||
// 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
|
||||
try {
|
||||
const userHeaders = await headers()
|
||||
// request.headers['x-data-ip'] = header.get('x-forwarded-for')
|
||||
// request.headers['x-data-ua'] = header.get('user-agent')
|
||||
const reqHeaders = await headers()
|
||||
const reqIP = reqHeaders.get('x-forwarded-for')
|
||||
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, {
|
||||
...request,
|
||||
headers: {
|
||||
...request.headers,
|
||||
'x-data-ip': userHeaders.get('x-forwarded-for') || '',
|
||||
'x-data-ua': userHeaders.get('user-agent') || '',
|
||||
},
|
||||
method: 'POST',
|
||||
headers: callHeaders,
|
||||
body,
|
||||
})
|
||||
}
|
||||
catch (e) {
|
||||
|
||||
35
src/actions/ip.ts
Normal file
35
src/actions/ip.ts
Normal 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 : '获取失败'}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user