作者 yangfu

用户有权限的菜单

... ... @@ -5,6 +5,7 @@ go 1.16
require (
github.com/ajg/form v1.5.1 // indirect
github.com/beego/beego/v2 v2.0.1
github.com/emirpasic/gods v1.12.0 // indirect
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
github.com/fatih/structs v1.1.0 // indirect
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
github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/emicklei/proto v1.9.0/go.mod h1:rn1FgRS/FANiZdD2djyH7TMA9jdRDcYQ9IEN9yvjX0A=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
... ...
package dto
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"sort"
)
type UserAccessMenuDto []*Menu
type Menu struct {
// 菜单编号
MenuId int64 `json:"menuId,omitempty"`
// 父级id
ParentId int64 `json:"parentId,omitempty"`
// 菜单名称
MenuName string `json:"menuName,omitempty"`
// 菜单别名
//MenuAlias string `json:"menuAlias,omitempty"`
// 菜单编码 SYSTEM_USER_EDIT / 100101 (字符编码)
Code string `json:"code,omitempty"`
// 权限编码 user:edit
//AccessCode string `json:"accessCode,omitempty"`
// 菜单类型 (目录catalog、菜单menu、按钮button)
MenuType string `json:"menuType,omitempty"`
// 菜单图标
Icon string `json:"icon,omitempty"`
// 排序
Sort int `json:"sort,omitempty"`
// 菜单说明
//Remark string `json:"remark,omitempty"`
// 菜单类别 (web:1、app:2)
//Category string `json:"category,omitempty"`
// 路径节点路径("0,11,12,")
//ParentPath string `json:"parentPath,omitempty"`
// 菜单是否公开状态,[1:显示],[2:隐藏],默认显示
//IsPublish int `json:"isPublish,omitempty"`
// 启用状态(启用:1 禁用:2),默认启用
EnableStatus int `json:"enableStatus,omitempty"`
}
func (dto *UserAccessMenuDto) LoadDto(menus []*domain.Menu, alias []*domain.CustomizeMenu, access []interface{}, allDisableMenu int) error {
var mapMenus = make(map[int64]*domain.Menu)
for i := range menus {
menus[i].EnableStatus = domain.MenuStatusDisable
mapMenus[menus[i].MenuId] = menus[i]
}
// 设置别名
for i := range alias {
if v, ok := mapMenus[alias[i].MenuId]; ok {
v.MenuName = alias[i].MenuAlias
v.Sort = alias[i].Sort
}
}
if allDisableMenu == 1 {
for i := range access {
if v, ok := mapMenus[access[i].(int64)]; ok {
*dto = append(*dto, &Menu{
MenuId: v.MenuId,
ParentId: v.ParentId,
MenuName: v.MenuName,
Code: v.Code,
MenuType: v.MenuType,
Icon: v.Icon,
Sort: v.Sort,
EnableStatus: v.EnableStatus,
})
}
}
} else {
for i := range access {
if v, ok := mapMenus[access[i].(int64)]; ok {
v.EnableStatus = domain.MenuStatusEnable
}
}
for _, v := range mapMenus {
*dto = append(*dto, &Menu{
MenuId: v.MenuId,
ParentId: v.ParentId,
MenuName: v.MenuName,
Code: v.Code,
MenuType: v.MenuType,
Icon: v.Icon,
Sort: v.Sort,
EnableStatus: v.EnableStatus,
})
}
}
sort.Stable(dto)
return nil
}
func (x UserAccessMenuDto) Len() int { return len(x) }
func (x UserAccessMenuDto) Less(i, j int) bool { return x[i].MenuId < x[j].MenuId }
func (x UserAccessMenuDto) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func NewUserAccessMenuDto() *UserAccessMenuDto {
var dto UserAccessMenuDto = make([]*Menu, 0)
return &dto
}
... ...
... ... @@ -10,9 +10,13 @@ import (
type GetUserAccessMenusQuery struct {
// 用户Id 用户唯一标识
UserId int64 `cname:"用户Id 用户唯一标识" json:"userId,string" valid:"Required"`
UserId int64 `cname:"用户Id 用户唯一标识" json:"userId" valid:"Required"`
// 当前登录组织orgId
OrgId int64 `cname:"当前登录组织Id" json:"orgId"`
// 菜单类别 web app
MenuCategory string `cname:"菜单类别 web app" json:"menuCategory,omitempty"`
// 返回所有不可以用的菜单
ALLDisableMenu int `cname:"同时返回所有不可以用的菜单 1:包含不可用的菜单 0:过滤不可用的菜单 " json:"aLLDisableMenu"`
}
func (getUserAccessMenusQuery *GetUserAccessMenusQuery) Valid(validation *validation.Validation) {
... ...
... ... @@ -2,6 +2,7 @@ package service
import (
"fmt"
"github.com/emirpasic/gods/sets/hashset"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/command"
... ... @@ -9,6 +10,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/user/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
"strconv"
"time"
)
... ... @@ -16,7 +18,7 @@ import (
type UserService struct {
}
// 批量添加
// TODO:批量添加
func (userService *UserService) BatchAdd(batchAddCommand *command.BatchAddCommand) (interface{}, error) {
if err := batchAddCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ... @@ -342,13 +344,67 @@ func (userService *UserService) GetUserAccessMenus(getUserAccessMenusQuery *quer
defer func() {
transactionContext.RollbackTransaction()
}()
// 1.用户角色包含的菜单Set
_, user, err := factory.FastPgUser(transactionContext, getUserAccessMenusQuery.UserId)
if err != nil {
return nil, err
}
menuIdSet := hashset.New()
for i := range user.UserRole {
var role *domain.Role
if _, role, err = factory.FastPgRole(transactionContext, user.UserRole[i].RoleId); err != nil {
return nil, err
}
// 只要当前登录组织的有权限菜单
if getUserAccessMenusQuery.OrgId > 0 && getUserAccessMenusQuery.OrgId != role.OrgId {
continue
}
for i := 0; i < len(role.AccessMenus); i++ {
menuIdSet.Add(role.AccessMenus[i])
}
}
// 2.所有的菜单
menuRepository, _, err := factory.FastPgMenu(transactionContext, 0)
if err != nil {
return nil, err
}
queryOptions := make(map[string]interface{})
queryOptions["isPublish"] = domain.MenuPublic
if len(getUserAccessMenusQuery.MenuCategory) > 0 {
queryOptions["code"] = ""
if m, e := menuRepository.FindOne(map[string]interface{}{"code": getUserAccessMenusQuery.MenuCategory}); e == nil && m != nil {
queryOptions["category"] = strconv.Itoa(int(m.MenuId))
}
}
_, menus, err := menuRepository.Find(queryOptions)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 3.自定义菜单
customizeMenuRepository, _, err := factory.FastPgCustomizeMenu(transactionContext, 0)
if err != nil {
return nil, err
}
_, customizeMenus, err := customizeMenuRepository.Find(map[string]interface{}{"companyId": user.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 4.适配
accessMenuDto := dto.NewUserAccessMenuDto()
accessMenuDto.LoadDto(menus, customizeMenus, menuIdSet.Values(), getUserAccessMenusQuery.ALLDisableMenu)
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
return map[string]interface{}{
"menus": accessMenuDto,
}, nil
}
// 获取用户概要数据
// TODO:获取用户概要数据
func (userService *UserService) GetUserProfile(getUserProfileQuery *query.GetUserProfileQuery) (interface{}, error) {
if err := getUserProfileQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
... ...
... ... @@ -4,8 +4,8 @@ import "time"
// 共创信息
type CooperationInfo struct {
// 共创公司
// 共创公司 cooperationCompany
CooperationCompany string `json:"cooperationCompany"`
// 共创到期时间 (yyyy-MM-dd)
// 共创到期时间 (yyyy-MM-dd) cooperationDeadline
CooperationDeadline time.Time `json:"cooperationDeadline"`
}
... ...
... ... @@ -62,6 +62,8 @@ func (controller *UserController) GetUserAccessMenus() {
getUserAccessMenusQuery.UserId = userId
menuCategory := controller.GetString("menuCategory")
getUserAccessMenusQuery.MenuCategory = menuCategory
getUserAccessMenusQuery.OrgId, _ = controller.GetInt64("orgId", 0)
getUserAccessMenusQuery.ALLDisableMenu, _ = controller.GetInt("aLLDisableMenu", 0)
data, err := userService.GetUserAccessMenus(getUserAccessMenusQuery)
controller.Response(data, err)
}
... ...