toggle_admin_user.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package admin_user
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. "sghgogs.com/micro/auth-client/model/base"
  6. "sghgogs.com/micro/auth-client/response"
  7. "sghgogs.com/micro/auth-client/utils"
  8. pb "sghgogs.com/micro/auth-service/proto"
  9. "sghgogs.com/micro/common"
  10. "strconv"
  11. )
  12. type ToggleRequest struct {
  13. Status base.Status `json:"status" binding:"required"` // "enabled"|"disabled"
  14. }
  15. // ToggleAdminUser
  16. // @summary 启用/禁用
  17. // @Tags User 用户管理
  18. // @Accept json
  19. // @Produce json
  20. // @Param Authorization header string true "Bearer 用户令牌"
  21. // @Param body body ToggleRequest true "请求body"
  22. // @Success 200 {object} response.ApiResponse "成功"
  23. // @Failure 400 {object} response.ApiResponse "请求错误"
  24. // @Failure 500 {object} response.ApiResponse "内部错误"
  25. // @Router /v1/api/admin/user/{userID}/toggle [put]
  26. func (svc *ApiAdminUser) ToggleAdminUser(c *gin.Context) {
  27. var req ToggleRequest
  28. if err := c.ShouldBindJSON(&req); err != nil {
  29. c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
  30. return
  31. }
  32. UserID, err := strconv.ParseInt(c.Param("userID"), 10, 64)
  33. if err != nil || UserID <= 0 {
  34. c.JSON(http.StatusBadRequest, gin.H{"error": common.ErrorMessage[common.InvalidUserID]})
  35. return
  36. }
  37. // ToggleRequest
  38. if !base.IsValidStatus(string(req.Status)) {
  39. c.JSON(http.StatusBadRequest, gin.H{"error": common.ErrorMessage[common.StatusIsNotValid]})
  40. return
  41. }
  42. enum, _ := base.MapStatusToEnum(req.Status)
  43. ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminUserService.ToggleAdminUser")
  44. _, err = svc.Service.ToggleAdminUser(ctx, &pb.ToggleAdminUserRequest{
  45. UserId: UserID,
  46. Status: enum,
  47. })
  48. if err != nil {
  49. code, mgs := response.MicroErrorRequest(err)
  50. c.JSON(code, response.ErrorResponse(code, mgs))
  51. return
  52. }
  53. c.JSON(http.StatusOK, response.SuccessResponse(nil))
  54. }