diff --git a/src/main/java/com/mall/xiaomi/controller/LoginController.java b/src/main/java/com/mall/xiaomi/controller/LoginController.java index 2e73f80237a7879c294758b383b4438f5fd481a4..057495812bfc711727836245715b7a6574b7bb2c 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 dadc753daee76f04b9c4d59ceee91b6cf6f013cf..8f94067c67d70c593880d6eda6f459b7e2ef4b31 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 559bcbb016c4b036538c192f56e71ce720b35ac8..68722685f15cc9f2245d1f4ec48166fa4a4e26e6 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); + } + } }