Blame view

泰额版/Food Labeling Management Code/Yi.Vben5.Vue3/apps/web-antd/src/api/th/abp-unwrap.ts 1.08 KB
91821909   杨鑫   最新
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
  /** 解析 ABP RESTfulResult 并返回 data(与 request.ts 逻辑一致) */
  export function unwrapAbpResponse<T>(body: unknown): T {
    if (!body || typeof body !== 'object') {
      throw new Error('接口返回为空');
    }
    const o = body as Record<string, unknown>;
    const hasWrapper =
      'statusCode' in o ||
      'StatusCode' in o ||
      'succeeded' in o ||
      'Succeeded' in o;
    if (!hasWrapper) {
      return body as T;
    }
    const statusCode = Number(o.statusCode ?? o.StatusCode ?? 0);
    const succeeded = (o.succeeded ?? o.Succeeded) as boolean | undefined;
    if (statusCode === 200 && succeeded !== false) {
      return (o.data ?? o.Data) as T;
    }
    const errors = o.errors ?? o.Errors;
    if (Array.isArray(errors) && errors.length > 0) {
      const msg = errors
        .map((item) => {
          if (item && typeof item === 'object' && 'errorMessage' in item) {
            return String((item as { errorMessage: string }).errorMessage);
          }
          return String(item);
        })
        .join(', ');
      throw new Error(msg || '请求失败');
    }
    throw new Error('请求失败');
  }