🐛修复了一些bug

This commit is contained in:
coward 2024-05-17 17:30:26 +08:00
parent eed038bf01
commit e216725096
6 changed files with 69 additions and 13 deletions

View File

@ -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()
}

View File

@ -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

View File

@ -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
}

View File

@ -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) // 删除用户
}
}

View File

@ -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,

View File

@ -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 {