瀏覽代碼

角色管理,用户管理

longan 2 年之前
父節點
當前提交
d13dcc90cc

+ 1 - 1
app/system/controller/menu.go

@@ -145,7 +145,7 @@ func (c *menu) Tree(ctx *gin.Context) {
 
 func (c *menu) GrantTree(ctx *gin.Context) {
 	//菜单权限
-	tree, err := service.MenuService.GetNameTree()
+	tree, err := service.MenuService.GetTree()
 	if err != nil {
 		ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
 		return

+ 2 - 11
app/system/controller/role.go

@@ -59,13 +59,13 @@ func (c *role) List(ctx *gin.Context) {
 }
 
 func (c *role) Tree(ctx *gin.Context) {
-	role, err := service.RoleService.Tree()
+	roleTree, err := service.RoleService.GetTree()
 	if err != nil {
 		ctx.JSON(http.StatusOK, err)
 		return
 	}
 
-	ctx.JSON(http.StatusOK, role)
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, roleTree))
 }
 
 func (c *role) Submit(ctx *gin.Context) {
@@ -79,15 +79,6 @@ func (c *role) Submit(ctx *gin.Context) {
 }
 
 func (c *role) Remove(ctx *gin.Context) {
-	//参数在url后面时:
-	i, _ := strconv.Atoi(ctx.Query("ids"))
-	ids := int64(i)
-	if ids > 0 {
-		err := service.RoleService.Remove(ids)
-		ctx.JSON(http.StatusOK, err)
-		return
-	}
-	//参数在请求体里面时:
 	var req *model.ReqRoleRemove
 	if err := ctx.ShouldBindJSON(&req); err != nil {
 		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))

+ 5 - 1
app/system/controller/user.go

@@ -78,7 +78,11 @@ func (c *user) Update(ctx *gin.Context) {
 		return
 	}
 	err := service.UserService.Update(req)
-	ctx.JSON(http.StatusOK, err)
+	if err == nil {
+		ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
+	} else {
+		ctx.JSON(http.StatusOK, err)
+	}
 }
 
 func (c *user) Remove(ctx *gin.Context) {

+ 6 - 0
app/system/dao/role.go

@@ -73,3 +73,9 @@ func (c *Role) UpdateRoles(RoleIds []string, roleIds string) error {
 	err := Db.Debug().Model(&c).Where("id in ?", RoleIds).Updates(map[string]interface{}{"role_id": roleIds}).Error
 	return err
 }
+
+func (c *Role) GetByParentId() ([]Role, error) {
+	var Roles []Role
+	err := Db.Debug().Model(&c).Where("is_deleted = 0 AND parent_id = ?", c.ParentId).Find(&Roles).Error
+	return Roles, err
+}

+ 13 - 2
app/system/model/role.go

@@ -28,12 +28,23 @@ type RspRoleTree struct {
 
 type RoleDetail struct {
 	HasChildren bool         `json:"hasChildren"`
-	ID          string       `json:"id"`
-	ParentId    string       `json:"parentId"`
+	ID          int64        `json:"id"`
+	ParentId    int64        `json:"parentId"`
 	Title       string       `json:"title"`
 	Key         string       `json:"key"`
 	Value       string       `json:"value"`
 	Children    []RoleDetail `json:"children"`
 }
 
+// 绑定前端json数据的中间结构体
+type BindRole struct {
+	ID        int64  `json:"id"`
+	TenantId  string `json:"tenantId"`
+	ParentId  string `json:"parentId"`
+	RoleName  string `json:"roleName"`
+	Sort      int    `json:"sort"`
+	RoleAlias string `json:"roleAlias"`
+	IsDeleted int    `json:"isDeleted"`
+}
+
 const ExistChild = "需要删除的记录中存在子角色,请先删除子角色!"

+ 4 - 7
app/system/service/menuService.go

@@ -159,16 +159,13 @@ func (s *menuService) LazyList(parentId int64, name, code, alias string) ([]mode
 	return rsp, nil
 }
 
-// 请求获取menu名称树(id,parent_id和name)
-func (s *menuService) GetNameTree() ([]model.MenuRouteDetail, error) {
-	menuDao := dao.Menu{
-		ParentId: int64(0),
-	}
+func (s *menuService) GetTree() ([]model.MenuRouteDetail, error) {
+	menuDao := dao.Menu{ParentId: int64(0)}
 	var menuDetails []model.MenuRouteDetail
 	var temp model.MenuRouteDetail
 
 	//所有菜单包装为菜单详情
-	menus, _ := menuDao.GetMenuTree()
+	menus, err := menuDao.GetAll()
 	var allDetails []model.MenuRouteDetail
 	for _, v := range menus {
 		temp.Menu = v
@@ -181,7 +178,7 @@ func (s *menuService) GetNameTree() ([]model.MenuRouteDetail, error) {
 		temp.Children, temp.HasChildren = s.getChildren(temp, allDetails)
 		menuDetails = append(menuDetails, temp)
 	}
-	return menuDetails, nil
+	return menuDetails, err
 }
 
 func (s *menuService) getChildren(parent model.MenuRouteDetail,

+ 38 - 57
app/system/service/roleService.go

@@ -7,7 +7,7 @@ import (
 	"iot_manager_service/util/cache"
 	"iot_manager_service/util/common"
 	"iot_manager_service/util/logger"
-	"strconv"
+	//"strconv"
 )
 
 // 角色管理服务
@@ -47,62 +47,6 @@ func (s *roleService) List(roleName, tenantId, roleAlias string, current, size i
 	return roles, counts, nil
 }
 
-func (s *roleService) Tree() (*model.RspRoleTree, error) {
-	role := &dao.Role{}
-	roles, err := role.GetAll()
-
-	if err != nil {
-		return nil, err
-	}
-	rsp := &model.RspRoleTree{}
-	//上级角色——多个下级角色数组
-	tmp := make(map[int64][]dao.Role)
-	for _, role := range roles {
-		childes, isExist := tmp[role.ParentId]
-		if isExist {
-			childes = append(childes, role)
-			tmp[role.ParentId] = childes
-		} else {
-			tmp[role.ParentId] = []dao.Role{role}
-		}
-	}
-
-	if len(tmp) == 0 {
-		return nil, nil
-	}
-	roots, _ := tmp[0]
-	rsp.RoleDetails = dfs(rsp.RoleDetails, roots, tmp)
-
-	return rsp, nil
-}
-
-func dfs(result []model.RoleDetail, roles []dao.Role, tmp map[int64][]dao.Role) []model.RoleDetail {
-	if len(roles) == 0 {
-		return nil
-	}
-	for _, child := range roles {
-		id := strconv.FormatInt(child.ID, 10)
-		parentId := strconv.FormatInt(child.ParentId, 10)
-		detail := model.RoleDetail{
-			ID:       id,
-			ParentId: parentId,
-			Title:    child.RoleName,
-			Key:      id,
-			Value:    id,
-		}
-		childes, isExist := tmp[child.ID]
-		if isExist {
-			detail.HasChildren = true
-			detail.Children = dfs([]model.RoleDetail{}, childes, tmp)
-			result = append(result, detail)
-		} else {
-			detail.HasChildren = false
-			result = append(result, detail)
-		}
-	}
-	return result
-}
-
 func (s *roleService) Submit(req dao.Role) *common.Errors {
 	role := &req
 	if role.ID == 0 {
@@ -174,3 +118,40 @@ func (s *roleService) GetRoleName(tenantId int, roleId int64) string {
 	}
 	return name
 }
+
+func (s *roleService) GetTree() ([]model.RoleDetail, error) {
+	roleDao := dao.Role{ParentId: int64(0)}
+	var results []model.RoleDetail
+	var temp model.RoleDetail
+	//所有role包装为roleDetail
+	roles, err := roleDao.GetAll()
+	var details []model.RoleDetail
+	for _, role := range roles {
+		temp.ID = role.ID
+		temp.ParentId = role.ParentId
+		temp.Title = role.RoleName
+		details = append(details, temp)
+	}
+	topRoles, err := roleDao.GetByParentId()
+	for _, v := range topRoles {
+		temp.ID = v.ID
+		temp.ParentId = v.ParentId
+		temp.Title = v.RoleName
+		temp.Children, temp.HasChildren = s.getChildren(temp, details)
+		results = append(results, temp)
+	}
+	return results, err
+}
+
+func (s *roleService) getChildren(parent model.RoleDetail,
+	all []model.RoleDetail) ([]model.RoleDetail, bool) {
+	pId := parent.ID
+	var children []model.RoleDetail
+	for _, v := range all {
+		if pId == v.ParentId {
+			v.Children, v.HasChildren = s.getChildren(v, all)
+			children = append(children, v)
+		}
+	}
+	return children, children != nil
+}