提交代码
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
/**
|
||||
* 系统参数配置接口封装。
|
||||
* 后端实现见 HardwareWeb/Controllers/SystemConfigController.cs:
|
||||
* POST /api/system-config/auth -> { token, expiresAt }
|
||||
* GET /api/system-config -> SystemConfig[]
|
||||
* POST /api/system-config -> SystemConfig
|
||||
* PUT /api/system-config/{id} -> SystemConfig
|
||||
* DELETE /api/system-config/{id} -> 204
|
||||
* 除密码校验外,其余接口都需要携带 X-Admin-Token 请求头。
|
||||
*/
|
||||
|
||||
export interface SystemConfig {
|
||||
configId: number
|
||||
configClass: string
|
||||
configName: string
|
||||
configValue: string
|
||||
configDescript: string
|
||||
}
|
||||
|
||||
export interface SystemConfigInput {
|
||||
configClass: string
|
||||
configName: string
|
||||
configValue: string
|
||||
configDescript: string
|
||||
}
|
||||
|
||||
interface AdminAuthResult {
|
||||
token: string
|
||||
expiresAt: string
|
||||
}
|
||||
|
||||
const API_BASE = (import.meta.env.VITE_API_BASE ?? '').replace(/\/$/, '')
|
||||
const REQUEST_TIMEOUT = 8000
|
||||
const TOKEN_KEY = 'libs-admin-token'
|
||||
|
||||
export class SystemConfigApiError extends Error {
|
||||
readonly status: number
|
||||
|
||||
constructor(message: string, status = 0) {
|
||||
super(message)
|
||||
this.name = 'SystemConfigApiError'
|
||||
this.status = status
|
||||
}
|
||||
}
|
||||
|
||||
export function getAdminToken(): string {
|
||||
return window.sessionStorage.getItem(TOKEN_KEY) ?? ''
|
||||
}
|
||||
|
||||
export function hasAdminSession(): boolean {
|
||||
return getAdminToken() !== ''
|
||||
}
|
||||
|
||||
export function clearAdminSession() {
|
||||
window.sessionStorage.removeItem(TOKEN_KEY)
|
||||
}
|
||||
|
||||
function setAdminToken(token: string) {
|
||||
window.sessionStorage.setItem(TOKEN_KEY, token)
|
||||
}
|
||||
|
||||
async function request<T>(
|
||||
path: string,
|
||||
init?: RequestInit,
|
||||
withToken = true,
|
||||
): Promise<T> {
|
||||
const controller = new AbortController()
|
||||
const timer = window.setTimeout(() => controller.abort(), REQUEST_TIMEOUT)
|
||||
|
||||
try {
|
||||
const token = withToken ? getAdminToken() : ''
|
||||
const response = await fetch(`${API_BASE}${path}`, {
|
||||
...init,
|
||||
cache: 'no-store',
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
...(init?.body ? { 'Content-Type': 'application/json' } : {}),
|
||||
...(token ? { 'X-Admin-Token': token } : {}),
|
||||
...(init?.headers ?? {}),
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
let detail = `HTTP ${response.status}`
|
||||
try {
|
||||
const body = (await response.json()) as { message?: string; error?: string }
|
||||
detail = body.message ?? body.error ?? detail
|
||||
} catch {
|
||||
// 响应不是 JSON 时保留状态码信息
|
||||
}
|
||||
|
||||
if (response.status === 401 && withToken) {
|
||||
clearAdminSession()
|
||||
}
|
||||
|
||||
throw new SystemConfigApiError(detail, response.status)
|
||||
}
|
||||
|
||||
if (response.status === 204) {
|
||||
return undefined as T
|
||||
}
|
||||
|
||||
return (await response.json()) as T
|
||||
} catch (error) {
|
||||
if (error instanceof SystemConfigApiError) throw error
|
||||
if (error instanceof DOMException && error.name === 'AbortError') {
|
||||
throw new SystemConfigApiError('系统参数服务响应超时')
|
||||
}
|
||||
throw new SystemConfigApiError(
|
||||
error instanceof Error ? error.message : '无法连接系统参数服务',
|
||||
)
|
||||
} finally {
|
||||
window.clearTimeout(timer)
|
||||
}
|
||||
}
|
||||
|
||||
/** 校验管理密码,成功后令牌会写入 sessionStorage。 */
|
||||
export async function verifyAdminPassword(password: string): Promise<void> {
|
||||
const result = await request<AdminAuthResult>(
|
||||
'/api/system-config/auth',
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ password }),
|
||||
},
|
||||
false,
|
||||
)
|
||||
setAdminToken(result.token)
|
||||
}
|
||||
|
||||
export function listSystemConfigs(keyword = ''): Promise<SystemConfig[]> {
|
||||
const query = keyword ? `?keyword=${encodeURIComponent(keyword)}` : ''
|
||||
return request<SystemConfig[]>(`/api/system-config${query}`)
|
||||
}
|
||||
|
||||
export function createSystemConfig(input: SystemConfigInput): Promise<SystemConfig> {
|
||||
return request<SystemConfig>('/api/system-config', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(input),
|
||||
})
|
||||
}
|
||||
|
||||
export function updateSystemConfig(
|
||||
configId: number,
|
||||
input: SystemConfigInput,
|
||||
): Promise<SystemConfig> {
|
||||
return request<SystemConfig>(`/api/system-config/${configId}`, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(input),
|
||||
})
|
||||
}
|
||||
|
||||
export function deleteSystemConfig(configId: number): Promise<void> {
|
||||
return request<void>(`/api/system-config/${configId}`, { method: 'DELETE' })
|
||||
}
|
||||
Reference in New Issue
Block a user