@litianxiang/portal-core 0.2.5 → 0.2.6

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/README.md CHANGED
@@ -71,18 +71,31 @@ export const { http, logoutToAuth } = createAuthHttpClient({
71
71
  })
72
72
  ```
73
73
 
74
+ 如果业务项目只需要一个标准站点 http 模块,也可以直接使用更薄的封装:
75
+
76
+ ```ts
77
+ import { createSiteHttpModule } from '@litianxiang/portal-core'
78
+
79
+ export const { http, logoutToAuth } = createSiteHttpModule({
80
+ getUserStore: () => useUserStore(),
81
+ getLoadingStore: () => useLoadingStore(),
82
+ onShowError: (msg) => console.error(msg)
83
+ })
84
+ ```
85
+
74
86
  ## 导出方法说明(逐方法)
75
87
 
76
88
  ### app
77
89
 
78
- - `setupPortalApp(options)`:统一执行应用启动编排(插件注册、图标注册、beforeMount、mount)。
90
+ - `setupApp(options)`:统一执行应用启动编排(插件注册、图标注册、beforeMount、mount)。
91
+ - `setupRuntime(options)`:统一执行挂载前的运行时初始化(i18n、登录态同步、菜单状态、portal-ui http 绑定)。
79
92
 
80
93
  最小示例:
81
94
 
82
95
  ```ts
83
- import { setupPortalApp } from '@litianxiang/portal-core'
96
+ import { setupApp } from '@litianxiang/portal-core'
84
97
 
85
- setupPortalApp({
98
+ setupApp({
86
99
  app,
87
100
  plugins: [router, pinia],
88
101
  elementIcons: ElementPlusIconsVue,
@@ -108,6 +121,20 @@ const router = createAppRouter({
108
121
  })
109
122
  ```
110
123
 
124
+ - `createStaticAppRouter(options)`:创建静态站点路由,可选统一登录守卫,适用于 portal.web、portal.auth.web 这类无动态菜单站点。
125
+
126
+ 最小示例:
127
+
128
+ ```ts
129
+ import { createStaticAppRouter } from '@litianxiang/portal-core'
130
+
131
+ const router = createStaticAppRouter({
132
+ staticRoutes,
133
+ getUserStore: () => useUserStore(),
134
+ requireAuth: true
135
+ })
136
+ ```
137
+
111
138
  ### auth
112
139
 
113
140
  - `createLogoutToAuth(options)`:生成统一退出函数,负责清理用户态并跳转到认证站。
package/dist/index.d.ts CHANGED
@@ -1,22 +1,32 @@
1
1
  import * as vue_router from 'vue-router';
2
- import { RouteRecordRaw } from 'vue-router';
3
2
  import { Dayjs } from 'dayjs';
4
- import { App, Plugin } from 'vue';
5
3
  import * as vue_i18n from 'vue-i18n';
6
4
 
7
5
  interface AppRouterOptions {
8
- staticRoutes: RouteRecordRaw[];
6
+ staticRoutes: any[];
9
7
  getUserStore: () => any;
10
8
  getFirstPage?: (allMenu: any[]) => string | null | undefined;
11
9
  authLoginUrl?: string;
12
10
  /** 是否开启从 URL 参数恢复 token,默认关闭 */
13
11
  enableUrlTokenRecovery?: boolean;
14
12
  }
13
+ interface StaticAppRouterOptions {
14
+ staticRoutes: any[];
15
+ getUserStore?: () => any;
16
+ authLoginUrl?: string;
17
+ requireAuth?: boolean;
18
+ }
15
19
  /**
16
20
  * 创建带统一登录、动态菜单和 Tab 管理的 Router
17
21
  * 各站点只需注入各自的 userStore / tabStore / getFirstPage 即可复用
18
22
  */
19
23
  declare function createAppRouter(options: AppRouterOptions): vue_router.Router;
24
+ /**
25
+ * 创建静态站点路由:
26
+ * - 可选统一登录守卫
27
+ * - 适用于 portal.web / portal.auth.web 这类无动态菜单站点
28
+ */
29
+ declare function createStaticAppRouter(options: StaticAppRouterOptions): vue_router.Router;
20
30
 
21
31
  interface LogoutToAuthOptions {
22
32
  getUserStore: () => {
@@ -69,10 +79,16 @@ interface CreateAuthHttpClientOptions {
69
79
  inactiveBufferMs?: number;
70
80
  onShowError?: (msg: string) => void;
71
81
  }
82
+ interface CreateSiteHttpModuleOptions extends CreateAuthHttpClientOptions {
83
+ }
72
84
  declare function createAuthHttpClient(options: CreateAuthHttpClientOptions): {
73
85
  http: any;
74
86
  logoutToAuth: () => void;
75
87
  };
88
+ declare function createSiteHttpModule(options: CreateSiteHttpModuleOptions): {
89
+ http: any;
90
+ logoutToAuth: () => void;
91
+ };
76
92
 
77
93
  interface PermissionHelperOptions {
78
94
  /** 返回用户 Store 实例,需至少提供 getUserALLMenu 或 getUserPageMenu */
@@ -504,15 +520,38 @@ interface AuthConfig {
504
520
  }
505
521
  declare const AUTH_CONFIG: AuthConfig;
506
522
 
507
- type PluginTuple = [Plugin, ...unknown[]];
508
- type UsePluginItem = Plugin | PluginTuple;
509
- interface SetupPortalAppOptions {
510
- app: App;
523
+ type AppLike = {
524
+ use: (plugin: any, ...args: any[]) => unknown;
525
+ component: (name: string, component: any) => unknown;
526
+ mount: (rootContainer: string | Element, ...args: any[]) => unknown;
527
+ };
528
+ type PluginTuple = [any, ...unknown[]];
529
+ type UsePluginItem = any | PluginTuple;
530
+ interface PortalBootstrapUserStoreLike {
531
+ syncFromAuthStore?: () => unknown;
532
+ setLoadUserMenulist?: () => unknown;
533
+ }
534
+ interface SetupAppOptions {
535
+ app: AppLike;
511
536
  plugins?: UsePluginItem[];
512
537
  elementIcons?: Record<string, unknown>;
513
538
  beforeMount?: () => void;
514
539
  mountSelector?: string;
515
540
  }
541
+ interface SetupRuntimeOptions {
542
+ initI18n?: () => void;
543
+ getUserStore?: () => PortalBootstrapUserStoreLike | null | undefined;
544
+ bindPortalUiHttp?: () => void;
545
+ beforeReady?: () => void;
546
+ }
547
+ /**
548
+ * 统一处理业务前端在挂载前的常见初始化:
549
+ * 1. 绑定 i18n 工具
550
+ * 2. 恢复统一登录缓存
551
+ * 3. 重置菜单加载状态
552
+ * 4. 注入 portal-ui http 实例
553
+ */
554
+ declare function setupRuntime(options: SetupRuntimeOptions): void;
516
555
  /**
517
556
  * 按 Portal 约定完成应用启动编排:
518
557
  * 1. 注册插件
@@ -520,7 +559,7 @@ interface SetupPortalAppOptions {
520
559
  * 3. 执行挂载前回调
521
560
  * 4. 挂载应用
522
561
  */
523
- declare function setupPortalApp(options: SetupPortalAppOptions): void;
562
+ declare function setupApp(options: SetupAppOptions): void;
524
563
 
525
564
  interface CreateI18nOptions {
526
565
  /** 翻译消息,key 为 locale(如 'zh-CN'),value 为翻译对象 */
@@ -538,4 +577,4 @@ interface CreateI18nOptions {
538
577
  */
539
578
  declare function createPortalI18n(options: CreateI18nOptions): vue_i18n.I18n<Record<string, any>, {}, {}, string, false>;
540
579
 
541
- export { AUTH_CONFIG, type AppRouterOptions, type AuthConfig, type BaseUserStoreLike, type BreadcrumbItem, type CoreMenuItem, type CoreMenuUserState, type CoreUserStoreLike, type CreateAuthHttpClientOptions, type CreateBaseUserActionsOptions, type CreateI18nOptions, type CreateMenuUserActionsOptions, DEFAULT_AUTH_LOGIN_ENV_KEY, DEFAULT_AUTH_LOGIN_PATH, DEFAULT_HOME_PATH, DEFAULT_SSO_STORAGE_KEY, type FetchUserMenuForStoreOptions, type FontSize, type InactivityConfig, type InactivityResult, type LoadingState, type LogoutToAuthOptions, type MenuHelper, type MenuHelperOptions, type PermissionHelper, type PermissionHelperOptions, type PresetRangeKey, type SetupPortalAppOptions, type SidebarMenuFilterOptions, type SyncFromAuthStoreOptions, type TabItem, type ThemeConfig, type ThemeMode, type TimeInput, type TimeRange, type TransformMenuOptions, addTab, applyTheme, buildAllMenuTree, buildPageMenuList, calcInactivityAction, closeAll, closeLeft, closeOthers, closeRight, createAppRouter, createAuthHttpClient, createBaseUserActions, createBaseUserGetters, createBaseUserState, createLoadingStoreOptions, createLogoutToAuth, createMenuHelper, createMenuUserActions, createMenuUserGetters, createMenuUserState, createPermissionHelper, createPortalI18n, createRange, createTabStoreOptions, fetchUserMenuForStore, filterSidebarMenu, findFirstPagePath, formatRange, formatTime, formatToMonthDay, formatToWanShou, formatToYi, formatWanYuanToYi, getDefaultAuthLoginUrl, getPresetRange, humanizeTime, initTheme, isInRange, removeTab, setupPortalApp, syncFromAuthStoreToStore, timeDiff, transformMenuTree, useThemeWatcher, useTime };
580
+ export { AUTH_CONFIG, type AppRouterOptions, type AuthConfig, type BaseUserStoreLike, type BreadcrumbItem, type CoreMenuItem, type CoreMenuUserState, type CoreUserStoreLike, type CreateAuthHttpClientOptions, type CreateBaseUserActionsOptions, type CreateI18nOptions, type CreateMenuUserActionsOptions, type CreateSiteHttpModuleOptions, DEFAULT_AUTH_LOGIN_ENV_KEY, DEFAULT_AUTH_LOGIN_PATH, DEFAULT_HOME_PATH, DEFAULT_SSO_STORAGE_KEY, type FetchUserMenuForStoreOptions, type FontSize, type InactivityConfig, type InactivityResult, type LoadingState, type LogoutToAuthOptions, type MenuHelper, type MenuHelperOptions, type PermissionHelper, type PermissionHelperOptions, type PortalBootstrapUserStoreLike, type PresetRangeKey, type SetupAppOptions, type SetupRuntimeOptions, type SidebarMenuFilterOptions, type StaticAppRouterOptions, type SyncFromAuthStoreOptions, type TabItem, type ThemeConfig, type ThemeMode, type TimeInput, type TimeRange, type TransformMenuOptions, addTab, applyTheme, buildAllMenuTree, buildPageMenuList, calcInactivityAction, closeAll, closeLeft, closeOthers, closeRight, createAppRouter, createAuthHttpClient, createBaseUserActions, createBaseUserGetters, createBaseUserState, createLoadingStoreOptions, createLogoutToAuth, createMenuHelper, createMenuUserActions, createMenuUserGetters, createMenuUserState, createPermissionHelper, createPortalI18n, createRange, createSiteHttpModule, createStaticAppRouter, createTabStoreOptions, fetchUserMenuForStore, filterSidebarMenu, findFirstPagePath, formatRange, formatTime, formatToMonthDay, formatToWanShou, formatToYi, formatWanYuanToYi, getDefaultAuthLoginUrl, getPresetRange, humanizeTime, initTheme, isInRange, removeTab, setupApp, setupRuntime, syncFromAuthStoreToStore, timeDiff, transformMenuTree, useThemeWatcher, useTime };
package/dist/index.js CHANGED
@@ -176,6 +176,9 @@ function findFirstPagePath(menuList) {
176
176
  }
177
177
 
178
178
  // src/router/router.ts
179
+ function resolveAuthLoginUrl(authLoginUrl) {
180
+ return authLoginUrl || `${window.location.origin}/auth/#/login`;
181
+ }
179
182
  function createAppRouter(options) {
180
183
  const {
181
184
  staticRoutes,
@@ -211,7 +214,7 @@ function createAppRouter(options) {
211
214
  history: createWebHashHistory(),
212
215
  routes: staticRoutes
213
216
  });
214
- const AUTH_LOGIN_URL = authLoginUrl || `${window.location.origin}/auth/#/login`;
217
+ const AUTH_LOGIN_URL = resolveAuthLoginUrl(authLoginUrl);
215
218
  const modules = import.meta.glob("@/views/**/*.vue");
216
219
  const registeredPaths = /* @__PURE__ */ new Set();
217
220
  const resolveRouteAndView = (item) => {
@@ -373,6 +376,43 @@ function createAppRouter(options) {
373
376
  });
374
377
  return router;
375
378
  }
379
+ function createStaticAppRouter(options) {
380
+ const {
381
+ staticRoutes,
382
+ getUserStore,
383
+ authLoginUrl,
384
+ requireAuth = false
385
+ } = options;
386
+ const router = createRouter({
387
+ history: createWebHashHistory(),
388
+ routes: staticRoutes
389
+ });
390
+ if (!requireAuth || !getUserStore) {
391
+ return router;
392
+ }
393
+ const AUTH_LOGIN_URL = resolveAuthLoginUrl(authLoginUrl);
394
+ router.beforeEach((_to, _from, next) => {
395
+ const userStore = getUserStore();
396
+ let accessToken = userStore.getUserAccessToken;
397
+ if (accessToken && !userStore.getUserName) {
398
+ userStore.syncFromAuthStore?.();
399
+ accessToken = userStore.getUserAccessToken;
400
+ }
401
+ if (!accessToken) {
402
+ const restored = userStore.syncFromAuthStore?.();
403
+ if (restored) {
404
+ accessToken = userStore.getUserAccessToken;
405
+ }
406
+ }
407
+ if (!accessToken) {
408
+ const currentUrl = encodeURIComponent(window.location.href);
409
+ window.location.href = `${AUTH_LOGIN_URL}?redirect=${currentUrl}`;
410
+ return;
411
+ }
412
+ next();
413
+ });
414
+ return router;
415
+ }
376
416
 
377
417
  // src/config/auth-config.ts
378
418
  var DEFAULT_SSO_STORAGE_KEY = "auth-user-store";
@@ -616,6 +656,13 @@ function createAuthHttpClient(options) {
616
656
  );
617
657
  return { http, logoutToAuth };
618
658
  }
659
+ function createSiteHttpModule(options) {
660
+ const { http, logoutToAuth } = createAuthHttpClient(options);
661
+ return {
662
+ http,
663
+ logoutToAuth
664
+ };
665
+ }
619
666
 
620
667
  // src/permission/permission.ts
621
668
  function normalizePath2(raw) {
@@ -1180,7 +1227,21 @@ function usePlugin(app, plugin) {
1180
1227
  }
1181
1228
  app.use(plugin);
1182
1229
  }
1183
- function setupPortalApp(options) {
1230
+ function setupRuntime(options) {
1231
+ const {
1232
+ initI18n,
1233
+ getUserStore,
1234
+ bindPortalUiHttp,
1235
+ beforeReady
1236
+ } = options;
1237
+ initI18n?.();
1238
+ const userStore = getUserStore?.();
1239
+ userStore?.syncFromAuthStore?.();
1240
+ userStore?.setLoadUserMenulist?.();
1241
+ bindPortalUiHttp?.();
1242
+ beforeReady?.();
1243
+ }
1244
+ function setupApp(options) {
1184
1245
  const {
1185
1246
  app,
1186
1247
  plugins = [],
@@ -1249,6 +1310,8 @@ export {
1249
1310
  createPermissionHelper,
1250
1311
  createPortalI18n,
1251
1312
  createRange,
1313
+ createSiteHttpModule,
1314
+ createStaticAppRouter,
1252
1315
  createTabStoreOptions,
1253
1316
  fetchUserMenuForStore,
1254
1317
  filterSidebarMenu,
@@ -1265,7 +1328,8 @@ export {
1265
1328
  initTheme,
1266
1329
  isInRange,
1267
1330
  removeTab,
1268
- setupPortalApp,
1331
+ setupApp,
1332
+ setupRuntime,
1269
1333
  syncFromAuthStoreToStore,
1270
1334
  timeDiff,
1271
1335
  transformMenuTree,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@litianxiang/portal-core",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "dist/index.js",