Blame view

美国版/Food Labeling Management App UniApp/src/services/usAppAuth.ts 5.76 KB
3af4878d   杨鑫   产品 标签 关联
1
2
  import type { UsAppBoundLocationDto } from '../types/usAppBound'
  import { usAppApiRequest } from '../utils/usAppApiRequest'
699ea6e8   杨鑫   完善打印逻辑
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
  import { fetchWithOfflineCache } from '../utils/sqliteSync'
  
  /** GET /api/app/us-app-auth/my-profile → UsAppMyProfileOutputDto */
  export interface UsAppMyProfileOutputDto {
    fullName: string
    email: string
    phone: string
    employeeId: string
    roleDisplay: string
    primaryRoleCode: string | null
  }
  
  export interface UsAppChangePasswordInput {
    currentPassword: string
    newPassword: string
    confirmNewPassword: string
  }
  
  /** GET /api/app/us-app-auth/location-detail/{locationId} */
  export interface UsAppLocationDetailOutputDto {
    locationId: string
    locationName: string
    fullAddress: string
    storePhone: string
    operatingHours: string
    managerName: string
    managerPhone: string
  }
6faaf539   杨鑫   APP 登录门店对接
31
  
3af4878d   杨鑫   产品 标签 关联
32
  export type { UsAppBoundLocationDto }
6faaf539   杨鑫   APP 登录门店对接
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
70
71
72
  
  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   杨鑫   产品 标签 关联
73
  /** POST /api/app/us-app-auth/login(401 不触发全局跳转) */
6faaf539   杨鑫   APP 登录门店对接
74
  export async function usAppLogin(input: UsAppLoginInput): Promise<UsAppLoginOutputDto> {
3af4878d   杨鑫   产品 标签 关联
75
    const raw = await usAppApiRequest<unknown>({
6faaf539   杨鑫   APP 登录门店对接
76
77
      path: '/api/app/us-app-auth/login',
      method: 'POST',
3af4878d   杨鑫   产品 标签 关联
78
      skipUnauthorizedRedirect: true,
6faaf539   杨鑫   APP 登录门店对接
79
80
81
82
83
84
85
86
87
88
89
90
      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[]> {
699ea6e8   杨鑫   完善打印逻辑
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    return fetchWithOfflineCache('auth', 'my-locations', async () => {
      const raw = await usAppApiRequest<unknown>({
        path: '/api/app/us-app-auth/my-locations',
        method: 'GET',
        auth: true,
      })
      return normalizeLocationList(raw)
    })
  }
  
  function normalizeMyProfile(raw: unknown): UsAppMyProfileOutputDto {
    const o = (raw && typeof raw === 'object' ? raw : {}) as Record<string, unknown>
    const pr = o.primaryRoleCode ?? o.PrimaryRoleCode
    return {
      fullName: String(o.fullName ?? o.FullName ?? ''),
      email: String(o.email ?? o.Email ?? ''),
      phone: String(o.phone ?? o.Phone ?? ''),
      employeeId: String(o.employeeId ?? o.EmployeeId ?? ''),
      roleDisplay: String(o.roleDisplay ?? o.RoleDisplay ?? ''),
      primaryRoleCode: pr == null || pr === '' ? null : String(pr),
    }
  }
  
  /** GET /api/app/us-app-auth/my-profile */
  export async function usAppFetchMyProfile(): Promise<UsAppMyProfileOutputDto> {
    return fetchWithOfflineCache('auth', 'my-profile', async () => {
      const raw = await usAppApiRequest<unknown>({
        path: '/api/app/us-app-auth/my-profile',
        method: 'GET',
        auth: true,
      })
      return normalizeMyProfile(unwrapIfNeeded(raw))
    })
  }
  
  function unwrapIfNeeded(raw: unknown): unknown {
    if (raw == null || typeof raw !== 'object') return raw
    const o = raw as Record<string, unknown>
    if ('result' in o && o.result !== undefined) return o.result
    if (o.data !== undefined) return o.data
    if (o.Data !== undefined) return o.Data
    return raw
  }
  
  function normalizeLocationDetail(raw: unknown): UsAppLocationDetailOutputDto {
    const o = (unwrapIfNeeded(raw) ?? {}) as Record<string, unknown>
    return {
      locationId: String(o.locationId ?? o.LocationId ?? ''),
      locationName: String(o.locationName ?? o.LocationName ?? ''),
      fullAddress: String(o.fullAddress ?? o.FullAddress ?? ''),
      storePhone: String(o.storePhone ?? o.StorePhone ?? ''),
      operatingHours: String(o.operatingHours ?? o.OperatingHours ?? ''),
      managerName: String(o.managerName ?? o.ManagerName ?? ''),
      managerPhone: String(o.managerPhone ?? o.ManagerPhone ?? ''),
    }
  }
  
  /**
   * GET /api/app/us-app-auth/location-detail/{locationId}
   * 文档:locationId 为路径参数;需当前用户已绑定该门店。
   */
  export async function usAppFetchLocationDetail(locationId: string): Promise<UsAppLocationDetailOutputDto> {
    const id = (locationId || '').trim()
    return fetchWithOfflineCache('auth', `location-detail:${id}`, async () => {
      const raw = await usAppApiRequest<unknown>({
        path: `/api/app/us-app-auth/location-detail/${encodeURIComponent(id)}`,
        method: 'GET',
        auth: true,
      })
      return normalizeLocationDetail(raw)
    })
  }
  
  /** POST /api/app/us-app-auth/change-password */
  export async function usAppChangePassword(input: UsAppChangePasswordInput): Promise<void> {
    await usAppApiRequest<unknown>({
      path: '/api/app/us-app-auth/change-password',
      method: 'POST',
6faaf539   杨鑫   APP 登录门店对接
169
      auth: true,
699ea6e8   杨鑫   完善打印逻辑
170
171
172
173
174
      data: {
        currentPassword: input.currentPassword,
        newPassword: input.newPassword,
        confirmNewPassword: input.confirmNewPassword,
      },
6faaf539   杨鑫   APP 登录门店对接
175
    })
6faaf539   杨鑫   APP 登录门店对接
176
  }