usAppAuth.ts
2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import type { UsAppBoundLocationDto } from '../types/usAppBound'
import { usAppApiRequest } from '../utils/usAppApiRequest'
export type { UsAppBoundLocationDto }
export interface UsAppLoginInput {
email: string
password: string
uuid?: string
code?: string
}
export interface UsAppLoginOutputDto {
token: string
refreshToken: string
locations: UsAppBoundLocationDto[]
}
function normalizeLocation(raw: Record<string, unknown>): UsAppBoundLocationDto {
return {
id: String(raw.id ?? raw.Id ?? ''),
locationCode: String(raw.locationCode ?? raw.LocationCode ?? ''),
locationName: String(raw.locationName ?? raw.LocationName ?? ''),
fullAddress: String(raw.fullAddress ?? raw.FullAddress ?? ''),
state: raw.state !== false && raw.State !== false,
}
}
function normalizeLoginOutput(raw: unknown): UsAppLoginOutputDto {
const o = raw as Record<string, unknown>
const locs = o.locations ?? o.Locations
const arr = Array.isArray(locs) ? locs : []
return {
token: String(o.token ?? o.Token ?? ''),
refreshToken: String(o.refreshToken ?? o.RefreshToken ?? ''),
locations: arr.map((x) => normalizeLocation(x as Record<string, unknown>)),
}
}
function normalizeLocationList(raw: unknown): UsAppBoundLocationDto[] {
const arr = Array.isArray(raw) ? raw : []
return arr.map((x) => normalizeLocation(x as Record<string, unknown>))
}
/** POST /api/app/us-app-auth/login(401 不触发全局跳转) */
export async function usAppLogin(input: UsAppLoginInput): Promise<UsAppLoginOutputDto> {
const raw = await usAppApiRequest<unknown>({
path: '/api/app/us-app-auth/login',
method: 'POST',
skipUnauthorizedRedirect: true,
data: {
email: input.email.trim(),
password: input.password,
...(input.uuid ? { uuid: input.uuid } : {}),
...(input.code != null ? { code: input.code } : {}),
},
})
return normalizeLoginOutput(raw)
}
/** GET /api/app/us-app-auth/my-locations */
export async function usAppFetchMyLocations(): Promise<UsAppBoundLocationDto[]> {
const raw = await usAppApiRequest<unknown>({
path: '/api/app/us-app-auth/my-locations',
method: 'GET',
auth: true,
})
return normalizeLocationList(raw)
}