添加文章管理和文章分组页面
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use server"
|
||||
import { cookies, headers } from "next/headers"
|
||||
import { redirect } from "next/navigation"
|
||||
// import { redirect } from "next/navigation"
|
||||
import { cache } from "react"
|
||||
import {
|
||||
API_BASE_URL,
|
||||
@@ -164,21 +164,108 @@ async function call<R = undefined>(
|
||||
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
|
||||
// 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 = [/^\/admin.*/].some(item => item.test(pathname))
|
||||
// // 重定向到登录页
|
||||
// const match = [/^\/admin.*/].some(item => item.test(pathname))
|
||||
|
||||
if (match && !resp.success && resp.status === 401) {
|
||||
console.log("🚗🚗🚗🚗🚗 非正常重定向 🚗🚗🚗🚗🚗")
|
||||
redirect("/login?force=true")
|
||||
// if (match && !resp.success && resp.status === 401) {
|
||||
// console.log("🚗🚗🚗🚗🚗 非正常重定向 🚗🚗🚗🚗🚗")
|
||||
// redirect("/login?force=true")
|
||||
// }
|
||||
|
||||
// return resp
|
||||
// }
|
||||
|
||||
// ======================
|
||||
// upload
|
||||
// ======================
|
||||
|
||||
async function callByUserUpload<R = undefined>(
|
||||
endpoint: string,
|
||||
formData: FormData,
|
||||
): Promise<ApiResponse<R>> {
|
||||
const cookie = await cookies()
|
||||
const token = cookie.get("admin/auth_token")?.value
|
||||
if (!token) {
|
||||
return {
|
||||
success: false,
|
||||
status: 401,
|
||||
message: "会话已失效",
|
||||
}
|
||||
}
|
||||
|
||||
return resp
|
||||
return callUpload<R>(
|
||||
`${API_BASE_URL}${endpoint}`,
|
||||
formData,
|
||||
`Bearer ${token}`,
|
||||
)
|
||||
}
|
||||
|
||||
async function callUpload<R = undefined>(
|
||||
url: string,
|
||||
body: FormData,
|
||||
auth?: string,
|
||||
): Promise<ApiResponse<R>> {
|
||||
let response: Response
|
||||
try {
|
||||
const reqHeaders = await headers()
|
||||
const reqIP = reqHeaders.get("x-forwarded-for")
|
||||
const reqUA = reqHeaders.get("user-agent")
|
||||
const callHeaders: RequestInit["headers"] = {}
|
||||
if (auth) callHeaders["Authorization"] = auth
|
||||
if (reqIP) callHeaders["X-Forwarded-For"] = reqIP
|
||||
if (reqUA) callHeaders["User-Agent"] = reqUA
|
||||
|
||||
response = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: callHeaders,
|
||||
body,
|
||||
})
|
||||
} 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 || "请求失败",
|
||||
}
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
data: json,
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`无法解析响应数据,未处理的 Content-Type: ${type}`)
|
||||
}
|
||||
|
||||
// 导出
|
||||
export { callPublic, callByDevice, callByUser }
|
||||
export { callPublic, callByDevice, callByUser, callByUserUpload }
|
||||
|
||||
Reference in New Issue
Block a user