Files
crawler-plugin/frontend-vue/src/shared/api/http.ts
T
huangzd1997 c086d402a7 feat(web): 品牌检测/图片生成服务端化——URL 统一 https+域名,入口文件带 hash 防长缓存失效
- brand/image 本地执行 API 服务端化(主机A shufu-web-api:15126,nginx 转发 /api/brand/* 等)
- 前端撤销本地回连逻辑(local-http 删除,http.ts/brand.ts 还原为域名相对路径)
- 版本更新改桌面桥 do_update_app(原 Flask /api/update/do 桥化,Web 无更新入口)
- 品牌检测页 Web 可用(hasBridge 恢复 blanket 判定:文件选择走浏览器降级、任务/SSE 走域名)
- vite 入口文件带内容 hash(/assets 配了 immutable 长缓存,无 hash 入口导致更新永不生效)
- public/logo.jpg:Web 版 /logo.jpg 素材(原 app_client/logo.jpg 随桌面瘦身移除)
2026-09-07 17:11:33 +08:00

139 lines
3.4 KiB
TypeScript

import axios, {
AxiosError,
type AxiosInstance,
type AxiosRequestConfig,
type AxiosResponse,
type InternalAxiosRequestConfig,
} from 'axios'
export interface LegacyApiSuccess<T> {
success: true
msg?: string
data?: T
}
export interface LegacyApiFailure {
success: false
error?: string
}
export type ApiResponse<T> = LegacyApiSuccess<T> | LegacyApiFailure
export interface JavaApiResponse<T> {
success: boolean
message: string
data: T | null
}
export type RequestOptions<D = unknown> = AxiosRequestConfig<D>
export function extractErrorMessage(error: unknown) {
if (error instanceof AxiosError) {
const data = error.response?.data
if (data && typeof data === 'object') {
for (const field of ['error', 'message', 'msg'] as const) {
if (field in data && typeof (data as Record<string, unknown>)[field] === 'string') {
const value = ((data as Record<string, string>)[field]).trim()
if (value) {
return value
}
}
}
}
if (typeof data === 'string' && data.trim()) {
return data
}
return error.message || '请求失败'
}
return error instanceof Error ? error.message : '请求失败'
}
function createHttpClient(): AxiosInstance {
const instance = axios.create({
withCredentials: true,
timeout: 30000,
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
})
instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
// 携带本地登录令牌:桌面端同源 cookie 仍可用;dev(5173) 无 cookie 时靠 Bearer 过 Java 鉴权
try {
if (config.url && config.url.indexOf('/newApi') === 0 && typeof window !== 'undefined') {
const token = window.localStorage.getItem('aiimage_auth_token') || ''
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
}
} catch {
/* 忽略读取令牌异常 */
}
return config
})
instance.interceptors.response.use(
(response: AxiosResponse) => response,
(error: unknown) => Promise.reject(new Error(extractErrorMessage(error))),
)
return instance
}
export const http = createHttpClient()
export async function request<T = unknown, D = unknown>(config: RequestOptions<D>): Promise<T> {
const response = await http.request<T, AxiosResponse<T>, D>(config)
return response.data
}
export function get<T = unknown>(url: string, config?: RequestOptions) {
return request<T>({
url,
method: 'GET',
...config,
})
}
export function post<T = unknown, D = unknown>(url: string, data?: D, config?: RequestOptions<D>) {
return request<T, D>({
url,
method: 'POST',
data,
...config,
})
}
export function put<T = unknown, D = unknown>(url: string, data?: D, config?: RequestOptions<D>) {
return request<T, D>({
url,
method: 'PUT',
data,
...config,
})
}
export function del<T = unknown>(url: string, config?: RequestOptions) {
return request<T>({
url,
method: 'DELETE',
...config,
})
}
export const requestJson = request
export const requestGetJson = get
export const requestPostJson = post
export const requestPutJson = put
export const requestDeleteJson = del
export async function unwrapJavaResponse<T>(promise: Promise<JavaApiResponse<T>>) {
const response = await promise
if (!response.success) {
throw new Error(response.message || '请求失败')
}
return response.data as T
}