From 5b711805ab9253d007feabb257746be9ea5217d0 Mon Sep 17 00:00:00 2001 From: cloud <1148717251@qq.com> Date: Fri, 26 Feb 2021 15:40:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A9=AC=E6=B5=A9=E7=A5=BA=20=20=E5=91=A8?= =?UTF-8?q?=E4=BA=94=E4=B8=8B=E5=8D=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaomi/controller/LoginController.java | 61 +++++++++++++++++-- .../com/mall/xiaomi/service/LoginService.java | 4 ++ .../xiaomi/service/impl/LoginServiceImpl.java | 48 +++++++++++++++ 3 files changed, 109 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/mall/xiaomi/controller/LoginController.java b/src/main/java/com/mall/xiaomi/controller/LoginController.java index 2e73f80..0574958 100644 --- a/src/main/java/com/mall/xiaomi/controller/LoginController.java +++ b/src/main/java/com/mall/xiaomi/controller/LoginController.java @@ -7,14 +7,12 @@ import com.mall.xiaomi.util.CookieUtil; import com.mall.xiaomi.util.MD5Util; import com.mall.xiaomi.util.ResultMessage; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.Map; import java.util.concurrent.TimeUnit; /** @@ -22,6 +20,7 @@ import java.util.concurrent.TimeUnit; * @ClassName LoginController * @Description TODO * @Date 2021/2/25 14:26 + * * @Version 1.0 **/ @RestController @@ -35,6 +34,14 @@ public class LoginController { @Resource private RedisTemplate redisTemplate; + + /* + * @Author mahaoqi + * @Description 登录 + * @Date 2021/2/26 10:14 + * @Param [user, request, response] + * @return com.mall.xiaomi.util.ResultMessage + **/ @RequestMapping(value = "/login",method = RequestMethod.POST) public ResultMessage login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response){ // 返回对象 判断对象是否正确 @@ -56,4 +63,50 @@ public class LoginController { resultMessage.success("001","登陆成功!",user); return resultMessage; } + + /* + * @Author mahaoqi + * @Description 判断用户名是否唯一 + * @Date 2021/2/26 10:25 + * @Param [username] + * @return com.mall.xiaomi.util.ResultMessage + **/ + @RequestMapping(value = "/username/{username}",method = RequestMethod.GET) + public ResultMessage username(@PathVariable String username){ + loginService.isUsername(username); + resultMessage.success("001","可注册!"); + return resultMessage; + } + + /* + * @Author mahaoqi + * @Description 注册 + * @Date 2021/2/26 10:18 + * @Param [user] + * @return com.mall.xiaomi.util.ResultMessage + **/ + @RequestMapping(value = "/register",method = RequestMethod.POST) + public ResultMessage register(@RequestBody User user){ + loginService.register(user); + resultMessage.success("001","注册成功!"); + return resultMessage; + } + + + @RequestMapping(value = "/token",method = RequestMethod.GET) + public ResultMessage token(@CookieValue("XM_TOKEN")String token,HttpServletRequest request,HttpServletResponse response) throws Exception { + Map map = redisTemplate.opsForHash().entries(token); + // 可能map为空 , 即redis中时间已过期,但是cookie还存在。 + // 这个时候应该删除cookie,让用户重新登录 + if(map.isEmpty()){ + CookieUtil.delCookie(request,token); + resultMessage.fail("002", "账号过期,请重新登录"); + return resultMessage; + } + redisTemplate.expire(token,30*60,TimeUnit.SECONDS); + User user = BeanUtil.map2bean(map,User.class); + user.setPassword(null); + resultMessage.success("001", user); + return resultMessage; + } } diff --git a/src/main/java/com/mall/xiaomi/service/LoginService.java b/src/main/java/com/mall/xiaomi/service/LoginService.java index dadc753..8f94067 100644 --- a/src/main/java/com/mall/xiaomi/service/LoginService.java +++ b/src/main/java/com/mall/xiaomi/service/LoginService.java @@ -13,4 +13,8 @@ public interface LoginService { public User login(User user); + + public void register(User user); + + public void isUsername(String username); } diff --git a/src/main/java/com/mall/xiaomi/service/impl/LoginServiceImpl.java b/src/main/java/com/mall/xiaomi/service/impl/LoginServiceImpl.java index 559bcbb..6872268 100644 --- a/src/main/java/com/mall/xiaomi/service/impl/LoginServiceImpl.java +++ b/src/main/java/com/mall/xiaomi/service/impl/LoginServiceImpl.java @@ -39,4 +39,52 @@ public class LoginServiceImpl implements LoginService { } throw new XmException(ExceptionEnum.GET_USER_NOT_FOUND); } + + + + + /* + * @Author mahaoqi + * @Description 注册 + * @Date 2021/2/26 10:23 + * @Param [user] + * @return void + **/ + @Override + public void register(User user) { + User resultUser = new User(); + resultUser.setUsername(user.getUsername()); + //判断用户名是否存在 + if(loginMapper.selectCount(resultUser)==1){ + // 用户名已存在 + throw new XmException(ExceptionEnum.SAVE_USER_REUSE); + } + user.setPassword(MD5Util.MD5Encode(user.getPassword()+"","UTF-8")); + + try { + loginMapper.insert(user); + } catch (Exception e) { + e.printStackTrace(); + throw new XmException(ExceptionEnum.SAVE_USER_ERROR); + } + } + + + /* + * @Author mahaoqi + * @Description 判断用户名是否存在 + * @Date 2021/2/26 10:26 + * @Param [username] + * @return void + **/ + @Override + public void isUsername(String username) { + User resultUser = new User(); + resultUser.setUsername(username); + //判断用户名是否存在 + if(loginMapper.selectCount(resultUser)==1){ + // 用户名已存在 + throw new XmException(ExceptionEnum.SAVE_USER_REUSE); + } + } } -- Gitee