From e2c185ce66ffae36b0cae965b16eba427a9e82d6 Mon Sep 17 00:00:00 2001 From: ShiSiWei <12764015+ssw_1_0@user.noreply.gitee.com> Date: Thu, 26 Oct 2023 22:45:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E5=86=8C=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 33 ++-- .../four/auth/controller/TokenController.java | 33 +++- .../four/auth/form/HealthRegisterBody.java | 151 ++++++++++++++++++ .../java/com/four/auth/form/LoginBody.java | 28 ++++ .../four/auth/service/SysLoginService.java | 56 +++++++ .../com/four/auth/service/SysMailService.java | 82 ++++++++++ src/main/resources/bootstrap.yml | 11 ++ 7 files changed, 379 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/four/auth/form/HealthRegisterBody.java create mode 100644 src/main/java/com/four/auth/service/SysMailService.java diff --git a/pom.xml b/pom.xml index 3204ad6..0e5a4ea 100644 --- a/pom.xml +++ b/pom.xml @@ -11,51 +11,62 @@ 3.6.3 four-auth - + four-auth认证授权 - + - + com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery - + com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config - + com.alibaba.cloud spring-cloud-starter-alibaba-sentinel - + org.springframework.boot spring-boot-starter-web - + org.springframework.boot spring-boot-starter-actuator - + com.four four-common-security - + + + com.four + four-common-duck + + + + + org.springframework.boot + spring-boot-starter-mail + + - + ${project.artifactId} @@ -72,5 +83,5 @@ - + diff --git a/src/main/java/com/four/auth/controller/TokenController.java b/src/main/java/com/four/auth/controller/TokenController.java index d35e22c..b0190ec 100644 --- a/src/main/java/com/four/auth/controller/TokenController.java +++ b/src/main/java/com/four/auth/controller/TokenController.java @@ -1,11 +1,12 @@ package com.four.auth.controller; import javax.servlet.http.HttpServletRequest; + +import com.four.auth.form.HealthRegisterBody; +import com.four.auth.service.SysMailService; +import com.four.common.duck.interrogation.RegistrationInformation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import com.four.auth.form.LoginBody; import com.four.auth.form.RegisterBody; import com.four.auth.service.SysLoginService; @@ -32,6 +33,9 @@ public class TokenController @Autowired private SysLoginService sysLoginService; + @Autowired + private SysMailService sysMailService; + @PostMapping("login") public R login(@RequestBody LoginBody form) { @@ -77,6 +81,8 @@ public class TokenController return R.ok(); } + + @PostMapping("register") public R register(@RequestBody RegisterBody registerBody) { @@ -84,4 +90,23 @@ public class TokenController sysLoginService.register(registerBody.getUsername(), registerBody.getPassword()); return R.ok(); } + + + @PostMapping("healthRegisterBody") + public R healthRegisterBody(@RequestBody HealthRegisterBody healthRegisterBody){ + + sysMailService.verifyCode(healthRegisterBody.getEmail(),healthRegisterBody.getCode()); + + sysLoginService.healthRegister(healthRegisterBody); + return R.ok(); + } + + + @GetMapping("/sendCode/{email}") + public R sendCode(@PathVariable String email){ + sysMailService.sendCode(email); + return R.ok(); + } + + } diff --git a/src/main/java/com/four/auth/form/HealthRegisterBody.java b/src/main/java/com/four/auth/form/HealthRegisterBody.java new file mode 100644 index 0000000..021cf72 --- /dev/null +++ b/src/main/java/com/four/auth/form/HealthRegisterBody.java @@ -0,0 +1,151 @@ +package com.four.auth.form; + +public class HealthRegisterBody extends LoginBody{ + + + /** + * 邀请码 + * */ + private String invitationCode; + + /** + * 真实姓名 + * */ + private String realName; + + /** + * 个人简历 + * */ + private String personalResume; + + /** + * 形象地址 + * */ + private String avatar; + + /** + * 所属医院 + */ + private String affiliatedHospital; + + /** + * 擅长领域 + * */ + private String areaExpertise; + /** + * 昵称 + * */ + private String nickName; + + /** + * 职称id + * */ + private Long professionalTitleDoctorId; + + + /** + * 性别 + * */ + private String sex; + + public HealthRegisterBody(String invitationCode, String realName, String personalResume, String avatar, String affiliatedHospital, String nickName, Long professionalTitleDoctorId, String sex) { + this.invitationCode = invitationCode; + this.realName = realName; + this.personalResume = personalResume; + this.avatar = avatar; + this.affiliatedHospital = affiliatedHospital; + this.nickName = nickName; + this.professionalTitleDoctorId = professionalTitleDoctorId; + this.sex = sex; + } + + @Override + public String toString() { + return "HealthRegisterBody{" + + "invitationCode='" + invitationCode + '\'' + + ", realName='" + realName + '\'' + + ", personalResume='" + personalResume + '\'' + + ", avatar='" + avatar + '\'' + + ", affiliatedHospital='" + affiliatedHospital + '\'' + + ", nickName='" + nickName + '\'' + + ", professionalTitleDoctorId=" + professionalTitleDoctorId + + ", sex='" + sex + '\'' + + '}'; + } + + public HealthRegisterBody() { + } + + + public Long getProfessionalTitleDoctorId() { + return professionalTitleDoctorId; + } + + public void setProfessionalTitleDoctorId(Long professionalTitleDoctorId) { + this.professionalTitleDoctorId = professionalTitleDoctorId; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public String getAreaExpertise() { + return areaExpertise; + } + + public void setAreaExpertise(String areaExpertise) { + this.areaExpertise = areaExpertise; + } + + public void setInvitationCode(String invitationCode) { + this.invitationCode = invitationCode; + } + + public void setRealName(String realName) { + this.realName = realName; + } + + public void setPersonalResume(String personalResume) { + this.personalResume = personalResume; + } + + public void setAvatar(String avatar) { + this.avatar = avatar; + } + + public void setAffiliatedHospital(String affiliatedHospital) { + this.affiliatedHospital = affiliatedHospital; + } + + public void setNickName(String nickName) { + this.nickName = nickName; + } + + public String getInvitationCode() { + return invitationCode; + } + + public String getRealName() { + return realName; + } + + public String getPersonalResume() { + return personalResume; + } + + public String getAvatar() { + return avatar; + } + + public String getAffiliatedHospital() { + return affiliatedHospital; + } + + public String getNickName() { + return nickName; + } +} diff --git a/src/main/java/com/four/auth/form/LoginBody.java b/src/main/java/com/four/auth/form/LoginBody.java index 1b6e338..032b656 100644 --- a/src/main/java/com/four/auth/form/LoginBody.java +++ b/src/main/java/com/four/auth/form/LoginBody.java @@ -18,12 +18,40 @@ public class LoginBody */ private String password; + + private Long roleId; + private String code; + public String getUsername() { return userName; } + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public Long getRoleId() { + return roleId; + } + + public void setRoleId(Long roleId) { + this.roleId = roleId; + } + public String getEmail() { return email; } diff --git a/src/main/java/com/four/auth/service/SysLoginService.java b/src/main/java/com/four/auth/service/SysLoginService.java index cffb06a..6e7ce32 100644 --- a/src/main/java/com/four/auth/service/SysLoginService.java +++ b/src/main/java/com/four/auth/service/SysLoginService.java @@ -1,5 +1,7 @@ package com.four.auth.service; +import com.four.auth.form.HealthRegisterBody; +import com.four.auth.form.RegisterBody; import org.apache.catalina.Contained; import org.apache.catalina.User; import org.springframework.beans.factory.annotation.Autowired; @@ -19,6 +21,7 @@ import com.four.common.security.utils.SecurityUtils; import com.four.system.api.RemoteUserService; import com.four.system.api.domain.SysUser; import com.four.system.api.model.LoginUser; +import org.springframework.stereotype.Service; import java.util.GregorianCalendar; @@ -198,4 +201,57 @@ public class SysLoginService recordLogService.recordLogininfor(userName,Constants.LOGIN_SUCCESS,"登录成功"); return dataInfo; } + + + public void healthRegister(HealthRegisterBody healthRegisterBody) { + + if(StringUtils.isAllBlank(healthRegisterBody.getUsername(),healthRegisterBody.getPassword())){ + throw new ServiceException("用户/密码必须填写"); + } + if(healthRegisterBody.getPassword().length() < UserConstants.PASSWORD_MIN_LENGTH + || healthRegisterBody.getPassword().length()> UserConstants.PASSWORD_MAX_LENGTH){ + throw new ServiceException("密码长度必须在5到20个字符"); + } + SysUser sysUser = new SysUser(); + sysUser.setRoleId(healthRegisterBody.getRoleId()); + sysUser.setUserName(healthRegisterBody.getUserName()); + sysUser.setEmail(healthRegisterBody.getEmail()); + sysUser.setNickName(healthRegisterBody.getNickName()); + sysUser.setPassword(SecurityUtils.encryptPassword(healthRegisterBody.getPassword())); + + if(healthRegisterBody.getRoleId() == 100){ + + //职称Id + sysUser.setRoleId(healthRegisterBody.getRoleId()); + + //真实姓名 + sysUser.setNickName(healthRegisterBody.getNickName()); + + //个人简历 + sysUser.setPersonalResume(healthRegisterBody.getPersonalResume()); + + //所属医院 + sysUser.setAffiliatedHospital(healthRegisterBody.getAffiliatedHospital()); + + //area_expertise + sysUser.setAreaExpertise(healthRegisterBody.getAreaExpertise()); + } + + if(healthRegisterBody.getRoleId() == 101){ + /** + * 邀请码 + * */ + sysUser.setInvitationCode(healthRegisterBody.getInvitationCode()); + /** + * 性别 + * */ + sysUser.setSex(healthRegisterBody.getSex()); + } + System.out.println(sysUser); + R registerResult = remoteUserService.healthRegister(sysUser, SecurityConstants.INNER); + if(R.FAIL == registerResult.getCode()){ + throw new ServiceException(registerResult.getMsg()); + } + recordLogService.recordLogininfor(healthRegisterBody.getEmail(), Constants.REGISTER,"注册成功"); + } } diff --git a/src/main/java/com/four/auth/service/SysMailService.java b/src/main/java/com/four/auth/service/SysMailService.java new file mode 100644 index 0000000..749309e --- /dev/null +++ b/src/main/java/com/four/auth/service/SysMailService.java @@ -0,0 +1,82 @@ +package com.four.auth.service; + +import com.four.common.core.constant.SecurityConstants; +import com.four.common.core.domain.R; +import com.four.common.core.exception.ServiceException; +import com.four.common.core.utils.StringUtils; +import com.four.common.redis.service.RedisService; +import com.four.system.api.RemoteUserService; +import com.four.system.api.model.LoginUser; + +import org.apache.commons.lang.RandomStringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.stereotype.Component; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import java.util.concurrent.TimeUnit; + +@Component +public class SysMailService { + + @Autowired + private RemoteUserService remoteUserService; + + @Autowired + private RedisService redisService; + + @Autowired + private JavaMailSender javaMailSender; + public void verifyCode(String email, String code) { + if(StringUtils.isAnyBlank(email) || StringUtils.isAllBlank(code)){ + throw new ServiceException("邮箱验证码不能为空"); + } + + //判断缓存中是否存在验证码 + String verifyCode = redisService.getCacheObject(email); + if(StringUtils.isAllBlank(verifyCode)){ + throw new ServiceException("请先获取验证码"); + } + + //验证码校验 + if(!code.equals(verifyCode)){ + throw new ServiceException("验证码错误"); + } + } + + public void sendCode(String email) { + if(StringUtils.isAllBlank(email)){ + throw new ServiceException("邮箱为空"); + } + + String verifyCode = redisService.getCacheObject(email); + if(!StringUtils.isAnyBlank(verifyCode)){ + throw new ServiceException("验证码已发送,请稍后重试"); + } + + + R userInfoByEmail = remoteUserService.getUserInfoByEmail(email, SecurityConstants.INNER); + if(!StringUtils.isNull(userInfoByEmail.getData())){ + throw new ServiceException("该邮箱:" + email + "已被注册"); + } + + + verifyCode = RandomStringUtils.randomNumeric(4); + + MimeMessage mimeMessage = javaMailSender.createMimeMessage(); + try { + MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); + helper.setFrom("1733802689@qq.com"); + helper.setText(verifyCode); + helper.setTo(email); + helper.setSubject("我来发邮件来了"); + javaMailSender.send(mimeMessage); + } catch (MessagingException e) { + throw new ServiceException("邮箱服务出错了:{}" + e.getMessage()); + } + + redisService.setCacheObject(email,verifyCode,5L, TimeUnit.MINUTES); + } +} diff --git a/src/main/resources/bootstrap.yml b/src/main/resources/bootstrap.yml index 2a9d783..e597c5f 100644 --- a/src/main/resources/bootstrap.yml +++ b/src/main/resources/bootstrap.yml @@ -25,3 +25,14 @@ spring: # 共享配置 shared-configs: - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} + mail: + host: smtp.qq.com + port: 587 + username: 1733802689@qq.com + password: lqttdbsoccpvbgjj + defaultencoding: UTF-8 + properties: + mail: + smtp: + socketFactoryClass: javax.net.ssl.SSLSocketFactory +