Skip to content

Commit

Permalink
修改角色功能
Browse files Browse the repository at this point in the history
  • Loading branch information
xufqing committed Sep 23, 2020
1 parent 1417b47 commit 96f1699
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion api/v1/sys_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func GetAllMenuByRoleId(c *gin.Context) {
}
var resp response.MenuTreeWithAccessResp
resp.AccessIds = ids
utils.Struct2StructByJson(menus, &resp.List)
utils.Struct2StructByJson(menus, &resp.DataList)
response.SuccessWithData(resp)
}

Expand Down
19 changes: 16 additions & 3 deletions api/v1/sys_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,23 @@ func GetRoles(c *gin.Context) {
response.FailWithMsg(err.Error())
return
}

// 返回树结构
//for _,menu := range roles{
// menu.Menus = service.GenMenuTree(nil,menu.Menus)
//}

// 转为ResponseStruct, 隐藏部分字段
var respStruct []response.RoleListResp
utils.Struct2StructByJson(roles, &respStruct)
response.SuccessWithData(respStruct)

// 返回分页数据
var resp response.PageData
// 设置分页参数
resp.PageInfo = req.PageInfo
// 设置数据列表
resp.DataList = respStruct
response.SuccessWithData(resp)
}

// 创建角色
Expand Down Expand Up @@ -92,7 +105,7 @@ func UpdateRoleById(c *gin.Context) {
// 更新角色的权限菜单
func UpdateRoleMenusById(c *gin.Context) {
// 绑定参数
var req request.UpdateIncrementalIdsReq
var req request.IdsReq
err := c.Bind(&req)
if err != nil {
response.FailWithMsg(fmt.Sprintf("参数绑定失败, %v", err))
Expand All @@ -107,7 +120,7 @@ func UpdateRoleMenusById(c *gin.Context) {
// 创建服务
s := service.New(c)
// 更新数据
err = s.UpdateRoleMenusById(roleId, req)
err = s.UpdateRoleMenusById(roleId, req.Ids)
if err != nil {
response.FailWithMsg(err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion dto/response/sys_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ type MenuTreeResp struct {

// 菜单树信息响应, 包含有权限访问的id列表
type MenuTreeWithAccessResp struct {
List []MenuTreeResp `json:"list"`
DataList []MenuTreeResp `json:"dataList"`
AccessIds []uint `json:"accessIds"`
}
10 changes: 10 additions & 0 deletions dto/response/sys_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ import (
"anew-server/models"
)

// 角色返回菜单信息
type RoleMenusResp struct {
Id uint `json:"id"`
Name string `json:"name"`
ParentId uint `json:"parentId"`
Children []RoleMenusResp `json:"children"`
Status *bool `json:"status"`
}

// 角色信息响应, 字段含义见models
type RoleListResp struct {
Id uint `json:"id"`
Name string `json:"name"`
Keyword string `json:"keyword"`
Desc string `json:"desc"`
Status *bool `json:"status"`
Menus []RoleMenusResp `json:"menus"`
Creator string `json:"creator"`
CreatedAt models.LocalTime `json:"createdAt"`
}
40 changes: 15 additions & 25 deletions dto/service/sys_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ func (s *MysqlService) GetRoles(req *request.RoleListReq) ([]models.SysRole, err
db = db.Where("status = ?", 0)
}
}

if req.PageInfo.All {
// 不使用分页
err = db.Find(&list).Error
} else {
// 获取分页参数
limit, offset := req.GetLimit()
err = db.Limit(limit).Offset(offset).Find(&list).Error
// 查询条数
err = db.Find(&list).Count(&req.PageInfo.Total).Error
if err == nil {
if req.PageInfo.All {
// 不使用分页
err = db.Preload("Menus").Find(&list).Error
} else {
// 获取分页参数
limit, offset := req.GetLimit()
err = db.Preload("Menus").Limit(limit).Offset(offset).Find(&list).Error
}
}

return list, err
}

Expand Down Expand Up @@ -75,26 +77,14 @@ func (s *MysqlService) UpdateRoleById(id uint, req gin.H) (err error) {
}

// 更新角色的权限菜单
func (s *MysqlService) UpdateRoleMenusById(id uint, req request.UpdateIncrementalIdsReq) (err error) {
// 查询全部菜单
allMenu := s.getAllMenu()
// 查询角色拥有菜单
roleMenus := s.getRoleMenus(id)
// 获取当前菜单编号集合
menuIds := make([]uint, 0)
for _, menu := range roleMenus {
menuIds = append(menuIds, menu.Id)
}
// 获取菜单增量
incremental := req.GetIncremental(menuIds, allMenu)
// 查询所有菜单
var incrementalMenus []models.SysMenu
err = s.tx.Where("id in (?)", incremental).Find(&incrementalMenus).Error
func (s *MysqlService) UpdateRoleMenusById(id uint, req []uint) (err error) {
var menus []models.SysMenu
err = s.tx.Where("id in (?)", req).Find(&menus).Error
if err != nil {
return
}
// 替换菜单
err = s.tx.Where("id = ?", id).First(&models.SysRole{}).Association("Menus").Replace(&incrementalMenus).Error
err = s.tx.Where("id = ?", id).First(&models.SysRole{}).Association("Menus").Replace(&menus).Error
return
}

Expand Down

0 comments on commit 96f1699

Please sign in to comment.