@befly-addon/admin 1.0.27 → 1.0.28

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@befly-addon/admin",
3
- "version": "1.0.27",
3
+ "version": "1.0.28",
4
4
  "description": "Befly - 管理后台功能组件",
5
5
  "type": "module",
6
6
  "private": false,
@@ -18,7 +18,8 @@
18
18
  "apis/",
19
19
  "tables/",
20
20
  "views/",
21
- "util",
21
+ "utils/",
22
+ "types/",
22
23
  "addon.config.json",
23
24
  "README.md",
24
25
  "package.json",
@@ -39,7 +40,7 @@
39
40
  "url": "https://github.com/chenbimo/befly.git",
40
41
  "directory": "packages/addon-admin"
41
42
  },
42
- "gitHead": "ccd0726d53a6902503bbfa58164f0644f28b8d5b",
43
+ "gitHead": "62797bf96f317ff0e666db73b4edac4435a6ed35",
43
44
  "dependencies": {
44
45
  "befly": "3.8.12"
45
46
  }
@@ -0,0 +1,12 @@
1
+ import type { RouteRecordRaw, Component } from 'vue-router';
2
+
3
+ /**
4
+ * 布局配置接口
5
+ */
6
+ export interface LayoutConfig {
7
+ path: string;
8
+ layoutName: string;
9
+ component: Component;
10
+ children?: LayoutConfig[];
11
+ meta?: Record<string, any>;
12
+ }
@@ -0,0 +1,70 @@
1
+ import type { RouteRecordRaw } from 'vue-router';
2
+ import type { LayoutConfig } from '../types/layout';
3
+
4
+ /**
5
+ * 自定义布局处理函数
6
+ * 根据文件名后缀判断使用哪个布局
7
+ * @param routes - 原始路由配置
8
+ * @param inheritLayout - 继承的布局名称(来自父级目录)
9
+ * @returns 处理后的布局配置(不包含实际的布局组件导入)
10
+ */
11
+ export function Layouts(routes: RouteRecordRaw[], inheritLayout = ''): LayoutConfig[] {
12
+ const result: LayoutConfig[] = [];
13
+
14
+ for (const route of routes) {
15
+ const currentPath = route.path || '';
16
+
17
+ // 检查当前路径是否有 _数字 格式
18
+ const pathMatch = currentPath.match(/_(\d+)$/);
19
+ const currentLayout = pathMatch ? pathMatch[1] : inheritLayout;
20
+
21
+ // 如果有子路由,说明这是中间节点(目录),不包裹布局,只递归处理子路由
22
+ if (route.children && route.children.length > 0) {
23
+ // 清理路径:如果是 xxx_数字 格式,去掉 _数字
24
+ const cleanPath = pathMatch ? currentPath.replace(/_\d+$/, '') : currentPath;
25
+
26
+ // 直接递归处理子路由,不添加当前层级到结果
27
+ const childConfigs = Layouts(route.children, currentLayout);
28
+
29
+ // 将子路由的路径前缀加上当前路径
30
+ for (const child of childConfigs) {
31
+ result.push({
32
+ ...child,
33
+ path: cleanPath ? `${cleanPath}/${child.path}`.replace(/\/+/g, '/') : child.path
34
+ });
35
+ }
36
+ continue;
37
+ }
38
+
39
+ // 没有子路由的叶子节点,需要包裹布局
40
+ const lastPart = currentPath;
41
+
42
+ // 匹配 _数字 格式(如 index_1, news_2)
43
+ const match = lastPart.match(/_(\d+)$/);
44
+ // 优先使用文件自己的布局,其次使用继承的布局,最后使用 default
45
+ const layoutName = match ? match[1] : currentLayout || 'default';
46
+
47
+ // 计算清理后的路径
48
+ let cleanPath: string;
49
+ if (lastPart === 'index' || (lastPart.startsWith('index_') && match)) {
50
+ // index 或 index_数字 → 改为空路径(由父级路径表示)
51
+ cleanPath = '';
52
+ } else if (match) {
53
+ // xxx_数字 → 去掉 _数字 后缀
54
+ cleanPath = lastPart.replace(/_\d+$/, '');
55
+ } else {
56
+ // 其他 → 保持原样
57
+ cleanPath = lastPart;
58
+ }
59
+
60
+ // 返回布局配置(不执行实际导入)
61
+ result.push({
62
+ path: cleanPath,
63
+ layoutName: layoutName,
64
+ component: route.component!,
65
+ meta: route.meta
66
+ });
67
+ }
68
+
69
+ return result;
70
+ }
@@ -0,0 +1,43 @@
1
+ import { readdirSync, existsSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+
4
+ /**
5
+ * 扫描所有 @befly-addon 包的 views 目录
6
+ * 用于 unplugin-vue-router 的 routesFolder 配置
7
+ * 注意:此函数只能在 vite.config.ts 中使用(Node.js 环境),不能在浏览器中使用
8
+ * @returns 路由文件夹配置数组
9
+ */
10
+ export function scanBeflyAddonViews() {
11
+ // 使用绝对路径:基于项目根目录(process.cwd())
12
+ const projectRoot = process.cwd();
13
+ const addonBasePath = join(projectRoot, 'node_modules', '@befly-addon');
14
+ const routesFolders: Array<{ src: string; path: string }> = [];
15
+
16
+ if (!existsSync(addonBasePath)) {
17
+ return routesFolders;
18
+ }
19
+
20
+ try {
21
+ const addonDirs = readdirSync(addonBasePath);
22
+
23
+ for (const addonName of addonDirs) {
24
+ const addonPath = join(addonBasePath, addonName);
25
+
26
+ // 检查是否为目录(包括符号链接)
27
+ if (!existsSync(addonPath)) continue;
28
+
29
+ const viewsPath = join(addonPath, 'views');
30
+
31
+ if (existsSync(viewsPath)) {
32
+ routesFolders.push({
33
+ src: viewsPath,
34
+ path: `addon/${addonName}/`
35
+ });
36
+ }
37
+ }
38
+ } catch (error) {
39
+ console.error('扫描 @befly-addon 目录失败:', error);
40
+ }
41
+
42
+ return routesFolders;
43
+ }