@mu-cabin/opms-permission 0.9.12 → 0.9.13

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.
package/dist/index.cjs CHANGED
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
+ ApiClient: () => ApiClient,
33
34
  DataHandler: () => DataHandler,
34
35
  EnumOrgQueryMode: () => EnumOrgQueryMode,
35
36
  EnumOrgQueryScope: () => EnumOrgQueryScope,
@@ -40,45 +41,16 @@ __export(index_exports, {
40
41
  USER_INFO_KEY: () => USER_INFO_KEY,
41
42
  USER_ORG_KEY: () => USER_ORG_KEY,
42
43
  USER_TOTAL_COMPANY_KEY: () => USER_TOTAL_COMPANY_KEY,
43
- getOrgTree: () => getOrgTree,
44
- getUserInfo: () => getUserInfo,
45
- getUserOrgTree: () => getUserOrgTree,
46
44
  jumpToSSOLogin: () => jumpToSSOLogin,
47
- jumpToSSOLogout: () => jumpToSSOLogout,
48
- login: () => login,
49
- logout: () => logout,
50
- queryOrgCompanies: () => queryOrgCompanies,
51
- queryOrgCustom: () => queryOrgCustom,
52
- queryResource: () => queryResource,
53
- superLogin: () => superLogin
45
+ jumpToSSOLogout: () => jumpToSSOLogout
54
46
  });
55
47
  module.exports = __toCommonJS(index_exports);
56
48
 
57
49
  // src/utils/storage.ts
58
50
  var Storage = class {
59
- constructor() {
60
- this.currentVersion = "1.0.0";
61
- }
62
- setSystemId(systemId) {
51
+ constructor(systemId, currentVersion) {
63
52
  this.systemId = systemId;
64
- }
65
- /**
66
- * Set the current version for new data
67
- * If version changes, all existing data for this systemId will be cleared
68
- */
69
- setVersion(version) {
70
- if (this.currentVersion !== version) {
71
- this.clear();
72
- this.currentVersion = version;
73
- } else {
74
- this.currentVersion = version;
75
- }
76
- }
77
- /**
78
- * Get the current version
79
- */
80
- getVersion() {
81
- return this.currentVersion;
53
+ this.currentVersion = currentVersion;
82
54
  }
83
55
  prefixKey(key) {
84
56
  return this.systemId !== void 0 ? `systemId-${this.systemId}-${key}` : key;
@@ -166,7 +138,6 @@ var Storage = class {
166
138
  };
167
139
  }
168
140
  };
169
- var storage = new Storage();
170
141
 
171
142
  // src/utils/dataHandler.ts
172
143
  var getCompletePath = (item, hasSubApp) => {
@@ -317,9 +288,6 @@ function handlePermissionTree(data, key) {
317
288
  };
318
289
  }
319
290
 
320
- // src/api.ts
321
- var import_axios = __toESM(require("axios"));
322
-
323
291
  // src/config.ts
324
292
  var USER_INFO_KEY = "opms_user_info";
325
293
  var RESOURCE_KEY = "opms_resources";
@@ -327,98 +295,6 @@ var TOKEN_KEY = "opms_authorization";
327
295
  var USER_TOTAL_COMPANY_KEY = "opms_user_total_company";
328
296
  var USER_ORG_KEY = "opms_user_orgs";
329
297
 
330
- // src/api.ts
331
- var axiosClient = import_axios.default.create();
332
- axiosClient.interceptors.request.use((config) => {
333
- const token = storage.getItem(TOKEN_KEY);
334
- if (token) {
335
- config.headers.Authorization = token;
336
- }
337
- return config;
338
- });
339
- axiosClient.interceptors.response.use(
340
- (response) => {
341
- const res = response.data;
342
- const { success, code, msg } = res;
343
- if (success === false) {
344
- return Promise.reject({
345
- code,
346
- msg
347
- });
348
- }
349
- return res;
350
- },
351
- (error) => {
352
- return Promise.reject(error);
353
- }
354
- );
355
- var EnumOrgQueryMode = /* @__PURE__ */ ((EnumOrgQueryMode2) => {
356
- EnumOrgQueryMode2["BY_ROLES"] = "BY_ROLES";
357
- EnumOrgQueryMode2["BY_DEPT"] = "BY_DEPT";
358
- return EnumOrgQueryMode2;
359
- })(EnumOrgQueryMode || {});
360
- var EnumOrgQueryScope = /* @__PURE__ */ ((EnumOrgQueryScope2) => {
361
- EnumOrgQueryScope2["ONLY_BRANCH"] = "ONLY_BRANCH";
362
- EnumOrgQueryScope2["STEWARD_UNIT"] = "STEWARD_UNIT";
363
- EnumOrgQueryScope2["WITH_DEPARTMENT"] = "WITH_DEPARTMENT";
364
- return EnumOrgQueryScope2;
365
- })(EnumOrgQueryScope || {});
366
- async function login(baseUrl, authorizationCode) {
367
- return await axiosClient.get(
368
- `${baseUrl}/opmsDefaultAuth/oauthLogin`,
369
- {
370
- params: { authorizationCode }
371
- }
372
- );
373
- }
374
- async function superLogin(baseUrl, params) {
375
- return await axiosClient.post(
376
- `${baseUrl}/opmsDefaultAuth/superLogin`,
377
- params
378
- );
379
- }
380
- async function logout(baseUrl) {
381
- return await axiosClient.post(
382
- `${baseUrl}/opmsDefaultAuth/logout`
383
- );
384
- }
385
- async function getUserInfo(baseUrl) {
386
- return await axiosClient.post(
387
- `${baseUrl}/opmsDefaultUser/userInfo`,
388
- {}
389
- );
390
- }
391
- async function queryResource(baseUrl, params) {
392
- return await axiosClient.post(
393
- `${baseUrl}/opmsDefaultUser/userResource`,
394
- params
395
- );
396
- }
397
- async function getOrgTree(baseUrl, params) {
398
- return await axiosClient.post(
399
- `${baseUrl}/opmsDefaultUser/orgTree`,
400
- params
401
- );
402
- }
403
- async function getUserOrgTree(baseUrl, params) {
404
- return await axiosClient.post(
405
- `${baseUrl}/opmsDefaultUser/userOrgTree`,
406
- params
407
- );
408
- }
409
- async function queryOrgCompanies(baseUrl, params) {
410
- return await axiosClient.post(
411
- `${baseUrl}/opmsDefaultUser/branches`,
412
- params
413
- );
414
- }
415
- function queryOrgCustom(baseUrl, params) {
416
- return import_axios.default.post(
417
- `${baseUrl}/opmsDefaultUser/queryOrgCustom`,
418
- params
419
- );
420
- }
421
-
422
298
  // src/utils/eventEmitter.ts
423
299
  var EventEmitter = class {
424
300
  constructor(allowedEvents) {
@@ -517,6 +393,98 @@ var EventEmitter = class {
517
393
  }
518
394
  };
519
395
 
396
+ // src/api.ts
397
+ var import_axios = __toESM(require("axios"));
398
+ var ApiClient = class {
399
+ constructor(baseUrl, storage) {
400
+ this.baseUrl = baseUrl;
401
+ this.storage = storage;
402
+ this.axiosClient = import_axios.default.create();
403
+ this.axiosClient.interceptors.request.use((config) => {
404
+ const token = this.storage.getItem(TOKEN_KEY);
405
+ if (token) {
406
+ config.headers.Authorization = token;
407
+ }
408
+ return config;
409
+ });
410
+ this.axiosClient.interceptors.response.use(
411
+ (response) => {
412
+ const res = response.data;
413
+ const { success, code, msg } = res;
414
+ if (success === false) {
415
+ return Promise.reject({ code, msg });
416
+ }
417
+ return res;
418
+ },
419
+ (error) => Promise.reject(error)
420
+ );
421
+ }
422
+ async login(authorizationCode) {
423
+ return await this.axiosClient.get(
424
+ `${this.baseUrl}/opmsDefaultAuth/oauthLogin`,
425
+ { params: { authorizationCode } }
426
+ );
427
+ }
428
+ async superLogin(params) {
429
+ return await this.axiosClient.post(
430
+ `${this.baseUrl}/opmsDefaultAuth/superLogin`,
431
+ params
432
+ );
433
+ }
434
+ async logout() {
435
+ return await this.axiosClient.post(
436
+ `${this.baseUrl}/opmsDefaultAuth/logout`
437
+ );
438
+ }
439
+ async getUserInfo() {
440
+ return await this.axiosClient.post(
441
+ `${this.baseUrl}/opmsDefaultUser/userInfo`,
442
+ {}
443
+ );
444
+ }
445
+ async queryResource(params) {
446
+ return await this.axiosClient.post(
447
+ `${this.baseUrl}/opmsDefaultUser/userResource`,
448
+ params
449
+ );
450
+ }
451
+ async getOrgTree(params) {
452
+ return await this.axiosClient.post(
453
+ `${this.baseUrl}/opmsDefaultUser/orgTree`,
454
+ params
455
+ );
456
+ }
457
+ async getUserOrgTree(params) {
458
+ return await this.axiosClient.post(
459
+ `${this.baseUrl}/opmsDefaultUser/userOrgTree`,
460
+ params
461
+ );
462
+ }
463
+ async queryOrgCompanies(params) {
464
+ return await this.axiosClient.post(
465
+ `${this.baseUrl}/opmsDefaultUser/branches`,
466
+ params
467
+ );
468
+ }
469
+ async queryOrgCustom(params) {
470
+ return import_axios.default.post(
471
+ `${this.baseUrl}/opmsDefaultUser/queryOrgCustom`,
472
+ params
473
+ );
474
+ }
475
+ };
476
+ var EnumOrgQueryMode = /* @__PURE__ */ ((EnumOrgQueryMode2) => {
477
+ EnumOrgQueryMode2["BY_ROLES"] = "BY_ROLES";
478
+ EnumOrgQueryMode2["BY_DEPT"] = "BY_DEPT";
479
+ return EnumOrgQueryMode2;
480
+ })(EnumOrgQueryMode || {});
481
+ var EnumOrgQueryScope = /* @__PURE__ */ ((EnumOrgQueryScope2) => {
482
+ EnumOrgQueryScope2["ONLY_BRANCH"] = "ONLY_BRANCH";
483
+ EnumOrgQueryScope2["STEWARD_UNIT"] = "STEWARD_UNIT";
484
+ EnumOrgQueryScope2["WITH_DEPARTMENT"] = "WITH_DEPARTMENT";
485
+ return EnumOrgQueryScope2;
486
+ })(EnumOrgQueryScope || {});
487
+
520
488
  // src/permission.ts
521
489
  var DataHandler = {
522
490
  iterateOrgTree(orgTree) {
@@ -592,9 +560,8 @@ var Permission = class {
592
560
  this.baseUrl = options.baseUrl;
593
561
  this.systemId = options.systemId;
594
562
  this.hasSubApp = options.hasSubApp ?? false;
595
- this.storage = new Storage();
596
- this.storage.setSystemId(this.systemId);
597
- this.storage.setVersion("1.0.0");
563
+ this.storage = new Storage(this.systemId, "1.0.0");
564
+ this.api = new ApiClient(this.baseUrl, this.storage);
598
565
  }
599
566
  /**
600
567
  * Add event listener
@@ -644,7 +611,7 @@ var Permission = class {
644
611
  }
645
612
  async superLogin(userName, password) {
646
613
  this.clear();
647
- const { obj, msg, code } = await superLogin(this.baseUrl, {
614
+ const { obj, msg, code } = await this.api.superLogin({
648
615
  userName,
649
616
  password
650
617
  });
@@ -675,7 +642,7 @@ var Permission = class {
675
642
  obj,
676
643
  msg,
677
644
  code: loginCode
678
- } = await login(this.baseUrl, authCode);
645
+ } = await this.api.login(authCode);
679
646
  if (loginCode !== 200) {
680
647
  return Promise.reject({
681
648
  code: loginCode,
@@ -692,7 +659,7 @@ var Permission = class {
692
659
  * Logout and clear userInfo
693
660
  */
694
661
  async logout(clearData = true) {
695
- await logout(this.baseUrl);
662
+ await this.api.logout();
696
663
  clearData && this.clear();
697
664
  }
698
665
  clear() {
@@ -700,7 +667,7 @@ var Permission = class {
700
667
  this.emit("tokenChange", "");
701
668
  }
702
669
  async getUserInfo() {
703
- const data = await getUserInfo(this.baseUrl);
670
+ const data = await this.api.getUserInfo();
704
671
  const { obj, success, msg, code } = data;
705
672
  this.storage.setItem(USER_INFO_KEY, obj);
706
673
  return obj;
@@ -715,7 +682,7 @@ var Permission = class {
715
682
  async getResources() {
716
683
  let resources = this.storage.getItem(RESOURCE_KEY) || null;
717
684
  if (!resources) {
718
- const { obj, success, msg, code } = await queryResource(this.baseUrl, {
685
+ const { obj, success, msg, code } = await this.api.queryResource({
719
686
  systemId: this.systemId
720
687
  });
721
688
  if (!success) {
@@ -773,7 +740,7 @@ var Permission = class {
773
740
  orgTreeData = this.storage.getItem(cacheKey) || null;
774
741
  }
775
742
  if (!orgTreeData) {
776
- const res = await getUserOrgTree(this.baseUrl, params);
743
+ const res = await this.api.getUserOrgTree(params);
777
744
  const { obj } = res;
778
745
  orgTreeData = iterateNestedArray(obj, (item) => {
779
746
  return {
@@ -795,8 +762,6 @@ var Permission = class {
795
762
  const { tree } = data;
796
763
  return {
797
764
  orgTree: tree
798
- // orgNoAuthMap: noAuthMap,
799
- // authOrgTree: tree,
800
765
  };
801
766
  } catch (error) {
802
767
  console.log(error);
@@ -813,7 +778,7 @@ var Permission = class {
813
778
  orgCompanyList = this.storage.getItem(USER_TOTAL_COMPANY_KEY);
814
779
  }
815
780
  if (!orgCompanyList) {
816
- const { obj } = await queryOrgCustom(this.baseUrl, {
781
+ const { obj } = await this.api.queryOrgCustom({
817
782
  resultView: "LIST",
818
783
  orgCodeSource: "COS",
819
784
  direction: "FROM_SELF_TO_LEAF",
@@ -851,24 +816,6 @@ var Permission = class {
851
816
  this.storage.setItem(TOKEN_KEY, token);
852
817
  this.emit("tokenChange", token);
853
818
  }
854
- // --- Getters ---
855
- // get userInfo(): UserInfo | null {
856
- // if (!this._userInfo) {
857
- // this._userInfo = storage.getItem(USER_INFO_KEY);
858
- // }
859
- // if (!this._userInfo) return null;
860
- // const { account, name, ehrId, crewCode, crewId } = this._userInfo;
861
- // return { account, name, ehrId, crewCode, crewId };
862
- // }
863
- // get hasRootAuth() {
864
- // const orgs = this._userInfo?.organizations || [];
865
- // const first = orgs[0];
866
- // if (!first) return false;
867
- // return first.searchPath === '#1';
868
- // }
869
- // get userOrganizations(): UserOrganization[] {
870
- // return this._userInfo?.userOrganizations || [];
871
- // }
872
819
  /**
873
820
  * Get storage information for debugging and monitoring
874
821
  */
@@ -899,7 +846,7 @@ var Permission = class {
899
846
  * Get current storage version
900
847
  */
901
848
  getStorageVersion() {
902
- return this.storage.getVersion();
849
+ return this.storage.currentVersion;
903
850
  }
904
851
  /**
905
852
  * Clear all storage data for this system
@@ -980,6 +927,7 @@ function jumpToSSOLogout({
980
927
  }
981
928
  // Annotate the CommonJS export names for ESM import in node:
982
929
  0 && (module.exports = {
930
+ ApiClient,
983
931
  DataHandler,
984
932
  EnumOrgQueryMode,
985
933
  EnumOrgQueryScope,
@@ -990,15 +938,6 @@ function jumpToSSOLogout({
990
938
  USER_INFO_KEY,
991
939
  USER_ORG_KEY,
992
940
  USER_TOTAL_COMPANY_KEY,
993
- getOrgTree,
994
- getUserInfo,
995
- getUserOrgTree,
996
941
  jumpToSSOLogin,
997
- jumpToSSOLogout,
998
- login,
999
- logout,
1000
- queryOrgCompanies,
1001
- queryOrgCustom,
1002
- queryResource,
1003
- superLogin
942
+ jumpToSSOLogout
1004
943
  });
package/dist/index.d.mts CHANGED
@@ -1,16 +1,7 @@
1
1
  declare class Storage {
2
- private systemId?;
2
+ private systemId;
3
3
  private currentVersion;
4
- setSystemId(systemId: number | string): void;
5
- /**
6
- * Set the current version for new data
7
- * If version changes, all existing data for this systemId will be cleared
8
- */
9
- setVersion(version: string): void;
10
- /**
11
- * Get the current version
12
- */
13
- getVersion(): string;
4
+ constructor(systemId: number | string, currentVersion: string);
14
5
  private prefixKey;
15
6
  /**
16
7
  * Set an item in localStorage, with optional expiration in minutes
@@ -48,6 +39,35 @@ declare class Storage {
48
39
  };
49
40
  }
50
41
 
42
+ declare class ApiClient {
43
+ private axiosClient;
44
+ private baseUrl;
45
+ private storage;
46
+ constructor(baseUrl: string, storage: Storage);
47
+ login(authorizationCode: string): Promise<ApiResponse<{
48
+ token: string;
49
+ }>>;
50
+ superLogin(params: {
51
+ userName: string;
52
+ password: string;
53
+ }): Promise<ApiResponse<{
54
+ token: string;
55
+ }>>;
56
+ logout(): Promise<ApiResponse<void>>;
57
+ getUserInfo(): Promise<ApiResponse<UserInfo$1>>;
58
+ queryResource(params: {
59
+ systemId: string;
60
+ }): Promise<ApiResponse<Resource[]>>;
61
+ getOrgTree(params: {
62
+ systemId: string;
63
+ name: CodeSourceType;
64
+ }): Promise<ApiResponse<OrgRecord$1>>;
65
+ getUserOrgTree(params: UserOrgTreeParams): Promise<ApiResponse<OrgRecord$1[]>>;
66
+ queryOrgCompanies(params: {
67
+ queryAllBranches?: boolean;
68
+ }): Promise<ApiResponse<OrgRecord$1[]>>;
69
+ queryOrgCustom(params: QueryOrgCustomParams): Promise<ApiResponse<OrgRecord$1[]>>;
70
+ }
51
71
  interface ApiResponse<T> {
52
72
  obj: T;
53
73
  success: boolean;
@@ -148,28 +168,6 @@ interface UserOrgTreeParams {
148
168
  stewardUnitLevel?: number;
149
169
  keepHead?: boolean;
150
170
  }
151
- declare function login(baseUrl: string, authorizationCode: string): Promise<ApiResponse<{
152
- token: string;
153
- }>>;
154
- declare function superLogin(baseUrl: string, params: {
155
- userName: string;
156
- password: string;
157
- }): Promise<ApiResponse<{
158
- token: string;
159
- }>>;
160
- declare function logout(baseUrl: string): Promise<ApiResponse<void>>;
161
- declare function getUserInfo(baseUrl: string): Promise<ApiResponse<UserInfo$1>>;
162
- declare function queryResource(baseUrl: string, params: {
163
- systemId: string;
164
- }): Promise<ApiResponse<Resource[]>>;
165
- declare function getOrgTree(baseUrl: string, params: {
166
- systemId: string;
167
- name: CodeSourceType;
168
- }): Promise<ApiResponse<OrgRecord$1>>;
169
- declare function getUserOrgTree(baseUrl: string, params: UserOrgTreeParams): Promise<ApiResponse<OrgRecord$1[]>>;
170
- declare function queryOrgCompanies(baseUrl: string, params: {
171
- queryAllBranches?: boolean;
172
- }): Promise<ApiResponse<OrgRecord$1[]>>;
173
171
  type ResultViewType = 'LIST' | 'TREE' | 'FOREST';
174
172
  type OrgDirectionType = 'NONE' | 'FROM_SELF_TO_ROOT' | 'FROM_SELF_TO_LEAF' | 'TWO_WAY';
175
173
  type OrgType = 'HEAD' | 'BRANCH' | 'DEPARTMENT';
@@ -181,10 +179,6 @@ interface QueryOrgCustomParams {
181
179
  maxDepth?: number;
182
180
  orgTypes?: OrgType[];
183
181
  }
184
- /**
185
- * 根据查询条件自定义组织机构查询
186
- */
187
- declare function queryOrgCustom(baseUrl: string, params: QueryOrgCustomParams): Promise<ApiResponse<OrgRecord$1[]>>;
188
182
 
189
183
  declare const USER_INFO_KEY = "opms_user_info";
190
184
  declare const RESOURCE_KEY = "opms_resources";
@@ -374,6 +368,7 @@ declare class Permission {
374
368
  };
375
369
  hasSubApp: boolean;
376
370
  storage: Storage;
371
+ private api;
377
372
  private eventEmitter;
378
373
  constructor(options: PermissionOptions);
379
374
  /**
@@ -508,4 +503,4 @@ declare function jumpToSSOLogout({ baseUrl, redirectToUrl, clientId, }: {
508
503
  clientId?: string;
509
504
  }): void;
510
505
 
511
- export { type ApiResponse, type ChildOrganization, type CodeSourceType, DataHandler, EnumOrgQueryMode, EnumOrgQueryScope, EventEmitter, type MenuItem, type OpenIndicatorType, Permission as OpmsPermission, type Option, type OrgDirectionType, type OrgRecord, type OrgTreeItem, type OrgType, type PermissionEventListener, type PermissionEventMap, type PermissionEventType, type QueryOrgCustomParams, RESOURCE_KEY, type Resource, type ResourceType, type ResultViewType, TOKEN_KEY, USER_INFO_KEY, USER_ORG_KEY, USER_TOTAL_COMPANY_KEY, type UserInfo, type UserOrgTreeParams, type UserOrganization, type UserRole, getOrgTree, getUserInfo, getUserOrgTree, jumpToSSOLogin, jumpToSSOLogout, login, logout, queryOrgCompanies, queryOrgCustom, queryResource, superLogin };
506
+ export { ApiClient, type ApiResponse, type ChildOrganization, type CodeSourceType, DataHandler, EnumOrgQueryMode, EnumOrgQueryScope, EventEmitter, type MenuItem, type OpenIndicatorType, Permission as OpmsPermission, type Option, type OrgDirectionType, type OrgRecord, type OrgTreeItem, type OrgType, type PermissionEventListener, type PermissionEventMap, type PermissionEventType, type QueryOrgCustomParams, RESOURCE_KEY, type Resource, type ResourceType, type ResultViewType, TOKEN_KEY, USER_INFO_KEY, USER_ORG_KEY, USER_TOTAL_COMPANY_KEY, type UserInfo, type UserOrgTreeParams, type UserOrganization, type UserRole, jumpToSSOLogin, jumpToSSOLogout };
package/dist/index.d.ts CHANGED
@@ -1,16 +1,7 @@
1
1
  declare class Storage {
2
- private systemId?;
2
+ private systemId;
3
3
  private currentVersion;
4
- setSystemId(systemId: number | string): void;
5
- /**
6
- * Set the current version for new data
7
- * If version changes, all existing data for this systemId will be cleared
8
- */
9
- setVersion(version: string): void;
10
- /**
11
- * Get the current version
12
- */
13
- getVersion(): string;
4
+ constructor(systemId: number | string, currentVersion: string);
14
5
  private prefixKey;
15
6
  /**
16
7
  * Set an item in localStorage, with optional expiration in minutes
@@ -48,6 +39,35 @@ declare class Storage {
48
39
  };
49
40
  }
50
41
 
42
+ declare class ApiClient {
43
+ private axiosClient;
44
+ private baseUrl;
45
+ private storage;
46
+ constructor(baseUrl: string, storage: Storage);
47
+ login(authorizationCode: string): Promise<ApiResponse<{
48
+ token: string;
49
+ }>>;
50
+ superLogin(params: {
51
+ userName: string;
52
+ password: string;
53
+ }): Promise<ApiResponse<{
54
+ token: string;
55
+ }>>;
56
+ logout(): Promise<ApiResponse<void>>;
57
+ getUserInfo(): Promise<ApiResponse<UserInfo$1>>;
58
+ queryResource(params: {
59
+ systemId: string;
60
+ }): Promise<ApiResponse<Resource[]>>;
61
+ getOrgTree(params: {
62
+ systemId: string;
63
+ name: CodeSourceType;
64
+ }): Promise<ApiResponse<OrgRecord$1>>;
65
+ getUserOrgTree(params: UserOrgTreeParams): Promise<ApiResponse<OrgRecord$1[]>>;
66
+ queryOrgCompanies(params: {
67
+ queryAllBranches?: boolean;
68
+ }): Promise<ApiResponse<OrgRecord$1[]>>;
69
+ queryOrgCustom(params: QueryOrgCustomParams): Promise<ApiResponse<OrgRecord$1[]>>;
70
+ }
51
71
  interface ApiResponse<T> {
52
72
  obj: T;
53
73
  success: boolean;
@@ -148,28 +168,6 @@ interface UserOrgTreeParams {
148
168
  stewardUnitLevel?: number;
149
169
  keepHead?: boolean;
150
170
  }
151
- declare function login(baseUrl: string, authorizationCode: string): Promise<ApiResponse<{
152
- token: string;
153
- }>>;
154
- declare function superLogin(baseUrl: string, params: {
155
- userName: string;
156
- password: string;
157
- }): Promise<ApiResponse<{
158
- token: string;
159
- }>>;
160
- declare function logout(baseUrl: string): Promise<ApiResponse<void>>;
161
- declare function getUserInfo(baseUrl: string): Promise<ApiResponse<UserInfo$1>>;
162
- declare function queryResource(baseUrl: string, params: {
163
- systemId: string;
164
- }): Promise<ApiResponse<Resource[]>>;
165
- declare function getOrgTree(baseUrl: string, params: {
166
- systemId: string;
167
- name: CodeSourceType;
168
- }): Promise<ApiResponse<OrgRecord$1>>;
169
- declare function getUserOrgTree(baseUrl: string, params: UserOrgTreeParams): Promise<ApiResponse<OrgRecord$1[]>>;
170
- declare function queryOrgCompanies(baseUrl: string, params: {
171
- queryAllBranches?: boolean;
172
- }): Promise<ApiResponse<OrgRecord$1[]>>;
173
171
  type ResultViewType = 'LIST' | 'TREE' | 'FOREST';
174
172
  type OrgDirectionType = 'NONE' | 'FROM_SELF_TO_ROOT' | 'FROM_SELF_TO_LEAF' | 'TWO_WAY';
175
173
  type OrgType = 'HEAD' | 'BRANCH' | 'DEPARTMENT';
@@ -181,10 +179,6 @@ interface QueryOrgCustomParams {
181
179
  maxDepth?: number;
182
180
  orgTypes?: OrgType[];
183
181
  }
184
- /**
185
- * 根据查询条件自定义组织机构查询
186
- */
187
- declare function queryOrgCustom(baseUrl: string, params: QueryOrgCustomParams): Promise<ApiResponse<OrgRecord$1[]>>;
188
182
 
189
183
  declare const USER_INFO_KEY = "opms_user_info";
190
184
  declare const RESOURCE_KEY = "opms_resources";
@@ -374,6 +368,7 @@ declare class Permission {
374
368
  };
375
369
  hasSubApp: boolean;
376
370
  storage: Storage;
371
+ private api;
377
372
  private eventEmitter;
378
373
  constructor(options: PermissionOptions);
379
374
  /**
@@ -508,4 +503,4 @@ declare function jumpToSSOLogout({ baseUrl, redirectToUrl, clientId, }: {
508
503
  clientId?: string;
509
504
  }): void;
510
505
 
511
- export { type ApiResponse, type ChildOrganization, type CodeSourceType, DataHandler, EnumOrgQueryMode, EnumOrgQueryScope, EventEmitter, type MenuItem, type OpenIndicatorType, Permission as OpmsPermission, type Option, type OrgDirectionType, type OrgRecord, type OrgTreeItem, type OrgType, type PermissionEventListener, type PermissionEventMap, type PermissionEventType, type QueryOrgCustomParams, RESOURCE_KEY, type Resource, type ResourceType, type ResultViewType, TOKEN_KEY, USER_INFO_KEY, USER_ORG_KEY, USER_TOTAL_COMPANY_KEY, type UserInfo, type UserOrgTreeParams, type UserOrganization, type UserRole, getOrgTree, getUserInfo, getUserOrgTree, jumpToSSOLogin, jumpToSSOLogout, login, logout, queryOrgCompanies, queryOrgCustom, queryResource, superLogin };
506
+ export { ApiClient, type ApiResponse, type ChildOrganization, type CodeSourceType, DataHandler, EnumOrgQueryMode, EnumOrgQueryScope, EventEmitter, type MenuItem, type OpenIndicatorType, Permission as OpmsPermission, type Option, type OrgDirectionType, type OrgRecord, type OrgTreeItem, type OrgType, type PermissionEventListener, type PermissionEventMap, type PermissionEventType, type QueryOrgCustomParams, RESOURCE_KEY, type Resource, type ResourceType, type ResultViewType, TOKEN_KEY, USER_INFO_KEY, USER_ORG_KEY, USER_TOTAL_COMPANY_KEY, type UserInfo, type UserOrgTreeParams, type UserOrganization, type UserRole, jumpToSSOLogin, jumpToSSOLogout };
package/dist/index.mjs CHANGED
@@ -1,28 +1,8 @@
1
1
  // src/utils/storage.ts
2
2
  var Storage = class {
3
- constructor() {
4
- this.currentVersion = "1.0.0";
5
- }
6
- setSystemId(systemId) {
3
+ constructor(systemId, currentVersion) {
7
4
  this.systemId = systemId;
8
- }
9
- /**
10
- * Set the current version for new data
11
- * If version changes, all existing data for this systemId will be cleared
12
- */
13
- setVersion(version) {
14
- if (this.currentVersion !== version) {
15
- this.clear();
16
- this.currentVersion = version;
17
- } else {
18
- this.currentVersion = version;
19
- }
20
- }
21
- /**
22
- * Get the current version
23
- */
24
- getVersion() {
25
- return this.currentVersion;
5
+ this.currentVersion = currentVersion;
26
6
  }
27
7
  prefixKey(key) {
28
8
  return this.systemId !== void 0 ? `systemId-${this.systemId}-${key}` : key;
@@ -110,7 +90,6 @@ var Storage = class {
110
90
  };
111
91
  }
112
92
  };
113
- var storage = new Storage();
114
93
 
115
94
  // src/utils/dataHandler.ts
116
95
  var getCompletePath = (item, hasSubApp) => {
@@ -261,9 +240,6 @@ function handlePermissionTree(data, key) {
261
240
  };
262
241
  }
263
242
 
264
- // src/api.ts
265
- import axios from "axios";
266
-
267
243
  // src/config.ts
268
244
  var USER_INFO_KEY = "opms_user_info";
269
245
  var RESOURCE_KEY = "opms_resources";
@@ -271,98 +247,6 @@ var TOKEN_KEY = "opms_authorization";
271
247
  var USER_TOTAL_COMPANY_KEY = "opms_user_total_company";
272
248
  var USER_ORG_KEY = "opms_user_orgs";
273
249
 
274
- // src/api.ts
275
- var axiosClient = axios.create();
276
- axiosClient.interceptors.request.use((config) => {
277
- const token = storage.getItem(TOKEN_KEY);
278
- if (token) {
279
- config.headers.Authorization = token;
280
- }
281
- return config;
282
- });
283
- axiosClient.interceptors.response.use(
284
- (response) => {
285
- const res = response.data;
286
- const { success, code, msg } = res;
287
- if (success === false) {
288
- return Promise.reject({
289
- code,
290
- msg
291
- });
292
- }
293
- return res;
294
- },
295
- (error) => {
296
- return Promise.reject(error);
297
- }
298
- );
299
- var EnumOrgQueryMode = /* @__PURE__ */ ((EnumOrgQueryMode2) => {
300
- EnumOrgQueryMode2["BY_ROLES"] = "BY_ROLES";
301
- EnumOrgQueryMode2["BY_DEPT"] = "BY_DEPT";
302
- return EnumOrgQueryMode2;
303
- })(EnumOrgQueryMode || {});
304
- var EnumOrgQueryScope = /* @__PURE__ */ ((EnumOrgQueryScope2) => {
305
- EnumOrgQueryScope2["ONLY_BRANCH"] = "ONLY_BRANCH";
306
- EnumOrgQueryScope2["STEWARD_UNIT"] = "STEWARD_UNIT";
307
- EnumOrgQueryScope2["WITH_DEPARTMENT"] = "WITH_DEPARTMENT";
308
- return EnumOrgQueryScope2;
309
- })(EnumOrgQueryScope || {});
310
- async function login(baseUrl, authorizationCode) {
311
- return await axiosClient.get(
312
- `${baseUrl}/opmsDefaultAuth/oauthLogin`,
313
- {
314
- params: { authorizationCode }
315
- }
316
- );
317
- }
318
- async function superLogin(baseUrl, params) {
319
- return await axiosClient.post(
320
- `${baseUrl}/opmsDefaultAuth/superLogin`,
321
- params
322
- );
323
- }
324
- async function logout(baseUrl) {
325
- return await axiosClient.post(
326
- `${baseUrl}/opmsDefaultAuth/logout`
327
- );
328
- }
329
- async function getUserInfo(baseUrl) {
330
- return await axiosClient.post(
331
- `${baseUrl}/opmsDefaultUser/userInfo`,
332
- {}
333
- );
334
- }
335
- async function queryResource(baseUrl, params) {
336
- return await axiosClient.post(
337
- `${baseUrl}/opmsDefaultUser/userResource`,
338
- params
339
- );
340
- }
341
- async function getOrgTree(baseUrl, params) {
342
- return await axiosClient.post(
343
- `${baseUrl}/opmsDefaultUser/orgTree`,
344
- params
345
- );
346
- }
347
- async function getUserOrgTree(baseUrl, params) {
348
- return await axiosClient.post(
349
- `${baseUrl}/opmsDefaultUser/userOrgTree`,
350
- params
351
- );
352
- }
353
- async function queryOrgCompanies(baseUrl, params) {
354
- return await axiosClient.post(
355
- `${baseUrl}/opmsDefaultUser/branches`,
356
- params
357
- );
358
- }
359
- function queryOrgCustom(baseUrl, params) {
360
- return axios.post(
361
- `${baseUrl}/opmsDefaultUser/queryOrgCustom`,
362
- params
363
- );
364
- }
365
-
366
250
  // src/utils/eventEmitter.ts
367
251
  var EventEmitter = class {
368
252
  constructor(allowedEvents) {
@@ -461,6 +345,98 @@ var EventEmitter = class {
461
345
  }
462
346
  };
463
347
 
348
+ // src/api.ts
349
+ import axios from "axios";
350
+ var ApiClient = class {
351
+ constructor(baseUrl, storage) {
352
+ this.baseUrl = baseUrl;
353
+ this.storage = storage;
354
+ this.axiosClient = axios.create();
355
+ this.axiosClient.interceptors.request.use((config) => {
356
+ const token = this.storage.getItem(TOKEN_KEY);
357
+ if (token) {
358
+ config.headers.Authorization = token;
359
+ }
360
+ return config;
361
+ });
362
+ this.axiosClient.interceptors.response.use(
363
+ (response) => {
364
+ const res = response.data;
365
+ const { success, code, msg } = res;
366
+ if (success === false) {
367
+ return Promise.reject({ code, msg });
368
+ }
369
+ return res;
370
+ },
371
+ (error) => Promise.reject(error)
372
+ );
373
+ }
374
+ async login(authorizationCode) {
375
+ return await this.axiosClient.get(
376
+ `${this.baseUrl}/opmsDefaultAuth/oauthLogin`,
377
+ { params: { authorizationCode } }
378
+ );
379
+ }
380
+ async superLogin(params) {
381
+ return await this.axiosClient.post(
382
+ `${this.baseUrl}/opmsDefaultAuth/superLogin`,
383
+ params
384
+ );
385
+ }
386
+ async logout() {
387
+ return await this.axiosClient.post(
388
+ `${this.baseUrl}/opmsDefaultAuth/logout`
389
+ );
390
+ }
391
+ async getUserInfo() {
392
+ return await this.axiosClient.post(
393
+ `${this.baseUrl}/opmsDefaultUser/userInfo`,
394
+ {}
395
+ );
396
+ }
397
+ async queryResource(params) {
398
+ return await this.axiosClient.post(
399
+ `${this.baseUrl}/opmsDefaultUser/userResource`,
400
+ params
401
+ );
402
+ }
403
+ async getOrgTree(params) {
404
+ return await this.axiosClient.post(
405
+ `${this.baseUrl}/opmsDefaultUser/orgTree`,
406
+ params
407
+ );
408
+ }
409
+ async getUserOrgTree(params) {
410
+ return await this.axiosClient.post(
411
+ `${this.baseUrl}/opmsDefaultUser/userOrgTree`,
412
+ params
413
+ );
414
+ }
415
+ async queryOrgCompanies(params) {
416
+ return await this.axiosClient.post(
417
+ `${this.baseUrl}/opmsDefaultUser/branches`,
418
+ params
419
+ );
420
+ }
421
+ async queryOrgCustom(params) {
422
+ return axios.post(
423
+ `${this.baseUrl}/opmsDefaultUser/queryOrgCustom`,
424
+ params
425
+ );
426
+ }
427
+ };
428
+ var EnumOrgQueryMode = /* @__PURE__ */ ((EnumOrgQueryMode2) => {
429
+ EnumOrgQueryMode2["BY_ROLES"] = "BY_ROLES";
430
+ EnumOrgQueryMode2["BY_DEPT"] = "BY_DEPT";
431
+ return EnumOrgQueryMode2;
432
+ })(EnumOrgQueryMode || {});
433
+ var EnumOrgQueryScope = /* @__PURE__ */ ((EnumOrgQueryScope2) => {
434
+ EnumOrgQueryScope2["ONLY_BRANCH"] = "ONLY_BRANCH";
435
+ EnumOrgQueryScope2["STEWARD_UNIT"] = "STEWARD_UNIT";
436
+ EnumOrgQueryScope2["WITH_DEPARTMENT"] = "WITH_DEPARTMENT";
437
+ return EnumOrgQueryScope2;
438
+ })(EnumOrgQueryScope || {});
439
+
464
440
  // src/permission.ts
465
441
  var DataHandler = {
466
442
  iterateOrgTree(orgTree) {
@@ -536,9 +512,8 @@ var Permission = class {
536
512
  this.baseUrl = options.baseUrl;
537
513
  this.systemId = options.systemId;
538
514
  this.hasSubApp = options.hasSubApp ?? false;
539
- this.storage = new Storage();
540
- this.storage.setSystemId(this.systemId);
541
- this.storage.setVersion("1.0.0");
515
+ this.storage = new Storage(this.systemId, "1.0.0");
516
+ this.api = new ApiClient(this.baseUrl, this.storage);
542
517
  }
543
518
  /**
544
519
  * Add event listener
@@ -588,7 +563,7 @@ var Permission = class {
588
563
  }
589
564
  async superLogin(userName, password) {
590
565
  this.clear();
591
- const { obj, msg, code } = await superLogin(this.baseUrl, {
566
+ const { obj, msg, code } = await this.api.superLogin({
592
567
  userName,
593
568
  password
594
569
  });
@@ -619,7 +594,7 @@ var Permission = class {
619
594
  obj,
620
595
  msg,
621
596
  code: loginCode
622
- } = await login(this.baseUrl, authCode);
597
+ } = await this.api.login(authCode);
623
598
  if (loginCode !== 200) {
624
599
  return Promise.reject({
625
600
  code: loginCode,
@@ -636,7 +611,7 @@ var Permission = class {
636
611
  * Logout and clear userInfo
637
612
  */
638
613
  async logout(clearData = true) {
639
- await logout(this.baseUrl);
614
+ await this.api.logout();
640
615
  clearData && this.clear();
641
616
  }
642
617
  clear() {
@@ -644,7 +619,7 @@ var Permission = class {
644
619
  this.emit("tokenChange", "");
645
620
  }
646
621
  async getUserInfo() {
647
- const data = await getUserInfo(this.baseUrl);
622
+ const data = await this.api.getUserInfo();
648
623
  const { obj, success, msg, code } = data;
649
624
  this.storage.setItem(USER_INFO_KEY, obj);
650
625
  return obj;
@@ -659,7 +634,7 @@ var Permission = class {
659
634
  async getResources() {
660
635
  let resources = this.storage.getItem(RESOURCE_KEY) || null;
661
636
  if (!resources) {
662
- const { obj, success, msg, code } = await queryResource(this.baseUrl, {
637
+ const { obj, success, msg, code } = await this.api.queryResource({
663
638
  systemId: this.systemId
664
639
  });
665
640
  if (!success) {
@@ -717,7 +692,7 @@ var Permission = class {
717
692
  orgTreeData = this.storage.getItem(cacheKey) || null;
718
693
  }
719
694
  if (!orgTreeData) {
720
- const res = await getUserOrgTree(this.baseUrl, params);
695
+ const res = await this.api.getUserOrgTree(params);
721
696
  const { obj } = res;
722
697
  orgTreeData = iterateNestedArray(obj, (item) => {
723
698
  return {
@@ -739,8 +714,6 @@ var Permission = class {
739
714
  const { tree } = data;
740
715
  return {
741
716
  orgTree: tree
742
- // orgNoAuthMap: noAuthMap,
743
- // authOrgTree: tree,
744
717
  };
745
718
  } catch (error) {
746
719
  console.log(error);
@@ -757,7 +730,7 @@ var Permission = class {
757
730
  orgCompanyList = this.storage.getItem(USER_TOTAL_COMPANY_KEY);
758
731
  }
759
732
  if (!orgCompanyList) {
760
- const { obj } = await queryOrgCustom(this.baseUrl, {
733
+ const { obj } = await this.api.queryOrgCustom({
761
734
  resultView: "LIST",
762
735
  orgCodeSource: "COS",
763
736
  direction: "FROM_SELF_TO_LEAF",
@@ -795,24 +768,6 @@ var Permission = class {
795
768
  this.storage.setItem(TOKEN_KEY, token);
796
769
  this.emit("tokenChange", token);
797
770
  }
798
- // --- Getters ---
799
- // get userInfo(): UserInfo | null {
800
- // if (!this._userInfo) {
801
- // this._userInfo = storage.getItem(USER_INFO_KEY);
802
- // }
803
- // if (!this._userInfo) return null;
804
- // const { account, name, ehrId, crewCode, crewId } = this._userInfo;
805
- // return { account, name, ehrId, crewCode, crewId };
806
- // }
807
- // get hasRootAuth() {
808
- // const orgs = this._userInfo?.organizations || [];
809
- // const first = orgs[0];
810
- // if (!first) return false;
811
- // return first.searchPath === '#1';
812
- // }
813
- // get userOrganizations(): UserOrganization[] {
814
- // return this._userInfo?.userOrganizations || [];
815
- // }
816
771
  /**
817
772
  * Get storage information for debugging and monitoring
818
773
  */
@@ -843,7 +798,7 @@ var Permission = class {
843
798
  * Get current storage version
844
799
  */
845
800
  getStorageVersion() {
846
- return this.storage.getVersion();
801
+ return this.storage.currentVersion;
847
802
  }
848
803
  /**
849
804
  * Clear all storage data for this system
@@ -923,6 +878,7 @@ function jumpToSSOLogout({
923
878
  window.location.href = logoutUrl.toString();
924
879
  }
925
880
  export {
881
+ ApiClient,
926
882
  DataHandler,
927
883
  EnumOrgQueryMode,
928
884
  EnumOrgQueryScope,
@@ -933,15 +889,6 @@ export {
933
889
  USER_INFO_KEY,
934
890
  USER_ORG_KEY,
935
891
  USER_TOTAL_COMPANY_KEY,
936
- getOrgTree,
937
- getUserInfo,
938
- getUserOrgTree,
939
892
  jumpToSSOLogin,
940
- jumpToSSOLogout,
941
- login,
942
- logout,
943
- queryOrgCompanies,
944
- queryOrgCustom,
945
- queryResource,
946
- superLogin
893
+ jumpToSSOLogout
947
894
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mu-cabin/opms-permission",
3
- "version": "0.9.12",
3
+ "version": "0.9.13",
4
4
  "description": "Frontend SDK for OPMS permission and auth management.",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",