golang通用后台管理系统06(用户菜单树信息)
1.
SysMenu.go
package entity
import (
//"lostvip.com/db"
"time"
)
// SysMenu 菜单权限
type SysMenu struct {
MenuId int64 `gorm:"type:bigint(20);primary_key;auto_increment;菜单ID;" json:"menuId"`
MenuName string `gorm:"type:varchar(50);comment:菜单名称;" json:"menuName"`
ParentId int64 `gorm:"type:bigint(20);comment:父菜单ID;" json:"parentId"`
OrderNum int `gorm:"type:int(11);comment:显示顺序;" json:"orderNum"`
Url string `gorm:"type:varchar(200);comment:请求地址;" json:"url"`
Target string `gorm:"type:varchar(20);comment:打开方式(menuItem页签 menuBlank新窗口);" json:"target"`
MenuType string `gorm:"type:char(1);comment:菜单类型(M目录 C菜单 F按钮);" json:"menuType"`
Visible string `gorm:"type:char(1);comment:菜单状态(0显示 1隐藏);" json:"visible"`
Perms string `gorm:"type:varchar(100);comment:权限标识;" json:"perms"`
Icon string `gorm:"type:varchar(100);comment:菜单图标;" json:"icon"`
UpdateBy string `gorm:"type:varchar(64);comment:更新者;" json:"updateBy"`
UpdateTime time.Time `gorm:"type:datetime;comment:更新时间;" json:"updateTime" time_format:"2006-01-02 15:04:05"`
Remark string `gorm:"type:varchar(500);comment:备注;" json:"remark"`
CreateTime time.Time `gorm:"type:datetime;comment:创建时间;column:create_time;" json:"createTime" time_format:"2006-01-02 15:04:05"`
CreateBy string `gorm:"type:varchar(32);comment:创建人;column:create_by;" json:"createBy"`
DelFlag int `gorm:"type:tinyint(1);default:0;comment:删除标记;column:del_flag;" json:"delFlag"`
//
Children []*SysMenu `gorm:"-" json:"children"`
ParentName string `gorm:"-" json:"parentName"`
}
func (e *SysMenu) TableName() string {
return "sys_menu"
}
/*
// 增
func (e *SysMenu) Insert() error {
return db.GetMasterGorm().Save(e).Error
}
// 查
func (e *SysMenu) FindById() error {
err := db.GetMasterGorm().Take(e, e.MenuId).Error
return err
}
// 查第一条
func (e *SysMenu) FindOne() error {
tb := db.GetMasterGorm().Table("sys_menu")
if e.MenuId != 0 {
tb = tb.Where("menu_id=?", e.MenuId)
}
if e.ParentId != 0 {
tb = tb.Where("parent_id=?", e.ParentId)
}
if e.MenuName != "" {
tb = tb.Where("menu_name=?", e.MenuName)
}
if e.Perms != "" {
tb = tb.Where("perms=?", e.Perms)
}
err := tb.First(e).Error
return err
}
// 查第一条
func (e *SysMenu) FindLastOne() error {
tb := db.GetMasterGorm().Table("sys_menu")
if e.MenuId != 0 {
tb = tb.Where("menu_id=?", e.MenuId)
}
if e.ParentId != 0 {
tb = tb.Where("parent_id=?", e.ParentId)
}
if e.MenuName != "" {
tb = tb.Where("menu_name=?", e.MenuName)
}
if e.Perms != "" {
tb = tb.Where("perms=?", e.Perms)
}
tb.Order("menu_id desc")
tb.Limit(1)
err := tb.First(e).Error
return err
}
// 改
func (e *SysMenu) Update() error {
return db.GetMasterGorm().Table(e.TableName()).Updates(e).Error
}
// 删
func (e *SysMenu) Delete() error {
return db.GetMasterGorm().Table(e.TableName()).Delete(e).Error
}
*/
2.SysMenuService
package service
import (
"gin/common"
"gin/config"
sysEntity "gin/system/entity"
sysUtil "gin/system/util"
"github.com/gin-gonic/gin"
"strings"
)
type SysMenuService struct {
}
//系统菜单
func /*(svc *SysMenuService)*/ GetRouters(c *gin.Context) /* (*[]sysEntity.SysMenu,error)*/ {
var menus []sysEntity.SysMenu
svc := &SysMenuService{}
//登录用户token解析信息
resultList := GetProfile(c)
sysUserEntity := &sysEntity.SysUserEntity{}
if len(resultList) > 0 {
// 假设返回的切片中第一条记录就是我们要获取userName值的记录
userName := resultList[0]["userName"].(string)
userId := resultList[0]["userId"].(int64)
//userIdStr := fmt.Sprintf("%d", userId)
//fmt.Print("----------------------------------通过token解析的用户id为:" + userIdStr +"===========")
//fmt.Print("----------------------------------通过token解析的用户详细信息为:" + string(sysUtil.MapToJson(resultList)))
sysUserEntity.UserName = userName
sysUserEntity.UserId = userId
//fmt.Print("----------------------------------通过token解析的用户id为:" + fmt.Sprintf("%d", sysUserEntity.UserId) +"===========")
} else {
// 如果切片为空,说明没有查询到相关用户资料,可以进行相应的错误处理
common.ReturnError(c, 500, "登录用户账号信息查询错误,请联系管理员处理!", "", int64(len("")))
}
//fmt.Print("----------------------------------通过token解析的用户名称为:" + sysUserEntity.UserName)
//超级管理员特殊处理
if sysUserEntity.UserName == "admin" {
err := config.GormDb.Model(&sysEntity.LoginBody{}).Table("sys_menu as m").Select("distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.query, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time").
Where("m.menu_type in (" + "'M', 'C')" + " and m.status = 0").Scan(&menus).Error
if err != nil {
common.ReturnError(c, 500, "超级管理员查询用户菜单失败,请联系系统管理员处理!", "", int64(len("")))
panic("")
}
} else {
//TODO 非超级管理员账号拥有的菜单信息
}
//resultMap 所有菜单数据集
arr := make([]sysEntity.SysMenu, 0)
childrenMap := svc.InitChildMap(menus)
for _, menu := range menus {
if menu.ParentId == 0 { //发现子菜单
svc.FillAllChildren(&menu, childrenMap)
arr = append(arr, menu)
}
}
common.ReturnSucess(c, 200, "操作成功", &arr, int64(len(arr)))
//存入缓存
}
// 对象转菜单树
func (svc *SysMenuService) InitZtree(menuList *[]sysEntity.SysMenu, roleMenuList *[]string, permsFlag bool) (*[]common.Ztree, error) {
var result []common.Ztree
isCheck := false
if roleMenuList != nil && len(*roleMenuList) > 0 {
isCheck = true
}
for _, obj := range *menuList {
var ztree common.Ztree
ztree.Title = obj.MenuName
ztree.Id = obj.MenuId
ztree.Name = svc.transMenuName(obj.MenuName, permsFlag)
ztree.Pid = obj.ParentId
if isCheck {
tmp := sysUtil.String(obj.MenuId) + obj.Perms
tmpcheck := false
for j := range *roleMenuList {
if strings.Compare((*roleMenuList)[j], tmp) == 0 {
tmpcheck = true
break
}
}
ztree.Checked = tmpcheck
}
result = append(result, ztree)
}
return &result, nil
}
func (svc *SysMenuService) transMenuName(menuName string, permsFlag bool) string {
if permsFlag {
return "<font color=\"#888\"> " + menuName + "</font>"
} else {
return menuName
}
}
func (svc *SysMenuService) InitChildMap(menus []sysEntity.SysMenu) map[int64][]*sysEntity.SysMenu {
childrenMap := make(map[int64][]*sysEntity.SysMenu)
for i, _ := range menus {
if menus[i].MenuType == "F" { //忽略按钮
continue
}
//每个menu都预设子菜单项
childrenMap[menus[i].MenuId] = make([]*sysEntity.SysMenu, 0)
}
for i, _ := range menus {
if menus[i].MenuType == "F" { //忽略按钮
continue
}
pid := menus[i].ParentId
//组织父子关系
childrenMap[pid] = append(childrenMap[pid], &menus[i])
//svc.GenMenus(&menus[i], menus)
}
return childrenMap
}
func (svc *SysMenuService) FillAllChildren(m *sysEntity.SysMenu, childrenMap map[int64][]*sysEntity.SysMenu) {
children := childrenMap[m.MenuId]
if children == nil || len(children) <= 0 {
return
}
for i := range children {
m.Children = childrenMap[m.MenuId]
svc.FillAllChildren(children[i], childrenMap)
}
}
3.测试截图