基于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"`
}
相关推荐
水痕011 天前
gin结合minio来做文件存储
java·eureka·gin
一个热爱生活的普通人6 天前
解构 gin.Context:不止是 Context
后端·go·gin
Code季风8 天前
Gin 框架中的模板引擎使用指南
服务器·前端·gin
Code季风9 天前
深入理解 Gin 框架的路由机制:从基础使用到核心原理
ide·后端·macos·go·web·xcode·gin
zhuyasen10 天前
Sponge:一个重构Go开发体验的框架,让你在开发项目开"外挂"
go·gin·grpc
计算机毕设定制辅导-无忧学长17 天前
InfluxDB 与 Golang 框架集成:Gin 实战指南(一)
struts·golang·gin
阿祥~18 天前
FISCO BCOS Gin调用WeBASE-Front接口发请求
区块链·gin·fisocbocs
Code季风25 天前
Gin Web 服务与 Consul 集成实战:服务注册到发现全流程(下)
微服务·go·gin
车江毅1 个月前
bsfgo 一个轻量级的go gin框架,用于web站点和api开发【开源】
go·gin·web框架·bsf·bsfgo