作者 yangfu

用户有权限的菜单

@@ -5,6 +5,7 @@ go 1.16 @@ -5,6 +5,7 @@ go 1.16
5 require ( 5 require (
6 github.com/ajg/form v1.5.1 // indirect 6 github.com/ajg/form v1.5.1 // indirect
7 github.com/beego/beego/v2 v2.0.1 7 github.com/beego/beego/v2 v2.0.1
  8 + github.com/emirpasic/gods v1.12.0 // indirect
8 github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect 9 github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
9 github.com/fatih/structs v1.1.0 // indirect 10 github.com/fatih/structs v1.1.0 // indirect
10 github.com/gavv/httpexpect v2.0.0+incompatible 11 github.com/gavv/httpexpect v2.0.0+incompatible
@@ -73,6 +73,8 @@ github.com/elastic/go-elasticsearch/v6 v6.8.5/go.mod h1:UwaDJsD3rWLM5rKNFzv9hgox @@ -73,6 +73,8 @@ github.com/elastic/go-elasticsearch/v6 v6.8.5/go.mod h1:UwaDJsD3rWLM5rKNFzv9hgox
73 github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk= 73 github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
74 github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4= 74 github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
75 github.com/emicklei/proto v1.9.0/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A= 75 github.com/emicklei/proto v1.9.0/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A=
  76 +github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
  77 +github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
76 github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 78 github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
77 github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= 79 github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
78 github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= 80 github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
  1 +package dto
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
  5 + "sort"
  6 +)
  7 +
  8 +type UserAccessMenuDto []*Menu
  9 +type Menu struct {
  10 + // 菜单编号
  11 + MenuId int64 `json:"menuId,omitempty"`
  12 + // 父级id
  13 + ParentId int64 `json:"parentId,omitempty"`
  14 + // 菜单名称
  15 + MenuName string `json:"menuName,omitempty"`
  16 + // 菜单别名
  17 + //MenuAlias string `json:"menuAlias,omitempty"`
  18 + // 菜单编码 SYSTEM_USER_EDIT / 100101 (字符编码)
  19 + Code string `json:"code,omitempty"`
  20 + // 权限编码 user:edit
  21 + //AccessCode string `json:"accessCode,omitempty"`
  22 + // 菜单类型 (目录catalog、菜单menu、按钮button)
  23 + MenuType string `json:"menuType,omitempty"`
  24 + // 菜单图标
  25 + Icon string `json:"icon,omitempty"`
  26 + // 排序
  27 + Sort int `json:"sort,omitempty"`
  28 + // 菜单说明
  29 + //Remark string `json:"remark,omitempty"`
  30 + // 菜单类别 (web:1、app:2)
  31 + //Category string `json:"category,omitempty"`
  32 + // 路径节点路径("0,11,12,")
  33 + //ParentPath string `json:"parentPath,omitempty"`
  34 + // 菜单是否公开状态,[1:显示],[2:隐藏],默认显示
  35 + //IsPublish int `json:"isPublish,omitempty"`
  36 + // 启用状态(启用:1 禁用:2),默认启用
  37 + EnableStatus int `json:"enableStatus,omitempty"`
  38 +}
  39 +
  40 +func (dto *UserAccessMenuDto) LoadDto(menus []*domain.Menu, alias []*domain.CustomizeMenu, access []interface{}, allDisableMenu int) error {
  41 + var mapMenus = make(map[int64]*domain.Menu)
  42 + for i := range menus {
  43 + menus[i].EnableStatus = domain.MenuStatusDisable
  44 + mapMenus[menus[i].MenuId] = menus[i]
  45 + }
  46 + // 设置别名
  47 + for i := range alias {
  48 + if v, ok := mapMenus[alias[i].MenuId]; ok {
  49 + v.MenuName = alias[i].MenuAlias
  50 + v.Sort = alias[i].Sort
  51 + }
  52 + }
  53 + if allDisableMenu == 1 {
  54 + for i := range access {
  55 + if v, ok := mapMenus[access[i].(int64)]; ok {
  56 + *dto = append(*dto, &Menu{
  57 + MenuId: v.MenuId,
  58 + ParentId: v.ParentId,
  59 + MenuName: v.MenuName,
  60 + Code: v.Code,
  61 + MenuType: v.MenuType,
  62 + Icon: v.Icon,
  63 + Sort: v.Sort,
  64 + EnableStatus: v.EnableStatus,
  65 + })
  66 + }
  67 + }
  68 + } else {
  69 + for i := range access {
  70 + if v, ok := mapMenus[access[i].(int64)]; ok {
  71 + v.EnableStatus = domain.MenuStatusEnable
  72 + }
  73 + }
  74 + for _, v := range mapMenus {
  75 + *dto = append(*dto, &Menu{
  76 + MenuId: v.MenuId,
  77 + ParentId: v.ParentId,
  78 + MenuName: v.MenuName,
  79 + Code: v.Code,
  80 + MenuType: v.MenuType,
  81 + Icon: v.Icon,
  82 + Sort: v.Sort,
  83 + EnableStatus: v.EnableStatus,
  84 + })
  85 + }
  86 + }
  87 +
  88 + sort.Stable(dto)
  89 + return nil
  90 +}
  91 +
  92 +func (x UserAccessMenuDto) Len() int { return len(x) }
  93 +func (x UserAccessMenuDto) Less(i, j int) bool { return x[i].MenuId < x[j].MenuId }
  94 +func (x UserAccessMenuDto) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  95 +
  96 +func NewUserAccessMenuDto() *UserAccessMenuDto {
  97 + var dto UserAccessMenuDto = make([]*Menu, 0)
  98 + return &dto
  99 +}
@@ -10,9 +10,13 @@ import ( @@ -10,9 +10,13 @@ import (
10 10
11 type GetUserAccessMenusQuery struct { 11 type GetUserAccessMenusQuery struct {
12 // 用户Id 用户唯一标识 12 // 用户Id 用户唯一标识
13 - UserId int64 `cname:"用户Id 用户唯一标识" json:"userId,string" valid:"Required"` 13 + UserId int64 `cname:"用户Id 用户唯一标识" json:"userId" valid:"Required"`
  14 + // 当前登录组织orgId
  15 + OrgId int64 `cname:"当前登录组织Id" json:"orgId"`
14 // 菜单类别 web app 16 // 菜单类别 web app
15 MenuCategory string `cname:"菜单类别 web app" json:"menuCategory,omitempty"` 17 MenuCategory string `cname:"菜单类别 web app" json:"menuCategory,omitempty"`
  18 + // 返回所有不可以用的菜单
  19 + ALLDisableMenu int `cname:"同时返回所有不可以用的菜单 1:包含不可用的菜单 0:过滤不可用的菜单 " json:"aLLDisableMenu"`
16 } 20 }
17 21
18 func (getUserAccessMenusQuery *GetUserAccessMenusQuery) Valid(validation *validation.Validation) { 22 func (getUserAccessMenusQuery *GetUserAccessMenusQuery) Valid(validation *validation.Validation) {
@@ -2,6 +2,7 @@ package service @@ -2,6 +2,7 @@ package service
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
  5 + "github.com/emirpasic/gods/sets/hashset"
5 "github.com/linmadan/egglib-go/core/application" 6 "github.com/linmadan/egglib-go/core/application"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/command" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/command"
@@ -9,6 +10,7 @@ import ( @@ -9,6 +10,7 @@ import (
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/query" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/query"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils" 12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
  13 + "strconv"
12 "time" 14 "time"
13 ) 15 )
14 16
@@ -16,7 +18,7 @@ import ( @@ -16,7 +18,7 @@ import (
16 type UserService struct { 18 type UserService struct {
17 } 19 }
18 20
19 -// 批量添加 21 +// TODO:批量添加
20 func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) { 22 func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) {
21 if err := batchAddCommand.ValidateCommand(); err != nil { 23 if err := batchAddCommand.ValidateCommand(); err != nil {
22 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 24 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -342,13 +344,67 @@ func (userService *UserService) GetUserAccessMenus(getUserAccessMenusQuery *quer @@ -342,13 +344,67 @@ func (userService *UserService) GetUserAccessMenus(getUserAccessMenusQuery *quer
342 defer func() { 344 defer func() {
343 transactionContext.RollbackTransaction() 345 transactionContext.RollbackTransaction()
344 }() 346 }()
  347 +
  348 + // 1.用户角色包含的菜单Set
  349 + _, user, err := factory.FastPgUser(transactionContext, getUserAccessMenusQuery.UserId)
  350 + if err != nil {
  351 + return nil, err
  352 + }
  353 + menuIdSet := hashset.New()
  354 + for i := range user.UserRole {
  355 + var role *domain.Role
  356 + if _, role, err = factory.FastPgRole(transactionContext, user.UserRole[i].RoleId); err != nil {
  357 + return nil, err
  358 + }
  359 + // 只要当前登录组织的有权限菜单
  360 + if getUserAccessMenusQuery.OrgId > 0 && getUserAccessMenusQuery.OrgId != role.OrgId {
  361 + continue
  362 + }
  363 + for i := 0; i < len(role.AccessMenus); i++ {
  364 + menuIdSet.Add(role.AccessMenus[i])
  365 + }
  366 + }
  367 +
  368 + // 2.所有的菜单
  369 + menuRepository, _, err := factory.FastPgMenu(transactionContext, 0)
  370 + if err != nil {
  371 + return nil, err
  372 + }
  373 + queryOptions := make(map[string]interface{})
  374 + queryOptions["isPublish"] = domain.MenuPublic
  375 + if len(getUserAccessMenusQuery.MenuCategory) > 0 {
  376 + queryOptions["code"] = ""
  377 + if m, e := menuRepository.FindOne(map[string]interface{}{"code": getUserAccessMenusQuery.MenuCategory}); e == nil && m != nil {
  378 + queryOptions["category"] = strconv.Itoa(int(m.MenuId))
  379 + }
  380 + }
  381 + _, menus, err := menuRepository.Find(queryOptions)
  382 + if err != nil {
  383 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  384 + }
  385 +
  386 + // 3.自定义菜单
  387 + customizeMenuRepository, _, err := factory.FastPgCustomizeMenu(transactionContext, 0)
  388 + if err != nil {
  389 + return nil, err
  390 + }
  391 + _, customizeMenus, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": user.CompanyId})
  392 + if err != nil {
  393 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  394 + }
  395 +
  396 + // 4.适配
  397 + accessMenuDto := dto.NewUserAccessMenuDto()
  398 + accessMenuDto.LoadDto(menus, customizeMenus, menuIdSet.Values(), getUserAccessMenusQuery.ALLDisableMenu)
345 if err := transactionContext.CommitTransaction(); err != nil { 399 if err := transactionContext.CommitTransaction(); err != nil {
346 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 400 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
347 } 401 }
348 - return nil, nil 402 + return map[string]interface{}{
  403 + "menus": accessMenuDto,
  404 + }, nil
349 } 405 }
350 406
351 -// 获取用户概要数据 407 +// TODO:获取用户概要数据
352 func (userService *UserService) GetUserProfile(getUserProfileQuery *query.GetUserProfileQuery) (interface{}, error) { 408 func (userService *UserService) GetUserProfile(getUserProfileQuery *query.GetUserProfileQuery) (interface{}, error) {
353 if err := getUserProfileQuery.ValidateQuery(); err != nil { 409 if err := getUserProfileQuery.ValidateQuery(); err != nil {
354 return nil, application.ThrowError(application.ARG_ERROR, err.Error()) 410 return nil, application.ThrowError(application.ARG_ERROR, err.Error())
@@ -4,8 +4,8 @@ import "time" @@ -4,8 +4,8 @@ import "time"
4 4
5 // 共创信息 5 // 共创信息
6 type CooperationInfo struct { 6 type CooperationInfo struct {
7 - // 共创公司 7 + // 共创公司 cooperationCompany
8 CooperationCompany string `json:"cooperationCompany"` 8 CooperationCompany string `json:"cooperationCompany"`
9 - // 共创到期时间 (yyyy-MM-dd) 9 + // 共创到期时间 (yyyy-MM-dd) cooperationDeadline
10 CooperationDeadline time.Time `json:"cooperationDeadline"` 10 CooperationDeadline time.Time `json:"cooperationDeadline"`
11 } 11 }
@@ -62,6 +62,8 @@ func (controller *UserController) GetUserAccessMenus() { @@ -62,6 +62,8 @@ func (controller *UserController) GetUserAccessMenus() {
62 getUserAccessMenusQuery.UserId = userId 62 getUserAccessMenusQuery.UserId = userId
63 menuCategory := controller.GetString("menuCategory") 63 menuCategory := controller.GetString("menuCategory")
64 getUserAccessMenusQuery.MenuCategory = menuCategory 64 getUserAccessMenusQuery.MenuCategory = menuCategory
  65 + getUserAccessMenusQuery.OrgId, _ = controller.GetInt64("orgId", 0)
  66 + getUserAccessMenusQuery.ALLDisableMenu, _ = controller.GetInt("aLLDisableMenu", 0)
65 data, err := userService.GetUserAccessMenus(getUserAccessMenusQuery) 67 data, err := userService.GetUserAccessMenus(getUserAccessMenusQuery)
66 controller.Response(data, err) 68 controller.Response(data, err)
67 } 69 }