Blame view

泰额版/Food Labeling Management App UniApp/src/services/labelMultipleOption.ts 1.58 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
  import { unwrapApiPayload, usAppApiRequest } from '../utils/usAppApiRequest'
  import { fetchWithOfflineCache } from '../utils/sqliteSync'
  
  function parseOptionValuesJson(raw: unknown): string[] {
    if (raw == null) return []
    if (Array.isArray(raw)) return raw.map((x) => String(x))
    if (typeof raw === 'string') {
      const t = raw.trim()
      if (!t) return []
      try {
        const p = JSON.parse(t) as unknown
        if (Array.isArray(p)) return p.map((x) => String(x))
      } catch {
        return []
      }
    }
    return []
  }
  
  export type LabelMultipleOptionDetail = {
    id: string
    optionName: string
    optionCode: string
    optionValuesJson: string[]
  }
  
  /**
   * GET /api/app/label-multiple-option/{id}(与 Web 管理端一致)
   */
  export async function fetchLabelMultipleOptionById(id: string): Promise<LabelMultipleOptionDetail | null> {
    const tid = String(id ?? '').trim()
    if (!tid) return null
    return fetchWithOfflineCache('labeling', `multiple-option:${tid}`, async () => {
      const raw = await usAppApiRequest<unknown>({
        path: `/api/app/label-multiple-option/${encodeURIComponent(tid)}`,
        method: 'GET',
        auth: true,
      })
      const d = unwrapApiPayload(raw) as Record<string, unknown>
      if (!d || typeof d !== 'object') return null
      const oid = String(d.id ?? d.Id ?? tid)
      const optionValuesJson = parseOptionValuesJson(d.optionValuesJson ?? d.OptionValuesJson)
      return {
        id: oid,
        optionName: String(d.optionName ?? d.OptionName ?? '').trim() || oid,
        optionCode: String(d.optionCode ?? d.OptionCode ?? '').trim(),
        optionValuesJson,
      }
    })
  }