feat: support set user phone

This commit is contained in:
2025-12-20 14:27:29 +08:00
parent 8e95dada82
commit 0185e51396
5 changed files with 135 additions and 6 deletions

View File

@@ -193,6 +193,15 @@ func (r *Routes) Register(router fiber.Router) {
},
Body[UserBalance]("balance"),
))
r.log.Debugf("Registering route: Post /admin/v1/users/:id/phone -> users.SetPhone")
router.Post("/admin/v1/users/:id/phone"[len(r.Path()):], Func2(
r.users.SetPhone,
func(ctx fiber.Ctx) (*models.User, error) {
v := fiber.Params[int](ctx, "id")
return models.UserQuery.WithContext(ctx).Where(field.NewUnsafeFieldRaw("id = ?", v)).First()
},
Body[UserPhoneForm]("form"),
))
r.log.Info("Successfully registered all routes")
}

View File

@@ -29,8 +29,11 @@ type users struct{}
// @Bind pagination query
// @Bind query query
func (ctl *users) List(ctx fiber.Ctx, pagination *requests.Pagination, query *UserListQuery) (*requests.Pager, error) {
conds := []gen.Condition{
models.UserQuery.Username.Like(database.WrapLike(*query.Keyword)),
conds := []gen.Condition{}
if query.Keyword != nil && *query.Keyword != "" {
conds = append(conds,
models.UserQuery.Username.Like(database.WrapLike(*query.Keyword)),
)
}
return services.Users.List(ctx, pagination, conds...)
}
@@ -67,6 +70,10 @@ type UserBalance struct {
Balance int64 `json:"balance"`
}
type UserPhoneForm struct {
Phone string `json:"phone"` // 用户手机号11 位数字)
}
// Balance
//
// @Summary 调整用户余额
@@ -82,3 +89,19 @@ type UserBalance struct {
func (ctl *users) Balance(ctx fiber.Ctx, user *models.User, balance *UserBalance) error {
return services.Users.AddBalance(ctx, user.ID, balance.Balance)
}
// SetPhone
//
// @Summary 设置用户手机号
// @Tags Admin Users
// @Accept json
// @Produce json
// @Param id path int64 true "用户 ID"
// @Param form body UserPhoneForm true "请求体"
// @Success 200 {object} any "成功"
// @Router /admin/v1/users/:id/phone [post]
// @Bind user path key(id) model(id)
// @Bind form body
func (ctl *users) SetPhone(ctx fiber.Ctx, user *models.User, form *UserPhoneForm) error {
return services.Users.SetPhone(ctx, user.ID, form.Phone)
}