sys_menu.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package system
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/go-playground/validator/v10"
  5. "hongze/fms_api/controller/resp"
  6. "hongze/fms_api/global"
  7. "hongze/fms_api/models/base"
  8. "hongze/fms_api/models/system"
  9. systemService "hongze/fms_api/services/system"
  10. "hongze/fms_api/utils"
  11. )
  12. type SysMenuController struct {
  13. }
  14. func (m *SysMenuController) Add(c *gin.Context) {
  15. req := new(system.SysMenuAddReq)
  16. err := c.ShouldBind(&req)
  17. if err != nil {
  18. errs, ok := err.(validator.ValidationErrors)
  19. if !ok {
  20. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  21. return
  22. }
  23. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  24. return
  25. }
  26. item := new(system.SysMenu)
  27. item.ParentId = req.ParentId
  28. item.Name = req.Name
  29. item.Sort = req.Sort
  30. item.Hidden = req.Hidden
  31. item.HiddenLayout = req.HiddenLayout
  32. item.Component = req.Component
  33. item.IconPath = req.IconPath
  34. item.MenuType = req.MenuType
  35. item.Path = req.Path
  36. item.PathName = req.PathName
  37. item.ButtonCode = req.ButtonCode
  38. //添加校验,如果是按钮类型,则父级菜单不允许为0
  39. if req.MenuType == 1 {
  40. if req.ParentId == 0 {
  41. resp.Fail("请设置按钮的父级菜单", c)
  42. return
  43. }
  44. if req.ButtonCode == "" {
  45. resp.Fail("请设置按钮标识", c)
  46. return
  47. }
  48. }
  49. if req.ParentId > 0 {
  50. parentInfo, tErr := item.GetMenuByMenuId(req.ParentId)
  51. if tErr != nil {
  52. if tErr == utils.ErrNoRow {
  53. resp.Fail("上级菜单不存在", c)
  54. return
  55. }
  56. resp.FailMsg("查询上级菜单出错", tErr.Error(), c)
  57. return
  58. }
  59. // 按钮只能挂在二级菜单下面
  60. /*if req.MenuType == 1 && parentInfo.ParentId == 0{
  61. resp.Fail("按钮只允许挂在二级菜单下面", c)
  62. return
  63. }*/
  64. if parentInfo.RootId == 0 {
  65. item.RootId = parentInfo.MenuId
  66. } else {
  67. item.RootId = parentInfo.RootId
  68. }
  69. item.Level = parentInfo.Level + 1
  70. if item.Level >= 3 {
  71. resp.Fail("不允许设置四级菜单", c)
  72. return
  73. }
  74. }
  75. // 判断是否存在相同的菜单名称
  76. cond := "parent_id = ? and name = ? and menu_type=?"
  77. pars := make([]interface{}, 0)
  78. pars = append(pars, item.ParentId, item.Name, item.MenuType)
  79. exitList, err := item.GetMenuListByCondition(cond, pars, "")
  80. if err != nil {
  81. resp.FailMsg("查询菜单出错", err.Error(), c)
  82. return
  83. }
  84. if len(exitList) > 0 {
  85. resp.Fail("该菜单名称已存在", c)
  86. return
  87. }
  88. err = item.Add()
  89. if err != nil {
  90. resp.FailMsg("保存失败", "保存失败,Err:"+err.Error(), c)
  91. return
  92. }
  93. resp.Ok("保存成功", c)
  94. }
  95. func (m *SysMenuController) List(c *gin.Context) {
  96. //获取jwt数据失败
  97. claims, _ := c.Get("adminInfo")
  98. adminInfo := claims.(*system.SysAdmin)
  99. if adminInfo.RoleId <= 0 {
  100. resp.Ok("获取成功", c)
  101. return
  102. }
  103. list, err, errMsg := systemService.MenuList(adminInfo)
  104. if err != nil {
  105. resp.FailMsg(errMsg, err.Error(), c)
  106. return
  107. }
  108. resultPage := &base.BaseData{
  109. List: list,
  110. }
  111. resp.OkData("获取成功", resultPage, c)
  112. }
  113. func (m *SysMenuController) AllList(c *gin.Context) {
  114. req := new(system.MenuListReq)
  115. err := c.BindQuery(&req)
  116. if err != nil {
  117. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  118. return
  119. }
  120. page := new(base.Page)
  121. page.SetPageSize(req.PageSize)
  122. page.SetCurrent(req.Current)
  123. count, list, err, _ := systemService.AllMenuList(page, req)
  124. if err != nil {
  125. resp.FailMsg("获取失败", "获取失败,Err:"+err.Error(), c)
  126. return
  127. }
  128. page.SetTotal(count)
  129. resultPage := &base.BaseData{
  130. Page: page,
  131. List: list,
  132. }
  133. resp.OkData("获取成功", resultPage, c)
  134. }
  135. func (m *SysMenuController) ButtonList(c *gin.Context) {
  136. req := new(system.MenuShortListItem)
  137. err := c.BindQuery(&req)
  138. if err != nil {
  139. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  140. return
  141. }
  142. claims, _ := c.Get("adminInfo")
  143. adminInfo := claims.(*system.SysAdmin)
  144. if adminInfo.RoleId <= 0 {
  145. resp.Ok("获取成功", c)
  146. return
  147. }
  148. list, err, errMsg := systemService.ButtonList(adminInfo, req)
  149. if err != nil {
  150. resp.FailMsg(errMsg, err.Error(), c)
  151. return
  152. }
  153. buttonList := make([]*system.SysMenuButtonResp, 0)
  154. for _, v := range list {
  155. tmp := new(system.SysMenuButtonResp)
  156. tmp.MenuId = v.MenuId
  157. tmp.ParentId = v.ParentId
  158. tmp.MenuType = v.MenuType
  159. tmp.Name = v.Name
  160. tmp.ButtonCode = v.ButtonCode
  161. buttonList = append(buttonList, tmp)
  162. }
  163. resultPage := &base.BaseOnlyData{
  164. List: buttonList,
  165. }
  166. resp.OkData("获取成功", resultPage, c)
  167. }
  168. func (m *SysMenuController) Edit(c *gin.Context) {
  169. req := new(system.SysMenuEditReq)
  170. err := c.ShouldBind(&req)
  171. if err != nil {
  172. errs, ok := err.(validator.ValidationErrors)
  173. if !ok {
  174. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  175. return
  176. }
  177. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  178. return
  179. }
  180. menu := new(system.SysMenu)
  181. item, err := menu.GetMenuByMenuId(req.MenuId)
  182. if err != nil {
  183. if err == utils.ErrNoRow {
  184. resp.Fail("该菜单不存在", c)
  185. return
  186. }
  187. resp.FailMsg("查询菜单出错", err.Error(), c)
  188. return
  189. }
  190. item.ParentId = req.ParentId
  191. item.Name = req.Name
  192. item.Sort = req.Sort
  193. item.Hidden = req.Hidden
  194. item.HiddenLayout = req.HiddenLayout
  195. item.Component = req.Component
  196. item.IconPath = req.IconPath
  197. item.MenuType = req.MenuType
  198. item.Path = req.Path
  199. item.PathName = req.PathName
  200. item.ButtonCode = req.ButtonCode
  201. updateCols := make([]string, 0)
  202. updateCols = append(updateCols, "parent_id", "name", "sort", "component", "icon_path", "menu_type", "path", "path_name", "button_code", "root_id", "level", "hidden", "hidden_layout")
  203. //添加校验,如果是按钮类型,则父级菜单不允许为0
  204. if req.MenuType == 1 {
  205. if req.ParentId == 0 {
  206. resp.Fail("请设置按钮的父级菜单", c)
  207. return
  208. }
  209. if req.ButtonCode == "" {
  210. resp.Fail("请设置按钮标识", c)
  211. return
  212. }
  213. }
  214. if req.ParentId > 0 {
  215. parentInfo, tErr := item.GetMenuByMenuId(req.ParentId)
  216. if tErr != nil {
  217. if tErr == utils.ErrNoRow {
  218. resp.Fail("上级菜单不存在", c)
  219. return
  220. }
  221. resp.FailMsg("查询上级菜单出错", tErr.Error(), c)
  222. return
  223. }
  224. // 按钮只能挂在二级菜单下面
  225. /*if req.MenuType == 1 && parentInfo.ParentId == 0{
  226. resp.Fail("按钮只允许挂在二级菜单下面", c)
  227. return
  228. }*/
  229. if parentInfo.RootId == 0 {
  230. item.RootId = parentInfo.MenuId
  231. } else {
  232. item.RootId = parentInfo.RootId
  233. }
  234. item.Level = parentInfo.Level + 1
  235. if item.Level >= 3 {
  236. resp.Fail("不允许设置四级菜单", c)
  237. return
  238. }
  239. } else if req.ParentId == 0 {
  240. item.RootId = 0
  241. item.Level = 0
  242. }
  243. // 判断是否存在相同的菜单名称
  244. if req.Name != item.Name {
  245. cond := "parent_id = ? and name = ? and menu_type=?"
  246. pars := make([]interface{}, 0)
  247. pars = append(pars, item.ParentId, item.Name, item.MenuType)
  248. exitList, err := item.GetMenuListByCondition(cond, pars, "")
  249. if err != nil {
  250. resp.FailMsg("查询菜单出错", err.Error(), c)
  251. return
  252. }
  253. if len(exitList) > 0 {
  254. resp.Fail("该菜单名称已存在", c)
  255. return
  256. }
  257. }
  258. err = item.Update(updateCols)
  259. if err != nil {
  260. resp.FailMsg("保存失败", "保存失败,Err:"+err.Error(), c)
  261. return
  262. }
  263. resp.Ok("保存成功", c)
  264. }
  265. // Delete 删除菜单
  266. func (m *SysMenuController) Delete(c *gin.Context) {
  267. req := new(system.MenuShortListItem)
  268. err := c.ShouldBind(&req)
  269. if err != nil {
  270. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  271. return
  272. }
  273. if req.MenuId <= 0 {
  274. resp.Fail("请选择要删除的菜单", c)
  275. return
  276. }
  277. err, errMsg := systemService.DeleteMenu(req)
  278. if err != nil {
  279. resp.FailMsg(errMsg, err.Error(), c)
  280. return
  281. }
  282. resp.Ok("删除成功", c)
  283. }