diff --git a/http/api/user.go b/http/api/user.go index ffc7c5c..3457d58 100644 --- a/http/api/user.go +++ b/http/api/user.go @@ -166,8 +166,8 @@ func (user) Save(c *gin.Context) { Account: p.Account, Email: p.Email, Password: p.Password, - IsAdmin: p.IsAdmin, - Status: p.Status, + IsAdmin: *p.IsAdmin, + Status: *p.Status, }); err != nil { utils.GinResponse(c).FailedWithMsg(err.Error()) return @@ -225,3 +225,28 @@ func (user) ChangeUserState(c *gin.Context) { utils.GinResponse(c).OK() } + +// DeleteUser +// @description: 删除用户 +// @receiver user +// @param c +func (user) DeleteUser(c *gin.Context) { + var id = c.Param("id") + if id == "" || id == "undefined" { + utils.GinResponse(c).FailedWithMsg("参数错误") + return + } + + loginUser, ok := c.Get("user") + if !ok { + utils.GinResponse(c).FailedWithMsg("获取信息失败") + return + } + + if err := repository.User().DeleteUser(loginUser.(*entity.User), id); err != nil { + utils.GinResponse(c).FailedWithErr("操作失败", err) + return + } + + utils.GinResponse(c).OK() +} diff --git a/http/param/user.go b/http/param/user.go index f4a916b..43a9893 100644 --- a/http/param/user.go +++ b/http/param/user.go @@ -14,14 +14,14 @@ type Login struct { // SaveUser // @description: 新增/编辑用户信息 type SaveUser struct { - ID string `json:"id" form:"id" binding:"omitempty"` - Name string `json:"name" form:"name" binding:"required"` // 用户名 - Account string `json:"account" form:"account" binding:"required"` // 账号 唯一 - Avatar string `json:"avatar" form:"avatar" binding:"omitempty"` // 头像 - Email string `json:"email" form:"email" binding:"omitempty"` // 联系邮箱 - Password string `json:"password" form:"password" binding:"omitempty"` // 密码 - IsAdmin constant.UserType `json:"isAdmin" form:"isAdmin" binding:"omitempty"` // 是否为管理员 0 - 否 | 1 - 是 - Status constant.UserStatus `json:"status" form:"status" binding:"required"` // 用户状态 0 - 禁用 | 1 - 正常 + ID string `json:"id" form:"id" binding:"omitempty"` + Name string `json:"name" form:"name" binding:"required"` // 用户名 + Account string `json:"account" form:"account" binding:"required"` // 账号 唯一 + Avatar string `json:"avatar" form:"avatar" binding:"omitempty"` // 头像 + Email string `json:"email" form:"email" binding:"omitempty"` // 联系邮箱 + Password string `json:"password" form:"password" binding:"omitempty"` // 密码 + IsAdmin *constant.UserType `json:"isAdmin" form:"isAdmin" binding:"omitempty"` // 是否为管理员 0 - 否 | 1 - 是 + Status *constant.UserStatus `json:"status" form:"status" binding:"required"` // 用户状态 0 - 禁用 | 1 - 正常 } // ChangePassword diff --git a/repository/user.go b/repository/user.go index 793548c..7010718 100644 --- a/repository/user.go +++ b/repository/user.go @@ -1,8 +1,10 @@ package repository import ( + "errors" "gorm.io/gorm" "wireguard-dashboard/client" + "wireguard-dashboard/constant" "wireguard-dashboard/http/param" "wireguard-dashboard/model/entity" "wireguard-dashboard/model/vo" @@ -28,7 +30,7 @@ func User() user { // @return err func (r user) List(p param.UserList) (data []vo.User, total int64, err error) { err = r.Model(&entity.User{}).Scopes(utils.Page(p.Current, p.Size)). - Select("id", "created_at", "updated_at", "avatar", "name", "account", "is_admin", "status").Order("created_at DESC"). + Select("id", "created_at", "updated_at", "avatar", "email", "name", "account", "is_admin", "status").Order("created_at DESC"). Find(&data).Offset(-1).Limit(-1).Count(&total).Error return @@ -110,3 +112,31 @@ func (r user) ChangePassword(p param.ChangePassword, userId string) (err error) func (r user) ChangeUserState(p param.ChangeUserState) (err error) { return r.Model(&entity.User{}).Where("id = ?", p.ID).Update("status", p.Status).Error } + +// DeleteUser +// @description: 删除管理员 +// @receiver r +// @param id +// @return err +func (r user) DeleteUser(loginUser *entity.User, id string) (err error) { + // 不能删除自身以及超级管理员,超级管理员只有 名为admin的管理员可以删除 + userInfo, err := r.GetUserById(id) + if err != nil { + return + } + + if userInfo.Id == loginUser.Id { + return errors.New("不可删除自己") + } + + if userInfo.IsAdmin == constant.SuperAdmin && loginUser.Account != "admin" { + return errors.New("非无敌管理员不可清空超管") + } + + if userInfo.Account == "admin" { + return errors.New("不可删除宇宙第一无敌管理员,删了你就G了!") + } + + // 可删除 + return r.Model(&entity.User{}).Where("id = ?", id).Delete(userInfo).Error +} diff --git a/route/user.go b/route/user.go index 7b61efe..7b615ce 100644 --- a/route/user.go +++ b/route/user.go @@ -22,5 +22,6 @@ func UserApi(r *gin.RouterGroup) { userApi.POST("change-password", api.UserApi().ChangePassword) // 更改密码 userApi.GET("list", middleware.Permission(), api.UserApi().List) // 用户列表 userApi.PUT("change-status", middleware.Permission(), api.UserApi().ChangeUserState) // 变更状态 + userApi.DELETE("delete/:id", middleware.Permission(), api.UserApi().DeleteUser) // 删除用户 } } diff --git a/script/script.go b/script/script.go index 195c8ac..87d529f 100644 --- a/script/script.go +++ b/script/script.go @@ -77,7 +77,7 @@ func (s Script) CreateSuperAdmin() error { if err = repository.User().Save(&entity.User{ Avatar: avatarPath, Name: "超牛管理员", - Account: "Admin", + Account: "admin", Email: "", Password: "admin123", IsAdmin: constant.SuperAdmin, diff --git a/utils/avatar.go b/utils/avatar.go index 2d31452..ea580bb 100644 --- a/utils/avatar.go +++ b/utils/avatar.go @@ -22,7 +22,7 @@ func Avatar() avatar { func (avatar) GenerateAvatar() (path string, err error) { rand.New(rand.NewSource(time.Now().UnixNano())) r := client.HttpClient.R() - result, err := r.Get(fmt.Sprintf("https://api.dicebear.com/7.x/croodles/png?seed=%d&scale=100&size=80&clip=true&randomizeIds=true&beard=variant01,variant02,variant03&"+ + result, err := r.Get(fmt.Sprintf("https://api.dicebear.com/7.x/croodles/png?seed=%d&scale=120&size=200&clip=true&randomizeIds=true&beard=variant01,variant02,variant03&"+ "eyes=variant01,variant02,variant03,variant04,variant05,variant06,variant07,variant08,variant09,variant10,variant11,variant12&mustache=variant01,variant02,variant03&"+ "topColor=000000,0fa958,699bf7", rand.Uint32())) if err != nil {