You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
7.9 KiB

  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global/response"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. resp "gin-vue-admin/model/response"
  8. "gin-vue-admin/service"
  9. "gin-vue-admin/utils"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // @Tags authorityAndMenu
  13. // @Summary 获取用户动态路由
  14. // @Security ApiKeyAuth
  15. // @Produce application/json
  16. // @Param data body request.RegisterAndLoginStruct true "可以什么都不填"
  17. // @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
  18. // @Router /menu/getMenu [post]
  19. func GetMenu(c *gin.Context) {
  20. claims, _ := c.Get("claims")
  21. waitUse := claims.(*request.CustomClaims)
  22. err, menus := service.GetMenuTree(waitUse.AuthorityId)
  23. if err != nil {
  24. response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c)
  25. } else {
  26. response.OkWithData(resp.SysMenusResponse{Menus: menus}, c)
  27. }
  28. }
  29. // @Tags menu
  30. // @Summary 分页获取基础menu列表
  31. // @Security ApiKeyAuth
  32. // @accept application/json
  33. // @Produce application/json
  34. // @Param data body request.PageInfo true "分页获取基础menu列表"
  35. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  36. // @Router /menu/getMenuList [post]
  37. func GetMenuList(c *gin.Context) {
  38. var pageInfo request.PageInfo
  39. _ = c.ShouldBindJSON(&pageInfo)
  40. PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"])
  41. if PageVerifyErr != nil {
  42. response.FailWithMessage(PageVerifyErr.Error(), c)
  43. return
  44. }
  45. err, menuList, total := service.GetInfoList()
  46. if err != nil {
  47. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  48. } else {
  49. response.OkWithData(resp.PageResult{
  50. List: menuList,
  51. Total: total,
  52. Page: pageInfo.Page,
  53. PageSize: pageInfo.PageSize,
  54. }, c)
  55. }
  56. }
  57. // @Tags menu
  58. // @Summary 新增菜单
  59. // @Security ApiKeyAuth
  60. // @accept application/json
  61. // @Produce application/json
  62. // @Param data body model.SysBaseMenu true "新增菜单"
  63. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  64. // @Router /menu/addBaseMenu [post]
  65. func AddBaseMenu(c *gin.Context) {
  66. var menu model.SysBaseMenu
  67. _ = c.ShouldBindJSON(&menu)
  68. MenuVerify := utils.Rules{
  69. "Path": {utils.NotEmpty()},
  70. "ParentId": {utils.NotEmpty()},
  71. "Name": {utils.NotEmpty()},
  72. "Component": {utils.NotEmpty()},
  73. "Sort": {utils.Ge("0")},
  74. }
  75. MenuVerifyErr := utils.Verify(menu, MenuVerify)
  76. if MenuVerifyErr != nil {
  77. response.FailWithMessage(MenuVerifyErr.Error(), c)
  78. return
  79. }
  80. MetaVerify := utils.Rules{
  81. "Title": {utils.NotEmpty()},
  82. }
  83. MetaVerifyErr := utils.Verify(menu.Meta, MetaVerify)
  84. if MetaVerifyErr != nil {
  85. response.FailWithMessage(MetaVerifyErr.Error(), c)
  86. return
  87. }
  88. err := service.AddBaseMenu(menu)
  89. if err != nil {
  90. response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c)
  91. } else {
  92. response.OkWithMessage("添加成功", c)
  93. }
  94. }
  95. // @Tags authorityAndMenu
  96. // @Summary 获取用户动态路由
  97. // @Security ApiKeyAuth
  98. // @Produce application/json
  99. // @Param data body request.RegisterAndLoginStruct true "可以什么都不填"
  100. // @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
  101. // @Router /menu/getBaseMenuTree [post]
  102. func GetBaseMenuTree(c *gin.Context) {
  103. err, menus := service.GetBaseMenuTree()
  104. if err != nil {
  105. response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c)
  106. } else {
  107. response.OkWithData(resp.SysBaseMenusResponse{Menus: menus}, c)
  108. }
  109. }
  110. // @Tags authorityAndMenu
  111. // @Summary 增加menu和角色关联关系
  112. // @Security ApiKeyAuth
  113. // @accept application/json
  114. // @Produce application/json
  115. // @Param data body request.AddMenuAuthorityInfo true "增加menu和角色关联关系"
  116. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  117. // @Router /menu/addMenuAuthority [post]
  118. func AddMenuAuthority(c *gin.Context) {
  119. var addMenuAuthorityInfo request.AddMenuAuthorityInfo
  120. _ = c.ShouldBindJSON(&addMenuAuthorityInfo)
  121. MenuVerify := utils.Rules{
  122. "AuthorityId": {"notEmpty"},
  123. }
  124. MenuVerifyErr := utils.Verify(addMenuAuthorityInfo, MenuVerify)
  125. if MenuVerifyErr != nil {
  126. response.FailWithMessage(MenuVerifyErr.Error(), c)
  127. return
  128. }
  129. err := service.AddMenuAuthority(addMenuAuthorityInfo.Menus, addMenuAuthorityInfo.AuthorityId)
  130. if err != nil {
  131. response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c)
  132. } else {
  133. response.OkWithMessage("添加成功", c)
  134. }
  135. }
  136. // @Tags authorityAndMenu
  137. // @Summary 获取指定角色menu
  138. // @Security ApiKeyAuth
  139. // @accept application/json
  140. // @Produce application/json
  141. // @Param data body request.AuthorityIdInfo true "增加menu和角色关联关系"
  142. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  143. // @Router /menu/GetMenuAuthority [post]
  144. func GetMenuAuthority(c *gin.Context) {
  145. var authorityIdInfo request.AuthorityIdInfo
  146. _ = c.ShouldBindJSON(&authorityIdInfo)
  147. MenuVerify := utils.Rules{
  148. "AuthorityId": {"notEmpty"},
  149. }
  150. MenuVerifyErr := utils.Verify(authorityIdInfo, MenuVerify)
  151. if MenuVerifyErr != nil {
  152. response.FailWithMessage(MenuVerifyErr.Error(), c)
  153. return
  154. }
  155. err, menus := service.GetMenuAuthority(authorityIdInfo.AuthorityId)
  156. if err != nil {
  157. response.FailWithDetailed(response.ERROR, resp.SysMenusResponse{Menus: menus}, fmt.Sprintf("添加失败,%v", err), c)
  158. } else {
  159. response.Result(response.SUCCESS, gin.H{"menus": menus}, "获取成功", c)
  160. }
  161. }
  162. // @Tags menu
  163. // @Summary 删除菜单
  164. // @Security ApiKeyAuth
  165. // @accept application/json
  166. // @Produce application/json
  167. // @Param data body request.GetById true "删除菜单"
  168. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  169. // @Router /menu/deleteBaseMenu [post]
  170. func DeleteBaseMenu(c *gin.Context) {
  171. var idInfo request.GetById
  172. _ = c.ShouldBindJSON(&idInfo)
  173. IdVerifyErr := utils.Verify(idInfo, utils.CustomizeMap["IdVerify"])
  174. if IdVerifyErr != nil {
  175. response.FailWithMessage(IdVerifyErr.Error(), c)
  176. return
  177. }
  178. err := service.DeleteBaseMenu(idInfo.Id)
  179. if err != nil {
  180. response.FailWithMessage(fmt.Sprintf("删除失败:%v", err), c)
  181. } else {
  182. response.OkWithMessage("删除成功", c)
  183. }
  184. }
  185. // @Tags menu
  186. // @Summary 更新菜单
  187. // @Security ApiKeyAuth
  188. // @accept application/json
  189. // @Produce application/json
  190. // @Param data body model.SysBaseMenu true "更新菜单"
  191. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  192. // @Router /menu/updateBaseMenu [post]
  193. func UpdateBaseMenu(c *gin.Context) {
  194. var menu model.SysBaseMenu
  195. _ = c.ShouldBindJSON(&menu)
  196. MenuVerify := utils.Rules{
  197. "Path": {"notEmpty"},
  198. "ParentId": {utils.NotEmpty()},
  199. "Name": {utils.NotEmpty()},
  200. "Component": {utils.NotEmpty()},
  201. "Sort": {utils.Ge("0")},
  202. }
  203. MenuVerifyErr := utils.Verify(menu, MenuVerify)
  204. if MenuVerifyErr != nil {
  205. response.FailWithMessage(MenuVerifyErr.Error(), c)
  206. return
  207. }
  208. MetaVerify := utils.Rules{
  209. "Title": {utils.NotEmpty()},
  210. }
  211. MetaVerifyErr := utils.Verify(menu.Meta, MetaVerify)
  212. if MetaVerifyErr != nil {
  213. response.FailWithMessage(MetaVerifyErr.Error(), c)
  214. return
  215. }
  216. err := service.UpdateBaseMenu(menu)
  217. if err != nil {
  218. response.FailWithMessage(fmt.Sprintf("修改失败:%v", err), c)
  219. } else {
  220. response.OkWithMessage("修改成功", c)
  221. }
  222. }
  223. // @Tags menu
  224. // @Summary 根据id获取菜单
  225. // @Security ApiKeyAuth
  226. // @accept application/json
  227. // @Produce application/json
  228. // @Param data body request.GetById true "根据id获取菜单"
  229. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  230. // @Router /menu/getBaseMenuById [post]
  231. func GetBaseMenuById(c *gin.Context) {
  232. var idInfo request.GetById
  233. _ = c.ShouldBindJSON(&idInfo)
  234. MenuVerify := utils.Rules{
  235. "Id": {"notEmpty"},
  236. }
  237. MenuVerifyErr := utils.Verify(idInfo, MenuVerify)
  238. if MenuVerifyErr != nil {
  239. response.FailWithMessage(MenuVerifyErr.Error(), c)
  240. return
  241. }
  242. err, menu := service.GetBaseMenuById(idInfo.Id)
  243. if err != nil {
  244. response.FailWithMessage(fmt.Sprintf("查询失败:%v", err), c)
  245. } else {
  246. response.OkWithData(resp.SysBaseMenuResponse{Menu: menu}, c)
  247. }
  248. }