手机号验证码登录

This commit is contained in:
2026-01-07 15:06:26 +08:00
parent bc54f58ddb
commit 62d85e29ba
7 changed files with 46 additions and 3 deletions

View File

@@ -31,7 +31,8 @@ public class WebConfig implements WebMvcConfigurer {
.excludePathPatterns(
"/user/login",
"/user/sendCode",
"/user/loginByCode"
"/user/loginByCode",
"/user/loginPhone"
);
}
}

View File

@@ -90,4 +90,23 @@ public class UserController {
return ok ? Result.success(ResultCode.SUCCESS, "登录成功") : Result.error(ResultCode.SYSTEM_ERROR, "登录失败");
}
/**
* 登录
* @param body 登录参数
*/
@PostMapping("/loginPhone")
public Result<?> loginPhone(@RequestBody Map<String, String> body) {
String phone = body.get("phone");
String code = body.get("code");
//校验验证码
boolean verify = userService.verifyCode(phone, code);
if (!verify){
return Result.error(ResultCode.SYSTEM_ERROR, "验证码错误或已过期");
}
//登录
UserLoginVO vo = userService.loginByPhone(phone);
return Result.success(ResultCode.SUCCESS, "登录成功", vo);
}
}

View File

@@ -3,6 +3,7 @@ package com.bao.dating.mapper;
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
import com.bao.dating.pojo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* 用户Mapper
@@ -33,4 +34,6 @@ public interface UserMapper {
*/
void updateUserInfoByUserId(UserInfoUpdateDTO userInfoUpdateDTO);
User selectByPhone(@Param("phone") String phone);
}

View File

@@ -49,4 +49,6 @@ public interface UserService {
void sendSmsCode(String phone);
boolean verifyCode(String phone, String code);
UserLoginVO loginByPhone(String phone);
}

View File

@@ -336,4 +336,18 @@ public class UserServiceImpl implements UserService {
stringRedisTemplate.delete(key);
return true;
}
@Override
public UserLoginVO loginByPhone(String phone) {
//根据手机号查询用户
User user = userMapper.selectByPhone(phone);
//创建token
String token = JwtUtil.generateToken(user.getUserId().toString());
//封装返回结果
UserLoginVO VO = new UserLoginVO();
VO.setUserId(user.getUserId());
VO.setToken(token);
return VO;
}
}

View File

@@ -42,7 +42,7 @@
signature,
created_at,
updated_at
FROM user WHERE user_id = #{userId}
FROM dating.user WHERE user_id = #{userId}
</select>
<!--根据ID更新动态-->
@@ -74,4 +74,8 @@
</set>
WHERE user_id = #{userId}
</update>
<select id="selectByPhone" resultType="com.bao.dating.pojo.entity.User">
select * from dating.user where user_phone =#{phone}
</select>
</mapper>