109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package logic
|
|
|
|
import (
|
|
"admin/internal/dao"
|
|
"admin/internal/model"
|
|
"admin/validator"
|
|
"context"
|
|
"strconv"
|
|
|
|
"admin/admin"
|
|
"admin/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"pkg.local/modelbase"
|
|
"pkg.local/utils"
|
|
)
|
|
|
|
type AuthorityItemsLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewAuthorityItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthorityItemsLogic {
|
|
return &AuthorityItemsLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *AuthorityItemsLogic) AuthorityItems(in *admin.AuthorityItemsRequest) (*admin.Response, error) {
|
|
var v validator.AuthorityItemsValidator
|
|
if fail := l.checkParams(in, &v); fail != nil {
|
|
return fail, nil
|
|
}
|
|
modelObj := model.AuthorityModel{}.Init()
|
|
w := modelbase.Params{
|
|
Order: "level ASC, parent_id ASC, sort ASC",
|
|
Eq: map[string]string{"service_id": strconv.FormatInt(in.ServiceId, utils.NumberTen)},
|
|
}
|
|
|
|
var data []dao.Authority
|
|
err := modelObj.Items(w, &data)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.ErrorNoLoginInfo), nil
|
|
}
|
|
|
|
items := l.getList(data, utils.NumberZero)
|
|
l.processTreeNodes(items, utils.NumberOne)
|
|
|
|
return l.ok(items), nil
|
|
}
|
|
|
|
func (l *AuthorityItemsLogic) processTreeNodes(nodes []*dao.AuthorityItems, level int) {
|
|
// processTreeNodes 递归处理节点,添加IsFirst、IsLast和Level标识
|
|
if len(nodes) == utils.NumberZero {
|
|
return
|
|
}
|
|
|
|
// 遍历当前层级的所有节点
|
|
for i, node := range nodes {
|
|
// 设置是否为第一个和最后一个节点
|
|
node.IsFirst = i == utils.NumberZero
|
|
node.IsLast = i == len(nodes)-utils.NumberOne
|
|
|
|
// 递归处理子节点,层级+1
|
|
if len(node.Children) > utils.NumberZero {
|
|
l.processTreeNodes(node.Children, level+utils.NumberOne)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 获取层级的下接数据 列表使用
|
|
func (l *AuthorityItemsLogic) getList(data []dao.Authority, pid int64) []*dao.AuthorityItems {
|
|
var dataArr []*dao.AuthorityItems
|
|
for _, v := range data {
|
|
if v.ParentId == pid {
|
|
// 这里可以理解为每次都从最原始的数据里面找出相对就的ID进行匹配,直到找不到就返回
|
|
children := l.getList(data, v.Id)
|
|
node := dao.AuthorityItems{
|
|
Id: v.Id,
|
|
Name: v.Name,
|
|
Sort: v.Sort,
|
|
ParentId: v.ParentId,
|
|
ParentIds: v.ParentIds,
|
|
Description: v.Description,
|
|
Status: v.Status,
|
|
Level: v.Level,
|
|
Children: children,
|
|
Api: v.Api,
|
|
Path: v.Path,
|
|
ViewPath: v.ViewPath,
|
|
Identification: v.Identification,
|
|
Type: v.Type,
|
|
IsShow: v.IsShow,
|
|
Icon: v.Icon,
|
|
Reason: v.Reason,
|
|
AdminName: v.AdminName,
|
|
AdminId: v.AdminId,
|
|
}
|
|
dataArr = append(dataArr, &node)
|
|
}
|
|
}
|
|
return dataArr
|
|
}
|