befly 3.10.13 → 3.10.15

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.
@@ -54,21 +54,21 @@ export default {
54
54
  required: ["email", "password"],
55
55
  handler: async (befly, ctx) => {
56
56
  // 检查邮箱是否已存在
57
- const exists = await befly.db.getDetail({
57
+ const existsRes = await befly.db.getOne({
58
58
  table: "user",
59
- columns: ["id"],
59
+ fields: ["id"],
60
60
  where: { email: ctx.body.email }
61
61
  });
62
62
 
63
- if (exists?.id) {
64
- return No("该邮箱已被注册");
63
+ if (existsRes.data?.id) {
64
+ return befly.tool.No("该邮箱已被注册");
65
65
  }
66
66
 
67
67
  // 加密密码
68
68
  const hashedPassword = await befly.cipher.hashPassword(ctx.body.password);
69
69
 
70
70
  // 创建用户
71
- const result = await befly.db.insData({
71
+ const idRes = await befly.db.insData({
72
72
  table: "user",
73
73
  data: {
74
74
  email: ctx.body.email,
@@ -77,7 +77,7 @@ export default {
77
77
  }
78
78
  });
79
79
 
80
- return Yes("注册成功", { id: result.insertId });
80
+ return befly.tool.Yes("注册成功", { id: idRes.data });
81
81
  }
82
82
  } as ApiRoute;
83
83
  ```
@@ -100,31 +100,33 @@ export default {
100
100
  required: ["email", "password"],
101
101
  handler: async (befly, ctx) => {
102
102
  // 查询用户
103
- const user = await befly.db.getDetail({
103
+ const userRes = await befly.db.getOne({
104
104
  table: "user",
105
- columns: ["id", "email", "password", "nickname", "avatar", "role", "state"],
105
+ fields: ["id", "email", "password", "nickname", "avatar", "role", "state", "loginCount"],
106
106
  where: { email: ctx.body.email }
107
107
  });
108
108
 
109
+ const user = userRes.data;
110
+
109
111
  if (!user?.id) {
110
- return No("用户不存在");
112
+ return befly.tool.No("用户不存在");
111
113
  }
112
114
 
113
115
  if (user.state !== 1) {
114
- return No("账户已被禁用");
116
+ return befly.tool.No("账户已被禁用");
115
117
  }
116
118
 
117
119
  // 验证密码
118
120
  const isValid = await befly.cipher.verifyPassword(ctx.body.password, user.password);
119
121
  if (!isValid) {
120
- return No("密码错误");
122
+ return befly.tool.No("密码错误");
121
123
  }
122
124
 
123
125
  // 更新登录信息
124
126
  await befly.db.updData({
125
127
  table: "user",
126
128
  data: {
127
- loginCount: user.loginCount + 1,
129
+ loginCount: (user.loginCount || 0) + 1,
128
130
  lastLoginAt: Date.now()
129
131
  },
130
132
  where: { id: user.id }
@@ -132,11 +134,11 @@ export default {
132
134
 
133
135
  // 签发令牌
134
136
  const token = befly.jwt.sign({
135
- userId: user.id,
137
+ id: user.id,
136
138
  role: user.role
137
139
  });
138
140
 
139
- return Yes("登录成功", {
141
+ return befly.tool.Yes("登录成功", {
140
142
  token: token,
141
143
  user: {
142
144
  id: user.id,
@@ -162,17 +164,19 @@ export default {
162
164
  method: "GET",
163
165
  auth: true,
164
166
  handler: async (befly, ctx) => {
165
- const user = await befly.db.getDetail({
167
+ const userRes = await befly.db.getOne({
166
168
  table: "user",
167
- columns: ["id", "email", "nickname", "avatar", "phone", "gender", "birthday", "bio", "role", "createdAt"],
168
- where: { id: ctx.user.userId }
169
+ fields: ["id", "email", "nickname", "avatar", "phone", "gender", "birthday", "bio", "role", "createdAt"],
170
+ where: { id: ctx.user?.id }
169
171
  });
170
172
 
173
+ const user = userRes.data;
174
+
171
175
  if (!user?.id) {
172
- return No("用户不存在");
176
+ return befly.tool.No("用户不存在");
173
177
  }
174
178
 
175
- return Yes("获取成功", user);
179
+ return befly.tool.Yes("获取成功", user);
176
180
  }
177
181
  } as ApiRoute;
178
182
  ```
@@ -208,16 +212,16 @@ export default {
208
212
  if (ctx.body.bio !== undefined) updateData.bio = ctx.body.bio;
209
213
 
210
214
  if (Object.keys(updateData).length === 0) {
211
- return No("没有需要更新的字段");
215
+ return befly.tool.No("没有需要更新的字段");
212
216
  }
213
217
 
214
218
  await befly.db.updData({
215
219
  table: "user",
216
220
  data: updateData,
217
- where: { id: ctx.user.userId }
221
+ where: { id: ctx.user?.id }
218
222
  });
219
223
 
220
- return Yes("更新成功");
224
+ return befly.tool.Yes("更新成功");
221
225
  }
222
226
  } as ApiRoute;
223
227
  ```
@@ -240,20 +244,22 @@ export default {
240
244
  required: ["oldPassword", "newPassword"],
241
245
  handler: async (befly, ctx) => {
242
246
  // 获取用户密码
243
- const user = await befly.db.getDetail({
247
+ const userRes = await befly.db.getOne({
244
248
  table: "user",
245
- columns: ["id", "password"],
246
- where: { id: ctx.user.userId }
249
+ fields: ["id", "password"],
250
+ where: { id: ctx.user?.id }
247
251
  });
248
252
 
253
+ const user = userRes.data;
254
+
249
255
  if (!user?.id) {
250
- return No("用户不存在");
256
+ return befly.tool.No("用户不存在");
251
257
  }
252
258
 
253
259
  // 验证原密码
254
260
  const isValid = await befly.cipher.verifyPassword(ctx.body.oldPassword, user.password);
255
261
  if (!isValid) {
256
- return No("原密码错误");
262
+ return befly.tool.No("原密码错误");
257
263
  }
258
264
 
259
265
  // 加密新密码
@@ -263,10 +269,10 @@ export default {
263
269
  await befly.db.updData({
264
270
  table: "user",
265
271
  data: { password: hashedPassword },
266
- where: { id: ctx.user.userId }
272
+ where: { id: ctx.user?.id }
267
273
  });
268
274
 
269
- return Yes("密码修改成功");
275
+ return befly.tool.Yes("密码修改成功");
270
276
  }
271
277
  } as ApiRoute;
272
278
  ```
@@ -310,14 +316,14 @@ export default {
310
316
 
311
317
  const result = await befly.db.getList({
312
318
  table: "user",
313
- columns: ["id", "email", "nickname", "avatar", "phone", "role", "state", "loginCount", "lastLoginAt", "createdAt"],
319
+ fields: ["id", "email", "nickname", "avatar", "phone", "role", "state", "loginCount", "lastLoginAt", "createdAt"],
314
320
  where: where,
315
321
  page: page || 1,
316
322
  limit: limit || 20,
317
- orderBy: { id: "desc" }
323
+ orderBy: ["id#DESC"]
318
324
  });
319
325
 
320
- return Yes("获取成功", result);
326
+ return befly.tool.Yes("获取成功", result.data);
321
327
  }
322
328
  } as ApiRoute;
323
329
  ```
@@ -398,7 +404,7 @@ export default {
398
404
  cover: cover || "",
399
405
  categoryId: categoryId || 0,
400
406
  tags: tags || [],
401
- authorId: ctx.user.userId,
407
+ authorId: ctx.user?.id,
402
408
  publishedAt: Date.now()
403
409
  }
404
410
  });
@@ -412,7 +418,7 @@ export default {
412
418
  });
413
419
  }
414
420
 
415
- return Yes("发布成功", { id: result.insertId });
421
+ return befly.tool.Yes("发布成功", { id: result.data });
416
422
  }
417
423
  } as ApiRoute;
418
424
  ```
@@ -442,19 +448,21 @@ export default {
442
448
  const { id, title, content, summary, cover, categoryId, tags } = ctx.body;
443
449
 
444
450
  // 检查文章是否存在
445
- const article = await befly.db.getDetail({
451
+ const articleRes = await befly.db.getOne({
446
452
  table: "article",
447
- columns: ["id", "authorId", "categoryId"],
453
+ fields: ["id", "authorId", "categoryId"],
448
454
  where: { id: id }
449
455
  });
450
456
 
457
+ const article = articleRes.data;
458
+
451
459
  if (!article?.id) {
452
- return No("文章不存在");
460
+ return befly.tool.No("文章不存在");
453
461
  }
454
462
 
455
463
  // 检查权限(只能编辑自己的文章,管理员除外)
456
- if (article.authorId !== ctx.user.userId && ctx.user.role !== "admin") {
457
- return No("没有权限编辑此文章");
464
+ if (article.authorId !== ctx.user?.id && ctx.user?.role !== "admin") {
465
+ return befly.tool.No("没有权限编辑此文章");
458
466
  }
459
467
 
460
468
  const updateData: Record<string, any> = {};
@@ -466,7 +474,7 @@ export default {
466
474
  if (tags !== undefined) updateData.tags = tags;
467
475
 
468
476
  if (Object.keys(updateData).length === 0) {
469
- return No("没有需要更新的字段");
477
+ return befly.tool.No("没有需要更新的字段");
470
478
  }
471
479
 
472
480
  await befly.db.updData({
@@ -493,7 +501,7 @@ export default {
493
501
  }
494
502
  }
495
503
 
496
- return Yes("更新成功");
504
+ return befly.tool.Yes("更新成功");
497
505
  }
498
506
  } as ApiRoute;
499
507
  ```
@@ -514,19 +522,21 @@ export default {
514
522
  },
515
523
  required: ["id"],
516
524
  handler: async (befly, ctx) => {
517
- const article = await befly.db.getDetail({
525
+ const articleRes = await befly.db.getOne({
518
526
  table: "article",
519
- columns: ["id", "authorId", "categoryId"],
527
+ fields: ["id", "authorId", "categoryId"],
520
528
  where: { id: ctx.body.id }
521
529
  });
522
530
 
531
+ const article = articleRes.data;
532
+
523
533
  if (!article?.id) {
524
- return No("文章不存在");
534
+ return befly.tool.No("文章不存在");
525
535
  }
526
536
 
527
537
  // 检查权限
528
- if (article.authorId !== ctx.user.userId && ctx.user.role !== "admin") {
529
- return No("没有权限删除此文章");
538
+ if (article.authorId !== ctx.user?.id && ctx.user?.role !== "admin") {
539
+ return befly.tool.No("没有权限删除此文章");
530
540
  }
531
541
 
532
542
  // 软删除
@@ -544,7 +554,7 @@ export default {
544
554
  });
545
555
  }
546
556
 
547
- return Yes("删除成功");
557
+ return befly.tool.Yes("删除成功");
548
558
  }
549
559
  } as ApiRoute;
550
560
  ```
@@ -585,20 +595,20 @@ export default {
585
595
  }
586
596
 
587
597
  // 排序
588
- let order: Record<string, "asc" | "desc"> = { isTop: "desc", publishedAt: "desc" };
589
- if (orderBy === "views") order = { viewCount: "desc" };
590
- if (orderBy === "likes") order = { likeCount: "desc" };
598
+ let orderByList: string[] = ["isTop#DESC", "publishedAt#DESC"];
599
+ if (orderBy === "views") orderByList = ["viewCount#DESC"];
600
+ if (orderBy === "likes") orderByList = ["likeCount#DESC"];
591
601
 
592
602
  const result = await befly.db.getList({
593
603
  table: "article",
594
- columns: ["id", "title", "summary", "cover", "categoryId", "tags", "authorId", "viewCount", "likeCount", "commentCount", "isTop", "isRecommend", "publishedAt"],
604
+ fields: ["id", "title", "summary", "cover", "categoryId", "tags", "authorId", "viewCount", "likeCount", "commentCount", "isTop", "isRecommend", "publishedAt"],
595
605
  where: where,
596
606
  page: page || 1,
597
607
  limit: limit || 10,
598
- orderBy: order
608
+ orderBy: orderByList
599
609
  });
600
610
 
601
- return Yes("获取成功", result);
611
+ return befly.tool.Yes("获取成功", result.data);
602
612
  }
603
613
  } as ApiRoute;
604
614
  ```
@@ -619,13 +629,15 @@ export default {
619
629
  },
620
630
  required: ["id"],
621
631
  handler: async (befly, ctx) => {
622
- const article = await befly.db.getDetail({
632
+ const articleRes = await befly.db.getOne({
623
633
  table: "article",
624
634
  where: { id: ctx.body.id, state: 1 }
625
635
  });
626
636
 
637
+ const article = articleRes.data;
638
+
627
639
  if (!article?.id) {
628
- return No("文章不存在");
640
+ return befly.tool.No("文章不存在");
629
641
  }
630
642
 
631
643
  // 增加浏览量
@@ -636,25 +648,41 @@ export default {
636
648
  });
637
649
 
638
650
  // 获取作者信息
639
- const author = await befly.db.getDetail({
651
+ const authorRes = await befly.db.getOne({
640
652
  table: "user",
641
- columns: ["id", "nickname", "avatar"],
653
+ fields: ["id", "nickname", "avatar"],
642
654
  where: { id: article.authorId }
643
655
  });
644
656
 
657
+ const author = authorRes.data;
658
+
645
659
  // 获取分类信息
646
660
  let category = null;
647
661
  if (article.categoryId) {
648
- category = await befly.db.getDetail({
662
+ const categoryRes = await befly.db.getOne({
649
663
  table: "category",
650
- columns: ["id", "name", "slug"],
664
+ fields: ["id", "name", "slug"],
651
665
  where: { id: article.categoryId }
652
666
  });
667
+
668
+ category = categoryRes.data;
653
669
  }
654
670
 
655
- return Yes("获取成功", {
656
- ...article,
657
- viewCount: article.viewCount + 1,
671
+ return befly.tool.Yes("获取成功", {
672
+ id: article.id,
673
+ title: article.title,
674
+ content: article.content,
675
+ summary: article.summary,
676
+ cover: article.cover,
677
+ categoryId: article.categoryId,
678
+ tags: article.tags,
679
+ authorId: article.authorId,
680
+ viewCount: (article.viewCount || 0) + 1,
681
+ likeCount: article.likeCount,
682
+ commentCount: article.commentCount,
683
+ isTop: article.isTop,
684
+ isRecommend: article.isRecommend,
685
+ publishedAt: article.publishedAt,
658
686
  author: author,
659
687
  category: category
660
688
  });
@@ -684,18 +712,18 @@ export default {
684
712
  const file = formData.get("file") as File | null;
685
713
 
686
714
  if (!file) {
687
- return No("请选择文件");
715
+ return befly.tool.No("请选择文件");
688
716
  }
689
717
 
690
718
  // 检查文件大小(10MB)
691
719
  if (file.size > 10 * 1024 * 1024) {
692
- return No("文件大小不能超过 10MB");
720
+ return befly.tool.No("文件大小不能超过 10MB");
693
721
  }
694
722
 
695
723
  // 检查文件类型
696
724
  const allowedTypes = ["image/jpeg", "image/png", "image/gif", "image/webp"];
697
725
  if (!allowedTypes.includes(file.type)) {
698
- return No("只支持 jpg/png/gif/webp 格式");
726
+ return befly.tool.No("只支持 jpg/png/gif/webp 格式");
699
727
  }
700
728
 
701
729
  // 生成文件名
@@ -716,7 +744,7 @@ export default {
716
744
  // 返回 URL
717
745
  const url = `/uploads/${new Date().toISOString().slice(0, 7)}/${fileName}`;
718
746
 
719
- return Yes("上传成功", {
747
+ return befly.tool.Yes("上传成功", {
720
748
  url: url,
721
749
  name: file.name,
722
750
  size: file.size,
@@ -746,7 +774,7 @@ export default {
746
774
  // 获取所有用户
747
775
  const result = await befly.db.getList({
748
776
  table: "user",
749
- columns: ["id", "email", "nickname", "phone", "role", "state", "createdAt"],
777
+ fields: ["id", "email", "nickname", "phone", "role", "state", "createdAt"],
750
778
  where: { state: { $gte: 0 } },
751
779
  page: 1,
752
780
  limit: 10000
@@ -754,7 +782,7 @@ export default {
754
782
 
755
783
  // 生成 CSV
756
784
  const headers = ["ID", "邮箱", "昵称", "手机号", "角色", "状态", "注册时间"];
757
- const rows = result.list.map((user: any) => [user.id, user.email, user.nickname, user.phone || "", user.role, user.state === 1 ? "正常" : "禁用", new Date(user.createdAt).toLocaleString()]);
785
+ const rows = result.data.lists.map((user: any) => [user.id, user.email, user.nickname, user.phone || "", user.role, user.state === 1 ? "正常" : "禁用", new Date(user.createdAt).toLocaleString()]);
758
786
 
759
787
  const csv = [headers.join(","), ...rows.map((row: any[]) => row.map((cell) => `"${cell}"`).join(","))].join("\n");
760
788
 
@@ -794,43 +822,49 @@ if (Object.keys(updateData).length === 0) {
794
822
  await befly.db.updData({
795
823
  table: "user",
796
824
  data: updateData,
797
- where: { id: ctx.user.userId }
825
+ where: { id: ctx.user?.id }
798
826
  });
799
827
  ```
800
828
 
801
829
  #### 优化写法(简洁)
802
830
 
803
831
  ```typescript
804
- // 直接传入,undefined 值自动过滤
805
- const { nickname, avatar, phone, gender, birthday, bio } = ctx.body;
832
+ import { fieldClear } from "befly/utils/fieldClear";
806
833
 
807
- const data = { nickname: nickname, avatar: avatar, phone: phone, gender: gender, birthday: birthday, bio: bio };
808
-
809
- // 使用 cleanFields 检查是否有有效数据
810
- const cleanData = befly.tool.cleanFields(data);
834
+ // 直接传入,undefined 值自动过滤
835
+ const data = {
836
+ nickname: ctx.body.nickname,
837
+ avatar: ctx.body.avatar,
838
+ phone: ctx.body.phone,
839
+ gender: ctx.body.gender,
840
+ birthday: ctx.body.birthday,
841
+ bio: ctx.body.bio
842
+ };
843
+
844
+ // 使用 fieldClear 检查是否有有效数据
845
+ const cleanData = fieldClear(data, { excludeValues: [null, undefined] });
811
846
  if (Object.keys(cleanData).length === 0) {
812
- return No("没有需要更新的字段");
847
+ return befly.tool.No("没有需要更新的字段");
813
848
  }
814
849
 
815
850
  await befly.db.updData({
816
851
  table: "user",
817
852
  data: cleanData,
818
- where: { id: ctx.user.userId }
853
+ where: { id: ctx.user?.id }
819
854
  });
820
855
  ```
821
856
 
822
- ### 使用 cleanFields 进行精细控制
857
+ ### 使用 fieldClear 进行精细控制
823
858
 
824
- 当需要保留特定值(如 0、空字符串)时,使用 `cleanFields` 的高级参数:
859
+ 当需要保留特定值(如 0、空字符串)时,使用 `fieldClear` 的高级参数:
825
860
 
826
861
  ```typescript
827
862
  const { nickname, sort, state, remark } = ctx.body;
828
863
 
829
864
  // 保留 0 值(sort 和 state 允许为 0)
830
- const data = befly.tool.cleanFields(
865
+ const data = fieldClear(
831
866
  { nickname: nickname, sort: sort, state: state, remark: remark },
832
- [null, undefined], // 排除 null undefined
833
- { sort: true, state: true } // 保留这些字段的 0 值
867
+ { excludeValues: [null, undefined], keepMap: { sort: 0, state: 0 } }
834
868
  );
835
869
 
836
870
  await befly.db.updData({
@@ -850,7 +884,7 @@ const { keyword, state, categoryId, startDate, endDate } = ctx.body;
850
884
 
851
885
  const result = await befly.db.getList({
852
886
  table: "article",
853
- columns: ["id", "title", "createdAt"],
887
+ fields: ["id", "title", "createdAt"],
854
888
  where: {
855
889
  state: state, // undefined 时忽略
856
890
  categoryId: categoryId, // undefined 时忽略
@@ -87,26 +87,28 @@ export default {
87
87
  required: ["email", "password"],
88
88
  handler: async (befly, ctx) => {
89
89
  // 查询用户
90
- const user = await befly.db.getDetail({
90
+ const userRes = await befly.db.getOne({
91
91
  table: "user",
92
- columns: ["id", "email", "password", "nickname"],
92
+ fields: ["id", "email", "password", "nickname"],
93
93
  where: { email: ctx.body.email }
94
94
  });
95
95
 
96
+ const user = userRes.data;
97
+
96
98
  if (!user?.id) {
97
- return No("用户不存在");
99
+ return befly.tool.No("用户不存在");
98
100
  }
99
101
 
100
102
  // 验证密码
101
103
  const isValid = await befly.cipher.verifyPassword(ctx.body.password, user.password);
102
104
  if (!isValid) {
103
- return No("密码错误");
105
+ return befly.tool.No("密码错误");
104
106
  }
105
107
 
106
108
  // 签发令牌
107
- const token = befly.jwt.sign({ userId: user.id });
109
+ const token = befly.jwt.sign({ id: user.id });
108
110
 
109
- return Yes("登录成功", { token: token, user: { id: user.id, nickname: user.nickname } });
111
+ return befly.tool.Yes("登录成功", { token: token, user: { id: user.id, nickname: user.nickname } });
110
112
  }
111
113
  } as ApiRoute;
112
114
  ```