gaoluyang
2026-06-24 c0cb161bb52ce0fbdce5c66ec391d107c75e2452
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
import { requestClient } from '#/api/request';
 
/** OAuth2.0 授权信息响应 */
export namespace SystemOAuth2ClientApi {
  /** 授权信息 */
  export interface AuthorizeInfoRespVO {
    client: {
      logo: string;
      name: string;
    };
    scopes: {
      key: string;
      value: boolean;
    }[];
  }
}
 
/** 获得授权信息 */
export function getAuthorize(clientId: string) {
  return requestClient.get<SystemOAuth2ClientApi.AuthorizeInfoRespVO>(
    `/system/oauth2/authorize?clientId=${clientId}`,
  );
}
 
/** 发起授权 */
export function authorize(
  responseType: string,
  clientId: string,
  redirectUri: string,
  state: string,
  autoApprove: boolean,
  checkedScopes: string[],
  uncheckedScopes: string[],
) {
  // 构建 scopes
  const scopes: Record<string, boolean> = {};
  for (const scope of checkedScopes) {
    scopes[scope] = true;
  }
  for (const scope of uncheckedScopes) {
    scopes[scope] = false;
  }
 
  // 发起请求
  return requestClient.post<string>('/system/oauth2/authorize', null, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    params: {
      response_type: responseType,
      client_id: clientId,
      redirect_uri: redirectUri,
      state,
      auto_approve: autoApprove,
      scope: JSON.stringify(scopes),
    },
  });
}