feat: support set user phone
This commit is contained in:
@@ -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