sys_menu.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package controller
  2. import (
  3. "Medical_OAuth/app/admin/model"
  4. "Medical_OAuth/app/admin/service"
  5. "Medical_OAuth/app/admin/service/dto"
  6. "Medical_OAuth/common/actions"
  7. "encoding/json"
  8. "github.com/gin-gonic/gin"
  9. "github.com/gin-gonic/gin/binding"
  10. "gogs.baozhida.cn/zoie/OAuth-core/api"
  11. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  12. _ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  13. )
  14. type SysMenuController struct {
  15. api.Api
  16. }
  17. // GetPage 获取菜单列表
  18. // @Summary 获取菜单列表
  19. // @Description 获取菜单列表
  20. // @Tags 菜单
  21. // @Param menuName query string false "菜单名称"
  22. // @Param title query string false "显示名称"
  23. // @Param visible query string false "显示状态"
  24. // @Param pageSize query int false "页条数"
  25. // @Param pageIndex query int false "页码"
  26. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysMenu}} "{"code": 200, "data": [...]}"
  27. // @Router /api/menu [get]
  28. // @Security Bearer
  29. func (e SysMenuController) GetPage(c *gin.Context) {
  30. s := service.SysMenu{}
  31. req := dto.SysMenuGetPageReq{}
  32. err := e.MakeContext(c).
  33. MakeOrm().
  34. Bind(&req, binding.Form).
  35. MakeService(&s.Service).
  36. Errors
  37. if err != nil {
  38. e.Logger.Error(err)
  39. e.Error(500, err, err.Error())
  40. return
  41. }
  42. var list = make([]model.SysMenu, 0)
  43. if err = s.GetPage(&req, &list).Error; err != nil {
  44. e.Error(500, err, "查询失败")
  45. return
  46. }
  47. e.OK(list, "查询成功")
  48. }
  49. // Get 通过id获取菜单
  50. // @Summary 通过id获取菜单
  51. // @Description 通过id获取菜单
  52. // @Tags 菜单
  53. // @Param id path string true "菜单id"
  54. // @Success 200 {object} response.Response{data=model.SysMenu} "{"code": 200, "data": [...]}"
  55. // @Router /api/menu/{id} [get]
  56. // @Security Bearer
  57. func (e SysMenuController) Get(c *gin.Context) {
  58. req := dto.SysMenuGetReq{}
  59. s := service.SysMenu{}
  60. err := e.MakeContext(c).
  61. MakeOrm().
  62. Bind(&req, nil).
  63. MakeService(&s.Service).
  64. Errors
  65. if err != nil {
  66. e.Logger.Error(err)
  67. e.Error(500, err, err.Error())
  68. return
  69. }
  70. var object = model.SysMenu{}
  71. err = s.Get(&req, &object).Error
  72. if err != nil {
  73. e.Error(500, err, "查询失败")
  74. return
  75. }
  76. e.OK(object, "查询成功")
  77. }
  78. // Insert 创建菜单
  79. // @Summary 创建菜单
  80. // @Description 创建菜单
  81. // @Tags 菜单
  82. // @Accept application/json
  83. // @Product application/json
  84. // @Param data body dto.SysMenuInsertReq true "data"
  85. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  86. // @Router /api/menu [post]
  87. // @Security Bearer
  88. func (e SysMenuController) Insert(c *gin.Context) {
  89. req := dto.SysMenuInsertReq{}
  90. s := service.SysMenu{}
  91. err := e.MakeContext(c).
  92. MakeOrm().
  93. Bind(&req, binding.JSON).
  94. MakeService(&s.Service).
  95. Errors
  96. if err != nil {
  97. e.Logger.Error(err)
  98. e.Error(500, err, err.Error())
  99. return
  100. }
  101. // 设置创建人
  102. req.SetCreateBy(user.GetUserId(c))
  103. err = s.Insert(&req).Error
  104. if err != nil {
  105. e.Error(500, err, "创建失败")
  106. return
  107. }
  108. e.OK(req.GetId(), "创建成功")
  109. }
  110. // Update 修改菜单
  111. // @Summary 修改菜单
  112. // @Description 修改菜单
  113. // @Tags 菜单
  114. // @Accept application/json
  115. // @Product application/json
  116. // @Param id path string true "菜单id"
  117. // @Param data body dto.SysMenuUpdateReq true "body"
  118. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  119. // @Router /api/menu/{id} [put]
  120. // @Security Bearer
  121. func (e SysMenuController) Update(c *gin.Context) {
  122. s := service.SysMenu{}
  123. req := dto.SysMenuUpdateReq{}
  124. err := e.MakeContext(c).
  125. MakeOrm().
  126. Bind(&req, binding.JSON, nil).
  127. MakeService(&s.Service).
  128. Errors
  129. if err != nil {
  130. e.Logger.Error(err)
  131. e.Error(500, err, err.Error())
  132. return
  133. }
  134. req.SetUpdateBy(user.GetUserId(c))
  135. err = s.Update(&req).Error
  136. if err != nil {
  137. e.Error(500, err, "更新失败")
  138. return
  139. }
  140. e.OK(req.GetId(), "更新成功")
  141. }
  142. // Delete 删除菜单
  143. // @Summary 删除菜单
  144. // @Description 删除菜单
  145. // @Tags 菜单
  146. // @Accept application/json
  147. // @Product application/json
  148. // @Param data body dto.SysMenuDeleteReq true "body"
  149. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  150. // @Router /api/menu [delete]
  151. // @Security Bearer
  152. func (e SysMenuController) Delete(c *gin.Context) {
  153. s := service.SysMenu{}
  154. req := dto.SysMenuDeleteReq{}
  155. err := e.MakeContext(c).
  156. MakeOrm().
  157. Bind(&req, binding.JSON).
  158. MakeService(&s.Service).
  159. Errors
  160. if err != nil {
  161. e.Logger.Error(err)
  162. e.Error(500, err, err.Error())
  163. return
  164. }
  165. req.SetUpdateBy(user.GetUserId(c))
  166. err = s.Remove(&req).Error
  167. if err != nil {
  168. e.Logger.Errorf("RemoveSysMenu error, %s", err)
  169. e.Error(500, err, "删除失败")
  170. return
  171. }
  172. e.OK(req.GetId(), "删除成功")
  173. }
  174. // GetMenuRole 根据登录角色名称获取菜单列表数据【左菜单使用】
  175. // @Summary 根据登录角色名称获取菜单列表数据【左菜单使用】
  176. // @Description 根据登录角色名称获取菜单列表数据【左菜单使用】
  177. // @Tags 菜单
  178. // @Success 200 {object} response.Response{data=[]model.SysMenu} "{"code": 200, "data": [...]}"
  179. // @Router /api/menu-role [get]
  180. // @Security Bearer
  181. func (e SysMenuController) GetMenuRole(c *gin.Context) {
  182. s := new(service.SysMenu)
  183. err := e.MakeContext(c).
  184. MakeOrm().
  185. MakeService(&s.Service).
  186. Errors
  187. if err != nil {
  188. e.Logger.Error(err)
  189. e.Error(500, err, err.Error())
  190. return
  191. }
  192. result, err := s.SetMenuRole(user.GetRoleName(c))
  193. if err != nil {
  194. e.Error(500, err, err.Error())
  195. return
  196. }
  197. e.OK(result, "")
  198. }
  199. func (e SysMenuController) List(c *gin.Context) {
  200. s := service.SysMenu{}
  201. req := dto.SysMenuGetPageReq{}
  202. err := e.MakeContext(c).
  203. MakeOrm().
  204. Bind(&req, binding.Form).
  205. MakeService(&s.Service).
  206. Errors
  207. if err != nil {
  208. e.Logger.Error(err)
  209. e.Error(500, err, err.Error())
  210. return
  211. }
  212. p := actions.GetPermissionFromContext(c)
  213. if p.DeptId == 0 {
  214. data := make([]map[string]interface{}, 0)
  215. err = json.Unmarshal([]byte(model.NoDeptMenu), &data)
  216. if err != nil {
  217. e.Error(500, err, err.Error())
  218. }
  219. e.OK(data, "查询成功")
  220. return
  221. }
  222. data := make([]map[string]interface{}, 0)
  223. err = json.Unmarshal([]byte(model.HaveDeptMenu), &data)
  224. if err != nil {
  225. e.Error(500, err, err.Error())
  226. }
  227. e.OK(data, "查询成功")
  228. }