@Summary 手机号验证码登录 @Description 使用手机号和验证码登录 @Tags User @Accept json @Produce json @Param request body SmsLoginRequest true "登录信息" @Success 200 {object} model.CommonResponse{data=SmsLoginResponse} "Success" @Router /api/sms_login [post]
(c *gin.Context)
| 159 | // @Success 200 {object} model.CommonResponse{data=SmsLoginResponse} "Success" |
| 160 | // @Router /api/sms_login [post] |
| 161 | func SmsLogin(c *gin.Context) { |
| 162 | var req SmsLoginRequest |
| 163 | if err := c.ShouldBindJSON(&req); err != nil { |
| 164 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err)) |
| 165 | return |
| 166 | } |
| 167 | |
| 168 | if !helper.IsValidPhone(req.Mobile) { |
| 169 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse(model.InvalidMobileOrEmail)) |
| 170 | return |
| 171 | } |
| 172 | |
| 173 | if req.VerifyCode == "" { |
| 174 | c.JSON(http.StatusUnauthorized, model.UnauthorizedError.ToNewErrorResponse(model.InvalidVerificationCode)) |
| 175 | return |
| 176 | } |
| 177 | |
| 178 | redisKey := fmt.Sprintf("Api:CheckVerificationCode:%s", req.Mobile) |
| 179 | code, err := common.RedisGet(redisKey) |
| 180 | if err != nil || code != req.VerifyCode { |
| 181 | c.JSON(http.StatusUnauthorized, model.UnauthorizedError.ToNewErrorResponse(model.InvalidVerificationCode)) |
| 182 | return |
| 183 | } |
| 184 | |
| 185 | eid := config.GetEID(c) |
| 186 | existingUser, err := model.GetUserByMobile(eid, req.Mobile) |
| 187 | if err != nil { |
| 188 | c.JSON(http.StatusUnauthorized, model.UnauthorizedError.ToResponse(err)) |
| 189 | return |
| 190 | } |
| 191 | |
| 192 | if err := existingUser.RefreshAccessToken(); err != nil { |
| 193 | c.JSON(http.StatusInternalServerError, model.SystemError.ToResponse(err)) |
| 194 | return |
| 195 | } |
| 196 | service.InvalidateInternalUserListCache(eid) |
| 197 | |
| 198 | c.JSON(http.StatusOK, model.Success.ToResponse(&SmsLoginResponse{ |
| 199 | LoginResponse: LoginResponse{ |
| 200 | AccessToken: existingUser.AccessToken, |
| 201 | UserID: existingUser.UserID, |
| 202 | }, |
| 203 | Username: existingUser.Username, |
| 204 | Nickname: existingUser.Nickname, |
| 205 | })) |
| 206 | } |
| 207 | |
| 208 | // Register User Register |
| 209 | // @Summary User Register |
nothing calls this directly
no test coverage detected