refactor: ♻️ 规范变量名称

main
熊二 2 years ago
parent 88668e63c3
commit e866ed3e3b
  1. 44
      app/rts.go

@ -52,14 +52,14 @@ func (u *userInfo) Bind(r *Request) error {
// CreateUser 创建用户
func CreateUser(w *ResponseWriter, r *Request) {
var user userInfo
if err := user.Bind(r); err != nil {
var ui userInfo
if err := ui.Bind(r); err != nil {
w.Error(err)
return
}
var count int64
if err := DB.Model(&User{}).Where("phone_number = ?", user.PhoneNumber).Count(&count).Error; err != nil {
if err := DB.Model(&User{}).Where("phone_number = ?", ui.PhoneNumber).Count(&count).Error; err != nil {
w.Error(err)
return
}
@ -68,22 +68,22 @@ func CreateUser(w *ResponseWriter, r *Request) {
return
}
hash, err := HashPassword(user.Password)
hash, err := HashPassword(ui.Password)
if err != nil {
LogError(err)
w.Error(NewError(1, "加密密码失败"))
return
}
u := User{
Name: user.Name,
PhoneNumber: user.PhoneNumber,
user := User{
Name: ui.Name,
PhoneNumber: ui.PhoneNumber,
Password: hash,
}
if err := DB.Create(&u).Error; err != nil {
if err := DB.Create(&user).Error; err != nil {
w.Error(err)
} else {
w.Ok(user, "创建用户成功")
w.Ok(ui, "创建用户成功")
}
}
@ -93,15 +93,15 @@ func UpdateUser(w *ResponseWriter, r *Request) {
if len(id) == 0 {
w.Error(NewError(1, "缺少用户ID"))
}
var user userInfo
if err := user.Bind(r); err != nil {
var ui userInfo
if err := ui.Bind(r); err != nil {
w.Error(err)
return
}
// 查询用户信息
var u User
if err := DB.First(&u, "id = ?", id).Error; err != nil {
var user User
if err := DB.First(&user, "id = ?", id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
err = NewError(1, "用户不存在")
}
@ -110,14 +110,14 @@ func UpdateUser(w *ResponseWriter, r *Request) {
}
// 用户信息未发生变化
if user.Name == u.Name && user.PhoneNumber == u.PhoneNumber && CheckPasswordHash(user.Password, u.Password) {
if ui.Name == user.Name && ui.PhoneNumber == user.PhoneNumber && CheckPasswordHash(ui.Password, user.Password) {
w.Ok(nil, "操作成功")
return
}
// 检查手机号码是否被使用
var count int64
if err := DB.Model(&User{}).Where("phone_number = ? AND id != ?", user.PhoneNumber, id).Count(&count).Error; err != nil {
if err := DB.Model(&User{}).Where("phone_number = ? AND id != ?", ui.PhoneNumber, id).Count(&count).Error; err != nil {
w.Error(err)
return
}
@ -127,10 +127,10 @@ func UpdateUser(w *ResponseWriter, r *Request) {
}
// 保存用户信息
u.Name = user.Name
u.PhoneNumber = user.PhoneNumber
u.Password = user.Password
if err := DB.Save(&u).Error; err != nil {
user.Name = ui.Name
user.PhoneNumber = ui.PhoneNumber
user.Password = ui.Password
if err := DB.Save(&user).Error; err != nil {
w.Error(err)
} else {
w.Ok(nil, "操作成功")
@ -145,8 +145,8 @@ func DeleteUser(w *ResponseWriter, r *Request) {
}
// 查询用户信息
var u User
if err := DB.First(&u, "id = ?", id).Error; err != nil {
var user User
if err := DB.First(&user, "id = ?", id).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
err = NewError(1, "用户不存在")
}
@ -155,7 +155,7 @@ func DeleteUser(w *ResponseWriter, r *Request) {
}
// 删除用户
if err := DB.Delete(&u).Error; err != nil {
if err := DB.Delete(&user).Error; err != nil {
LogError(err)
w.Error(NewError(1, "删除用户失败"))
} else {

Loading…
Cancel
Save