1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package admin_user
- import (
- "github.com/gin-gonic/gin"
- "net/http"
- "sghgogs.com/sghblog/authorization-client/response"
- "sghgogs.com/sghblog/authorization-client/utils"
- pb "sghgogs.com/sghblog/authorization-service/proto"
- "sghgogs.com/sghblog/common"
- "strconv"
- )
- type UpdateRequest struct {
- PhoneNumber string `json:"phone_number" binding:"required"` // 手机号
- Email string `json:"email" binding:"required,email"` // 邮箱
- Avatar string `json:"avatar"` // 头像
- Roles []int64 `json:"roles"` // 角色ID
- Teams []int64 `json:"teams"` // 团队ID
- }
- // UpdateAdminUser
- // @summary 修改个人信息
- // @Description 更新用户内容
- // @Tags User 用户管理
- // @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/user/{userID} [put]
- func (svc *ApiAdminUser) UpdateAdminUser(c *gin.Context) {
- var req UpdateRequest
- if err := c.ShouldBindJSON(&req); err != nil {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error()))
- return
- }
- if common.IsValidPhoneNumber(req.PhoneNumber) {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, "手机号格式有误"))
- return
- }
- if !common.IsValidEmail(req.Email) {
- c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, "邮箱格式不对"))
- return
- }
- if len(req.Roles) > 0 {
- if isBool, err := common.ValidateNumericInt64(req.Roles); !isBool {
- 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, response.ErrorResponse(http.StatusBadRequest, common.ErrorMessage[common.InvalidUserID]))
- return
- }
- ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "AdminUserService.UpdateAdminUser")
- var teams []int64
- userRequest := &pb.UpdateAdminUserRequest{
- Id: userID,
- PhoneNumber: req.PhoneNumber,
- Email: req.Email,
- Avatar: req.Avatar,
- Roles: req.Roles,
- Teams: teams,
- }
- _, err = svc.Service.UpdateAdminUser(ctx, userRequest)
- if err != nil {
- code, mgs := response.MicroErrorRequest(err)
- c.JSON(code, response.ErrorResponse(code, mgs))
- return
- }
- c.JSON(http.StatusOK, response.SuccessResponse(nil))
- }
|