2025-04-08 11:21:58 +08:00
|
|
|
|
'use server'
|
2025-04-26 14:18:08 +08:00
|
|
|
|
import {API_BASE_URL, ApiResponse, CLIENT_ID, CLIENT_SECRET} from '@/lib/api'
|
2025-04-21 19:02:14 +08:00
|
|
|
|
import {cookies, headers} from 'next/headers'
|
2025-04-23 19:00:53 +08:00
|
|
|
|
import {cache} from 'react'
|
2025-04-26 14:18:08 +08:00
|
|
|
|
import {redirect} from 'next/navigation'
|
2025-12-01 19:11:53 +08:00
|
|
|
|
import {userAgent} from 'next/server'
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
|
|
|
|
|
// ======================
|
2025-04-26 14:18:08 +08:00
|
|
|
|
// public
|
2025-04-08 11:21:58 +08:00
|
|
|
|
// ======================
|
|
|
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
|
async function callPublic<R = undefined>(
|
2025-04-23 19:00:53 +08:00
|
|
|
|
endpoint: string,
|
2025-04-26 14:18:08 +08:00
|
|
|
|
data?: unknown,
|
2025-04-23 19:00:53 +08:00
|
|
|
|
): Promise<ApiResponse<R>> {
|
2025-04-26 14:18:08 +08:00
|
|
|
|
return _callPublic(endpoint, data ? JSON.stringify(data) : undefined)
|
2025-04-23 19:00:53 +08:00
|
|
|
|
}
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
|
const _callPublic = cache(async <R = undefined>(
|
2025-04-23 19:00:53 +08:00
|
|
|
|
endpoint: string,
|
|
|
|
|
|
data?: string,
|
|
|
|
|
|
): Promise<ApiResponse<R>> => {
|
2025-04-26 14:18:08 +08:00
|
|
|
|
return call(`${API_BASE_URL}${endpoint}`, {
|
2025-05-09 09:36:23 +08:00
|
|
|
|
method: 'POST',
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
2025-04-26 14:18:08 +08:00
|
|
|
|
},
|
2025-05-09 09:36:23 +08:00
|
|
|
|
body: data,
|
|
|
|
|
|
},
|
2025-04-26 14:18:08 +08:00
|
|
|
|
)
|
2025-04-23 19:00:53 +08:00
|
|
|
|
})
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
|
|
|
|
|
// ======================
|
2025-04-26 14:18:08 +08:00
|
|
|
|
// device
|
2025-04-08 11:21:58 +08:00
|
|
|
|
// ======================
|
|
|
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
|
async function callByDevice<R = undefined>(
|
|
|
|
|
|
endpoint: string,
|
|
|
|
|
|
data: unknown,
|
|
|
|
|
|
): Promise<ApiResponse<R>> {
|
|
|
|
|
|
return _callByDevice(endpoint, data ? JSON.stringify(data) : undefined)
|
|
|
|
|
|
}
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
|
const _callByDevice = cache(async <R = undefined>(
|
|
|
|
|
|
endpoint: string,
|
|
|
|
|
|
data?: string,
|
|
|
|
|
|
): Promise<ApiResponse<R>> => {
|
|
|
|
|
|
// 获取设备令牌
|
|
|
|
|
|
if (!CLIENT_ID || !CLIENT_SECRET) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
status: 401,
|
|
|
|
|
|
message: '未配置 CLIENT_ID 或 CLIENT_SECRET',
|
|
|
|
|
|
}
|
2025-04-08 11:21:58 +08:00
|
|
|
|
}
|
2025-04-26 14:18:08 +08:00
|
|
|
|
const token = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64url')
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
|
// 发起请求
|
|
|
|
|
|
return call(`${API_BASE_URL}${endpoint}`, {
|
2025-04-08 11:21:58 +08:00
|
|
|
|
method: 'POST',
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
2025-04-26 14:18:08 +08:00
|
|
|
|
'Authorization': `Basic ${token}`,
|
2025-04-08 11:21:58 +08:00
|
|
|
|
},
|
2025-04-26 14:18:08 +08:00
|
|
|
|
body: data,
|
2025-04-08 11:21:58 +08:00
|
|
|
|
})
|
2025-04-26 14:18:08 +08:00
|
|
|
|
})
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
|
// ======================
|
|
|
|
|
|
// user
|
|
|
|
|
|
// ======================
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
|
|
|
|
|
async function callByUser<R = undefined>(
|
|
|
|
|
|
endpoint: string,
|
2025-04-12 11:10:51 +08:00
|
|
|
|
data?: unknown,
|
2025-04-08 11:21:58 +08:00
|
|
|
|
): Promise<ApiResponse<R>> {
|
2025-06-06 16:17:33 +08:00
|
|
|
|
return _callByUser(endpoint, data ? JSON.stringify(data) : undefined)
|
2025-04-23 19:00:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const _callByUser = cache(async <R = undefined>(
|
|
|
|
|
|
endpoint: string,
|
|
|
|
|
|
data?: string,
|
|
|
|
|
|
): Promise<ApiResponse<R>> => {
|
|
|
|
|
|
const header = await headers()
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
|
// 获取用户令牌
|
|
|
|
|
|
const cookie = await cookies()
|
|
|
|
|
|
const token = cookie.get('auth_token')?.value
|
|
|
|
|
|
if (!token) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
status: 401,
|
|
|
|
|
|
message: '会话已失效',
|
2025-04-23 19:00:53 +08:00
|
|
|
|
}
|
2025-04-08 11:21:58 +08:00
|
|
|
|
}
|
2025-04-26 14:18:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 发起请求
|
|
|
|
|
|
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,
|
|
|
|
|
|
})
|
2025-04-23 19:00:53 +08:00
|
|
|
|
})
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
2025-04-16 09:43:12 +08:00
|
|
|
|
// ======================
|
2025-04-26 14:18:08 +08:00
|
|
|
|
// call
|
2025-04-16 09:43:12 +08:00
|
|
|
|
// ======================
|
|
|
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
|
async function call<R = undefined>(url: string, request: RequestInit): Promise<ApiResponse<R>> {
|
2025-05-09 09:36:23 +08:00
|
|
|
|
let response: Response
|
|
|
|
|
|
try {
|
2025-12-01 19:11:53 +08:00
|
|
|
|
const userHeaders = await headers()
|
|
|
|
|
|
// request.headers['x-data-ip'] = header.get('x-forwarded-for')
|
|
|
|
|
|
// request.headers['x-data-ua'] = header.get('user-agent')
|
|
|
|
|
|
response = await fetch(url, {
|
|
|
|
|
|
...request,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
...request.headers,
|
|
|
|
|
|
'x-data-ip': userHeaders.get('x-forwarded-for') || '',
|
|
|
|
|
|
'x-data-ua': userHeaders.get('user-agent') || '',
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
2025-05-09 09:36:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (e) {
|
|
|
|
|
|
console.error('后端请求失败', url, (e as Error).message)
|
|
|
|
|
|
throw new Error(`请求失败,网络错误`)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-08 11:21:58 +08:00
|
|
|
|
const type = response.headers.get('Content-Type') ?? 'text/plain'
|
2025-05-09 09:36:23 +08:00
|
|
|
|
if (type.indexOf('text/plain') !== -1) {
|
|
|
|
|
|
const text = await response.text()
|
2025-04-08 11:21:58 +08:00
|
|
|
|
if (!response.ok) {
|
2025-05-09 09:36:23 +08:00
|
|
|
|
console.log('后端请求失败', url, `status=${response.status}`, text)
|
2025-04-08 11:21:58 +08:00
|
|
|
|
return {
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
status: response.status,
|
2025-05-09 09:36:23 +08:00
|
|
|
|
message: text || '请求失败',
|
2025-04-08 11:21:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-05-09 09:36:23 +08:00
|
|
|
|
|
|
|
|
|
|
if (!!text?.trim()?.length) {
|
|
|
|
|
|
console.log('未处理的响应成功', `type=text`, `text=${text}`)
|
|
|
|
|
|
}
|
2025-04-08 11:21:58 +08:00
|
|
|
|
return {
|
|
|
|
|
|
success: true,
|
2025-05-09 09:36:23 +08:00
|
|
|
|
data: undefined as R, // 强转类型,考虑优化
|
2025-04-08 11:21:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-05-09 09:36:23 +08:00
|
|
|
|
else if (type.indexOf('application/json') !== -1) {
|
|
|
|
|
|
const json = await response.json()
|
2025-04-08 11:21:58 +08:00
|
|
|
|
if (!response.ok) {
|
2025-05-09 09:36:23 +08:00
|
|
|
|
console.log('后端请求失败', url, `status=${response.status}`, json)
|
|
|
|
|
|
|
2025-04-08 11:21:58 +08:00
|
|
|
|
return {
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
status: response.status,
|
2025-06-06 16:17:33 +08:00
|
|
|
|
message: json.message || json.error_description || '请求失败', // 业务错误(message)或者 oauth 错误(error_description)
|
2025-04-08 11:21:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
|
|
|
success: true,
|
2025-05-09 09:36:23 +08:00
|
|
|
|
data: json,
|
2025-04-08 11:21:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-04-26 14:18:08 +08:00
|
|
|
|
|
|
|
|
|
|
throw new Error(`无法解析响应数据,未处理的 Content-Type: ${type}`)
|
2025-04-08 11:21:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-26 14:18:08 +08:00
|
|
|
|
async function postCall<R = undefined>(rawResp: Promise<ApiResponse<R>>) {
|
|
|
|
|
|
const header = await headers()
|
|
|
|
|
|
const pathname = header.get('x-pathname') || '/'
|
|
|
|
|
|
const resp = await rawResp
|
|
|
|
|
|
|
|
|
|
|
|
// 重定向到登录页
|
|
|
|
|
|
const match = [
|
|
|
|
|
|
RegExp(`^/admin.*`),
|
|
|
|
|
|
].some(item => item.test(pathname))
|
|
|
|
|
|
|
|
|
|
|
|
if (match && !resp.success && resp.status === 401) {
|
2025-05-12 10:58:17 +08:00
|
|
|
|
console.log('🚗🚗🚗🚗🚗 非正常重定向 🚗🚗🚗🚗🚗')
|
2025-05-09 09:36:23 +08:00
|
|
|
|
redirect('/login?force=true')
|
2025-04-26 14:18:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
|
}
|
2025-04-08 11:21:58 +08:00
|
|
|
|
|
|
|
|
|
|
// 导出
|
|
|
|
|
|
export {
|
2025-04-26 14:18:08 +08:00
|
|
|
|
callPublic,
|
2025-04-08 11:21:58 +08:00
|
|
|
|
callByDevice,
|
|
|
|
|
|
callByUser,
|
|
|
|
|
|
}
|