2026-07-15 10:52:41 +08:00
|
|
|
import type { ApiResponse } from '@/types/api'
|
2026-07-15 14:47:42 +08:00
|
|
|
import { getToken } from '@/utils/auth'
|
2026-07-14 15:39:32 +08:00
|
|
|
|
|
|
|
|
export interface LoginRequest {
|
|
|
|
|
username: string
|
|
|
|
|
password: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UserInfo {
|
|
|
|
|
id: number
|
|
|
|
|
username: string
|
|
|
|
|
display_name: string
|
|
|
|
|
email: string
|
|
|
|
|
business_line: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface LoginResponse {
|
|
|
|
|
token: string
|
|
|
|
|
expires_in: number
|
|
|
|
|
user: UserInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const authApi = {
|
2026-07-15 10:52:41 +08:00
|
|
|
login(_data: LoginRequest): Promise<ApiResponse<LoginResponse>> {
|
2026-07-15 14:47:42 +08:00
|
|
|
return Promise.reject(new Error('password login is disabled'))
|
2026-07-14 15:39:32 +08:00
|
|
|
},
|
|
|
|
|
|
2026-07-15 10:52:41 +08:00
|
|
|
logout(): Promise<ApiResponse<null>> {
|
2026-07-15 14:47:42 +08:00
|
|
|
return Promise.resolve({ code: 0, message: 'success', data: null })
|
2026-07-14 15:39:32 +08:00
|
|
|
},
|
|
|
|
|
|
2026-07-15 14:47:42 +08:00
|
|
|
async getUserInfo(): Promise<ApiResponse<UserInfo>> {
|
|
|
|
|
const token = getToken()
|
|
|
|
|
const response = await fetch('/auth/api/v1/users/me', {
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
|
|
|
},
|
2026-07-14 15:39:32 +08:00
|
|
|
})
|
2026-07-15 14:47:42 +08:00
|
|
|
const data = await response.json().catch(() => ({}))
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(data.error || `HTTP ${response.status}`)
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
code: 0,
|
|
|
|
|
message: 'success',
|
|
|
|
|
data: {
|
|
|
|
|
id: data.id,
|
|
|
|
|
username: data.username,
|
|
|
|
|
display_name: data.display_name || data.username,
|
|
|
|
|
email: data.email || '',
|
|
|
|
|
business_line: data.business_line || '',
|
|
|
|
|
},
|
|
|
|
|
}
|
2026-07-14 15:39:32 +08:00
|
|
|
},
|
|
|
|
|
}
|