基于gin关于多级菜单的处理

多级菜单是很多业务场景需要的。下面是一种处理方式

go 复制代码
// 生成树结构
func tree(menus []*video.XkVideoCategory, parentId uint) []*video.XkVideoCategory {
	//定义子节点目录
	var nodes []*video.XkVideoCategory
	if reflect.ValueOf(menus).IsValid() {
		//循环所有一级菜单
		for _, v := range menus {
			//查询所有该菜单下的所有子菜单
			if v.ParentId == parentId {
				//特别注意压入元素不是单个所有加三个 **...** 告诉切片无论多少元素一并压入
				v.Children = append(v.Children, tree(menus, v.ID)...)
				nodes = append(nodes, v)
			}

		}
	}
	return nodes
}

下面是model

sql 复制代码
type XkVideoCategory struct {
	ID           uint      `gorm:"primarykey;comment:主键ID" json:"id" form:"id"`
	CategoryName string    `json:"categoryName" gorm:"not null;default:'';comment:分类名称"`
	Description  string    `json:"description" gorm:"not null;default:'';comment:分类描述"`
	CreateTime   time.Time `gorm:"type:datetime(0);comment:创建时间" json:"createTime"`
	UpdatedTime  time.Time `gorm:"type:datetime(0);comment:更新时间" json:"updatedTime"`
	ParentId     uint      `json:"parentId" gorm:"not null;default:0;comment:分类的主ID"`
	Status       int8      `json:"status" gorm:"not null;default:1;comment:0 未发布 1 发布"`
	Sorted       int8      `json:"sorted" gorm:"not null;default:1;comment:0 排序"`
	IsDelete     int8      `json:"isDelete" gorm:"not null;default:0;comment:0 未删除 1 删除"`
	// 忽略该字段,- 表示无读写,-:migration 表示无迁移权限,-:all 表示无读写迁移权限
	Children []*XkVideoCategory `gorm:"-" json:"children"`
}
相关推荐
希望20174 天前
Golang | http/server & Gin框架简述
http·golang·gin
朗迹 - 张伟5 天前
Gin-Vue-Admin学习笔记
vue.js·学习·gin
creator_Li6 天前
Gin框架学习
go·gin
kite01218 天前
Gin + Zap 日志:构建高性能、结构化的应用日志系统
golang·gin·log·zap
OxYGC8 天前
[玩转GoLang] 5分钟整合Gin / Gorm框架入门
开发语言·golang·gin
张烫麻辣亮。9 天前
golang-gin包
开发语言·golang·gin
小红帽2.011 天前
GOFLY开源客服系统-处理gin框架下的session中间件
中间件·gin
小红帽2.012 天前
从零搭建客服系统:我是如何用Gin实现页面路由与模板渲染的
gin
戎码江湖12 天前
使用CI/CD部署后端项目(gin)
ci/cd·golang·gin·后端自动部署项目·自动化部署项目
夏沫mds19 天前
【基于hyperledger fabric的教育证书管理系统】
运维·go·vue·区块链·gin·fabric