Blame view

泰额版/Food Labeling Management App UniApp/src/services/usAppAuth.ts 5.76 KB
59e51671   “wangming”   1
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
169
170
171
172
173
174
175
176
  import type { UsAppBoundLocationDto } from '../types/usAppBound'
  import { usAppApiRequest } from '../utils/usAppApiRequest'
  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
  }
  
  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[]> {
    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',
      auth: true,
      data: {
        currentPassword: input.currentPassword,
        newPassword: input.newPassword,
        confirmNewPassword: input.confirmNewPassword,
      },
    })
  }