187 lines
4.6 KiB
TypeScript
187 lines
4.6 KiB
TypeScript
'use server'
|
||
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'
|
||
|
||
// ======================
|
||
// public
|
||
// ======================
|
||
|
||
async function callPublic<R = undefined>(
|
||
endpoint: string,
|
||
data?: unknown,
|
||
): Promise<ApiResponse<R>> {
|
||
return _callPublic(endpoint, data ? JSON.stringify(data) : undefined)
|
||
}
|
||
|
||
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,
|
||
},
|
||
)
|
||
})
|
||
|
||
// ======================
|
||
// device
|
||
// ======================
|
||
|
||
async function callByDevice<R = undefined>(
|
||
endpoint: string,
|
||
data: unknown,
|
||
): Promise<ApiResponse<R>> {
|
||
return _callByDevice(endpoint, data ? JSON.stringify(data) : undefined)
|
||
}
|
||
|
||
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',
|
||
}
|
||
}
|
||
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,
|
||
})
|
||
})
|
||
|
||
// ======================
|
||
// user
|
||
// ======================
|
||
|
||
async function callByUser<R = undefined>(
|
||
endpoint: string,
|
||
data?: unknown,
|
||
): Promise<ApiResponse<R>> {
|
||
return _callByUser(endpoint, data ? JSON.stringify(data) : undefined)
|
||
}
|
||
|
||
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
|
||
if (!token) {
|
||
return {
|
||
success: false,
|
||
status: 401,
|
||
message: '会话已失效',
|
||
}
|
||
}
|
||
|
||
// 发起请求
|
||
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,
|
||
})
|
||
})
|
||
|
||
// ======================
|
||
// call
|
||
// ======================
|
||
|
||
async function call<R = undefined>(url: string, request: RequestInit): Promise<ApiResponse<R>> {
|
||
let response: Response
|
||
try {
|
||
response = await fetch(url, request)
|
||
}
|
||
catch (e) {
|
||
console.error('后端请求失败', url, (e as Error).message)
|
||
throw new Error(`请求失败,网络错误`)
|
||
}
|
||
|
||
const type = response.headers.get('Content-Type') ?? 'text/plain'
|
||
if (type.indexOf('text/plain') !== -1) {
|
||
const text = await response.text()
|
||
if (!response.ok) {
|
||
console.log('后端请求失败', url, `status=${response.status}`, text)
|
||
return {
|
||
success: false,
|
||
status: response.status,
|
||
message: text || '请求失败',
|
||
}
|
||
}
|
||
|
||
if (!!text?.trim()?.length) {
|
||
console.log('未处理的响应成功', `type=text`, `text=${text}`)
|
||
}
|
||
return {
|
||
success: true,
|
||
data: undefined as R, // 强转类型,考虑优化
|
||
}
|
||
}
|
||
else if (type.indexOf('application/json') !== -1) {
|
||
const json = await response.json()
|
||
if (!response.ok) {
|
||
console.log('后端请求失败', url, `status=${response.status}`, json)
|
||
|
||
return {
|
||
success: false,
|
||
status: response.status,
|
||
message: json.message || json.error_description || '请求失败', // 业务错误(message)或者 oauth 错误(error_description)
|
||
}
|
||
}
|
||
return {
|
||
success: true,
|
||
data: json,
|
||
}
|
||
}
|
||
|
||
throw new Error(`无法解析响应数据,未处理的 Content-Type: ${type}`)
|
||
}
|
||
|
||
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) {
|
||
console.log('🚗🚗🚗🚗🚗 非正常重定向 🚗🚗🚗🚗🚗')
|
||
redirect('/login?force=true')
|
||
}
|
||
|
||
return resp
|
||
}
|
||
|
||
// 导出
|
||
export {
|
||
callPublic,
|
||
callByDevice,
|
||
callByUser,
|
||
}
|