menu.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package system
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "go-nc/configs/global"
  7. "go-nc/model"
  8. "go-nc/internal/utils"
  9. "github.com/gin-gonic/gin"
  10. "github.com/jinzhu/copier"
  11. )
  12. // 根据用户id获取路由菜单
  13. func GetUserMenu(c *gin.Context) {
  14. // 拿到token, 解析token
  15. userInfoInterface, _ := c.Get("userInfo")
  16. userInfo, _ := userInfoInterface.(model.Sys_user)
  17. // 根据用户id 查询角色ID
  18. var userRoleList []model.Sys_user_role
  19. global.App.DB.Model(&model.Sys_user_role{}).Where("user_id = ?", userInfo.Id).Find(&userRoleList)
  20. fmt.Println(userRoleList)
  21. if len(userRoleList) == 0 {
  22. // 设置状态码
  23. c.Status(886)
  24. c.Error(errors.New("用户没有角色"))
  25. return
  26. }
  27. // 清洗出角色ID
  28. var roleIds []uint
  29. for _, v := range userRoleList {
  30. roleIds = append(roleIds, v.RoleId)
  31. }
  32. // 查询菜单关系
  33. var menuList []model.Sys_role_menu
  34. global.App.DB.Where("role_id in (?)", roleIds).Find(&menuList)
  35. if len(menuList) == 0 {
  36. // 设置状态码
  37. c.Status(886)
  38. c.Error(errors.New("请联系管理员给予权限!"))
  39. return
  40. }
  41. // 清洗出菜单ID
  42. var menuIds []uint
  43. for _, v := range menuList {
  44. menuIds = append(menuIds, v.MenuId)
  45. }
  46. // 查询菜单
  47. var menuAll []model.Sys_menu
  48. global.App.DB.Where("id in (?)", menuIds).Find(&menuAll)
  49. if len(menuAll) == 0 {
  50. // 设置状态码
  51. c.Status(886)
  52. c.Error(errors.New("请联系管理员给予权限!"))
  53. return
  54. }
  55. // 复制一份
  56. var router []SysMenuType
  57. copier.Copy(&router, &menuAll)
  58. // // 清洗数据称tree
  59. tree := BuildTree(router)
  60. c.Set("res_data", tree)
  61. }
  62. // 获取菜单
  63. func GetMenu(c *gin.Context) {
  64. var menuList []model.Sys_menu
  65. global.App.DB.Find(&menuList)
  66. // 复制一份
  67. var router []SysMenuType
  68. copier.Copy(&router, &menuList)
  69. // 清洗数据称tree
  70. tree := BuildTree(router)
  71. c.Set("res_data", tree)
  72. }
  73. // 删除菜单
  74. func DeleteMenu(c *gin.Context) {
  75. param := SysMenuType{}
  76. if err := c.ShouldBindJSON(&param); err != nil {
  77. c.Error(errors.New("缺少参数:" + err.Error()))
  78. return
  79. }
  80. if param.Id == 0 {
  81. c.Error(errors.New("缺少参数:id"))
  82. return
  83. }
  84. var menu model.Sys_menu
  85. global.App.DB.Where("menu_id = ?", param.Id).First(&menu)
  86. fmt.Println(menu)
  87. if menu.Id != 0 {
  88. c.Error(errors.New("该菜单存在下级菜单, 不可删除!"))
  89. return
  90. }
  91. // 硬删除菜单
  92. global.App.DB.Where("menu_id = ?", param.Id).Delete(&menu)
  93. if err := global.App.DB.Unscoped().Delete(&model.Sys_menu{}, param.Id).Error; err != nil {
  94. c.Error(err)
  95. return
  96. }
  97. c.Set("res_data", "删除成功!")
  98. }
  99. // 添加菜单
  100. func AddMenu(c *gin.Context) {
  101. type permsList struct {
  102. Name string `json:"name"`
  103. Perms string `json:"perms"`
  104. }
  105. type Param struct {
  106. MenuId uint `json:"menuId" comment:"父菜单ID"`
  107. Name string ` json:"name" comment:"菜单名称"`
  108. Url string `json:"url" comment:"链接"`
  109. Perms []permsList `json:"permsArr" comment:"权限标识"`
  110. Type string ` json:"type" comment:"类型"`
  111. Path string ` json:"path" comment:"路由标识"`
  112. Refresh string ` json:"refresh" comment:"是否缓存"`
  113. Icon string `json:"icon" comment:"图标"`
  114. SortNumber int `json:"sortNumber" comment:"排序编号"`
  115. }
  116. // param := model.Sys_menu{}
  117. param := Param{}
  118. if err := c.ShouldBindJSON(&param); err != nil {
  119. c.Error(errors.New("缺少参数:" + err.Error()))
  120. return
  121. }
  122. err := utils.ValidateStruct(param)
  123. if err != nil {
  124. c.Error(err)
  125. return
  126. }
  127. paramSrt, err := json.Marshal(param.Perms)
  128. if err != nil {
  129. c.Error(err)
  130. return
  131. }
  132. // 处理权限标识
  133. dbData := model.Sys_menu{
  134. MenuId: param.MenuId,
  135. Name: param.Name,
  136. Url: param.Url,
  137. Perms: string(paramSrt), //fmt.Sprintf("%v", param.Perms),
  138. Type: param.Type,
  139. Path: param.Path,
  140. Refresh: param.Refresh,
  141. Icon: param.Icon,
  142. SortNumber: param.SortNumber,
  143. }
  144. if err := global.App.DB.Create(&dbData).Error; err != nil {
  145. c.Error(err)
  146. return
  147. }
  148. c.Set("res_data", dbData)
  149. }
  150. // 更新菜单
  151. func UpdateMenu(c *gin.Context) {
  152. type permsList struct {
  153. Name string `json:"name"`
  154. Perms string `json:"perms"`
  155. }
  156. type Param struct {
  157. Id uint `json:"id" comment:"ID"`
  158. MenuId uint `json:"menuId" comment:"父菜单ID"`
  159. Name string ` json:"name" comment:"菜单名称"`
  160. Url string `json:"url" comment:"链接"`
  161. Perms []permsList `json:"permsArr" comment:"权限标识"`
  162. Type string ` json:"type" comment:"类型"`
  163. Path string ` json:"path" comment:"路由标识"`
  164. Refresh string ` json:"refresh" comment:"是否缓存"`
  165. Icon string `json:"icon" comment:"图标"`
  166. SortNumber int `json:"sortNumber" comment:"排序编号"`
  167. }
  168. // param := model.Sys_menu{}
  169. param := Param{}
  170. if err := c.ShouldBindJSON(&param); err != nil {
  171. c.Error(errors.New("缺少参数:" + err.Error()))
  172. return
  173. }
  174. err := utils.ValidateStruct(param)
  175. if err != nil {
  176. c.Error(err)
  177. return
  178. }
  179. paramSrt, err := json.Marshal(param.Perms)
  180. if err != nil {
  181. c.Error(err)
  182. return
  183. }
  184. // 处理权限标识
  185. dbData := model.Sys_menu{
  186. MenuId: param.MenuId,
  187. Name: param.Name,
  188. Url: param.Url,
  189. Perms: string(paramSrt), //fmt.Sprintf("%v", param.Perms),
  190. Type: param.Type,
  191. Path: param.Path,
  192. Refresh: param.Refresh,
  193. Icon: param.Icon,
  194. SortNumber: param.SortNumber,
  195. }
  196. global.App.DB.Where("id = ?", param.Id).Updates(&dbData)
  197. c.Set("res_data", dbData)
  198. }
  199. // 构建树
  200. func BuildTree(router []SysMenuType) []SysMenuType {
  201. // 创建一个map来存储菜单项
  202. menuMap := make(map[int]SysMenuType)
  203. for _, menu := range router {
  204. menuMap[int(menu.Id)] = menu
  205. }
  206. // 创建一个切片来存储树形结构
  207. tree := make([]SysMenuType, 0)
  208. // 遍历菜单项,根据menu_id来确定父子关系
  209. for _, menu := range router {
  210. if menu.MenuId == 0 {
  211. // 如果menu_id为空,则为一级菜单
  212. tree = append(tree, menu)
  213. } else {
  214. // 如果menu_id不为空,则为子菜单
  215. parentMenu, ok := menuMap[int(menu.MenuId)]
  216. if ok {
  217. // 如果父菜单存在,则添加子菜单到父菜单的Children字段中
  218. parentMenu.Children = append(parentMenu.Children, menu)
  219. menuMap[int(menu.MenuId)] = parentMenu
  220. // 更新 tree 中的 parentMenu 值
  221. for i, t := range tree {
  222. if t.Id == parentMenu.Id {
  223. tree[i] = parentMenu
  224. }
  225. }
  226. }
  227. }
  228. }
  229. return tree
  230. }