Browse Source

system: menu

Yi 11 months ago
parent
commit
ef284520fe
6 changed files with 67 additions and 50 deletions
  1. 1 1
      go.mod
  2. 2 0
      go.sum
  3. 1 7
      http/handler/user.go
  4. 2 2
      http/route/system_api.go
  5. 45 34
      model/system_menu.go
  6. 16 6
      module/backend/system_service.go

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20240409092953-5fb8d55c7e27
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240410062335-c8e3d2cdf42e
 	gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/getsentry/sentry-go v0.23.0

+ 2 - 0
go.sum

@@ -96,6 +96,8 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240409091955-ba718340ac5d h1:YaptUIwd
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240409091955-ba718340ac5d/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240409092953-5fb8d55c7e27 h1:3zx37XXutz3KR2xDX5wcfe62k5PZ6lnKylyKDYpUxBc=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240409092953-5fb8d55c7e27/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240410062335-c8e3d2cdf42e h1:9ULDek9hY5Wwq2kowvlkP4cs0EHyit61Y5UXMm30qNQ=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240410062335-c8e3d2cdf42e/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015 h1:dfb5dRd57L2HKjdwLT93UFmPYFPOmEl56gtZmqcNnaE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015/go.mod h1:Fk4GYI/v0IK3XFrm1Gn+VkgCz5Y7mfswD5hsTJYOG6A=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 1 - 7
http/handler/user.go

@@ -49,13 +49,7 @@ func Details(c *gin.Context) {
 
 // GetUserMenu 获取系统用户菜单权限
 func GetUserMenu(c *gin.Context) {
-	token := middleware.GetToken(c)
-	if err := valid.Validate(token, valid.Required); err != nil {
-		apierr.ClassifiedAbort(c, err)
-		return
-	}
-
-	res, err := middleware.BackendOperation(c).OpsService.GetSystemUserPermissions(c, token)
+	res, err := middleware.BackendOperation(c).OpsService.GetSystemUserMenu(c)
 	if err != nil {
 		apierr.ClassifiedAbort(c, err)
 		return

+ 2 - 2
http/route/system_api.go

@@ -50,7 +50,7 @@ func SystemAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		systemRoute.POST("/dept/createOrUpdate", system.DepCreateOrUpdate)
 
 		// 登录用户的相关接口
-		systemRoute.POST("/user/menu", handler.GetUserMenu)
+		systemRoute.GET("/user/menu", handler.GetUserMenu)
 
 		systemRoute.POST("/user_info", handler.GetUserInfo)
 		systemRoute.GET("/user/details/:user_id", handler.Details)
@@ -61,6 +61,6 @@ func SystemAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 func authRouteGroup(s *gin.Engine, relativePath string) *gin.RouterGroup {
 	group := s.Group(relativePath)
 	// 中间件鉴权
-	group.Use( /*middleware.RequireAdmin(),*/ middleware.GinLogger(), middleware.CORS())
+	group.Use(middleware.RequireAdmin(), middleware.GinLogger(), middleware.CORS())
 	return group
 }

+ 45 - 34
model/system_menu.go

@@ -1,10 +1,12 @@
 package model
 
 import (
-	"fmt"
 	"sort"
 	"time"
 
+	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
+	"go.uber.org/zap"
+
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
 
 	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
@@ -105,51 +107,60 @@ func (s SystemMenuSlice) ToPB() []*pasturePb.SearchMenuRequest {
 	}
 	return res
 }
+
 func (s SystemMenuSlice) ToTree() []*pasturePb.MenuTree {
 	res := make([]*pasturePb.MenuTree, len(s))
 	if 0 == len(s) {
 		return res
 	}
-	// 排序
+	// 排序
 	sort.Slice(s, func(i, j int) bool {
-		return s[i].ParentId > s[j].ParentId
+		return s[i].ParentId < s[j].ParentId
 	})
-
-	for i, menu := range s {
-		var frameLoading, keepalive, hiddenTag, showLink, showParent bool
-		if menu.FrameLoading == pasturePb.IsShow_Ok {
-			frameLoading = true
-		}
-		if menu.Keepalive == pasturePb.IsShow_Ok {
-			keepalive = true
-		}
-		if menu.HiddenTag == pasturePb.IsShow_Ok {
-			hiddenTag = true
-		}
-		if menu.ShowLink == pasturePb.IsShow_Ok {
-			showLink = true
-		}
-		if menu.ShowParent == pasturePb.IsShow_Ok {
-			showParent = true
+	zaplog.Info("ToTree", zap.Any("s", s))
+	for i, v := range s {
+		res[i] = &pasturePb.MenuTree{
+			Id:       int32(v.Id),
+			Path:     v.Path,
+			ParentId: int32(v.ParentId),
+			Meta: &pasturePb.MenuMeta{
+				Icon:  v.Icon,
+				Title: v.Title,
+				Rank:  v.Rank,
+			},
+			Children: make([]*pasturePb.MenuTree, 0),
 		}
+	}
+	zaplog.Info("ToTree", zap.Any("res", res))
+	// 构建树
+	return buildTree(res)
+}
 
-		if 0 == i {
-			res[menu.Id] = &pasturePb.MenuTree{
-				Id:   int32(menu.Id),
-				Path: menu.Path,
-				Meta: &pasturePb.MenuMeta{
-					Icon:  menu.Icon,
-					Title: menu.Title,
-					Rank:  menu.Rank,
-					Roles: make([]string, 0),
-				},
-				Children: make([]*pasturePb.MenuTree, 0),
-			}
+func buildTree(menuTreeList []*pasturePb.MenuTree) []*pasturePb.MenuTree {
+	tree := make([]*pasturePb.MenuTree, 0)
+	for _, menu := range menuTreeList {
+		if menu.ParentId == 0 {
+			tree = append(tree, menu)
 		} else {
-			fmt.Println(frameLoading, keepalive, hiddenTag, showLink, showParent)
+			parent := findByID(menuTreeList, menu.ParentId)
+			if parent != nil {
+				parent.Children = append(parent.Children, menu)
+			}
 		}
 	}
-	return res
+	return tree
+}
+
+func findByID(menuTreeList []*pasturePb.MenuTree, id int32) *pasturePb.MenuTree {
+	for _, v := range menuTreeList {
+		if id == v.Id {
+			return v
+		}
+		if child := findByID(v.Children, id); child != nil {
+			return child
+		}
+	}
+	return nil
 }
 
 func BooleanToIsShow(isShow bool) pasturePb.IsShow_Kind {

+ 16 - 6
module/backend/system_service.go

@@ -24,7 +24,10 @@ const CurrentUserName = "userName"
 // Login 用户登录
 func (s *StoreEntry) Login(ctx context.Context, req *pasturePb.SearchUserRequest) (*pasturePb.SystemUserResponse, error) {
 	systemUser := &model.SystemUser{}
-	if err := s.DB.Where("name = ?", req.Name).Find(systemUser).Error; err != nil {
+	if err := s.DB.Where("name = ?", req.Name).
+		Where("is_delete = ?", pasturePb.IsShow_Ok).
+		Where("is_show = ?", pasturePb.IsShow_Ok).
+		Find(systemUser).Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
 
@@ -36,6 +39,12 @@ func (s *StoreEntry) Login(ctx context.Context, req *pasturePb.SearchUserRequest
 		return nil, xerr.Customf("该账号已被禁用,请联系管理员")
 	}
 
+	systemRole := &model.SystemRole{Id: systemUser.RoleId}
+	if err := s.DB.Where("is_show = ? and is_delete = ?", pasturePb.IsShow_Ok, pasturePb.IsShow_Ok).
+		First(systemRole).Error; err != nil {
+		zaplog.Error("Login", zap.Any("systemRole", err))
+	}
+
 	jwtToken := jwt.NewJWTTokenGen(s.Cfg.AppName, s.Cfg.JwtTokenKeyConfig.PrivateKey)
 	token, err := jwtToken.GenerateToken(systemUser.Name, s.Cfg.JwtExpireTime)
 	if err != nil {
@@ -52,9 +61,9 @@ func (s *StoreEntry) Login(ctx context.Context, req *pasturePb.SearchUserRequest
 		Data: &pasturePb.SystemUserData{
 			AccessToken:  token,
 			Expires:      expires,
-			RefreshToken: expires,
+			RefreshToken: token,
 			Username:     systemUser.Name,
-			Roles:        []string{"admin"},
+			Roles:        []string{systemRole.Name},
 			Avatar:       systemUser.Avatar,
 		},
 	}, nil
@@ -320,11 +329,12 @@ func (s *StoreEntry) GetSystemUserMenu(ctx context.Context) (*pasturePb.SystemUs
 		return nil, xerr.WithStack(err)
 	}
 	// 获取用户菜单
-	systemMenu := &model.SystemMenu{}
+	systemMenuList := make([]*model.SystemMenu, 0)
 	if err = s.DB.Table(fmt.Sprintf("%s as a", new(model.SystemRoleMenu).TableName())).
+		Select("b.*").
 		Joins("LEFT JOIN system_menu AS b ON a.menu_id = b.id").
 		Where("a.role_id = ? and b.is_delete = ?", systemRole.Id, pasturePb.IsShow_Ok).
-		Find(&systemMenu).Error; err != nil {
+		Find(&systemMenuList).Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
 
@@ -332,7 +342,7 @@ func (s *StoreEntry) GetSystemUserMenu(ctx context.Context) (*pasturePb.SystemUs
 	return &pasturePb.SystemUserMenuTreeResponse{
 		Code:    http.StatusOK,
 		Message: "ok",
-		Data:    nil,
+		Data:    model.SystemMenuSlice(systemMenuList).ToTree(),
 	}, nil
 }