role.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package system
  2. import (
  3. mycasbin "device-manage/pkg/casbin"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "device-manage/app/admin/models"
  7. "device-manage/tools"
  8. "device-manage/tools/app"
  9. )
  10. // @Summary 角色列表数据
  11. // @Description Get JSON
  12. // @Tags 角色/Role
  13. // @Param roleName query string false "roleName"
  14. // @Param status query string false "status"
  15. // @Param roleKey query string false "roleKey"
  16. // @Param pageSize query int false "页条数"
  17. // @Param pageIndex query int false "页码"
  18. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  19. // @Router /api/v1/rolelist [get]
  20. // @Security Bearer
  21. func GetRoleList(c *gin.Context) {
  22. var data models.SysRole
  23. var err error
  24. var pageSize = 10
  25. var pageIndex = 1
  26. if size := c.Request.FormValue("pageSize"); size != "" {
  27. pageSize, err = tools.StringToInt(size)
  28. }
  29. if index := c.Request.FormValue("pageIndex"); index != "" {
  30. pageIndex, err = tools.StringToInt(index)
  31. }
  32. data.RoleKey = c.Request.FormValue("roleKey")
  33. data.RoleName = c.Request.FormValue("roleName")
  34. data.Status = c.Request.FormValue("status")
  35. data.DataScope = tools.GetUserIdStr(c)
  36. result, count, err := data.GetPage(pageSize, pageIndex)
  37. tools.HasError(err, "", -1)
  38. app.PageOK(c, result, count, pageIndex, pageSize, "")
  39. }
  40. // @Summary 获取Role数据
  41. // @Description 获取JSON
  42. // @Tags 角色/Role
  43. // @Param roleId path string false "roleId"
  44. // @Success 200 {string} string "{"code": 200, "data": [...]}"
  45. // @Success 200 {string} string "{"code": -1, "message": "抱歉未找到相关信息"}"
  46. // @Router /api/v1/role [get]
  47. // @Security Bearer
  48. func GetRole(c *gin.Context) {
  49. var Role models.SysRole
  50. Role.RoleId, _ = tools.StringToInt(c.Param("roleId"))
  51. result, err := Role.Get()
  52. menuIds := make([]int, 0)
  53. menuIds, err = Role.GetRoleMeunId()
  54. tools.HasError(err, "抱歉未找到相关信息", -1)
  55. result.MenuIds = menuIds
  56. app.OK(c, result, "")
  57. }
  58. // @Summary 创建角色
  59. // @Description 获取JSON
  60. // @Tags 角色/Role
  61. // @Accept application/json
  62. // @Product application/json
  63. // @Param data body models.SysRole true "data"
  64. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  65. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  66. // @Router /api/v1/role [post]
  67. func InsertRole(c *gin.Context) {
  68. var data models.SysRole
  69. data.CreateBy = tools.GetUserIdStr(c)
  70. fmt.Println(">> data.RoleName:" + data.RoleName)
  71. fmt.Println(">> data.RoleKey:" + data.RoleKey)
  72. //bind 匹配相应数据
  73. err := c.Bind(&data)
  74. tools.HasError(err, "数据解析失败", 500)
  75. id, err := data.Insert()
  76. fmt.Println("data.RoleName:" + data.RoleName)
  77. fmt.Println("data.RoleKey:" + data.RoleKey)
  78. fmt.Println(">> data.DataScope:" + data.DataScope)
  79. tools.HasError(err, "", -1)
  80. data.RoleId = id
  81. var t models.RoleMenu
  82. if len(data.MenuIds) > 0 {
  83. _, err = t.Insert(id, data.MenuIds)
  84. tools.HasError(err, "", -1)
  85. }
  86. _, err = mycasbin.LoadPolicy()
  87. tools.HasError(err, "", -1)
  88. app.OK(c, data, "添加成功")
  89. }
  90. // @Summary 修改用户角色
  91. // @Description 获取JSON
  92. // @Tags 角色/Role
  93. // @Accept application/json
  94. // @Product application/json
  95. // @Param data body models.SysRole true "body"
  96. // @Success 200 {string} string "{"code": 200, "message": "修改成功"}"
  97. // @Success 200 {string} string "{"code": -1, "message": "修改失败"}"
  98. // @Router /api/v1/role [put]
  99. func UpdateRole(c *gin.Context) {
  100. var data models.SysRole
  101. data.UpdateBy = tools.GetUserIdStr(c)
  102. err := c.Bind(&data)
  103. tools.HasError(err, "数据解析失败", -1)
  104. result, err := data.Update(data.RoleId)
  105. tools.HasError(err, "", -1)
  106. var t models.RoleMenu
  107. _, err = t.DeleteRoleMenu(data.RoleId)
  108. tools.HasError(err, "修改失败(delete rm)", -1)
  109. if len(data.MenuIds) > 0 {
  110. _, err2 := t.Insert(data.RoleId, data.MenuIds)
  111. tools.HasError(err2, "修改失败(insert)", -1)
  112. }
  113. _, err = mycasbin.LoadPolicy()
  114. tools.HasError(err, "", -1)
  115. app.OK(c, result, "修改成功")
  116. }
  117. func UpdateRoleDataScope(c *gin.Context) {
  118. var data models.SysRole
  119. data.UpdateBy = tools.GetUserIdStr(c)
  120. err := c.Bind(&data)
  121. tools.HasError(err, "数据解析失败", -1)
  122. result, err := data.Update(data.RoleId)
  123. var t models.SysRoleDept
  124. _, err = t.DeleteRoleDept(data.RoleId)
  125. tools.HasError(err, "添加失败1", -1)
  126. if data.DataScope == "2" {
  127. _, err2 := t.Insert(data.RoleId, data.DeptIds)
  128. tools.HasError(err2, "添加失败2", -1)
  129. }
  130. app.OK(c, result, "修改成功")
  131. }
  132. // @Summary 删除用户角色
  133. // @Description 删除数据
  134. // @Tags 角色/Role
  135. // @Param roleId path int true "roleId"
  136. // @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
  137. // @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
  138. // @Router /api/v1/role/{roleId} [delete]
  139. func DeleteRole(c *gin.Context) {
  140. var Role models.SysRole
  141. Role.UpdateBy = tools.GetUserIdStr(c)
  142. IDS := tools.IdsStrToIdsIntGroup("roleId", c)
  143. _, err := Role.BatchDelete(IDS)
  144. tools.HasError(err, "删除失败", -1)
  145. _, err = mycasbin.LoadPolicy()
  146. tools.HasError(err, "", -1)
  147. app.OK(c, "", "删除成功")
  148. }