sys_role.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. package controller
  2. import (
  3. "Medical_OAuth/common/actions"
  4. "Medical_OAuth/common/global"
  5. "github.com/gin-gonic/gin"
  6. "net/http"
  7. "Medical_OAuth/app/admin/model"
  8. "Medical_OAuth/app/admin/service"
  9. "Medical_OAuth/app/admin/service/dto"
  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. "gogs.baozhida.cn/zoie/OAuth-core/sdk"
  14. "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
  15. "github.com/gin-gonic/gin/binding"
  16. )
  17. type SysRoleController struct {
  18. api.Api
  19. }
  20. // GetPage 获取角色列表
  21. // @Summary 角色列表数据
  22. // @Description 获取角色列表
  23. // @Tags 角色
  24. // @Param roleName query string false "角色名称"
  25. // @Param pageSize query int false "页条数"
  26. // @Param pageIndex query int false "页码"
  27. // @Success 200 {object} response.Response{data=response.Page{list=[]model.SysRole}} "{"code": 200, "data": [...]}"
  28. // @Router /api/role [get]
  29. // @Security Bearer
  30. func (e SysRoleController) GetPage(c *gin.Context) {
  31. s := service.SysRole{}
  32. req := dto.SysRoleGetPageReq{}
  33. err := e.MakeContext(c).
  34. MakeOrm().
  35. Bind(&req, binding.Form).
  36. MakeService(&s.Service).
  37. Errors
  38. if err != nil {
  39. e.Logger.Error(err)
  40. e.Error(500, err, err.Error())
  41. return
  42. }
  43. list := make([]model.SysRole, 0)
  44. var count int64
  45. //数据权限检查
  46. p := actions.GetPermissionFromContext(c)
  47. err = s.GetPage(&req, &list, &count, p)
  48. if err != nil {
  49. e.Logger.Error(err)
  50. e.Error(500, err, err.Error())
  51. return
  52. }
  53. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  54. }
  55. // Get 通过id获取角色
  56. // @Summary 通过id获取角色
  57. // @Description 通过id获取角色
  58. // @Tags 角色
  59. // @Param id path string true "角色Id"
  60. // @Success 200 {object} response.Response{data=model.SysRole} "{"code": 200, "data": [...]}"
  61. // @Router /api/role/{id} [get]
  62. // @Security Bearer
  63. func (e SysRoleController) Get(c *gin.Context) {
  64. s := service.SysRole{}
  65. req := dto.SysRoleGetReq{}
  66. err := e.MakeContext(c).
  67. MakeOrm().
  68. Bind(&req, nil).
  69. MakeService(&s.Service).
  70. Errors
  71. if err != nil {
  72. e.Logger.Error(err)
  73. e.Error(500, err, err.Error())
  74. return
  75. }
  76. var object model.SysRole
  77. //数据权限检查
  78. p := actions.GetPermissionFromContext(c)
  79. err = s.Get(&req, &object, p)
  80. if err != nil {
  81. e.Logger.Error(err)
  82. e.Error(http.StatusUnprocessableEntity, err, err.Error())
  83. return
  84. }
  85. e.OK(object, "查询成功")
  86. }
  87. // Insert 创建角色
  88. // @Summary 创建角色
  89. // @Description 创建角色
  90. // @Tags 角色
  91. // @Accept application/json
  92. // @Product application/json
  93. // @Param data body dto.SysRoleInsertReq true "data"
  94. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  95. // @Router /api/role [post]
  96. // @Security Bearer
  97. func (e SysRoleController) Insert(c *gin.Context) {
  98. s := service.SysRole{}
  99. req := dto.SysRoleInsertReq{}
  100. err := e.MakeContext(c).
  101. MakeOrm().
  102. Bind(&req, binding.JSON).
  103. MakeService(&s.Service).
  104. Errors
  105. if err != nil {
  106. e.Logger.Error(err)
  107. e.Error(500, err, err.Error())
  108. return
  109. }
  110. // 设置创建人
  111. req.CreateBy = user.GetUserId(c)
  112. err = s.Insert(&req)
  113. if err != nil {
  114. e.Error(500, err, err.Error())
  115. return
  116. }
  117. _, err = global.LoadPolicy(c)
  118. if err != nil {
  119. e.Logger.Error(err)
  120. e.Error(500, err, err.Error())
  121. return
  122. }
  123. e.OK(req.GetId(), "创建成功")
  124. }
  125. // Update 修改用户角色
  126. // @Summary 修改用户角色
  127. // @Description 修改用户角色
  128. // @Tags 角色
  129. // @Accept application/json
  130. // @Product application/json
  131. // @Param id path string true "角色Id"
  132. // @Param data body dto.SysRoleUpdateReq true "body"
  133. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  134. // @Router /api/role/{id} [put]
  135. // @Security Bearer
  136. func (e SysRoleController) Update(c *gin.Context) {
  137. s := service.SysRole{}
  138. req := dto.SysRoleUpdateReq{}
  139. err := e.MakeContext(c).
  140. MakeOrm().
  141. Bind(&req, nil, binding.JSON).
  142. MakeService(&s.Service).
  143. Errors
  144. if err != nil {
  145. e.Logger.Error(err)
  146. e.Error(500, err, err.Error())
  147. return
  148. }
  149. req.SetUpdateBy(user.GetUserId(c))
  150. //数据权限检查
  151. p := actions.GetPermissionFromContext(c)
  152. err = s.Update(&req, p)
  153. if err != nil {
  154. e.Logger.Error(err)
  155. e.Error(500, err, err.Error())
  156. return
  157. }
  158. _, err = global.LoadPolicy(c)
  159. if err != nil {
  160. e.Logger.Error(err)
  161. e.Error(500, err, err.Error())
  162. return
  163. }
  164. e.OK(req.GetId(), "更新成功")
  165. }
  166. // Delete 删除用户角色
  167. // @Summary 删除用户角色
  168. // @Description 删除用户角色
  169. // @Tags 角色
  170. // @Accept application/json
  171. // @Product application/json
  172. // @Param data body dto.SysRoleDeleteReq true "body"
  173. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  174. // @Router /api/role [delete]
  175. // @Security Bearer
  176. func (e SysRoleController) Delete(c *gin.Context) {
  177. s := new(service.SysRole)
  178. req := dto.SysRoleDeleteReq{}
  179. userSvc := service.SysUser{}
  180. err := e.MakeContext(c).
  181. MakeOrm().
  182. Bind(&req, binding.JSON).
  183. MakeService(&s.Service).
  184. MakeService(&userSvc.Service).
  185. Errors
  186. if err != nil {
  187. e.Logger.Error(err)
  188. e.Error(500, err, err.Error())
  189. return
  190. }
  191. userReq := dto.SysUserGetCountReq{RoleIds: []int{req.Id}}
  192. var count int64
  193. err = userSvc.GetCount(&userReq, &count)
  194. if err != nil {
  195. e.Error(500, err, err.Error())
  196. return
  197. }
  198. if count > 0 {
  199. e.Error(500, err, "有用户关联,禁止删除!")
  200. return
  201. }
  202. //数据权限检查
  203. p := actions.GetPermissionFromContext(c)
  204. //cb := sdk.Runtime.GetCasbinKey(c.Request.Host)
  205. cb := sdk.Runtime.GetCasbinKey(config.ApplicationConfig.Host)
  206. // fixme 删除角色与路由,角色与菜单之间的关联
  207. err = s.Remove(&req, p, cb)
  208. if err != nil {
  209. e.Logger.Error(err)
  210. e.Error(500, err, err.Error())
  211. return
  212. }
  213. e.OK(req.GetId(), "删除成功")
  214. }
  215. // UpdateRoleApi 修改角色路由权限
  216. // @Summary 修改角色路由权限
  217. // @Description 修改角色路由权限
  218. // @Tags 角色
  219. // @Accept application/json
  220. // @Product application/json
  221. // @Param id path string true "角色Id"
  222. // @Param data body dto.SysRoleUpdateRoleApiReq true "body"
  223. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  224. // @Router /api/role-api/{id} [put]
  225. // @Security Bearer
  226. func (e SysRoleController) UpdateRoleApi(c *gin.Context) {
  227. s := service.SysRole{}
  228. req := dto.SysRoleUpdateRoleApiReq{}
  229. err := e.MakeContext(c).
  230. MakeOrm().
  231. Bind(&req, nil, binding.JSON).
  232. MakeService(&s.Service).
  233. Errors
  234. if err != nil {
  235. e.Logger.Error(err)
  236. e.Error(500, err, err.Error())
  237. return
  238. }
  239. req.SetUpdateBy(user.GetUserId(c))
  240. cb := sdk.Runtime.GetCasbinKey(config.ApplicationConfig.Host)
  241. err = s.UpdateRoleApi(&req, cb)
  242. if err != nil {
  243. e.Logger.Error(err)
  244. e.Error(500, err, err.Error())
  245. return
  246. }
  247. _, err = global.LoadPolicy(c)
  248. if err != nil {
  249. e.Logger.Error(err)
  250. e.Error(500, err, "更新失败,"+err.Error())
  251. return
  252. }
  253. e.OK(req.GetId(), "更新角色路由权限成功")
  254. }
  255. // GetRoleApi 获取角色路由权限
  256. // @Summary 获取角色路由权限
  257. // @Description 获取角色路由权限
  258. // @Tags 角色
  259. // @Accept application/json
  260. // @Product application/json
  261. // @Param id path string true "角色Id"
  262. // @Param service_id query int true "服务id"
  263. // @Success 200 {object} response.Response{data=dto.SysRoleGetRoleApiListResp} "{"code": 200, "data": [...]}"
  264. // @Router /api/role-api/{id} [get]
  265. // @Security Bearer
  266. func (e SysRoleController) GetRoleApi(c *gin.Context) {
  267. s := service.SysRole{}
  268. req := dto.SysRoleGetRoleApiListReq{}
  269. err := e.MakeContext(c).
  270. MakeOrm().
  271. Bind(&req, nil, binding.Form).
  272. MakeService(&s.Service).
  273. Errors
  274. if err != nil {
  275. e.Logger.Error(err)
  276. e.Error(500, err, err.Error())
  277. return
  278. }
  279. var apiList []model.ServApi
  280. var apiIds []int
  281. err = s.GetRoleApiList(&req, &apiIds, &apiList)
  282. if err != nil {
  283. e.Error(500, err, err.Error())
  284. return
  285. }
  286. e.OK(dto.SysRoleGetRoleApiListResp{
  287. ApiList: apiList,
  288. ApiIds: apiIds,
  289. }, "查询成功")
  290. }
  291. // UpdateRoleMenu 修改角色菜单权限
  292. // @Summary 修改角色菜单权限
  293. // @Description 修改角色菜单权限
  294. // @Tags 角色
  295. // @Accept application/json
  296. // @Product application/json
  297. // @Param id path string true "角色Id"
  298. // @Param data body dto.SysRoleUpdateRoleMenuReq true "body"
  299. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  300. // @Router /api/role-menu/{id} [put]
  301. // @Security Bearer
  302. func (e SysRoleController) UpdateRoleMenu(c *gin.Context) {
  303. s := service.SysRole{}
  304. req := dto.SysRoleUpdateRoleMenuReq{}
  305. err := e.MakeContext(c).
  306. MakeOrm().
  307. Bind(&req, nil, binding.JSON).
  308. MakeService(&s.Service).
  309. Errors
  310. if err != nil {
  311. e.Logger.Error(err)
  312. e.Error(500, err, err.Error())
  313. return
  314. }
  315. req.SetUpdateBy(user.GetUserId(c))
  316. err = s.UpdateRoleMenu(&req)
  317. if err != nil {
  318. e.Logger.Error(err)
  319. e.Error(500, err, err.Error())
  320. return
  321. }
  322. e.OK(req.GetId(), "更新角色路由权限成功")
  323. }
  324. // GetRoleMenu 修改角色菜单权限
  325. // @Summary 修改角色菜单权限
  326. // @Description 修改角色菜单权限
  327. // @Tags 角色
  328. // @Accept application/json
  329. // @Product application/json
  330. // @Param id path string true "角色Id"
  331. // @Success 200 {object} response.Response{data=dto.SysRoleGetRoleMenuListResp} "{"code": 200, "data": [...]}"
  332. // @Router /api/role-menu/{id} [get]
  333. // @Security Bearer
  334. func (e SysRoleController) GetRoleMenu(c *gin.Context) {
  335. req := dto.SysRoleGetRoleMenuListReq{}
  336. m := service.SysMenu{}
  337. r := service.SysRole{}
  338. err := e.MakeContext(c).
  339. MakeOrm().
  340. MakeService(&m.Service).
  341. MakeService(&r.Service).
  342. Bind(&req, nil).
  343. Errors
  344. if err != nil {
  345. e.Logger.Error(err)
  346. e.Error(500, err, err.Error())
  347. return
  348. }
  349. result, err := m.SetLabel()
  350. if err != nil {
  351. e.Error(500, err, err.Error())
  352. return
  353. }
  354. menuIds := make([]int, 0)
  355. if req.Id != 0 {
  356. menuIds, err = r.GetRoleMenuId(req.Id)
  357. if err != nil {
  358. e.Error(500, err, "")
  359. return
  360. }
  361. }
  362. e.OK(gin.H{
  363. "menus": result,
  364. "checkedKeys": menuIds,
  365. }, "获取成功")
  366. }