@litianxiang/portal-core 0.2.0 → 0.2.2
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 +22 -0
- package/dist/index.js +32 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,6 +16,26 @@ pnpm add @litianxiang/portal-core
|
|
|
16
16
|
npm i @litianxiang/portal-core
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
+
## 升级说明
|
|
20
|
+
|
|
21
|
+
### 0.2.0(Breaking Change)
|
|
22
|
+
|
|
23
|
+
- 移除 `createPortalHttpClient`。
|
|
24
|
+
- 统一使用 `createAuthHttpClient`。
|
|
25
|
+
- 业务项目原来通过 `createPortalHttpClient` 传入的 `baseURL: ''`、`timeout: 10000` 可以不再显式传递,`createAuthHttpClient` 内部已提供相同默认值。
|
|
26
|
+
|
|
27
|
+
迁移示例:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { createAuthHttpClient } from '@litianxiang/portal-core'
|
|
31
|
+
|
|
32
|
+
const { http, logoutToAuth } = createAuthHttpClient({
|
|
33
|
+
getUserStore: () => useUserStore(),
|
|
34
|
+
getLoadingStore: () => useLoadingStore(),
|
|
35
|
+
onShowError: (msg) => console.error(msg)
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
19
39
|
## 快速开始
|
|
20
40
|
|
|
21
41
|
### 1) 路由接入
|
|
@@ -73,6 +93,8 @@ setupPortalApp({
|
|
|
73
93
|
### router
|
|
74
94
|
|
|
75
95
|
- `createAppRouter(options)`:创建统一路由实例,内置登录态校验、菜单动态挂载、首屏跳转逻辑。
|
|
96
|
+
- 菜单类别为 `page` 的动态路由会挂载到 `main` 下。
|
|
97
|
+
- 菜单类别为 `flow-page` 的动态路由会挂载为顶级路由(不进入 `main` 主框架)。
|
|
76
98
|
|
|
77
99
|
最小示例:
|
|
78
100
|
|
package/dist/index.js
CHANGED
|
@@ -212,7 +212,8 @@ function createAppRouter(options) {
|
|
|
212
212
|
const resolveRouteAndView = (item) => {
|
|
213
213
|
const [rawPath] = (item.path || "").split("?");
|
|
214
214
|
const normalizedPath = rawPath.startsWith("/") ? rawPath : `/${rawPath}`;
|
|
215
|
-
const
|
|
215
|
+
const childPath = normalizedPath.slice(1);
|
|
216
|
+
const rootPath = normalizedPath;
|
|
216
217
|
const candidates = [
|
|
217
218
|
`/main${normalizedPath}`,
|
|
218
219
|
// /views/main/system/user.vue
|
|
@@ -231,40 +232,44 @@ function createAppRouter(options) {
|
|
|
231
232
|
const fallbackPath = `/main${normalizedPath}`;
|
|
232
233
|
viewKey = `/src/views${fallbackPath}.vue`;
|
|
233
234
|
}
|
|
234
|
-
return {
|
|
235
|
+
return { childPath, rootPath, viewKey };
|
|
235
236
|
};
|
|
236
|
-
const
|
|
237
|
+
const collectDynamicRoutes = (menuItems) => {
|
|
237
238
|
const routes = [];
|
|
238
|
-
for (const item of menuItems) {
|
|
239
|
-
if (item
|
|
240
|
-
|
|
241
|
-
const isPage = item.category === "page" || item.category === "flow-page";
|
|
242
|
-
if (!registeredPaths.has(routePath)) {
|
|
243
|
-
registeredPaths.add(routePath);
|
|
244
|
-
routes.push({
|
|
245
|
-
path: routePath,
|
|
246
|
-
name: `menu_${item.id}`,
|
|
247
|
-
component: isPage ? modules[viewKey] : void 0,
|
|
248
|
-
meta: {
|
|
249
|
-
id: item.id,
|
|
250
|
-
parent_id: item.parent_id,
|
|
251
|
-
icon: item.icon,
|
|
252
|
-
category: item.category,
|
|
253
|
-
keepAlive: true,
|
|
254
|
-
title: item.name,
|
|
255
|
-
fullPath: item.path
|
|
256
|
-
},
|
|
257
|
-
children: item.children ? transformRoutes(item.children) : []
|
|
258
|
-
});
|
|
259
|
-
} else if (item.children?.length) {
|
|
260
|
-
routes.push(...transformRoutes(item.children));
|
|
239
|
+
for (const item of menuItems || []) {
|
|
240
|
+
if (item?.children?.length) {
|
|
241
|
+
routes.push(...collectDynamicRoutes(item.children));
|
|
261
242
|
}
|
|
243
|
+
const isDynamicPage = item?.category === "page" || item?.category === "flow-page";
|
|
244
|
+
if (!isDynamicPage) continue;
|
|
245
|
+
const { childPath, rootPath, viewKey } = resolveRouteAndView(item);
|
|
246
|
+
const routePath = item?.category === "flow-page" ? rootPath : childPath;
|
|
247
|
+
if (registeredPaths.has(routePath)) continue;
|
|
248
|
+
registeredPaths.add(routePath);
|
|
249
|
+
routes.push({
|
|
250
|
+
path: routePath,
|
|
251
|
+
name: `menu_${item.id}`,
|
|
252
|
+
component: modules[viewKey],
|
|
253
|
+
meta: {
|
|
254
|
+
id: item.id,
|
|
255
|
+
parent_id: item.parent_id,
|
|
256
|
+
icon: item.icon,
|
|
257
|
+
category: item.category,
|
|
258
|
+
keepAlive: true,
|
|
259
|
+
title: item.name,
|
|
260
|
+
fullPath: item.path
|
|
261
|
+
}
|
|
262
|
+
});
|
|
262
263
|
}
|
|
263
264
|
return routes;
|
|
264
265
|
};
|
|
265
266
|
const addDynamicRoutes = (menuData) => {
|
|
266
|
-
const dynamicRoutes =
|
|
267
|
+
const dynamicRoutes = collectDynamicRoutes(menuData);
|
|
267
268
|
dynamicRoutes.forEach((route) => {
|
|
269
|
+
if (route.meta?.category === "flow-page") {
|
|
270
|
+
router.addRoute(route);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
268
273
|
router.addRoute("main", route);
|
|
269
274
|
});
|
|
270
275
|
};
|