修复实名认证后的状态更新 & 使用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) {
|
||||
|
||||
Reference in New Issue
Block a user