262 lines
6.4 KiB
TypeScript
262 lines
6.4 KiB
TypeScript
'use server'
|
|
import {API_BASE_URL, CLIENT_ID, CLIENT_SECRET, ApiResponse, UnauthorizedError} from '@/lib/api'
|
|
import {cookies, headers} from 'next/headers'
|
|
import {redirect} from 'next/navigation'
|
|
import {cache} from 'react'
|
|
|
|
// ======================
|
|
// region device token
|
|
// ======================
|
|
|
|
async function callByDevice<R = undefined>(
|
|
endpoint: string,
|
|
data: unknown,
|
|
): Promise<ApiResponse<R>> {
|
|
return _callByDevice(endpoint, data ? JSON.stringify(data) : undefined)
|
|
}
|
|
|
|
// 通用的API调用函数
|
|
const _callByDevice = cache(async <R = undefined>(
|
|
endpoint: string,
|
|
data?: string,
|
|
): Promise<ApiResponse<R>> => {
|
|
try {
|
|
// 获取设备令牌
|
|
if (!CLIENT_ID || !CLIENT_SECRET) {
|
|
throw new Error('缺少CLIENT_ID或CLIENT_SECRET环境变量')
|
|
}
|
|
const token = Buffer.from(`${CLIENT_ID}:${CLIENT_SECRET}`).toString('base64url')
|
|
|
|
// 构造请求
|
|
const url = `${API_BASE_URL}${endpoint}`
|
|
const request = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Basic ${token}`,
|
|
},
|
|
body: data,
|
|
}
|
|
|
|
// 发送请求
|
|
const response = await fetch(`${API_BASE_URL}${endpoint}`, request)
|
|
|
|
// 检查响应状态
|
|
return handleResponse(response)
|
|
}
|
|
catch (e) {
|
|
throw new Error('服务调用失败', {cause: e})
|
|
}
|
|
})
|
|
|
|
// endregion
|
|
|
|
// ======================
|
|
// region user token
|
|
// ======================
|
|
|
|
async function getUserToken(refresh = false): Promise<string> {
|
|
// 从 cookie 中获取用户令牌
|
|
const cookie = await cookies()
|
|
const userToken = cookie.get('auth_token')?.value
|
|
const userRefresh = cookie.get('auth_refresh')?.value
|
|
|
|
// 检查缓存的令牌是否可用
|
|
if (!refresh && userToken) {
|
|
return userToken
|
|
}
|
|
|
|
// 如果没有刷新令牌,抛出异常
|
|
if (!userRefresh) {
|
|
throw UnauthorizedError
|
|
}
|
|
|
|
// 请求刷新访问令牌
|
|
const addr = `${API_BASE_URL}/api/auth/token`
|
|
const body = {
|
|
grant_type: 'refresh_token',
|
|
client_id: CLIENT_ID,
|
|
client_secret: CLIENT_SECRET,
|
|
refresh_token: userRefresh,
|
|
}
|
|
|
|
const response = await fetch(addr, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(body),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
console.log('刷新令牌失败', `status=${response.status}`, await response.text())
|
|
throw UnauthorizedError
|
|
}
|
|
|
|
// 保存新的用户令牌到 cookie
|
|
const data = await response.json()
|
|
const nextAccessToken = data.access_token
|
|
const nextRefreshToken = data.refresh_token
|
|
const expiresIn = data.expires_in
|
|
|
|
cookie.set('auth_token', nextAccessToken, {
|
|
httpOnly: true,
|
|
sameSite: 'strict',
|
|
maxAge: Math.max(expiresIn, 0),
|
|
})
|
|
cookie.set('auth_refresh', nextRefreshToken, {
|
|
httpOnly: true,
|
|
sameSite: 'strict',
|
|
maxAge: 7 * 24 * 3600, // 7天
|
|
})
|
|
|
|
return nextAccessToken
|
|
}
|
|
|
|
// 使用用户令牌的API调用函数
|
|
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()
|
|
try {
|
|
let token = await getUserToken()
|
|
|
|
// 构造请求
|
|
const request = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`,
|
|
} as Record<string, string>,
|
|
body: data,
|
|
}
|
|
|
|
const userIp = header.get('x-forwarded-for')
|
|
if (userIp) {
|
|
request.headers['X-Forwarded-For'] = userIp
|
|
}
|
|
|
|
// 发送请求
|
|
let response = await fetch(`${API_BASE_URL}${endpoint}`, request)
|
|
if (response.status === 401) {
|
|
token = await getUserToken(true)
|
|
request.headers['Authorization'] = `Bearer ${token}`
|
|
response = await fetch(`${API_BASE_URL}${endpoint}`, request)
|
|
}
|
|
|
|
if (response.status === 401) {
|
|
throw UnauthorizedError
|
|
}
|
|
|
|
return handleResponse(response)
|
|
}
|
|
catch (e) {
|
|
if (e === UnauthorizedError) {
|
|
const referer = header.get('referer')
|
|
let redirectUrl = '/login'
|
|
if (referer) {
|
|
const url = new URL(referer)
|
|
redirectUrl = `/login?redirect=${encodeURIComponent(url.pathname)}`
|
|
}
|
|
return redirect(redirectUrl)
|
|
}
|
|
throw new Error('服务调用失败', {cause: e})
|
|
}
|
|
})
|
|
|
|
// endregion
|
|
|
|
// ======================
|
|
// region no token
|
|
// ======================
|
|
|
|
// 不需要令牌的公共API调用函数
|
|
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>> => {
|
|
try {
|
|
const url = `${API_BASE_URL}${endpoint}`
|
|
const request: RequestInit = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: data,
|
|
}
|
|
|
|
const response = await fetch(url, request)
|
|
return handleResponse(response)
|
|
}
|
|
catch (e) {
|
|
throw new Error('服务调用失败', {cause: e})
|
|
}
|
|
})
|
|
|
|
// endregion
|
|
|
|
// 统一响应解析
|
|
async function handleResponse<R = undefined>(response: Response): Promise<ApiResponse<R>> {
|
|
const type = response.headers.get('Content-Type') ?? 'text/plain'
|
|
if (type.indexOf('application/json') !== -1) {
|
|
const json = await response.json()
|
|
if (!response.ok) {
|
|
console.log('响应不成功', `status=${response.status}`, json)
|
|
return {
|
|
success: false,
|
|
status: response.status,
|
|
message: json.message || '请求失败',
|
|
}
|
|
}
|
|
return {
|
|
success: true,
|
|
data: json,
|
|
}
|
|
}
|
|
else if (type.indexOf('text/plain') !== -1) {
|
|
const text = await response.text()
|
|
if (!response.ok) {
|
|
console.log('响应不成功', `status=${response.status}`, text)
|
|
return {
|
|
success: false,
|
|
status: response.status,
|
|
message: text || '请求失败',
|
|
}
|
|
}
|
|
|
|
console.log('响应成功', `type=text`, `text=${text}`)
|
|
return {
|
|
success: true,
|
|
data: undefined as R, // 强转类型,考虑优化
|
|
}
|
|
}
|
|
else {
|
|
throw new Error(`无法解析响应数据,未处理的 Content-Type: ${type}`)
|
|
}
|
|
}
|
|
|
|
// 预定义错误
|
|
|
|
// 导出
|
|
export {
|
|
getUserToken,
|
|
callByDevice,
|
|
callByUser,
|
|
callPublic,
|
|
}
|