Blame view

泰额版/Food Labeling Management Code/Yi.Vben5.Vue3/apps/web-antd/src/api/th/th-web-auth.ts 1.63 KB
4950e3a0   杨鑫   前端最新修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  export interface ThWebLoginInput {
    tenantId: string;
    userName: string;
    password: string;
    uuid?: string;
    code?: string;
  }
  
  export interface ThWebLoginOutputDto {
    token: string;
    refreshToken: string;
    tenantId: string;
    tenantName: string;
  }
  
  let loginInFlight: Promise<ThWebLoginOutputDto> | null = null;
  
  function normalizeWebLoginOutput(raw: unknown): ThWebLoginOutputDto {
    const o =
      raw && typeof raw === 'object' ? (raw as Record<string, unknown>) : {};
e9fbe796   杨鑫   前端优化 bug
21
    const token = String(o.token ?? o.Token ?? '').trim();
4950e3a0   杨鑫   前端最新修改
22
    return {
e9fbe796   杨鑫   前端优化 bug
23
      token: token.replace(/^Bearer\s+/i, ''),
4950e3a0   杨鑫   前端最新修改
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
      refreshToken: String(o.refreshToken ?? o.RefreshToken ?? ''),
      tenantId: String(o.tenantId ?? o.TenantId ?? ''),
      tenantName: String(o.tenantName ?? o.TenantName ?? ''),
    };
  }
  
  export async function thWebLogin(
    input: ThWebLoginInput,
  ): Promise<ThWebLoginOutputDto> {
    if (loginInFlight) {
      return loginInFlight;
    }
  
    loginInFlight = (async () => {
      const { requestClient } = await import('#/api/request');
      const raw = await requestClient.post<unknown>(
        'th-web-auth/login',
        {
          tenantId: input.tenantId,
          userName: input.userName.trim(),
          password: input.password,
          ...(input.uuid ? { uuid: input.uuid } : {}),
          ...(input.code != null && input.code !== ''
            ? { code: input.code }
            : {}),
        },
        {
          errorMessageMode: 'none',
          headers: {
            __tenant: input.tenantId,
          },
          successMessageMode: 'none',
        },
      );
      return normalizeWebLoginOutput(raw);
    })().finally(() => {
      loginInFlight = null;
    });
  
    return loginInFlight;
  }