role.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package admin
  2. import (
  3. "zhiyuan/pkg/app"
  4. "zhiyuan/pkg/utils"
  5. "zhiyuan/services/form"
  6. "zhiyuan/services/role"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func RoleInfo(c *gin.Context) {
  10. id := utils.StrTo(c.Param("id")).MustInt()
  11. if id <= 0 {
  12. app.ErrorMsg(c, "role id must be a number", nil)
  13. return
  14. }
  15. auth, err := role.GetInfoByID(id, nil, nil)
  16. if err != nil {
  17. app.Error(c, err.Error())
  18. return
  19. }
  20. app.Success(c, auth)
  21. }
  22. func RoleList(c *gin.Context) {
  23. page := app.HandlePageNums(c)
  24. where := make(map[string]interface{})
  25. total, err := role.Count(where)
  26. if err != nil {
  27. app.Error(c, err.Error())
  28. return
  29. }
  30. type RoleList struct {
  31. ID int `json:"id"`
  32. Name string `json:"name"`
  33. CreatedAt string `json:"created_at"`
  34. AuthIds string `json:"auth_ids"`
  35. DataAuth int `json:"data_auth"`
  36. }
  37. var roleList []RoleList
  38. _, err = role.GetList(where, []string{"id", "name", "created_at", "auth_ids", "data_auth"}, page, &roleList)
  39. if err != nil {
  40. app.Error(c, err.Error())
  41. return
  42. }
  43. for k, v := range roleList {
  44. v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD")
  45. roleList[k] = v
  46. }
  47. data := gin.H{
  48. "list": roleList,
  49. "total": total,
  50. "limit": page.PageSize,
  51. }
  52. app.Success(c, data)
  53. }
  54. func RoleAdd(c *gin.Context) {
  55. var form form.RoleAdd
  56. if app.Bind(c, &form) != nil {
  57. return
  58. }
  59. id, err := role.Add(form)
  60. if err != nil {
  61. app.Error(c, err.Error())
  62. return
  63. }
  64. app.Success(c, gin.H{"id": id})
  65. }
  66. func RoleEdit(c *gin.Context) {
  67. id := utils.StrTo(c.Param("id")).MustInt()
  68. if id <= 0 {
  69. app.ErrorMsg(c, "auth id must be a number", nil)
  70. return
  71. }
  72. var form form.RoleAdd
  73. if app.Bind(c, &form) != nil {
  74. return
  75. }
  76. err := role.EditByID(form, id)
  77. if err != nil {
  78. app.Error(c, err.Error())
  79. return
  80. }
  81. app.Success(c, nil)
  82. }
  83. func RoleDel(c *gin.Context) {
  84. id := utils.StrTo(c.Param("id")).MustInt()
  85. if id <= 0 {
  86. app.ErrorMsg(c, "auth id must be a number", nil)
  87. return
  88. }
  89. err := role.DeleteByID(id)
  90. if err != nil {
  91. app.Error(c, err.Error())
  92. return
  93. }
  94. app.Success(c, nil)
  95. }