user.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/system/dao"
  5. "iot_manager_service/app/system/model"
  6. "iot_manager_service/app/system/service"
  7. "iot_manager_service/util"
  8. "math"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. )
  13. var User = new(user)
  14. type user struct{}
  15. func (c *user) GetDetail(ctx *gin.Context) {
  16. id := ctx.Query("id")
  17. iId, err := strconv.ParseInt(id, 10, 64)
  18. if err != nil {
  19. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  20. return
  21. }
  22. detail, err := service.UserService.Get(iId)
  23. if err != nil {
  24. ctx.JSON(http.StatusOK, err)
  25. return
  26. }
  27. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, detail))
  28. }
  29. func (c *user) List(ctx *gin.Context) {
  30. current, _ := strconv.Atoi(ctx.Query("current"))
  31. size, _ := strconv.Atoi(ctx.Query("size"))
  32. account := ctx.Query("account")
  33. realName := ctx.Query("realName")
  34. if current == 0 {
  35. current = 1
  36. }
  37. if size <= 0 || size > 100 {
  38. size = 10
  39. }
  40. users, err := service.UserService.List(account, realName, current, size)
  41. if err != nil {
  42. ctx.JSON(http.StatusOK, err)
  43. return
  44. }
  45. pages := math.Ceil(float64(len(users)) / float64(size))
  46. rsp := model.RsqUserList{
  47. Current: current,
  48. Size: size,
  49. Total: len(users),
  50. Pages: int(pages),
  51. Records: users,
  52. }
  53. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
  54. }
  55. func (c *user) Submit(ctx *gin.Context) {
  56. var req dao.User
  57. if err := ctx.ShouldBindJSON(&req); err != nil {
  58. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  59. return
  60. }
  61. err := service.UserService.Submit(req)
  62. ctx.JSON(http.StatusOK, err)
  63. }
  64. func (c *user) Update(ctx *gin.Context) {
  65. var req dao.User
  66. if err := ctx.ShouldBindJSON(&req); err != nil {
  67. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  68. return
  69. }
  70. err := service.UserService.Update(req)
  71. ctx.JSON(http.StatusOK, err)
  72. }
  73. func (c *user) Remove(ctx *gin.Context) {
  74. var req *model.ReqUserRemove
  75. if err := ctx.ShouldBindJSON(&req); err != nil {
  76. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  77. return
  78. }
  79. err := service.UserService.Remove(req.IDs)
  80. ctx.JSON(http.StatusOK, err)
  81. }
  82. func (c *user) ResetPwd(ctx *gin.Context) {
  83. var req *model.ReqUserResetPwd
  84. if err := ctx.ShouldBindJSON(&req); err != nil {
  85. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  86. return
  87. }
  88. err := service.UserService.ResetPwd(req.IDs)
  89. ctx.JSON(http.StatusOK, err)
  90. }
  91. func (c *user) GetList(ctx *gin.Context) {
  92. users, err := service.UserService.GetList()
  93. if err != nil {
  94. ctx.JSON(http.StatusOK, util.FailResponse(err.Error(), nil))
  95. }
  96. ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, users))
  97. }
  98. func (c *user) Grant(ctx *gin.Context) {
  99. var req *model.ReqUserGrant
  100. if err := ctx.ShouldBindJSON(&req); err != nil {
  101. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(err.Error(), nil))
  102. return
  103. }
  104. uIds := strings.Split(req.UserIds, ",")
  105. rIds := strings.Split(req.RoleIds, ",")
  106. if len(uIds) < 1 || len(rIds) < 1 {
  107. ctx.JSON(http.StatusOK, util.ParamsInvalidResponse(model.GrantInvalid, nil))
  108. return
  109. }
  110. err := service.UserService.Grant(uIds, req.RoleIds)
  111. ctx.JSON(http.StatusOK, err)
  112. }