Blame view

泰额版/Food Labeling Management App UniApp/src/services/labelCategory.ts 1.54 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
  import type { LabelCategoryListItemDto } from '../types/platformCategories'
  import { usAppApiRequest } from '../utils/usAppApiRequest'
  import { extractPagedItems } from '../utils/pagedList'
  import { fetchWithOfflineCache } from '../utils/sqliteSync'
  
  export type LabelCategoryListQuery = {
    skipCount?: number
    maxResultCount?: number
    sorting?: string
    keyword?: string
    state?: boolean
  }
  
  function buildQuery(q: LabelCategoryListQuery): string {
    const p = new URLSearchParams()
    /** 第一页为 1(页码语义,非 offset) */
    p.set('skipCount', String(q.skipCount ?? 1))
    p.set('maxResultCount', String(q.maxResultCount ?? 50))
    if (q.sorting != null && String(q.sorting).trim() !== '') {
      p.set('sorting', String(q.sorting).trim())
    }
    if (q.keyword != null && String(q.keyword).trim() !== '') {
      p.set('keyword', String(q.keyword).trim())
    }
    if (q.state === true) {
      p.set('state', 'true')
    }
    return p.toString()
  }
  
  /** GET /api/app/label-category — 《标签模块接口对接说明(6).md》接口 1.1 */
  export async function fetchLabelCategoryPage(
    query: LabelCategoryListQuery = {}
  ): Promise<{ items: LabelCategoryListItemDto[]; totalCount: number }> {
    const qs = buildQuery(query)
    return fetchWithOfflineCache('categories', `label:${qs}`, async () => {
      const body = await usAppApiRequest<unknown>({
        path: `/api/app/label-category?${qs}`,
        method: 'GET',
        auth: true,
      })
      const { items, totalCount } = extractPagedItems<LabelCategoryListItemDto>(body)
      return { items, totalCount }
    })
  }