package admin_common import ( "github.com/gin-gonic/gin" "net/http" "sghgogs.com/micro/auth-client/model/base" res "sghgogs.com/micro/auth-client/model/response/common" "sghgogs.com/micro/auth-client/response" "sghgogs.com/micro/auth-client/utils" pb "sghgogs.com/micro/auth-service/proto" "sghgogs.com/micro/common" ) type Request struct { Username string `json:"username" binding:"required,max=15,min=5"` // 用户名 Password string `json:"password" binding:"required,max=18,min=6"` // 密码 } // Login // @summary 登录 // @Description 登录API // @Tags Common // @Accept json // @Produce json // @Param body body Request true "请求body" // @Success 200 {object} response.ApiResponse{data=res.UserInfo} "成功" // @Failure 400 {object} response.ApiResponse "请求错误" // @Failure 500 {object} response.ApiResponse "内部错误" // @Router /v1/api/admin/login [post] func (svc *ApiAdminCommon) Login(c *gin.Context) { var req Request if err := c.BindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, err.Error())) return } if !common.ValidateUsername(req.Username) { c.JSON(http.StatusBadRequest, response.ErrorResponse(http.StatusBadRequest, "4到16位(字母,数字,下划线,减号)")) return } ctx, _ := utils.CreateContextWithToken(c, "authorizationservice", "CommonService.AdminLogin") info, err := svc.Service.AdminLogin(ctx, &pb.AdminLoginRequest{ Username: req.Username, Password: req.Password, }) if err != nil { code, mgs := response.MicroErrorRequest(err) c.JSON(code, response.ErrorResponse(code, mgs)) return } user := info.User c.JSON(http.StatusOK, response.SuccessResponse(gin.H{ "token": info.Token, "user": res.UserInfo{ Id: user.Id, Username: user.Username, PhoneNumber: user.PhoneNumber, Email: user.Email, Avatar: user.Avatar, Roles: base.RolesToResponse(user.Roles), Teams: base.TeamsToResponse(user.Teams), }, })) }