sys_role.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package apis
  2. import (
  3. "fmt"
  4. "go-admin/common/global"
  5. "net/http"
  6. "github.com/gin-gonic/gin/binding"
  7. "github.com/go-admin-team/go-admin-core/sdk"
  8. "go-admin/app/admin/models"
  9. "github.com/gin-gonic/gin"
  10. "github.com/go-admin-team/go-admin-core/sdk/api"
  11. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  12. _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  13. "go-admin/app/admin/service"
  14. "go-admin/app/admin/service/dto"
  15. )
  16. type SysRole struct {
  17. api.Api
  18. }
  19. // GetPage
  20. // @Summary 角色列表数据
  21. // @Description Get JSON
  22. // @Tags 角色/Role
  23. // @Param roleName query string false "roleName"
  24. // @Param status query string false "status"
  25. // @Param roleKey query string false "roleKey"
  26. // @Param pageSize query int false "页条数"
  27. // @Param pageIndex query int false "页码"
  28. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  29. // @Router /api/v1/role [get]
  30. // @Security Bearer
  31. func (e SysRole) GetPage(c *gin.Context) {
  32. s := service.SysRole{}
  33. req := dto.SysRoleGetPageReq{}
  34. err := e.MakeContext(c).
  35. MakeOrm().
  36. Bind(&req, binding.Form).
  37. MakeService(&s.Service).
  38. Errors
  39. if err != nil {
  40. e.Logger.Error(err)
  41. e.Error(500, err, err.Error())
  42. return
  43. }
  44. list := make([]models.SysRole, 0)
  45. var count int64
  46. err = s.GetPage(&req, &list, &count)
  47. if err != nil {
  48. e.Error(500, err, "查询失败")
  49. return
  50. }
  51. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  52. }
  53. // Get
  54. // @Summary 获取Role数据
  55. // @Description 获取JSON
  56. // @Tags 角色/Role
  57. // @Param roleId path string false "roleId"
  58. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  59. // @Router /api/v1/role/{id} [get]
  60. // @Security Bearer
  61. func (e SysRole) Get(c *gin.Context) {
  62. s := service.SysRole{}
  63. req := dto.SysRoleGetReq{}
  64. err := e.MakeContext(c).
  65. MakeOrm().
  66. Bind(&req, nil).
  67. MakeService(&s.Service).
  68. Errors
  69. if err != nil {
  70. e.Logger.Error(err)
  71. e.Error(500, err, fmt.Sprintf(" %s ", err.Error()))
  72. return
  73. }
  74. var object models.SysRole
  75. err = s.Get(&req, &object)
  76. if err != nil {
  77. e.Error(http.StatusUnprocessableEntity, err, "查询失败")
  78. return
  79. }
  80. e.OK(object, "查询成功")
  81. }
  82. // Insert
  83. // @Summary 创建角色
  84. // @Description 获取JSON
  85. // @Tags 角色/Role
  86. // @Accept application/json
  87. // @Product application/json
  88. // @Param data body dto.SysRoleInsertReq true "data"
  89. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  90. // @Router /api/v1/role [post]
  91. // @Security Bearer
  92. func (e SysRole) Insert(c *gin.Context) {
  93. s := service.SysRole{}
  94. req := dto.SysRoleInsertReq{}
  95. err := e.MakeContext(c).
  96. MakeOrm().
  97. Bind(&req, binding.JSON).
  98. MakeService(&s.Service).
  99. Errors
  100. if err != nil {
  101. e.Logger.Error(err)
  102. e.Error(500, err, err.Error())
  103. return
  104. }
  105. // 设置创建人
  106. req.CreateBy = user.GetUserId(c)
  107. if req.Status == "" {
  108. req.Status = "2"
  109. }
  110. cb := sdk.Runtime.GetCasbinKey(c.Request.Host)
  111. err = s.Insert(&req, cb)
  112. if err != nil {
  113. e.Logger.Error(err)
  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 获取JSON
  128. // @Tags 角色/Role
  129. // @Accept application/json
  130. // @Product application/json
  131. // @Param data body dto.SysRoleUpdateReq true "body"
  132. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  133. // @Router /api/v1/role/{id} [put]
  134. // @Security Bearer
  135. func (e SysRole) Update(c *gin.Context) {
  136. s := service.SysRole{}
  137. req := dto.SysRoleUpdateReq{}
  138. err := e.MakeContext(c).
  139. MakeOrm().
  140. Bind(&req, nil, binding.JSON).
  141. MakeService(&s.Service).
  142. Errors
  143. if err != nil {
  144. e.Logger.Error(err)
  145. e.Error(500, err, err.Error())
  146. return
  147. }
  148. cb := sdk.Runtime.GetCasbinKey(c.Request.Host)
  149. req.SetUpdateBy(user.GetUserId(c))
  150. err = s.Update(&req, cb)
  151. if err != nil {
  152. e.Logger.Error(err)
  153. return
  154. }
  155. _, err = global.LoadPolicy(c)
  156. if err != nil {
  157. e.Logger.Error(err)
  158. e.Error(500, err, "更新失败,"+err.Error())
  159. return
  160. }
  161. e.OK(req.GetId(), "更新成功")
  162. }
  163. // Delete
  164. // @Summary 删除用户角色
  165. // @Description 删除数据
  166. // @Tags 角色/Role
  167. // @Param data body dto.SysRoleDeleteReq true "body"
  168. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  169. // @Router /api/v1/role [delete]
  170. // @Security Bearer
  171. func (e SysRole) Delete(c *gin.Context) {
  172. s := new(service.SysRole)
  173. req := dto.SysRoleDeleteReq{}
  174. err := e.MakeContext(c).
  175. MakeOrm().
  176. Bind(&req, binding.JSON).
  177. MakeService(&s.Service).
  178. Errors
  179. if err != nil {
  180. e.Logger.Error(err)
  181. e.Error(500, err, fmt.Sprintf("删除角色 %v 失败,\r\n失败信息 %s", req.Ids, err.Error()))
  182. return
  183. }
  184. cb := sdk.Runtime.GetCasbinKey(c.Request.Host)
  185. err = s.Remove(&req, cb)
  186. if err != nil {
  187. e.Logger.Error(err)
  188. e.Error(500, err, "")
  189. return
  190. }
  191. e.OK(req.GetId(), fmt.Sprintf("删除角色角色 %v 状态成功!", req.GetId()))
  192. }
  193. // Update2Status 修改用户角色状态
  194. // @Summary 修改用户角色
  195. // @Description 获取JSON
  196. // @Tags 角色/Role
  197. // @Accept application/json
  198. // @Product application/json
  199. // @Param data body dto.UpdateStatusReq true "body"
  200. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  201. // @Router /api/v1/role-status/{id} [put]
  202. // @Security Bearer
  203. func (e SysRole) Update2Status(c *gin.Context) {
  204. s := service.SysRole{}
  205. req := dto.UpdateStatusReq{}
  206. err := e.MakeContext(c).
  207. MakeOrm().
  208. Bind(&req, binding.JSON, nil).
  209. MakeService(&s.Service).
  210. Errors
  211. if err != nil {
  212. e.Logger.Error(err)
  213. e.Error(500, err, fmt.Sprintf("更新角色状态失败,失败原因:%s ", err.Error()))
  214. return
  215. }
  216. req.SetUpdateBy(user.GetUserId(c))
  217. err = s.UpdateStatus(&req)
  218. if err != nil {
  219. e.Error(500, err, fmt.Sprintf("更新角色状态失败,失败原因:%s ", err.Error()))
  220. return
  221. }
  222. e.OK(req.GetId(), fmt.Sprintf("更新角色 %v 状态成功!", req.GetId()))
  223. }
  224. // Update2DataScope 更新角色数据权限
  225. // @Summary 更新角色数据权限
  226. // @Description 获取JSON
  227. // @Tags 角色/Role
  228. // @Accept application/json
  229. // @Product application/json
  230. // @Param data body dto.RoleDataScopeReq true "body"
  231. // @Success 200 {object} response.Response "{"code": 200, "data": [...]}"
  232. // @Router /api/v1/role-status/{id} [put]
  233. // @Security Bearer
  234. func (e SysRole) Update2DataScope(c *gin.Context) {
  235. s := service.SysRole{}
  236. req := dto.RoleDataScopeReq{}
  237. err := e.MakeContext(c).
  238. MakeOrm().
  239. Bind(&req, binding.JSON, nil).
  240. MakeService(&s.Service).
  241. Errors
  242. if err != nil {
  243. e.Logger.Error(err)
  244. e.Error(500, err, err.Error())
  245. return
  246. }
  247. data := &models.SysRole{
  248. RoleId: req.RoleId,
  249. DataScope: req.DataScope,
  250. DeptIds: req.DeptIds,
  251. }
  252. data.UpdateBy = user.GetUserId(c)
  253. err = s.UpdateDataScope(&req).Error
  254. if err != nil {
  255. e.Error(500, err, fmt.Sprintf("更新角色数据权限失败!错误详情:%s", err.Error()))
  256. return
  257. }
  258. e.OK(nil, "操作成功")
  259. }