diff --git a/app/rts.go b/app/rts.go index 6e9a689..10b511c 100644 --- a/app/rts.go +++ b/app/rts.go @@ -152,7 +152,28 @@ func UpdateUser(w *ResponseWriter, r *Request) { // DeleteUser 删除用户 func DeleteUser(w *ResponseWriter, r *Request) { + id := chi.URLParam(r.Request, "id") + if len(id) == 0 { + w.Error(NewError(1, "缺少用户ID")) + } + + // 查询用户信息 + var u User + if err := DB.First(&u, "id = ?", id).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + err = NewError(1, "用户不存在") + } + w.Error(err) + return + } + // 删除用户 + if err := DB.Delete(&u).Error; err != nil { + LogError(err) + w.Error(NewError(1, "删除用户失败")) + } else { + w.Ok(nil, "删除用户成功") + } } // ListUser 用户列表