| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- package system
- import (
- "go_server/model/common"
- "gorm.io/gorm"
- "strings"
- )
- type Apis struct {
- common.GormFullModel
- ParentId int64 `json:"parentId" gorm:"column:parent_id;comment:上级接口Id"`
- Group string `json:"group" gorm:"column:group;type:varchar(50);comment:分组名称"`
- Name string `json:"name" gorm:"column:name;type:varchar(50);comment:接口名称"`
- Method string `json:"method" gorm:"column:method;type:varchar(20);comment:请求方法"`
- Path string `json:"path" gorm:"column:path;type:varchar(80);comment:接口全路径;unique"`
- }
- func (*Apis) TableName() string {
- return common.ModelPrefix + "apis"
- }
- func NewApis() *Apis {
- return &Apis{}
- }
- func (*Apis) Comment() string {
- return "api权限表"
- }
- func (s *Apis) FindOrCreateAuth(db *gorm.DB, path, method string) (*Apis, error) {
- if err := db.Where("path", path).First(&s).Error; err != nil {
- if !strings.Contains(path, "*") {
- pathList := strings.Split(path, "/")
- if len(pathList) > 2 {
- s.Group = pathList[2]
- }
- s.ParentId = 1
- s.Path = path
- s.Method = method
- s.Name = "待设置"
- if err := db.Create(&s).Error; err != nil {
- return s, err
- }
- }
- return s, err
- }
- return s, nil
- }
- type ApiNode struct {
- Id int64 `json:"id"`
- ParentId int64 `json:"parentId"`
- Group string `json:"group"`
- Name string `json:"name"`
- Method string `json:"method"`
- Path string `json:"path"`
- Children []*ApiNode `json:"children"`
- }
- func (s *Apis) GetUserTree(db *gorm.DB, ids []string) ([]ApiNode, error) {
- menusNodeTree := make([]ApiNode, 0) // 根节点
- // 所有节点
- allMenus := make([]Apis, 0)
- if err := db.Model(&Apis{}).
- Where("id in (?)", ids).
- Find(&allMenus).Error; err != nil {
- return menusNodeTree, err
- }
- for _, m := range allMenus {
- childMenus := make([]*ApiNode, 0)
- if m.ParentId == 0 {
- rootNode := ApiNode{
- Id: m.ID,
- ParentId: m.ParentId,
- Group: m.Group,
- Name: m.Name,
- Method: m.Method,
- Path: m.Path,
- Children: childMenus,
- }
- menusNodeTree = append(menusNodeTree, rootNode)
- }
- }
- for i, _ := range menusNodeTree {
- s.walk(allMenus, &menusNodeTree[i])
- }
- return menusNodeTree, nil
- }
- // MenuTree 系统递归树[满足无限层级递归]
- func (s *Apis) SysTree(db *gorm.DB) ([]ApiNode, error) {
- menusNodeTree := make([]ApiNode, 0) // 根节点
- // 所有节点
- allMenus := make([]Apis, 0)
- if err := db.Model(&Apis{}).Where("1=1").Find(&allMenus).Error; err != nil {
- return menusNodeTree, err
- }
- for _, m := range allMenus {
- childMenus := make([]*ApiNode, 0)
- if m.ParentId == 0 {
- rootNode := ApiNode{
- Id: m.ID,
- ParentId: m.ParentId,
- Name: m.Name,
- Group: m.Group,
- Method: m.Method,
- Path: m.Path,
- Children: childMenus,
- }
- menusNodeTree = append(menusNodeTree, rootNode)
- }
- }
- for i, _ := range menusNodeTree {
- s.walk(allMenus, &menusNodeTree[i])
- }
- return menusNodeTree, nil
- }
- // 递归组装菜单树(根节点)
- func (s *Apis) walk(allMenus []Apis, rootNode *ApiNode) {
- // 列出所有下级子目录
- nodes := s.childrenList(allMenus, rootNode.Id)
- if len(nodes) == 0 {
- return
- }
- // 遍历这些文件
- for _, m := range nodes {
- newNode := ApiNode{
- Id: m.Id,
- ParentId: m.ParentId,
- Group: m.Group,
- Name: m.Name,
- Method: m.Method,
- Path: m.Path,
- Children: nil,
- }
- s.walk(allMenus, &newNode)
- rootNode.Children = append(rootNode.Children, &newNode)
- }
- return
- }
- // 获得子节点列表
- func (s *Apis) childrenList(allMenus []Apis, pId int64) (menusNodeTree []ApiNode) {
- for _, m := range allMenus {
- if m.ParentId == pId {
- rootNode := ApiNode{
- Id: m.ID,
- ParentId: m.ParentId,
- Name: m.Name,
- Group: m.Group,
- Method: m.Method,
- Path: m.Path,
- Children: nil,
- }
- menusNodeTree = append(menusNodeTree, rootNode)
- }
- }
- return
- }
|