123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package admin_user
- import (
- "github.com/gin-gonic/gin"
- "net/http"
- "sghgogs.com/micro/auth-client/model/base"
- "sghgogs.com/micro/auth-client/response"
- "sghgogs.com/micro/auth-client/utils"
- pb "sghgogs.com/micro/auth-service/proto"
- "sghgogs.com/micro/common"
- "strconv"
- )
- type ToggleRequest struct {
- Status base.Status `json:"status" binding:"required"` // "enabled"|"disabled"
- }
- // ToggleAdminUser
- // @summary 启用/禁用
- // @Tags User 用户管理
- // @Accept json
- // @Produce json
- // @Param Authorization header string true "Bearer 用户令牌"
- // @Param body body ToggleRequest true "请求body"
- // @Success 200 {object} response.ApiResponse "成功"
- // @Failure 400 {object} response.ApiResponse "请求错误"
- // @Failure 500 {object} response.ApiResponse "内部错误"
- // @Router /v1/api/admin/user/{userID}/toggle [put]
- func (svc *ApiAdminUser) ToggleAdminUser(c *gin.Context) {
- var req ToggleRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
- return
- }
- UserID, err := strconv.ParseInt(c.Param("userID"), 10, 64)
- if err != nil || UserID <= 0 {
- c.JSON(http.StatusBadRequest, gin.H{"error": common.ErrorMessage[common.InvalidUserID]})
- return
- }
- // ToggleRequest
- if !base.IsValidStatus(string(req.Status)) {
- c.JSON(http.StatusBadRequest, gin.H{"error": common.ErrorMessage[common.StatusIsNotValid]})
- return
- }
- enum, _ := base.MapStatusToEnum(req.Status)
- ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminUserService.ToggleAdminUser")
- _, err = svc.Service.ToggleAdminUser(ctx, &pb.ToggleAdminUserRequest{
- UserId: UserID,
- Status: enum,
- })
- if err != nil {
- code, mgs := response.MicroErrorRequest(err)
- c.JSON(code, response.ErrorResponse(code, mgs))
- return
- }
- c.JSON(http.StatusOK, response.SuccessResponse(nil))
- }
|