From 41852cade206e76a592d86fffee4c23e8ee0d294 Mon Sep 17 00:00:00 2001 From: barney <15270405776@163.com> Date: Sat, 15 Oct 2022 23:47:42 +0800 Subject: [PATCH] =?UTF-8?q?session=E7=9F=AD=E4=BF=A1=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/hmdp/config/MvcConfig.java | 18 +++++ .../com/hmdp/controller/UserController.java | 18 +++-- .../java/com/hmdp/service/IUserService.java | 7 ++ .../hmdp/service/impl/UserServiceImpl.java | 70 +++++++++++++++++++ .../java/com/hmdp/utils/LoginInterceptor.java | 34 +++++++++ 5 files changed, 140 insertions(+), 7 deletions(-) create mode 100644 hm-dianping/src/main/java/com/hmdp/config/MvcConfig.java create mode 100644 hm-dianping/src/main/java/com/hmdp/utils/LoginInterceptor.java 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
* 服务实现类
@@ -15,6 +26,65 @@ import org.springframework.stereotype.Service;
* @since 2021-12-22
*/
@Service
+@Slf4j
public class UserServiceImpl extends ServiceImpl