123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package frame
- /*var permissions []*TreePermission*/
- const (
- DIR = 1 //目录
- MENU = 2 //菜单
- AUTH = 3 //权限
- )
- // 权限输出
- type TreePermission struct {
- Label string `json:"label"`
- Id string `json:"id"`
- Children []*TreePermission `json:"children"`
- Pid string `json:"pid"`
- Cat int `json:"cat"`
- }
- type Permission struct {
- Cat int
- Code string
- Label string
- }
- /*func GetPermissions() []*TreePermission {
- return permissions
- }*/
- func (this *Permission) add(permissions *[]*TreePermission, parent *Permission, cat int, code string, label string) *[]*TreePermission {
- //判断是否查找到父节点,未查找到就直接放入数组中
- if parent == nil {
- parentVo := &TreePermission{
- Label: label,
- Id: code,
- Children: nil,
- Pid: "0",
- Cat: cat,
- }
- *permissions = append(*permissions, parentVo)
- } else {
- //找到code为parent的节点并添加进去
- parentVo := this.FindParent(*permissions, parent)
- if parentVo != nil {
- child := &TreePermission{
- Label: label,
- Id: code,
- Children: nil,
- Pid: parentVo.Id,
- Cat: cat,
- }
- if parentVo.Children == nil {
- childs := make([]*TreePermission, 0)
- parentVo.Children = childs
- }
- parentVo.Children = append(parentVo.Children, child)
- /* }*/
- }
- }
- return permissions
- }
- func (this *Permission) FindParent(permissions []*TreePermission, parent *Permission) *TreePermission {
- var vo *TreePermission
- for _, item := range permissions {
- if item.Id == parent.Code && item.Label == parent.Label {
- vo = item
- }
- if vo == nil {
- if item.Children != nil && len(item.Children) > 0 {
- vo = this.FindSubParent(item.Children, parent)
- }
- }
- }
- return vo
- }
- func (this *Permission) FindSubParent(permissions []*TreePermission, parent *Permission) *TreePermission {
- for _, item := range permissions {
- if item.Id == parent.Code {
- return item
- }
- if item.Children != nil && len(item.Children) > 0 {
- for _, child := range item.Children {
- if child.Id == parent.Code && child.Label == parent.Label {
- return child
- }
- if child.Children != nil && len(child.Children) > 0 {
- return this.FindSubParent(child.Children, parent)
- }
- }
- }
- }
- return nil
- }
|