Răsfoiți Sursa

1.添加邮箱api,发送模板。
2.修改登录获取用户信息时候,返回字段添加
3.添加修改密码功能

slambb 4 ani în urmă
părinte
comite
f5bee6eec6

+ 243 - 21
src/main/java/com/YuyeTech/TPlat/controller/LoginController.java

@@ -22,6 +22,8 @@ import com.YuyeTech.TPlat.enums.WxInfoEnum;
 import com.YuyeTech.TPlat.exception.UserException;
 import com.YuyeTech.TPlat.exception.WxInfoException;
 
+import freemarker.template.Template;
+import freemarker.template.TemplateException;
 import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.error.WxErrorException;
 import org.apache.commons.lang3.StringUtils;
@@ -32,13 +34,19 @@ import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.mail.MailException;
 import org.springframework.mail.SimpleMailMessage;
 import org.springframework.mail.javamail.JavaMailSender;
+import org.springframework.mail.javamail.MimeMessageHelper;
+import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
 
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
+import java.io.IOException;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
 
@@ -83,6 +91,10 @@ public class LoginController {
 
     @Autowired
     private JavaMailSender mailSender;
+    @Autowired
+    private FreeMarkerConfigurer freeMarkerConfigurer;
+
+
 
 
     @GetMapping("/getSessionToken")
@@ -345,47 +357,65 @@ public class LoginController {
      * 2021-09-18
      * 在手机号或者邮件中获取验证码。通用
      *
-     * @param count
+     * @param account
      * @param type 0 默认手机,1默认邮件
      * @return
      */
     @GetMapping("/getCodeAccordingType")
-    public ResultVO getCodeAccordingType(@RequestParam(value = "count") String count,
+    public ResultVO getCodeAccordingType(@RequestParam(value = "account") String account,
                                          @RequestParam(value = "type") Integer type) {
         Integer expire = RedisConstant.CODE_EXPIRE;//过期时间
         String code = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
-        if (count != null) {
+        if (account != null) {
 
             if(type.equals(0)){
                 //发送验证码给 手机
-                aliyunSMSUtil.sendSms(count, code);
+                aliyunSMSUtil.sendSms(account, code);
             }else if(type.equals(1)){
                 //发送验证码给 邮件
                 //创建简单邮件消息
-                SimpleMailMessage message = new SimpleMailMessage();
-                //谁发的
-                message.setFrom(mailUsername);
-                //谁要接收
-                message.setTo(count);
-                //邮件标题
-                message.setSubject("哔蹦平台登录验证码");
+//                SimpleMailMessage message = new SimpleMailMessage();
+
                 //邮件内容
-                message.setText("哔蹦平台登录的验证码:"+code+",请勿泄露给其他人!");
+//                message.setText("哔蹦平台登录的验证码:"+code+",请勿泄露给其他人!");
+                //freemarker
                 try {
+                    MimeMessage message = mailSender.createMimeMessage();
+                    MimeMessageHelper helper = new MimeMessageHelper(message);
+                    //谁发的
+                    helper.setFrom(mailUsername);
+                    //谁要接收
+                    helper.setTo(account);
+                    //邮件标题
+                    helper.setSubject("哔蹦平台验证码");
+                    //freemarker
+                    Template template = freeMarkerConfigurer.getConfiguration().getTemplate("/mail/sendSMS.ftl");
+                    Map<String, String> emailMap = new HashMap<>();
+                    emailMap.put("emailCode",code);
+                    String s = FreeMarkerTemplateUtils.processTemplateIntoString(template, emailMap);
+                    helper.setText(s,true);
                     mailSender.send(message);
-                    Map map = new HashMap();
-                    map.put("receiver",count);
-                    map.put("msg","发送普通邮件成功");
-                    return ResultVOUtil.success(map);
+
                 } catch (MailException e) {
-                    e.printStackTrace();
+                    return ResultVOUtil.error(200, "发送普通邮件方失败!");
+                }catch (MessagingException e) {
+                    return ResultVOUtil.error(200, "发送普通邮件方失败!");
+                } catch (IOException e) {
+                    return ResultVOUtil.error(200, "发送普通邮件方失败!");
+                } catch (TemplateException e) {
                     return ResultVOUtil.error(200, "发送普通邮件方失败!");
                 }
+
+
             }
             //成功后保存code到redis
-            redisTemplate.opsForValue().set(String.format(RedisConstant.SMS_PREFIX, count), code, expire, TimeUnit.SECONDS);
+            redisTemplate.opsForValue().set(String.format(RedisConstant.SMS_PREFIX, account), code, expire, TimeUnit.SECONDS);
             log.info("getCode:" + code);
-            return ResultVOUtil.success();
+            Map map = new HashMap();
+            map.put("receiver",account);
+            map.put("tip","发送验证码成功");
+            map.put("type",type);
+            return ResultVOUtil.success(map);
         } else {
             return ResultVOUtil.error(200, "获取code失败");
         }
@@ -393,12 +423,13 @@ public class LoginController {
 
     /**
      * 验证码绑定号码
-     *
+     * 2.4.12 版本后弃用
      * @param phoneNumber
      * @param code
      * @return
      */
     @GetMapping("/SMS_bind_phone")
+    @Deprecated
     public ResultVO userBindPhoneNumber(@RequestParam("phoneNumber") String phoneNumber,
                                         @RequestParam("code") String code,
                                         @RequestParam("userId") String userId) {
@@ -440,6 +471,67 @@ public class LoginController {
 
     }
 
+    /**
+     * 验证码绑定号码或者邮箱
+     *
+     * @param account
+     * @param code
+     * @return
+     */
+    @GetMapping("/SMS_bind_account")
+    public ResultVO userBindAccount(@RequestParam("account") String account,
+                                        @RequestParam("code") String code,
+                                        @RequestParam("type") Integer type,
+                                        @RequestParam("userId") String userId) {
+
+        String codeValue = redisTemplate.opsForValue().get(String.format(RedisConstant.SMS_PREFIX, account));
+        if (!StringUtils.equals(codeValue, code)) {
+            return ResultVOUtil.error(UserEnum.USER_CODE_ERROR.getCode(), UserEnum.USER_CODE_ERROR.getMessage());
+        }
+        log.info("codeValue:" + codeValue);
+
+        try {
+            MainInfo mainInfo = null;
+            if(type.equals(0)){
+                mainInfo = mainInfoService.findMainInfoByPhoneNumber(account);
+                if (mainInfo != null) {
+                    //如果存在,此手机号提示已注册
+                    return ResultVOUtil.error(UserEnum.USER_HAS_REGISTER_PHONE.getCode(), UserEnum.USER_HAS_REGISTER_PHONE.getMessage());
+                }
+            }else if(type.equals(1)){
+                mainInfo = mainInfoService.findMainInfoByEmail(account);
+                if (mainInfo != null) {
+                    //如果存在,此手机号提示已注册
+                    return ResultVOUtil.error(UserEnum.USER_HAS_REGISTER_MAILBOX.getCode(), UserEnum.USER_HAS_REGISTER_MAILBOX.getMessage());
+                }
+            }
+
+            mainInfo = mainInfoService.findMainInfoById(userId);
+            //TODO 验证码登录流程
+            //如果不存在用户
+            if (mainInfo == null) {
+                return ResultVOUtil.error(UserEnum.USER_REGISTER_ERROR.getCode(), UserEnum.USER_REGISTER_ERROR.getMessage());
+            }
+            if(type.equals(0)){
+                mainInfo.setTelephoneNumber(account);
+            }else if(type.equals(1)){
+                mainInfo.setEmail(account);
+            }
+            mainInfoService.addMainInfo(mainInfo);
+            //TODO 写入成功后,删除redis里面保存的code
+            Boolean bSuccess = redisTemplate.delete(String.format(RedisConstant.SMS_PREFIX, account));
+            if (!bSuccess) {
+                //删除不成功,redis不存在
+                log.info("redis 没有对应的token");
+            }
+            return ResultVOUtil.success();
+
+        } catch (UserException e) {
+            return ResultVOUtil.error(UserEnum.USER_REGISTER_ERROR.getCode(), UserEnum.USER_REGISTER_ERROR.getMessage());
+        }
+
+    }
+
 
     @GetMapping("/delete_phone")
     public ResultVO userDeletePhoneNumber(@RequestParam("userId") String userId) {
@@ -550,12 +642,13 @@ public class LoginController {
 
     /**
      * 密码登录
-     *
+     * 2.4.10 版本后弃用
      * @param phoneNumber
      * @param password
      * @return
      */
     @GetMapping("/password_login")
+    @Deprecated
     public ResultVO userPasswordLogin(String phoneNumber, String password) {
         try {
             MainInfo mainInfo = mainInfoService.findMainInfoByPhoneNumberAndPassword(phoneNumber, password);
@@ -592,6 +685,50 @@ public class LoginController {
         }
 
 
+    }
+
+    /**
+     * 密码登录
+     *
+     * @param account
+     * @param password
+     * @return
+     */
+    @GetMapping("/password_login_type")
+    public ResultVO userPasswordLoginOnTheType(String account, String password,Integer type) {
+        try {
+            MainInfo mainInfo = null;
+            if(type.equals(0)){
+                mainInfo = mainInfoService.findMainInfoByPhoneNumberAndPassword(account, password);
+            }else if(type.equals(1)){
+                mainInfo = mainInfoService.findMainInfoByEmailAndPassword(account, password);
+            }
+
+            if (mainInfo == null) {
+                return ResultVOUtil.error(UserEnum.USER_PASSWORD_ERROR.getCode(), UserEnum.USER_PASSWORD_ERROR.getMessage());
+            }
+            //token
+            String token = UUID.randomUUID().toString();
+            Integer expire = RedisConstant.EXPIRE;//过期时间
+            Map<String, String> tokenMap = new HashMap<>();
+
+            //TODO 登录成功,返回对应的字段
+            tokenMap.put("userId", mainInfo.getUserId());
+            redisTemplate.opsForHash().putAll(String.format(RedisConstant.TOKEN_PREFIX, token), tokenMap);
+            redisTemplate.expire(String.format(RedisConstant.TOKEN_PREFIX, token), expire, TimeUnit.SECONDS);
+
+            //返回一个token
+            LoginVO loginVO = new LoginVO();
+            loginVO.setToken(token);
+            loginVO.setNewUser(bUserInfoByUserId(mainInfo.getUserId()));
+            return ResultVOUtil.success(loginVO);
+        } catch (UserException e) {
+
+            log.error(e.getMessage(), e);
+            return ResultVOUtil.error(UserEnum.USER_REGISTER_ERROR.getCode(), UserEnum.USER_REGISTER_ERROR.getMessage());
+        }
+
+
     }
 
     /**
@@ -776,4 +913,89 @@ public class LoginController {
 
     }
 
+
+    /**
+     * 根据手机号或者邮箱获取验证码
+     * 用验证码修改密码
+     *
+     * @param account
+     * @param code
+     * @return
+     */
+    @GetMapping("/SMS_modify_password")
+    public ResultVO userModifyPassword(@RequestParam("account") String account,
+                                    @RequestParam("code") String code,
+                                    @RequestParam("type") Integer type,
+                                    @RequestParam("password") String password,
+                                    @RequestParam("userId") String userId) {
+
+        String codeValue = redisTemplate.opsForValue().get(String.format(RedisConstant.SMS_PREFIX, account));
+        if (!StringUtils.equals(codeValue, code)) {
+            return ResultVOUtil.error(UserEnum.USER_CODE_ERROR.getCode(), UserEnum.USER_CODE_ERROR.getMessage());
+        }
+        log.info("userModifyPassword codeValue:" + codeValue);
+        try {
+            MainInfo mainInfo = null;
+            if(type.equals(0)){
+                mainInfo = mainInfoService.findMainInfoByPhoneNumber(account);
+            }else if(type.equals(1)){
+                mainInfo = mainInfoService.findMainInfoByEmail(account);
+            }
+            //如果不存在用户
+            if (mainInfo == null) {
+                return ResultVOUtil.error(UserEnum.USER_REGISTER_ERROR.getCode(), UserEnum.USER_REGISTER_ERROR.getMessage());
+            }
+            //设置密码
+            mainInfo.setPassword(password);
+            mainInfoService.addMainInfo(mainInfo);
+            //TODO 写入成功后,删除redis里面保存的code
+            Boolean bSuccess = redisTemplate.delete(String.format(RedisConstant.SMS_PREFIX, account));
+            if (!bSuccess) {
+                //删除不成功,redis不存在
+                log.info("redis 没有对应的token");
+            }
+            return ResultVOUtil.success();
+
+        } catch (UserException e) {
+            return ResultVOUtil.error(UserEnum.USER_REGISTER_ERROR.getCode(), UserEnum.USER_REGISTER_ERROR.getMessage());
+        }
+
+    }
+
+    /**
+     * 根据手机号或者邮箱,加旧密码,修改成新密码
+     *
+     */
+    @GetMapping("/modify_password_from_account")
+    public ResultVO userModifyPasswordFromAccount(@RequestParam("account") String account,
+                                       @RequestParam("type") Integer type,
+                                       @RequestParam("oldPassword") String oldPassword,
+                                       @RequestParam("newPassword") String newPassword,
+                                       @RequestParam("userId") String userId) {
+
+        try {
+            MainInfo mainInfo = null;
+            if(type.equals(0)){
+                mainInfo = mainInfoService.findMainInfoByPhoneNumber(account);
+            }else if(type.equals(1)){
+                mainInfo = mainInfoService.findMainInfoByEmail(account);
+            }
+            //如果不存在用户
+            if (mainInfo == null) {
+                return ResultVOUtil.error(UserEnum.USER_MODIFY_ACCOUNT_ERROR.getCode(), UserEnum.USER_MODIFY_ACCOUNT_ERROR.getMessage());
+            }
+            if(!mainInfo.getPassword().equals(oldPassword))
+            {
+                //如果旧密码不对,提示
+                return ResultVOUtil.error(UserEnum.USER_OLD_PASSWORD_ERROR.getCode(), UserEnum.USER_OLD_PASSWORD_ERROR.getMessage());
+            }
+            //设置密码
+            mainInfo.setPassword(newPassword);
+            mainInfoService.addMainInfo(mainInfo);
+            return ResultVOUtil.success();
+        } catch (UserException e) {
+            return ResultVOUtil.error(UserEnum.USER_MODIFY_ERROR.getCode(), UserEnum.USER_MODIFY_ERROR.getMessage());
+        }
+
+    }
 }

+ 9 - 1
src/main/java/com/YuyeTech/TPlat/controller/UserInfoController.java

@@ -95,10 +95,18 @@ public class UserInfoController {
         BeanUtils.copyProperties(userInfo, userInfoVO);
 
         map.put("userInfo", userInfoVO);
-        //还需要返回openid 和 phoneNumber
+        //还需要返回openid 和 phoneNumber,邮箱号
         //mainInfo 肯定是不为null 的
         MainInfo mainInfo = mainInfoService.findMainInfoById(userId);
         map.put("phoneNumber", mainInfo.getTelephoneNumber());
+        map.put("mailboxNumber",mainInfo.getEmail());
+
+        //如果是用户名和密码一样,默认是新注册并且没有设置过密码
+        if(mainInfo.getUsername().equals(mainInfo.getPassword())){
+            map.put("setPasswordFirstTime",true);
+        }else {
+            map.put("setPasswordFirstTime",false);
+        }
 
         WxInfo wxInfo = wxInfoService.findWxInfoById(userId);
         if (wxInfo == null) {

+ 2 - 0
src/main/java/com/YuyeTech/TPlat/dataobject/MainInfo.java

@@ -21,4 +21,6 @@ public class MainInfo {
     private String password;
 
     private String telephoneNumber;
+
+    private String email;
 }

+ 5 - 0
src/main/java/com/YuyeTech/TPlat/enums/UserEnum.java

@@ -23,6 +23,11 @@ public enum UserEnum implements CodeEnum {
     USER_HAS_REGISTER_PHONE(213,"手机号已被使用"),
     USER_NOT_FAVORITES(214,"没有收藏的数据"),
     USER_NOT_RECENTLY(215,"没有最近玩的游戏"),
+    USER_HAS_REGISTER_MAILBOX(216,"邮箱已被使用"),
+    //用户修改密码
+    USER_MODIFY_ERROR(217,"修改密码发生错误"),
+    USER_MODIFY_ACCOUNT_ERROR(218, "修改密码账号错误"),
+    USER_OLD_PASSWORD_ERROR(219,"旧密码错误"),
     ;
     private Integer code;
 

+ 4 - 1
src/main/java/com/YuyeTech/TPlat/repository/MainInfoRepository.java

@@ -2,6 +2,7 @@ package com.YuyeTech.TPlat.repository;
 
 import com.YuyeTech.TPlat.dataobject.MainInfo;
 import org.springframework.data.jpa.repository.JpaRepository;
+import sun.applet.Main;
 
 import java.util.Optional;
 
@@ -12,6 +13,8 @@ import java.util.Optional;
 public interface MainInfoRepository extends JpaRepository<MainInfo,String> {
 
    Optional<MainInfo> findByTelephoneNumberAndPassword(String phoneNumber,String password);
-
    Optional<MainInfo> findByTelephoneNumber(String phoneNumber);
+   Optional<MainInfo> findByEmailAndPassword(String email,String password);
+   Optional<MainInfo> findByEmail(String email);
+
 }

+ 2 - 0
src/main/java/com/YuyeTech/TPlat/service/MainInfoService.java

@@ -10,6 +10,8 @@ public interface MainInfoService {
     MainInfo findMainInfoById(String userId);
     MainInfo findMainInfoByPhoneNumber(String phoneNumber);
     MainInfo findMainInfoByPhoneNumberAndPassword(String phoneNumber,String password);
+    MainInfo findMainInfoByEmailAndPassword(String email,String password);
+    MainInfo findMainInfoByEmail(String mail);
     MainInfo addMainInfo(MainInfo mainInfo);
     String deleteMainInfoById(String userId);
 }

+ 12 - 0
src/main/java/com/YuyeTech/TPlat/service/impl/MainInfoServiceImpl.java

@@ -54,6 +54,18 @@ public class MainInfoServiceImpl implements MainInfoService {
         return mainInfo;
     }
 
+    @Override
+    public MainInfo findMainInfoByEmailAndPassword(String email, String password) {
+        MainInfo mainInfo= mainInfoRepository.findByEmailAndPassword(email,password).orElse(null);
+        return mainInfo;
+    }
+
+    @Override
+    public MainInfo findMainInfoByEmail(String mail) {
+        MainInfo mainInfo= mainInfoRepository.findByEmail(mail).orElse(null);
+        return mainInfo;
+    }
+
     @Override
     public MainInfo findMainInfoByPhoneNumber(String telephoneNumber) {
         MainInfo mainInfo = mainInfoRepository.findByTelephoneNumber(telephoneNumber).orElse(null);

+ 3 - 0
src/main/resources/application.yml

@@ -38,10 +38,13 @@ spring:
     properties:
       mail:
         smtp:
+          ssl:
+            enable: true
           auth: true
           starttls:
             enable: true
             required: true
+    port: 465
 
 #url 前缀
 server:

+ 15 - 0
src/main/resources/templates/mail/sendSMS.ftl

@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+</head>
+<body>
+<div>
+    <p style="text-align: left;font-weight: bold">尊敬的用户:</p>
+    <p>&nbsp;&nbsp;&nbsp;&nbsp;您好!哔蹦平台需要您正在进行邮箱验证,本次请求的验证码为:</p>
+    <p style="font-size: 50px;font-weight: 900;text-align: center">${emailCode}</p>
+    <p>&nbsp;&nbsp;&nbsp;&nbsp;本次验证码有效期为5分钟。(请勿回复此邮件)</p>
+</div>
+</body>
+</html>