diff --git a/hm-dianping/src/main/java/com/hmdp/config/MvcConfig.java b/hm-dianping/src/main/java/com/hmdp/config/MvcConfig.java new file mode 100644 index 0000000..6d9fb21 --- /dev/null +++ b/hm-dianping/src/main/java/com/hmdp/config/MvcConfig.java @@ -0,0 +1,18 @@ +package com.hmdp.config; + +import com.hmdp.utils.LoginInterceptor; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class MvcConfig implements WebMvcConfigurer { + @Override + public void addInterceptors(InterceptorRegistry registry) { + // 添加登录拦截器 + registry.addInterceptor(new LoginInterceptor()).excludePathPatterns( + "/user/login", + "/user/code" + ); + } +} diff --git a/hm-dianping/src/main/java/com/hmdp/controller/UserController.java b/hm-dianping/src/main/java/com/hmdp/controller/UserController.java index 24545cb..27765ed 100644 --- a/hm-dianping/src/main/java/com/hmdp/controller/UserController.java +++ b/hm-dianping/src/main/java/com/hmdp/controller/UserController.java @@ -3,9 +3,12 @@ package com.hmdp.controller; import com.hmdp.dto.LoginFormDTO; import com.hmdp.dto.Result; +import com.hmdp.dto.UserDTO; +import com.hmdp.entity.User; import com.hmdp.entity.UserInfo; import com.hmdp.service.IUserInfoService; import com.hmdp.service.IUserService; +import com.hmdp.utils.UserHolder; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; @@ -34,10 +37,10 @@ public class UserController { /** * 发送手机验证码 */ - @PostMapping("code") + @PostMapping("/code") public Result sendCode(@RequestParam("phone") String phone, HttpSession session) { - // TODO 发送短信验证码并保存验证码 - return Result.fail("功能未完成"); + // 发送短信验证码并保存验证码 + return userService.sendPhone(phone,session); } /** @@ -46,8 +49,8 @@ public class UserController { */ @PostMapping("/login") public Result login(@RequestBody LoginFormDTO loginForm, HttpSession session){ - // TODO 实现登录功能 - return Result.fail("功能未完成"); + // 实现登录功能 + return userService.login(loginForm,session); } /** @@ -62,8 +65,9 @@ public class UserController { @GetMapping("/me") public Result me(){ - // TODO 获取当前登录的用户并返回 - return Result.fail("功能未完成"); + // 获取当前登录的用户并返回 + UserDTO user = UserHolder.getUser(); + return Result.ok(user); } @GetMapping("/info/{id}") diff --git a/hm-dianping/src/main/java/com/hmdp/service/IUserService.java b/hm-dianping/src/main/java/com/hmdp/service/IUserService.java index 23e740a..afdf66e 100644 --- a/hm-dianping/src/main/java/com/hmdp/service/IUserService.java +++ b/hm-dianping/src/main/java/com/hmdp/service/IUserService.java @@ -1,8 +1,12 @@ package com.hmdp.service; import com.baomidou.mybatisplus.extension.service.IService; +import com.hmdp.dto.LoginFormDTO; +import com.hmdp.dto.Result; import com.hmdp.entity.User; +import javax.servlet.http.HttpSession; + /** *

* 服务类 @@ -13,4 +17,7 @@ import com.hmdp.entity.User; */ public interface IUserService extends IService { + Result sendPhone(String phone, HttpSession session); + + Result login(LoginFormDTO loginForm, HttpSession session); } diff --git a/hm-dianping/src/main/java/com/hmdp/service/impl/UserServiceImpl.java b/hm-dianping/src/main/java/com/hmdp/service/impl/UserServiceImpl.java index 938954f..6ddd95a 100644 --- a/hm-dianping/src/main/java/com/hmdp/service/impl/UserServiceImpl.java +++ b/hm-dianping/src/main/java/com/hmdp/service/impl/UserServiceImpl.java @@ -1,11 +1,22 @@ package com.hmdp.service.impl; +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.util.RandomUtil; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.hmdp.dto.LoginFormDTO; +import com.hmdp.dto.Result; +import com.hmdp.dto.UserDTO; import com.hmdp.entity.User; import com.hmdp.mapper.UserMapper; import com.hmdp.service.IUserService; +import com.hmdp.utils.RegexUtils; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import javax.servlet.http.HttpSession; + +import static com.hmdp.utils.SystemConstants.USER_NICK_NAME_PREFIX; + /** *

* 服务实现类 @@ -15,6 +26,65 @@ import org.springframework.stereotype.Service; * @since 2021-12-22 */ @Service +@Slf4j public class UserServiceImpl extends ServiceImpl implements IUserService { + @Override + public Result sendPhone(String phone, HttpSession session) { + System.out.println("phone: " + phone); + // 1. 检验验证码 + if (RegexUtils.isPhoneInvalid(phone)) { + // 2.不符合,返回错误信息 + return Result.fail("手机号格式错误"); + } + // 3.符合,生成验证码 + String code = RandomUtil.randomNumbers(6); + + // 4.保存验证码到session中 + session.setAttribute("code",code); + + // 5.发送验证码 + log.debug("发送验证码成功,验证码: {}",code); + // 返回ok + return Result.ok(); + } + + @Override + public Result login(LoginFormDTO loginForm, HttpSession session) { + String phone = loginForm.getPhone(); + // 1.检验手机号 + if (RegexUtils.isPhoneInvalid(phone)) { + // 手机号不合法,直接返回错误 + Result.fail("手机号格式错误"); + } + // 2.校验验证码 + // 从session中取出验证码 + Object cacheCode = session.getAttribute("code"); + String code = loginForm.getCode(); + if (code == null || !cacheCode.toString().equals(code)) { + // 3.不一致或者验证码为空 + return Result.fail("验证码错误"); + } + // 4.一致,查询手机号对应的用户是否存在 + User user = query().eq("phone",phone).one(); + + // 5.用户不存在,则创建新用户 + if (user == null) { + // 6.不存在则根据手机号新建用户 + user = createUserWithPhone(phone); + } + // 7.将用户信息存入session + session.setAttribute("user", BeanUtil.copyProperties(user, UserDTO.class)); + return Result.ok(); + } + + private User createUserWithPhone(String phone) { + // 创建用户 + User user = new User(); + user.setPhone(phone); + user.setNickName(USER_NICK_NAME_PREFIX + RandomUtil.randomNumbers(10)); + // 保存用户 + save(user); + return user; + } } diff --git a/hm-dianping/src/main/java/com/hmdp/utils/LoginInterceptor.java b/hm-dianping/src/main/java/com/hmdp/utils/LoginInterceptor.java new file mode 100644 index 0000000..7b13814 --- /dev/null +++ b/hm-dianping/src/main/java/com/hmdp/utils/LoginInterceptor.java @@ -0,0 +1,34 @@ +package com.hmdp.utils; + +import com.hmdp.dto.UserDTO; +import com.hmdp.entity.User; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +public class LoginInterceptor implements HandlerInterceptor { + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + // 1.获取session + HttpSession session = request.getSession(); + // 2.获取session中的用户 + Object user = session.getAttribute("user"); + // 3.判断用户是否存在 + if (user == null) { + // 4.不存在,将请求拦截 + response.setStatus(401); + return false; + } + // 5.存在则保存用户信息到ThreadLocal + UserHolder.saveUser((UserDTO) user); + // 6.放行 + return true; + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + UserHolder.removeUser(); + } +}