Blame view

美国版/Food Labeling Management App UniApp/src/services/usAppAuth.ts 2.17 KB
3af4878d   杨鑫   产品 标签 关联
1
2
  import type { UsAppBoundLocationDto } from '../types/usAppBound'
  import { usAppApiRequest } from '../utils/usAppApiRequest'
6faaf539   杨鑫   APP 登录门店对接
3
  
3af4878d   杨鑫   产品 标签 关联
4
  export type { UsAppBoundLocationDto }
6faaf539   杨鑫   APP 登录门店对接
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
  
  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>))
  }
  
3af4878d   杨鑫   产品 标签 关联
45
  /** POST /api/app/us-app-auth/login(401 不触发全局跳转) */
6faaf539   杨鑫   APP 登录门店对接
46
  export async function usAppLogin(input: UsAppLoginInput): Promise<UsAppLoginOutputDto> {
3af4878d   杨鑫   产品 标签 关联
47
    const raw = await usAppApiRequest<unknown>({
6faaf539   杨鑫   APP 登录门店对接
48
49
      path: '/api/app/us-app-auth/login',
      method: 'POST',
3af4878d   杨鑫   产品 标签 关联
50
      skipUnauthorizedRedirect: true,
6faaf539   杨鑫   APP 登录门店对接
51
52
53
54
55
56
57
58
59
60
61
62
      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[]> {
3af4878d   杨鑫   产品 标签 关联
63
    const raw = await usAppApiRequest<unknown>({
6faaf539   杨鑫   APP 登录门店对接
64
65
66
67
68
69
      path: '/api/app/us-app-auth/my-locations',
      method: 'GET',
      auth: true,
    })
    return normalizeLocationList(raw)
  }