From a48c1056c1f885ecc438da955a2c6004b95a2621 Mon Sep 17 00:00:00 2001 From: hupeh Date: Mon, 2 Jan 2023 12:04:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E6=9B=B4=E6=96=B0=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/rts.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/app/rts.go b/app/rts.go index bac6ee9..6e9a689 100644 --- a/app/rts.go +++ b/app/rts.go @@ -110,6 +110,44 @@ func UpdateUser(w *ResponseWriter, r *Request) { return } + // 查询用户信息 + var u User + err = DB.First(&u, "id = ?", id).Error + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + err = NewError(1, "用户不存在") + } + w.Error(err) + return + } + + // 用户信息未发生变化 + if u.Name == user.Name && u.PhoneNumber == u.PhoneNumber && u.Password == user.Password { + w.Ok(nil, "操作成功") + return + } + + // 检查手机号码是否被使用 + var count int64 + err = DB.Model(&User{}).Where("phone_number = ? AND id != ?", user.PhoneNumber, id).Count(&count).Error + if err != nil { + w.Error(err) + return + } + if count > 0 { + w.Error(NewError(1, "手机号码已被使用")) + return + } + + // 保存用户信息 + u.Name = user.Name + u.PhoneNumber = user.PhoneNumber + u.Password = user.Password + if err := DB.Save(&u).Error; err != nil { + w.Error(err) + } else { + w.Ok(nil, "操作成功") + } } // DeleteUser 删除用户