befly 3.5.7 → 3.6.0

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.
Files changed (80) hide show
  1. package/lib/addon.ts +77 -0
  2. package/lib/logger.ts +6 -15
  3. package/lifecycle/checker.ts +20 -49
  4. package/lifecycle/lifecycle.ts +7 -5
  5. package/lifecycle/loader.ts +5 -5
  6. package/main.ts +10 -1
  7. package/package.json +2 -9
  8. package/paths.ts +5 -54
  9. package/util.ts +1 -83
  10. package/apis/admin/del.ts +0 -35
  11. package/apis/admin/info.ts +0 -50
  12. package/apis/admin/ins.ts +0 -61
  13. package/apis/admin/list.ts +0 -20
  14. package/apis/admin/roleDetail.ts +0 -35
  15. package/apis/admin/roleSave.ts +0 -40
  16. package/apis/admin/upd.ts +0 -51
  17. package/apis/api/all.ts +0 -37
  18. package/apis/auth/login.ts +0 -78
  19. package/apis/auth/logout.ts +0 -23
  20. package/apis/auth/register.ts +0 -50
  21. package/apis/auth/sendSmsCode.ts +0 -36
  22. package/apis/cache/refresh.ts +0 -34
  23. package/apis/dashboard/addonList.ts +0 -47
  24. package/apis/dashboard/changelog.ts +0 -37
  25. package/apis/dashboard/configStatus.ts +0 -54
  26. package/apis/dashboard/environmentInfo.ts +0 -46
  27. package/apis/dashboard/performanceMetrics.ts +0 -23
  28. package/apis/dashboard/permissionStats.ts +0 -31
  29. package/apis/dashboard/serviceStatus.ts +0 -82
  30. package/apis/dashboard/systemInfo.ts +0 -26
  31. package/apis/dashboard/systemOverview.ts +0 -32
  32. package/apis/dashboard/systemResources.ts +0 -119
  33. package/apis/dict/all.ts +0 -25
  34. package/apis/dict/del.ts +0 -19
  35. package/apis/dict/detail.ts +0 -21
  36. package/apis/dict/ins.ts +0 -27
  37. package/apis/dict/list.ts +0 -18
  38. package/apis/dict/upd.ts +0 -31
  39. package/apis/menu/all.ts +0 -68
  40. package/apis/menu/del.ts +0 -37
  41. package/apis/menu/ins.ts +0 -20
  42. package/apis/menu/list.ts +0 -21
  43. package/apis/menu/upd.ts +0 -29
  44. package/apis/role/apiDetail.ts +0 -30
  45. package/apis/role/apiSave.ts +0 -41
  46. package/apis/role/del.ts +0 -44
  47. package/apis/role/detail.ts +0 -24
  48. package/apis/role/ins.ts +0 -39
  49. package/apis/role/list.ts +0 -14
  50. package/apis/role/menuDetail.ts +0 -30
  51. package/apis/role/menuSave.ts +0 -38
  52. package/apis/role/save.ts +0 -44
  53. package/apis/role/upd.ts +0 -40
  54. package/bin/index.ts +0 -34
  55. package/checks/conflict.ts +0 -351
  56. package/checks/table.ts +0 -250
  57. package/commands/index.ts +0 -73
  58. package/commands/sync.ts +0 -88
  59. package/commands/syncApi.ts +0 -316
  60. package/commands/syncDb/apply.ts +0 -171
  61. package/commands/syncDb/constants.ts +0 -77
  62. package/commands/syncDb/ddl.ts +0 -191
  63. package/commands/syncDb/helpers.ts +0 -173
  64. package/commands/syncDb/index.ts +0 -217
  65. package/commands/syncDb/schema.ts +0 -199
  66. package/commands/syncDb/sqlite.ts +0 -50
  67. package/commands/syncDb/state.ts +0 -112
  68. package/commands/syncDb/table.ts +0 -214
  69. package/commands/syncDb/tableCreate.ts +0 -149
  70. package/commands/syncDb/types.ts +0 -92
  71. package/commands/syncDb/version.ts +0 -73
  72. package/commands/syncDb.ts +0 -34
  73. package/commands/syncDev.ts +0 -237
  74. package/commands/syncMenu.ts +0 -349
  75. package/commands/util.ts +0 -58
  76. package/tables/admin.json +0 -14
  77. package/tables/api.json +0 -8
  78. package/tables/dict.json +0 -8
  79. package/tables/menu.json +0 -8
  80. package/tables/role.json +0 -8
@@ -1,41 +0,0 @@
1
- /**
2
- * 保存角色的接口权限
3
- */
4
-
5
- import { Yes, No } from '../../util.js';
6
- import adminRoleTable from '../../tables/role.json';
7
-
8
- export default {
9
- name: '保存角色接口权限',
10
- fields: {
11
- apiIds: adminRoleTable.apis
12
- },
13
- handler: async (befly, ctx) => {
14
- // 查询角色是否存在
15
- const role = await befly.db.getOne({
16
- table: 'core_role',
17
- where: { id: ctx.body.roleId }
18
- });
19
-
20
- if (!role) {
21
- return No('角色不存在');
22
- }
23
-
24
- // 将数组转为逗号分隔的字符串存储
25
- const apiIdsStr = Array.isArray(ctx.body.apiIds) ? ctx.body.apiIds.join(',') : '';
26
-
27
- // 更新角色的接口权限
28
- await befly.db.updData({
29
- table: 'core_role',
30
- where: { id: ctx.body.roleId },
31
- data: {
32
- apis: apiIdsStr
33
- }
34
- });
35
-
36
- // 增量更新 Redis 缓存
37
- await befly.cache.cacheRolePermissions(befly, role.code, apiIdsStr);
38
-
39
- return Yes('操作成功');
40
- }
41
- };
package/apis/role/del.ts DELETED
@@ -1,44 +0,0 @@
1
- /**
2
- * 删除角色
3
- */
4
-
5
- import { Yes, No } from '../../util.js';
6
-
7
- export default {
8
- name: '删除角色',
9
- handler: async (befly, ctx) => {
10
- try {
11
- // 检查是否有用户使用此角色(使用 getList 代替 getAll)
12
- const adminList = await befly.db.getList({
13
- table: 'core_admin',
14
- where: { roleId: ctx.body.id }
15
- });
16
-
17
- if (adminList.total > 0) {
18
- return No('该角色已分配给用户,无法删除');
19
- }
20
-
21
- // 获取角色信息(用于删除缓存)
22
- const role = await befly.db.getDetail({
23
- table: 'core_role',
24
- where: { id: ctx.body.id }
25
- });
26
-
27
- // 删除角色
28
- await befly.db.delData({
29
- table: 'core_role',
30
- where: { id: ctx.body.id }
31
- });
32
-
33
- // 删除角色权限缓存
34
- if (role?.code) {
35
- await befly.cache.deleteRolePermissions(befly, role.code);
36
- }
37
-
38
- return Yes('操作成功');
39
- } catch (error) {
40
- befly.logger.error('删除角色失败:', error);
41
- return No('操作失败');
42
- }
43
- }
44
- };
@@ -1,24 +0,0 @@
1
- /**
2
- * 获取用户的角色(单角色模式)
3
- */
4
-
5
- import { Yes, No } from '../../util.js';
6
-
7
- export default {
8
- name: '获取用户角色',
9
- handler: async (befly, ctx) => {
10
- let roleInfo = null;
11
- if (ctx.body.id && ctx.user.roleType === 'admin') {
12
- roleInfo = await befly.db.getOne({
13
- table: 'core_role',
14
- where: { code: ctx.body.id }
15
- });
16
- }
17
-
18
- return Yes('操作成功', {
19
- roleId: ctx.body.id,
20
- roleCode: ctx.body.id,
21
- role: roleInfo
22
- });
23
- }
24
- };
package/apis/role/ins.ts DELETED
@@ -1,39 +0,0 @@
1
- import { Yes, No } from '../../util.js';
2
- import adminRoleTable from '../../tables/role.json';
3
-
4
- /**
5
- * 创建角色
6
- */
7
- export default {
8
- name: '创建角色',
9
- fields: adminRoleTable,
10
- handler: async (befly, ctx) => {
11
- // 检查角色代码是否已存在
12
- const existing = await befly.db.getOne({
13
- table: 'core_role',
14
- where: { code: ctx.body.code }
15
- });
16
-
17
- if (existing) {
18
- return No('角色代码已存在');
19
- }
20
-
21
- const roleId = await befly.db.insData({
22
- table: 'core_role',
23
- data: {
24
- name: ctx.body.name,
25
- code: ctx.body.code,
26
- description: ctx.body.description,
27
- menus: ctx.body.menus || '',
28
- apis: ctx.body.apis || '',
29
- sort: ctx.body.sort
30
- // state 由框架自动设置为 1
31
- }
32
- });
33
-
34
- // 增量缓存角色权限到 Redis Set
35
- await befly.cache.cacheRolePermissions(befly, ctx.body.code, ctx.body.apis || '');
36
-
37
- return Yes('操作成功', { id: roleId });
38
- }
39
- };
package/apis/role/list.ts DELETED
@@ -1,14 +0,0 @@
1
- import { Yes } from '../../util.js';
2
-
3
- export default {
4
- name: '获取角色列表',
5
- handler: async (befly, ctx) => {
6
- const roles = await befly.db.getList({
7
- limit: 30,
8
- table: 'core_role',
9
- orderBy: ['sort#ASC', 'id#ASC']
10
- });
11
-
12
- return Yes('操作成功', roles);
13
- }
14
- };
@@ -1,30 +0,0 @@
1
- /**
2
- * 获取角色的菜单权限
3
- */
4
-
5
- import { Yes, No } from '../../util.js';
6
-
7
- export default {
8
- name: '获取角色菜单权限',
9
- handler: async (befly, ctx) => {
10
- // 查询角色信息
11
- const role = await befly.db.getOne({
12
- table: 'core_role',
13
- where: { id: ctx.body.roleId }
14
- });
15
-
16
- if (!role) {
17
- return No('角色不存在');
18
- }
19
-
20
- // 解析菜单ID列表(逗号分隔的字符串转为数组)
21
- const menuIds = role.menus
22
- ? role.menus
23
- .split(',')
24
- .map((id: string) => parseInt(id.trim()))
25
- .filter((id: number) => !isNaN(id))
26
- : [];
27
-
28
- return Yes('操作成功', menuIds);
29
- }
30
- };
@@ -1,38 +0,0 @@
1
- /**
2
- * 保存角色的菜单权限
3
- */
4
-
5
- import { Yes, No } from '../../util.js';
6
- import adminRoleTable from '../../tables/role.json';
7
-
8
- export default {
9
- name: '保存角色菜单权限',
10
- fields: {
11
- menuIds: adminRoleTable.menus
12
- },
13
- handler: async (befly, ctx) => {
14
- // 查询角色是否存在
15
- const role = await befly.db.getOne({
16
- table: 'core_role',
17
- where: { id: ctx.body.roleId }
18
- });
19
-
20
- if (!role) {
21
- return No('角色不存在');
22
- }
23
-
24
- // 将数组转为逗号分隔的字符串存储
25
- const menuIdsStr = Array.isArray(ctx.body.menuIds) ? ctx.body.menuIds.join(',') : '';
26
-
27
- // 更新角色的菜单权限
28
- await befly.db.updData({
29
- table: 'core_role',
30
- where: { id: ctx.body.roleId },
31
- data: {
32
- menus: menuIdsStr
33
- }
34
- });
35
-
36
- return Yes('操作成功');
37
- }
38
- };
package/apis/role/save.ts DELETED
@@ -1,44 +0,0 @@
1
- /**
2
- * 保存用户的角色(单角色模式)
3
- */
4
-
5
- import { Yes, No } from '../../util.js';
6
-
7
- export default {
8
- name: '保存用户角色',
9
- fields: {
10
- roleCode: '角色编码|string|2|50|null|1|^[a-zA-Z0-9_]+$'
11
- },
12
- handler: async (befly, ctx) => {
13
- try {
14
- // 查询角色是否存在(使用 roleCode 而非 roleId)
15
- const role = await befly.db.getOne({
16
- table: 'core_role',
17
- where: { code: ctx.body.roleCode }
18
- });
19
-
20
- if (!role) {
21
- return No('角色不存在');
22
- }
23
-
24
- // 根据角色编码判断角色类型(硬编码规则)
25
- const roleType = role.code === 'dev' || role.code === 'admin' ? 'admin' : 'user';
26
-
27
- // 更新管理员的角色ID、角色编码和角色类型
28
- await befly.db.updData({
29
- table: 'core_admin',
30
- where: { id: ctx.body.adminId },
31
- data: {
32
- roleId: role.id,
33
- roleCode: role.code,
34
- roleType: roleType
35
- }
36
- });
37
-
38
- return Yes('操作成功');
39
- } catch (error) {
40
- befly.logger.error('保存用户角色失败:', error);
41
- return No('操作失败');
42
- }
43
- }
44
- };
package/apis/role/upd.ts DELETED
@@ -1,40 +0,0 @@
1
- import { Yes, No } from '../../util.js';
2
- import adminRoleTable from '../../tables/role.json';
3
-
4
- export default {
5
- name: '更新角色',
6
- fields: adminRoleTable,
7
- handler: async (befly, ctx) => {
8
- // 检查角色代码是否被其他角色占用
9
- const existing = await befly.db.getList({
10
- table: 'core_role',
11
- where: {
12
- code: ctx.body.code,
13
- id$ne: ctx.body.id
14
- }
15
- });
16
-
17
- if (existing.total > 0) {
18
- return No('角色代码已被其他角色使用');
19
- }
20
-
21
- await befly.db.updData({
22
- table: 'core_role',
23
- where: { id: ctx.body.id },
24
- data: {
25
- name: ctx.body.name,
26
- code: ctx.body.code,
27
- description: ctx.body.description,
28
- menus: ctx.body.menus || '',
29
- apis: ctx.body.apis || '',
30
- sort: ctx.body.sort
31
- // state 字段不在此处更新,需要禁用/启用时单独处理
32
- }
33
- });
34
-
35
- // 增量更新角色权限缓存(先删除再重建)
36
- await befly.cache.cacheRolePermissions(befly, ctx.body.code, ctx.body.apis || '');
37
-
38
- return Yes('操作成功');
39
- }
40
- };
package/bin/index.ts DELETED
@@ -1,34 +0,0 @@
1
- #!/usr/bin/env bun
2
- /**
3
- * Befly CLI - 命令行工具入口
4
- * 只提供 sync 命令,用于同步所有数据
5
- *
6
- * 使用方法:
7
- * befly sync # 使用当前环境(默认 development)
8
- *
9
- * 环境变量加载:
10
- * Bun 自动根据 NODE_ENV 加载对应的 .env 文件:
11
- * - NODE_ENV=development → .env.development
12
- * - NODE_ENV=production → .env.production
13
- * - NODE_ENV=test → .env.test
14
- */
15
-
16
- import { syncCommand } from '../commands/sync.js';
17
- import { Logger } from '../lib/logger.js';
18
-
19
- /**
20
- * 主函数
21
- */
22
- async function main() {
23
- // 执行 sync 命令
24
- try {
25
- await syncCommand();
26
- Logger.printEnv();
27
- } catch (error: any) {
28
- Logger.error('命令执行失败:', error.message || error);
29
- process.exit(1);
30
- }
31
- }
32
-
33
- // 运行主函数
34
- main();