package admin_role import ( "github.com/gin-gonic/gin" "net/http" "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 UpdateRequest struct { Description string `json:"description"` // 非必填,描述 Users []int64 `json:"users"` // 管理员 Permissions []int64 `json:"permissions"` // 权限 } // UpdateAdminRole // @summary 更新角色 // @Description 更新角色内容 // @Tags Role 角色管理 // @Accept json // @Produce json // @Param Authorization header string true "Bearer 用户令牌" // @Param id path int true "角色ID" // @Param body body UpdateRequest true "请求body" // @Success 200 {object} response.ApiResponse "成功" // @Failure 400 {object} response.ApiResponse "请求错误" // @Failure 500 {object} response.ApiResponse "内部错误" // @Router /v1/api/admin/role/{roleID} [put] func (svc *ApiAdminRole) UpdateAdminRole(c *gin.Context) { var req UpdateRequest if err := c.BindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error())) return } roleID, err := strconv.ParseInt(c.Param("roleID"), 10, 64) if err != nil || roleID <= 0 { c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, common.ErrorMessage[common.InvalidRoleID])) return } if len(req.Users) > 0 { if isBool, err := common.ValidateNumericInt64(req.Users); !isBool { c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error())) return } } if len(req.Permissions) > 0 { if isBool, err := common.ValidateNumericInt64(req.Permissions); !isBool { c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error())) return } } ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminRoleService.UpdateAdminRole") _, err = svc.Service.UpdateAdminRole(ctx, &pb.UpdateAdminRoleRequest{ RoleId: roleID, Description: req.Description, Users: req.Users, Permissions: req.Permissions, }) if err != nil { code, mgs := response.MicroErrorRequest(err) c.JSON(code, response.ErrorResponse(code, mgs)) return } c.JSON(http.StatusOK, response.SuccessResponse(nil)) }