feat: support set user phone
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -42,9 +42,12 @@ func (m *users) List(
|
||||
conds ...gen.Condition,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
_, query := models.UserQuery.QueryContext(ctx)
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
|
||||
items, cnt, err := query.Where(conds...).FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
||||
items, cnt, err := query.
|
||||
Where(conds...).
|
||||
Order(tbl.ID.Desc()).
|
||||
FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "query users error")
|
||||
}
|
||||
@@ -361,3 +364,35 @@ func (m *users) ValidatePhoneCode(ctx context.Context, phone, code string) (*mod
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// SetPhone 管理端设置用户手机号。
|
||||
func (m *users) SetPhone(ctx context.Context, userID int64, phone string) error {
|
||||
phone = strings.TrimSpace(phone)
|
||||
if phone == "" {
|
||||
return errors.New("手机号不能为空")
|
||||
}
|
||||
if len(phone) != 11 {
|
||||
return errors.New("手机号必须为 11 位数字")
|
||||
}
|
||||
for _, r := range phone {
|
||||
if r < '0' || r > '9' {
|
||||
return errors.New("手机号必须为 11 位数字")
|
||||
}
|
||||
}
|
||||
|
||||
// 业务约束:手机号建议全局唯一(至少在本系统内),避免登录/验证身份混淆。
|
||||
tbl, query := models.UserQuery.QueryContext(ctx)
|
||||
_, err := query.Where(tbl.Phone.Eq(phone), tbl.ID.Neq(userID)).First()
|
||||
if err == nil {
|
||||
return errors.New("手机号已被其他用户占用")
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(err, "failed to check phone uniqueness")
|
||||
}
|
||||
|
||||
// 仅更新 phone 字段,避免覆盖其它字段。
|
||||
if _, err := query.Where(tbl.ID.Eq(userID)).Update(tbl.Phone, phone); err != nil {
|
||||
return errors.Wrap(err, "failed to update user phone")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user