qing-client 0.0.36 → 0.0.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,6 +8,7 @@ export declare abstract class BaseClient {
8
8
  protected serviceBasePath: string;
9
9
  protected tokenStorage: TokenStorage;
10
10
  protected projectId?: string;
11
+ protected orgId?: string;
11
12
  protected appId?: string;
12
13
  protected userContext?: UserContext;
13
14
  constructor(config: ClientConfig, serviceName: string);
@@ -16,6 +17,8 @@ export declare abstract class BaseClient {
16
17
  setProjectId(projectId: string): this;
17
18
  setAppId(appId: string): this;
18
19
  setProjectAndApp(projectId: string, appId: string): this;
20
+ setOrgId(orgId: string): this;
21
+ setProjectOrgApp(projectId: string, orgId: string, appId: string): this;
19
22
  setTokenStorage(storage: TokenStorage): this;
20
23
  private getBaseUrl;
21
24
  setToken(token: string): Promise<void>;
@@ -27,8 +27,9 @@ class BaseClient {
27
27
  this.serviceBasePath = `/api/${serviceName}`;
28
28
  // 根据模式设置不同的配置
29
29
  if (this.isFrontendMode) {
30
- // 前端模式:使用项目和应用ID
30
+ // 前端模式:使用项目、组织和应用ID
31
31
  this.projectId = config.projectId;
32
+ this.orgId = config.orgId;
32
33
  this.appId = config.appId;
33
34
  }
34
35
  else {
@@ -66,11 +67,19 @@ class BaseClient {
66
67
  }
67
68
  // 后端模式:用户上下文相关方法
68
69
  initUserContext(context) {
69
- return {
70
+ const headers = {
70
71
  'v-user-id': context.userId,
71
72
  'v-user-role': context.role,
72
73
  'v-project-id': context.projectId
73
74
  };
75
+ // 可选字段
76
+ if (context.orgId) {
77
+ headers['v-org-id'] = context.orgId;
78
+ }
79
+ if (context.appId) {
80
+ headers['v-app-id'] = context.appId;
81
+ }
82
+ return headers;
74
83
  }
75
84
  setUserContext(context) {
76
85
  if (!this.isFrontendMode) {
@@ -110,6 +119,26 @@ class BaseClient {
110
119
  }
111
120
  return this;
112
121
  }
122
+ setOrgId(orgId) {
123
+ if (this.isFrontendMode) {
124
+ this.orgId = orgId;
125
+ }
126
+ else {
127
+ console.warn('setOrgId() 仅在前端模式下有效');
128
+ }
129
+ return this;
130
+ }
131
+ setProjectOrgApp(projectId, orgId, appId) {
132
+ if (this.isFrontendMode) {
133
+ this.projectId = projectId;
134
+ this.orgId = orgId;
135
+ this.appId = appId;
136
+ }
137
+ else {
138
+ console.warn('setProjectOrgApp() 仅在前端模式下有效');
139
+ }
140
+ return this;
141
+ }
113
142
  // 设置令牌存储(可在运行时更换存储策略)
114
143
  setTokenStorage(storage) {
115
144
  this.tokenStorage = storage;
@@ -154,12 +183,16 @@ class BaseClient {
154
183
  let headers = options.headers || {};
155
184
  // 根据模式添加不同的头信息
156
185
  if (this.isFrontendMode) {
157
- // 前端模式:添加项目和应用ID头
186
+ // 前端模式:添加项目、组织和应用ID头
158
187
  const projectId = options.projectId || this.projectId;
188
+ const orgId = options.orgId || this.orgId;
159
189
  const appId = options.appId || this.appId;
160
190
  if (projectId) {
161
191
  headers['x-project-id'] = projectId;
162
192
  }
193
+ if (orgId) {
194
+ headers['x-org-id'] = orgId;
195
+ }
163
196
  if (appId) {
164
197
  headers['x-app-id'] = appId;
165
198
  }
@@ -201,12 +234,16 @@ class BaseClient {
201
234
  let headers = options.headers || {};
202
235
  // 根据模式添加不同的头信息
203
236
  if (this.isFrontendMode) {
204
- // 前端模式:添加项目和应用ID头
237
+ // 前端模式:添加项目、组织和应用ID头
205
238
  const projectId = options.projectId || this.projectId;
239
+ const orgId = options.orgId || this.orgId;
206
240
  const appId = options.appId || this.appId;
207
241
  if (projectId) {
208
242
  headers['x-project-id'] = projectId;
209
243
  }
244
+ if (orgId) {
245
+ headers['x-org-id'] = orgId;
246
+ }
210
247
  if (appId) {
211
248
  headers['x-app-id'] = appId;
212
249
  }
@@ -1,10 +1,31 @@
1
1
  import { BaseClient } from "../client/BaseClient";
2
2
  import { ClientConfig, RequestOptions } from "../types";
3
- import { LoginResponse, TemporaryTokenRequest, TemporaryTokenResponse, VerifyTemporaryTokenRequest, VerifyTemporaryTokenResponse, WechatLoginCredentials } from "../types/user";
3
+ import { LoginResponse, TemporaryTokenRequest, TemporaryTokenResponse, VerifyTemporaryTokenRequest, VerifyTemporaryTokenResponse, WechatLoginCredentials, LoginCredentials, RegisterRequest, RegisterResponse } from "../types/user";
4
4
  export declare class AuthService extends BaseClient {
5
5
  constructor(config: ClientConfig);
6
+ /**
7
+ * 用户登录(表单格式,兼容旧版)
8
+ * @deprecated 推荐使用 loginJson 方法
9
+ */
6
10
  login(identifier: string, password: string, projectId?: number, options?: RequestOptions): Promise<LoginResponse>;
11
+ /**
12
+ * 用户登录(JSON格式,支持 org_id 和 app_id)
13
+ * @param credentials 登录凭证(包含 username, password, org_id?, app_id?)
14
+ * @param projectId 项目ID(通过 header 传递)
15
+ */
16
+ loginJson(credentials: LoginCredentials, projectId?: number, options?: RequestOptions): Promise<LoginResponse>;
17
+ /**
18
+ * 微信小程序登录
19
+ * @param credentials 微信登录凭证(包含 code, phone_code?, org_id?, app_id?)
20
+ * @param projectId 项目ID(通过 header 传递)
21
+ */
7
22
  wechatMiniProgramLogin(credentials: WechatLoginCredentials, projectId?: number, options?: RequestOptions): Promise<LoginResponse>;
23
+ /**
24
+ * 用户注册
25
+ * @param data 注册信息(包含 username, email, password, name?, phone?, org_id?, app_id?)
26
+ * @param projectId 项目ID(通过 header 传递)
27
+ */
28
+ register(data: RegisterRequest, projectId?: number, options?: RequestOptions): Promise<RegisterResponse>;
8
29
  createTemporaryToken(request?: TemporaryTokenRequest, options?: RequestOptions): Promise<TemporaryTokenResponse>;
9
30
  verifyTemporaryToken(request: VerifyTemporaryTokenRequest, options?: RequestOptions): Promise<VerifyTemporaryTokenResponse>;
10
31
  logout(token: string, options?: RequestOptions): Promise<void>;
@@ -11,7 +11,10 @@ class AuthService extends BaseClient_1.BaseClient {
11
11
  constructor(config) {
12
12
  super(config, 'auth');
13
13
  }
14
- // 用户登录 - 匹配后端请求格式
14
+ /**
15
+ * 用户登录(表单格式,兼容旧版)
16
+ * @deprecated 推荐使用 loginJson 方法
17
+ */
15
18
  async login(identifier, password, projectId = 0, options) {
16
19
  const body = qs_1.default.stringify({
17
20
  grant_type: "password",
@@ -31,7 +34,28 @@ class AuthService extends BaseClient_1.BaseClient {
31
34
  body: body
32
35
  });
33
36
  }
34
- // 微信小程序登录 - 新增接口
37
+ /**
38
+ * 用户登录(JSON格式,支持 org_id 和 app_id)
39
+ * @param credentials 登录凭证(包含 username, password, org_id?, app_id?)
40
+ * @param projectId 项目ID(通过 header 传递)
41
+ */
42
+ async loginJson(credentials, projectId = 0, options) {
43
+ return this.request('/login', {
44
+ ...options,
45
+ method: 'POST',
46
+ headers: {
47
+ ...options?.headers,
48
+ "Content-Type": "application/json",
49
+ "x-project-id": projectId.toString()
50
+ },
51
+ body: JSON.stringify(credentials)
52
+ });
53
+ }
54
+ /**
55
+ * 微信小程序登录
56
+ * @param credentials 微信登录凭证(包含 code, phone_code?, org_id?, app_id?)
57
+ * @param projectId 项目ID(通过 header 传递)
58
+ */
35
59
  async wechatMiniProgramLogin(credentials, projectId = 0, options) {
36
60
  return this.request('/wxmp/login', {
37
61
  ...options,
@@ -44,6 +68,23 @@ class AuthService extends BaseClient_1.BaseClient {
44
68
  body: JSON.stringify(credentials)
45
69
  });
46
70
  }
71
+ /**
72
+ * 用户注册
73
+ * @param data 注册信息(包含 username, email, password, name?, phone?, org_id?, app_id?)
74
+ * @param projectId 项目ID(通过 header 传递)
75
+ */
76
+ async register(data, projectId = 0, options) {
77
+ return this.request('/register', {
78
+ ...options,
79
+ method: 'POST',
80
+ headers: {
81
+ ...options?.headers,
82
+ "Content-Type": "application/json",
83
+ "x-project-id": projectId.toString()
84
+ },
85
+ body: JSON.stringify(data)
86
+ });
87
+ }
47
88
  // 签发临时令牌 - 新增接口
48
89
  async createTemporaryToken(request = { expires_in: 300 }, options) {
49
90
  return this.request('/temporary-token', {
@@ -35,6 +35,7 @@ export interface ClientConfig {
35
35
  orgServiceUrl?: string;
36
36
  tokenStorage?: TokenStorage;
37
37
  projectId?: string;
38
+ orgId?: string;
38
39
  appId?: string;
39
40
  userContext?: UserContext;
40
41
  }
@@ -42,6 +43,10 @@ export interface UserContext {
42
43
  userId: string;
43
44
  role: 'SUPER_ADMIN' | 'ADMIN' | 'STAFF' | 'USER' | 'SYSTEM';
44
45
  projectId: string;
46
+ /** 组织ID(可选) */
47
+ orgId?: string;
48
+ /** 应用ID(可选) */
49
+ appId?: string;
45
50
  }
46
51
  export interface RequestOptions {
47
52
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
@@ -50,6 +55,7 @@ export interface RequestOptions {
50
55
  body?: any;
51
56
  userContext?: UserContext;
52
57
  projectId?: string;
58
+ orgId?: string;
53
59
  appId?: string;
54
60
  }
55
61
  export * from "./provider";
@@ -12,6 +12,8 @@ export interface User {
12
12
  last_login?: string;
13
13
  created_at: string;
14
14
  updated_at: string;
15
+ /** 用户所属组织ID列表 */
16
+ organizations?: string[];
15
17
  }
16
18
  export interface UserCreateRequest {
17
19
  username: string;
@@ -34,6 +36,10 @@ export interface LoginResponse {
34
36
  token_type: string;
35
37
  expires_at: string;
36
38
  project_id: number;
39
+ /** 组织ID(如果登录时指定) */
40
+ org_id?: string;
41
+ /** 应用ID(如果登录时指定) */
42
+ app_id?: string;
37
43
  }
38
44
  export interface PasswordResetRequest {
39
45
  new_password: string;
@@ -54,7 +60,34 @@ export interface UserOperationResponse {
54
60
  export interface WechatLoginCredentials {
55
61
  code: string;
56
62
  phone_code?: string;
63
+ /** 组织ID(可选,用于选择登录到哪个组织) */
64
+ org_id?: string;
65
+ /** 应用ID(可选,记录来源应用) */
66
+ app_id?: string;
57
67
  }
68
+ /** 登录凭证(JSON格式) */
69
+ export interface LoginCredentials {
70
+ username: string;
71
+ password: string;
72
+ /** 组织ID(可选,用于选择登录到哪个组织) */
73
+ org_id?: string;
74
+ /** 应用ID(可选,记录来源应用) */
75
+ app_id?: string;
76
+ }
77
+ /** 注册请求 */
78
+ export interface RegisterRequest {
79
+ username: string;
80
+ email: string;
81
+ password: string;
82
+ name?: string;
83
+ phone?: string;
84
+ /** 组织ID(可选,注册后关联到此组织) */
85
+ org_id?: string;
86
+ /** 应用ID(可选,记录来源应用) */
87
+ app_id?: string;
88
+ }
89
+ /** 注册响应(与登录响应相同结构) */
90
+ export type RegisterResponse = LoginResponse;
58
91
  export interface TemporaryTokenRequest {
59
92
  expires_in?: number;
60
93
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qing-client",
3
- "version": "0.0.36",
3
+ "version": "0.0.38",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "scripts": {