From 75b6189e64342b30cceb4abda509c7417948922a Mon Sep 17 00:00:00 2001 From: PeterAlbus Date: Sun, 27 Mar 2022 22:16:58 +0800 Subject: [PATCH] add user register and login --- .gitignore | 1 - pom.xml | 10 ++++++ .../peteralbus/controller/UserController.java | 32 +++++++++++++++++-- .../com/peteralbus/service/UserService.java | 7 ++++ .../service/impl/UserServiceImpl.java | 8 +++++ 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 449bd92..66eb95a 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,3 @@ build/ application.yml 5963105_www.peteralbus.com.pfx SmsUtil.java -SmsUtil diff --git a/pom.xml b/pom.xml index 5ea668c..5182441 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,16 @@ com.aliyun aliyun-java-sdk-core 4.6.0 + + + commons-codec + commons-codec + + + activation + javax.activation + + com.aliyun diff --git a/src/main/java/com/peteralbus/controller/UserController.java b/src/main/java/com/peteralbus/controller/UserController.java index 2e3444c..6aa4846 100644 --- a/src/main/java/com/peteralbus/controller/UserController.java +++ b/src/main/java/com/peteralbus/controller/UserController.java @@ -51,7 +51,7 @@ public class UserController } @RequestMapping("/mailLogin") - public SaTokenInfo doLogin(String userMail, String userPassword) + public SaTokenInfo mailLogin(String userMail, String userPassword) { User user=userService.authByMail(userMail, userPassword); if(user!=null) @@ -62,6 +62,18 @@ public class UserController return null; } + @RequestMapping("/phoneLogin") + public SaTokenInfo phoneLogin(String userPhone, String userPassword) + { + User user=userService.authByPhone(userPhone, userPassword); + if(user!=null) + { + StpUtil.login(user.getUserId()); + return StpUtil.getTokenInfo(); + } + return null; + } + @RequestMapping("/isLogin") public User isLogin() { @@ -86,6 +98,14 @@ public class UserController @RequestMapping("/applyMailVerifyCode") public String applyMailVerifyCode(String account) { + if(redisUtils.exists("verifyCode:"+account)) + { + return "haveVerifyCode"; + } + if(userService.haveAccount(account)) + { + return "haveAccount"; + } String verifyCode=RandomUtil.generateVerifyCode(6); try { MimeMessage message=javaMailSender.createMimeMessage(); @@ -106,6 +126,14 @@ public class UserController @RequestMapping("/applyPhoneVerifyCode") public String applyPhoneVerifyCode(String account) { + if(redisUtils.exists("verifyCode:"+account)) + { + return "haveVerifyCode"; + } + if(userService.haveAccount(account)) + { + return "haveAccount"; + } final String success ="smsSendSuccess"; String result=""; String verifyCode=RandomUtil.generateVerifyCode(4); @@ -115,7 +143,7 @@ public class UserController if(Objects.equals(result, success)) { redisUtils.set("verifyCode:"+account,verifyCode,10L, TimeUnit.MINUTES); - return "sendMailSuccess"; + return "sendSmsSuccess"; } } catch (ClientException e) diff --git a/src/main/java/com/peteralbus/service/UserService.java b/src/main/java/com/peteralbus/service/UserService.java index 2eb49f4..df25955 100644 --- a/src/main/java/com/peteralbus/service/UserService.java +++ b/src/main/java/com/peteralbus/service/UserService.java @@ -61,4 +61,11 @@ public interface UserService */ int changePassword(Long userId,String oldPassword,String newPassword); + /** + * Have account boolean. + * + * @param account the account + * @return the boolean + */ + boolean haveAccount(String account); } diff --git a/src/main/java/com/peteralbus/service/impl/UserServiceImpl.java b/src/main/java/com/peteralbus/service/impl/UserServiceImpl.java index f665cab..7ddef17 100644 --- a/src/main/java/com/peteralbus/service/impl/UserServiceImpl.java +++ b/src/main/java/com/peteralbus/service/impl/UserServiceImpl.java @@ -105,4 +105,12 @@ public class UserServiceImpl implements UserService } return -1; } + + @Override + public boolean haveAccount(String account) + { + QueryWrapper userQueryWrapper=new QueryWrapper<>(); + userQueryWrapper.eq("user_mail",account).or().eq("user_phone",account); + return userMapper.selectCount(userQueryWrapper) > 0; + } }