123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- package system
- import (
- "encoding/json"
- "errors"
- "fmt"
- "go-nc/configs/global"
- "go-nc/model"
- "go-nc/internal/utils"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/copier"
- )
- // 根据用户id获取路由菜单
- func GetUserMenu(c *gin.Context) {
- // 拿到token, 解析token
- userInfoInterface, _ := c.Get("userInfo")
- userInfo, _ := userInfoInterface.(model.Sys_user)
- // 根据用户id 查询角色ID
- var userRoleList []model.Sys_user_role
- global.App.DB.Model(&model.Sys_user_role{}).Where("user_id = ?", userInfo.Id).Find(&userRoleList)
- fmt.Println(userRoleList)
- if len(userRoleList) == 0 {
- // 设置状态码
- c.Status(886)
- c.Error(errors.New("用户没有角色"))
- return
- }
- // 清洗出角色ID
- var roleIds []uint
- for _, v := range userRoleList {
- roleIds = append(roleIds, v.RoleId)
- }
- // 查询菜单关系
- var menuList []model.Sys_role_menu
- global.App.DB.Where("role_id in (?)", roleIds).Find(&menuList)
- if len(menuList) == 0 {
- // 设置状态码
- c.Status(886)
- c.Error(errors.New("请联系管理员给予权限!"))
- return
- }
- // 清洗出菜单ID
- var menuIds []uint
- for _, v := range menuList {
- menuIds = append(menuIds, v.MenuId)
- }
- // 查询菜单
- var menuAll []model.Sys_menu
- global.App.DB.Where("id in (?)", menuIds).Find(&menuAll)
- if len(menuAll) == 0 {
- // 设置状态码
- c.Status(886)
- c.Error(errors.New("请联系管理员给予权限!"))
- return
- }
- // 复制一份
- var router []SysMenuType
- copier.Copy(&router, &menuAll)
- // // 清洗数据称tree
- tree := BuildTree(router)
- c.Set("res_data", tree)
- }
- // 获取菜单
- func GetMenu(c *gin.Context) {
- var menuList []model.Sys_menu
- global.App.DB.Find(&menuList)
- // 复制一份
- var router []SysMenuType
- copier.Copy(&router, &menuList)
- // 清洗数据称tree
- tree := BuildTree(router)
- c.Set("res_data", tree)
- }
- // 删除菜单
- func DeleteMenu(c *gin.Context) {
- param := SysMenuType{}
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- if param.Id == 0 {
- c.Error(errors.New("缺少参数:id"))
- return
- }
- var menu model.Sys_menu
- global.App.DB.Where("menu_id = ?", param.Id).First(&menu)
- fmt.Println(menu)
- if menu.Id != 0 {
- c.Error(errors.New("该菜单存在下级菜单, 不可删除!"))
- return
- }
- // 硬删除菜单
- global.App.DB.Where("menu_id = ?", param.Id).Delete(&menu)
- if err := global.App.DB.Unscoped().Delete(&model.Sys_menu{}, param.Id).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", "删除成功!")
- }
- // 添加菜单
- func AddMenu(c *gin.Context) {
- type permsList struct {
- Name string `json:"name"`
- Perms string `json:"perms"`
- }
- type Param struct {
- MenuId uint `json:"menuId" comment:"父菜单ID"`
- Name string ` json:"name" comment:"菜单名称"`
- Url string `json:"url" comment:"链接"`
- Perms []permsList `json:"permsArr" comment:"权限标识"`
- Type string ` json:"type" comment:"类型"`
- Path string ` json:"path" comment:"路由标识"`
- Refresh string ` json:"refresh" comment:"是否缓存"`
- Icon string `json:"icon" comment:"图标"`
- SortNumber int `json:"sortNumber" comment:"排序编号"`
- }
- // param := model.Sys_menu{}
- param := Param{}
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- paramSrt, err := json.Marshal(param.Perms)
- if err != nil {
- c.Error(err)
- return
- }
- // 处理权限标识
- dbData := model.Sys_menu{
- MenuId: param.MenuId,
- Name: param.Name,
- Url: param.Url,
- Perms: string(paramSrt), //fmt.Sprintf("%v", param.Perms),
- Type: param.Type,
- Path: param.Path,
- Refresh: param.Refresh,
- Icon: param.Icon,
- SortNumber: param.SortNumber,
- }
- if err := global.App.DB.Create(&dbData).Error; err != nil {
- c.Error(err)
- return
- }
- c.Set("res_data", dbData)
- }
- // 更新菜单
- func UpdateMenu(c *gin.Context) {
- type permsList struct {
- Name string `json:"name"`
- Perms string `json:"perms"`
- }
- type Param struct {
- Id uint `json:"id" comment:"ID"`
- MenuId uint `json:"menuId" comment:"父菜单ID"`
- Name string ` json:"name" comment:"菜单名称"`
- Url string `json:"url" comment:"链接"`
- Perms []permsList `json:"permsArr" comment:"权限标识"`
- Type string ` json:"type" comment:"类型"`
- Path string ` json:"path" comment:"路由标识"`
- Refresh string ` json:"refresh" comment:"是否缓存"`
- Icon string `json:"icon" comment:"图标"`
- SortNumber int `json:"sortNumber" comment:"排序编号"`
- }
- // param := model.Sys_menu{}
- param := Param{}
- if err := c.ShouldBindJSON(¶m); err != nil {
- c.Error(errors.New("缺少参数:" + err.Error()))
- return
- }
- err := utils.ValidateStruct(param)
- if err != nil {
- c.Error(err)
- return
- }
- paramSrt, err := json.Marshal(param.Perms)
- if err != nil {
- c.Error(err)
- return
- }
- // 处理权限标识
- dbData := model.Sys_menu{
- MenuId: param.MenuId,
- Name: param.Name,
- Url: param.Url,
- Perms: string(paramSrt), //fmt.Sprintf("%v", param.Perms),
- Type: param.Type,
- Path: param.Path,
- Refresh: param.Refresh,
- Icon: param.Icon,
- SortNumber: param.SortNumber,
- }
- global.App.DB.Where("id = ?", param.Id).Updates(&dbData)
- c.Set("res_data", dbData)
- }
- // 构建树
- func BuildTree(router []SysMenuType) []SysMenuType {
- // 创建一个map来存储菜单项
- menuMap := make(map[int]SysMenuType)
- for _, menu := range router {
- menuMap[int(menu.Id)] = menu
- }
- // 创建一个切片来存储树形结构
- tree := make([]SysMenuType, 0)
- // 遍历菜单项,根据menu_id来确定父子关系
- for _, menu := range router {
- if menu.MenuId == 0 {
- // 如果menu_id为空,则为一级菜单
- tree = append(tree, menu)
- } else {
- // 如果menu_id不为空,则为子菜单
- parentMenu, ok := menuMap[int(menu.MenuId)]
- if ok {
- // 如果父菜单存在,则添加子菜单到父菜单的Children字段中
- parentMenu.Children = append(parentMenu.Children, menu)
- menuMap[int(menu.MenuId)] = parentMenu
- // 更新 tree 中的 parentMenu 值
- for i, t := range tree {
- if t.Id == parentMenu.Id {
- tree[i] = parentMenu
- }
- }
- }
- }
- }
- return tree
- }
|