From 67dba3d9ddc6516e06b68ec669a3d41b6df2802d Mon Sep 17 00:00:00 2001 From: mzh820631607 Date: Wed, 14 Oct 2020 17:45:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=A6=E7=94=9F=E7=AE=A1=E7=90=86+=E4=B8=AA?= =?UTF-8?q?=E4=BA=BA=E4=B8=AD=E5=BF=83=E9=82=AE=E7=AE=B1=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E6=89=8B=E6=9C=BA=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 48 ++++++ .../msdw/tms/api/StudentControllerApi.java | 6 + .../com/msdw/tms/api/UserControllerApi.java | 15 ++ .../tms/config/CheckCodeQuartzConfig.java | 33 ++++ .../com/msdw/tms/config/CheckCodeUtils.java | 39 +++++ .../java/com/msdw/tms/config/EmailUtils.java | 52 +++++++ .../java/com/msdw/tms/config/SendSMS.java | 91 +++++++++++ .../tms/config/quartz/CheckCodeQuartz.java | 24 +++ .../tms/controller/StudentController.java | 45 +++++- .../msdw/tms/controller/UserController.java | 145 ++++++++++++++---- .../java/com/msdw/tms/dao/StudentDao.java | 9 ++ src/main/java/com/msdw/tms/dao/UserDao.java | 23 +++ .../com/msdw/tms/entity/StudentEntity.java | 2 + .../java/com/msdw/tms/entity/vo/CheckVo.java | 29 ++++ .../com/msdw/tms/entity/vo/StudentVo.java | 8 +- .../com/msdw/tms/service/StudentService.java | 8 + .../com/msdw/tms/service/UserService.java | 26 ++++ .../tms/service/impl/StudentServiceImpl.java | 36 +++++ .../tms/service/impl/UserServiceImpl.java | 100 +++++++++++- src/main/resources/mapper/tms/StudentDao.xml | 50 ++++++ src/main/resources/mapper/tms/UserDao.xml | 103 ++++++++++++- src/main/resources/mapper/tms/UserInfoDao.xml | 4 +- src/test/java/com/msdw/tms/EmaiTest.java | 27 ++++ .../com/msdw/tms/service/StudentTest.java | 20 +-- 24 files changed, 886 insertions(+), 57 deletions(-) create mode 100644 src/main/java/com/msdw/tms/config/CheckCodeQuartzConfig.java create mode 100644 src/main/java/com/msdw/tms/config/CheckCodeUtils.java create mode 100644 src/main/java/com/msdw/tms/config/EmailUtils.java create mode 100644 src/main/java/com/msdw/tms/config/SendSMS.java create mode 100644 src/main/java/com/msdw/tms/config/quartz/CheckCodeQuartz.java create mode 100644 src/main/java/com/msdw/tms/entity/vo/CheckVo.java create mode 100644 src/test/java/com/msdw/tms/EmaiTest.java diff --git a/pom.xml b/pom.xml index 57f3860..ecba049 100644 --- a/pom.xml +++ b/pom.xml @@ -150,6 +150,54 @@ spring-boot-configuration-processor true + + commons-beanutils + commons-beanutils + 1.8.3 + + + commons-collections + commons-collections + 3.2.1 + + + commons-lang + commons-lang + 2.6 + + + commons-logging + commons-logging + 1.1.3 + + + org.springframework.boot + spring-boot-starter-quartz + + + + + + com.aliyun + aliyun-java-sdk-core + 4.0.8 + + + com.aliyun + aliyun-java-sdk-dysmsapi + 1.1.0 + + + + com.google.code.gson + gson + 2.8.4 + + + org.projectlombok + lombok + 1.18.12 + diff --git a/src/main/java/com/msdw/tms/api/StudentControllerApi.java b/src/main/java/com/msdw/tms/api/StudentControllerApi.java index 947ba24..dec5a8c 100644 --- a/src/main/java/com/msdw/tms/api/StudentControllerApi.java +++ b/src/main/java/com/msdw/tms/api/StudentControllerApi.java @@ -23,4 +23,10 @@ public interface StudentControllerApi { @ApiOperation(value = "编辑学生信息",notes = "编辑学生信息") public R updateStudent(StudentVo studentVo); + + @ApiOperation(value = "重置密码",notes = "重置密码") + public R reSetPassword(Integer userId); + + @ApiOperation(value = "禁用账号",notes = "禁用账号") + public R disableAccount(Integer userId); } diff --git a/src/main/java/com/msdw/tms/api/UserControllerApi.java b/src/main/java/com/msdw/tms/api/UserControllerApi.java index 105dd4d..f3dcc97 100644 --- a/src/main/java/com/msdw/tms/api/UserControllerApi.java +++ b/src/main/java/com/msdw/tms/api/UserControllerApi.java @@ -2,10 +2,14 @@ package com.msdw.tms.api; import com.msdw.tms.common.utils.R; import com.msdw.tms.entity.UserEntity; +import com.msdw.tms.entity.vo.CheckVo; import com.msdw.tms.entity.vo.UserEntityVo; import com.msdw.tms.entity.vo.UserProsonalEntityVo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import java.util.List; @@ -21,4 +25,15 @@ public interface UserControllerApi { @ApiOperation(value = "更新密码",notes = "更新密码") R examinePassword(UserEntityVo entity); + @ApiOperation(value = "发送邮箱验证码", notes = "发送邮箱验证码") + public R sendEmailCode(CheckVo checkVo); + + @ApiOperation(value = "邮箱验证并更新",notes = "邮箱验证并更新") + public R bindEmail(CheckVo checkVo); + + @ApiOperation(value = "发送手机验证码",notes = "发送手机验证码") + R sendPhoneCode(CheckVo checkVo); + + @ApiOperation(value = "校验手机验证码",notes = "校验手机验证码") + R bindPhone(CheckVo checkVo); } diff --git a/src/main/java/com/msdw/tms/config/CheckCodeQuartzConfig.java b/src/main/java/com/msdw/tms/config/CheckCodeQuartzConfig.java new file mode 100644 index 0000000..114dab9 --- /dev/null +++ b/src/main/java/com/msdw/tms/config/CheckCodeQuartzConfig.java @@ -0,0 +1,33 @@ +package com.msdw.tms.config; + +import com.msdw.tms.config.quartz.CheckCodeQuartz; +import org.quartz.*; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CheckCodeQuartzConfig { + //定义任务详情 + @Bean + public JobDetail orderjobDetail() { + //指定job的名称和持久化保存任务 + return JobBuilder + .newJob(CheckCodeQuartz.class) //自定义任务 + .withIdentity("checkCodeQuartz") //为任务定义名称 + .storeDurably() + .build(); + } + //定义触发器 + @Bean + public Trigger orderTrigger() { + //通知调度器,程序多久执行一次 + CronScheduleBuilder scheduleBuilder +// = CronScheduleBuilder.cronSchedule("5/20 * * ? * *"); //时间表达式 每分钟的5,25秒执行任务 + = CronScheduleBuilder.cronSchedule("0 0 18 * * ? "); //时间表达式,每天的18点执行任务 + return TriggerBuilder + .newTrigger() + .forJob(orderjobDetail()) + .withIdentity("checkCodeQuartz") + .withSchedule(scheduleBuilder).build(); + } +} diff --git a/src/main/java/com/msdw/tms/config/CheckCodeUtils.java b/src/main/java/com/msdw/tms/config/CheckCodeUtils.java new file mode 100644 index 0000000..ae4d957 --- /dev/null +++ b/src/main/java/com/msdw/tms/config/CheckCodeUtils.java @@ -0,0 +1,39 @@ +package com.msdw.tms.config; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Random; + +public class CheckCodeUtils { + + /** + * 获取六位随机数验证码 + * @return + */ + public String sendCode(){ + + Random random = new Random(); + String code=""; + for (int i=0;i<6;i++) + { + code+=random.nextInt(10); + } + return code; + } + + /** + * 生成时间 + */ + public String letTimes(int letTime){ + + Calendar instance = Calendar.getInstance(); + instance.add(Calendar.MINUTE,+letTime); + Date time = instance.getTime(); + SimpleDateFormat myFmt=new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); + String format1 = myFmt.format(time); + + return format1; + } + +} diff --git a/src/main/java/com/msdw/tms/config/EmailUtils.java b/src/main/java/com/msdw/tms/config/EmailUtils.java new file mode 100644 index 0000000..8d12a3e --- /dev/null +++ b/src/main/java/com/msdw/tms/config/EmailUtils.java @@ -0,0 +1,52 @@ +package com.msdw.tms.config; + +import org.apache.commons.mail.HtmlEmail; + +import java.util.Random; + +/** + * 邮箱验证工具 + */ +public class EmailUtils { + + //服务器地址 + private static final String SERVER_ADDRESS = "smtp.163.com"; + //邮箱地址 + private static final String EMAILADDRESS = "mzh820631607@163.com"; + //发送人姓名 + private static final String EMAILUSERNAME = "michonne"; + //授权码 + private static final String AUTHORIZATION_CODE = "ZPAASKPCYJDIFFDR"; + //邮件标题 + private static final String EMAILHEAD = "京喜"; + //邮件信息(后接验证码)头部信息 + private static final String EMAILMASSAGEHEAD = "尊敬的用户您好,您本次注册的验证码是: "; + //邮件信息(后接验证码)尾部信息 + private static final String EMAILMASSAGETAIL = ",有效时间为5分钟"; + + + //邮箱验证码 + public static boolean sendEmail(String emailaddress,String code){ + try { + HtmlEmail email = new HtmlEmail(); + email.setHostName(SERVER_ADDRESS); + email.setCharset("UTF-8"); + email.addTo(emailaddress); + + email.setFrom(EMAILADDRESS, EMAILUSERNAME); + + email.setAuthentication(EMAILADDRESS, AUTHORIZATION_CODE); + + email.setSubject(EMAILHEAD); + email.setMsg( EMAILMASSAGEHEAD + code + EMAILMASSAGETAIL); + + email.send(); + return true; + } + catch(Exception e){ + e.printStackTrace(); + return false; + } + } + +} diff --git a/src/main/java/com/msdw/tms/config/SendSMS.java b/src/main/java/com/msdw/tms/config/SendSMS.java new file mode 100644 index 0000000..c0a51f8 --- /dev/null +++ b/src/main/java/com/msdw/tms/config/SendSMS.java @@ -0,0 +1,91 @@ +package com.msdw.tms.config; + +import com.aliyuncs.DefaultAcsClient; +import com.aliyuncs.IAcsClient; +import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; +import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; +import com.aliyuncs.http.MethodType; +import com.aliyuncs.profile.DefaultProfile; +import com.aliyuncs.profile.IClientProfile; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class SendSMS { + + // 短信模板id,此处将其封装为变量,是为了由该方法的调用者决定自己来发送哪种类型的短信验证码,比如注册,重置密码类的 + private static final String PHONSMSTEMPLATE = "SMS_204155294"; + + public static void main(String[] args){ + String mobile="17376370737";//需要接受短信验证码的手机号 +// String sendTemplate="SMS_204155294";//短信模板ID,需要去阿里云平台申请,审核通过后方能使用,通常是以"SMS_"开头字符串 + String code = (int)((Math.random() * 9 + 1) * 100000)+ "";//要发送给用户的短信验证码,行业通常使用六位纯数字 + + boolean result= sendCode(mobile,code); + if(result){ + System.out.println("短信发送成功"); + }else{ + System.out.println("短信发送失败"); + } + + } + /** + * 通过阿里云平台发送短信,如果发送成功,返回"OK" ;否则返回的是错误信息 + * @param mobile 要接收该短信的用户手机号 + * @param + * @param code 您将发送到用户手机的短信验证码 + */ + public static Boolean sendCode(String mobile,String code){ + boolean isSend = false; + //请求失败这里会抛ClientException异常 + String result=""; + try { + //设置超时时间-可自行调整 + System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); + System.setProperty("sun.net.client.defaultReadTimeout", "10000"); + //初始化ascClient需要的几个参数 + final String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改) + final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改) + //替换成你的AK + final String accessKeyId = "LTAI4FzqQHnk4rozqLZ8jCNj";//你的accessKeyId,参考本文档步骤2 + final String accessKeySecret = "mveW7B1OyFoKUkHm8WsxmrjHmkJWHq";//你的accessKeySecret,参考本文档步骤2 + //初始化ascClient,暂时不支持多region(请勿修改) + IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, + accessKeySecret); + DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); + IAcsClient acsClient = new DefaultAcsClient(profile); + //组装请求对象 + SendSmsRequest request = new SendSmsRequest(); + //使用post提交 + request.setMethod(MethodType.POST); + //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000” + request.setPhoneNumbers(mobile); + //必填:短信签名-可在短信控制台中找到 + request.setSignName("职站");//注意要与你在短信控制台中的签名一致 + //必填:短信模板-可在短信控制台中找到 + request.setTemplateCode(PHONSMSTEMPLATE); + //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 + //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败 +// request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}"); + request.setTemplateParam("{\"code\":\""+code+"\"}"); + //可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段) + //request.setSmsUpExtendCode("90997"); + //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 +// request.setOutId("yourOutId"); + + SendSmsResponse response = acsClient.getAcsResponse(request); + result=response.getCode(); + if(result!=null&&"OK".equals(result.toUpperCase())){ + System.out.println("短信发送成功"); + isSend = true; + }else{ + isSend = false; + System.out.println("短信发送失败"); + } + } catch (Exception e) { + result=e.getMessage(); + isSend = false; + } + return isSend; + } + +} \ No newline at end of file diff --git a/src/main/java/com/msdw/tms/config/quartz/CheckCodeQuartz.java b/src/main/java/com/msdw/tms/config/quartz/CheckCodeQuartz.java new file mode 100644 index 0000000..34c73db --- /dev/null +++ b/src/main/java/com/msdw/tms/config/quartz/CheckCodeQuartz.java @@ -0,0 +1,24 @@ +package com.msdw.tms.config.quartz; + +import com.msdw.tms.service.UserService; +import lombok.extern.slf4j.Slf4j; +import org.quartz.Job; +import org.quartz.JobExecutionContext; +import org.quartz.JobExecutionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +public class CheckCodeQuartz implements Job { + + @Autowired + private UserService userService; + + @Override + public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { + boolean clean = userService.cleanUp(); + log.info("清理过期验证码"); + + } +} diff --git a/src/main/java/com/msdw/tms/controller/StudentController.java b/src/main/java/com/msdw/tms/controller/StudentController.java index 9fecd4d..d75203a 100644 --- a/src/main/java/com/msdw/tms/controller/StudentController.java +++ b/src/main/java/com/msdw/tms/controller/StudentController.java @@ -1,5 +1,6 @@ package com.msdw.tms.controller; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.msdw.tms.api.StudentControllerApi; import com.msdw.tms.common.utils.PageUtils; import com.msdw.tms.common.utils.R; @@ -34,12 +35,19 @@ public class StudentController implements StudentControllerApi{ @Override @PostMapping("/addStudent") public R addStudent(@RequestBody StudentVo studentVo){ + + String studentIdNumber = studentVo.getStudentIdNumber(); + Integer result = studentService.queryStudentIdNumber(studentIdNumber); + if (result==1){ + return R.error("学号不能重复"); + }else{ UserInfoEntity userInfoList = studentVo.getUserInfo(); - int userId= userInfoService.saveUserInfo(userInfoList); + userInfoService.saveUserInfo(userInfoList); studentVo.setUserId(userInfoList.getUserId()); boolean saveStudent = studentService.saveStudent(studentVo); return saveStudent ? R.ok() : R.error(); + } } /** @@ -76,11 +84,42 @@ public class StudentController implements StudentControllerApi{ * @return */ @Override - @PostMapping("/updateStudent") + @PutMapping("/updateStudent") public R updateStudent(@RequestBody StudentVo studentVo) { - return R.ok(); + String studentIdNumber = studentVo.getStudentIdNumber(); + Integer result = studentService.queryStudentIdNumber(studentIdNumber); + if (result==1){ + return R.error("学号不能重复"); + }else{ + boolean saveStudent = studentService.updateStudent(studentVo); + + return saveStudent ? R.ok() : R.error(); + } + } + + /** + * 重置密码 + * @param userId + * @return + */ + @Override + @PutMapping("/reSetPassword") + public R reSetPassword(@RequestBody Integer userId){ + boolean initial = studentService.initialPassword(userId); + return initial ? R.ok() : R.error(); } + /** + * 禁用账号 + * @param studentId + * @return + */ + @Override + @PutMapping("/disableAccount") + public R disableAccount(@RequestBody Integer studentId) { + boolean disable = studentService.disableAccount(studentId); + return disable ? R.ok() : R.error(); + } } diff --git a/src/main/java/com/msdw/tms/controller/UserController.java b/src/main/java/com/msdw/tms/controller/UserController.java index 3f79133..62d1f8e 100644 --- a/src/main/java/com/msdw/tms/controller/UserController.java +++ b/src/main/java/com/msdw/tms/controller/UserController.java @@ -3,22 +3,24 @@ package com.msdw.tms.controller; import com.msdw.tms.api.UserControllerApi; import com.msdw.tms.common.utils.PageUtils; import com.msdw.tms.common.utils.R; +import com.msdw.tms.config.CheckCodeUtils; +import com.msdw.tms.config.EmailUtils; +import com.msdw.tms.config.SendSMS; import com.msdw.tms.entity.PersonalFileEntity; import com.msdw.tms.entity.UserEntity; import com.msdw.tms.entity.UserInfoEntity; import com.msdw.tms.entity.UserPersonalFileEntity; +import com.msdw.tms.entity.vo.CheckVo; import com.msdw.tms.entity.vo.UserEntityVo; import com.msdw.tms.entity.vo.UserProsonalEntityVo; import com.msdw.tms.service.UserInfoService; import com.msdw.tms.service.UserService; +import com.sun.org.apache.bcel.internal.generic.CHECKCAST; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @RestController @RequestMapping("tms/user") @@ -85,39 +87,23 @@ public class UserController implements UserControllerApi { if (userPassword.equals(password)==true){ return R.error(400,"false,the password is the same!!!"); }else{ - userInfoService.updateUserInfoById(userInfo); + userInfoService.updateUserInfoById(userInfo); // userInfoService.updateById(userInfo); - for (int i = 0;i userEntityVoList = vo.getUserEntityVoList(); -// UserEntity userInfo = vo.getUserEntity(); -// userService.updateById(userInfo); -// for (int i = 0;i emailCode = userService.findEmailCode(email,types);//获取最近的验证码 + if (emailCode.size()==1){ + String checkCode = emailCode.get(0); + List result = userService.findEmailSetTime(email,checkCode,types);//验证最近验证码是否过期 + if (result.size()==1){ + String code = checkVo.getCode(); + if (code.equals(checkCode)){ + boolean bind = userService.bindEmail(checkVo); + return bind ? R.ok() : R.error(); + }else{ + return R.error(403,"验证失败!!!"); + } + }else{ + return R.error(403,"验证码过期!!!"); + } + }else{ + return R.error(403,"请发送验证码!!!"); + } + } + + /** + * 发送手机验证码 + * @param checkVo + * @return + */ + @Override + @PostMapping("/sendPhoneCode") + public R sendPhoneCode(@RequestBody CheckVo checkVo){ + boolean re = userService.queryPhone(checkVo); + if (re){ + return R.error(403,"电话号码不能重复"); + }else { + boolean save = userService.sendCheckPhoneCode(checkVo); + return save ? R.ok() : R.error(); + } + } + + /** + * 绑定手机 + * @param checkVo + * @return + */ + @Override + @PutMapping("/bindPhone") + public R bindPhone(@RequestBody CheckVo checkVo){ + String phone = checkVo.getPhone(); + Integer types = checkVo.getTypes(); + List phoneCode = userService.findPhoneCode(phone,types);//获取最近的验证码 + if (phoneCode.size()==1){ + String checkCode = phoneCode.get(0); + List result = userService.findPhoneSetTime(phone,checkCode,types);//验证最近验证码是否过期 + if (result.size()==1){ + if (checkVo.getCode().equals(checkCode)){ + boolean bind = userService.bindPhone(checkVo); + return bind ? R.ok() : R.error(); + }else{ + return R.error(403,"验证失败!!!"); + } + }else{ + return R.error(403,"验证码过期!!!"); + } + }else{ + return R.error(403,"请发送验证码!!!"); + } + } } diff --git a/src/main/java/com/msdw/tms/dao/StudentDao.java b/src/main/java/com/msdw/tms/dao/StudentDao.java index 385986e..c036ef3 100644 --- a/src/main/java/com/msdw/tms/dao/StudentDao.java +++ b/src/main/java/com/msdw/tms/dao/StudentDao.java @@ -12,9 +12,18 @@ import java.util.List; @Mapper public interface StudentDao extends BaseMapper { + + List queryStudentIdNumber(String studentIdNumber); + boolean saveStudent(StudentVo studentVo); IPage queryStudent(Page tpage,String searchContent,Integer schoolId); boolean deleteStudent(List studentId); + + boolean updateStudent(StudentVo studentVo); + + boolean initialPassword(Integer userId,String password); + + boolean disableAccount(Integer studentId); } diff --git a/src/main/java/com/msdw/tms/dao/UserDao.java b/src/main/java/com/msdw/tms/dao/UserDao.java index 794c5f3..acae17d 100644 --- a/src/main/java/com/msdw/tms/dao/UserDao.java +++ b/src/main/java/com/msdw/tms/dao/UserDao.java @@ -3,6 +3,7 @@ package com.msdw.tms.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.msdw.tms.entity.UserEntity; import com.msdw.tms.entity.UserPersonalFileEntity; +import com.msdw.tms.entity.vo.CheckVo; import com.msdw.tms.entity.vo.UserEntityVo; import com.msdw.tms.entity.vo.UserProsonalEntityVo; import org.apache.ibatis.annotations.Mapper; @@ -12,6 +13,12 @@ import java.util.List; @Mapper public interface UserDao extends BaseMapper { + boolean cleanUp(); + + List queryEmial(CheckVo checkVo); + + boolean saveCheckCode(CheckVo checkVo); + UserEntityVo getByUserId(UserEntity userEntity); void updateUserPersonal(UserPersonalFileEntity vo); @@ -23,4 +30,20 @@ public interface UserDao extends BaseMapper { List queryUserInfo(Integer userid); void insertPersonalInfo(UserPersonalFileEntity personalFile); + + boolean bindEmail(CheckVo checkVo); + + boolean bindPhone(CheckVo checkVo); + + List findEmailCode(String email,Integer types); + + List findEmailSetTime(String email, String checkCode,Integer types); + + List findPhoneCode(String phone,Integer types); + + List findPhoneSetTime(String phone, String checkCode,Integer types); + + boolean saveCheckPhoneCode(CheckVo checkVo); + + List queryPhone(CheckVo checkVo); } diff --git a/src/main/java/com/msdw/tms/entity/StudentEntity.java b/src/main/java/com/msdw/tms/entity/StudentEntity.java index b2a032d..453ba5b 100644 --- a/src/main/java/com/msdw/tms/entity/StudentEntity.java +++ b/src/main/java/com/msdw/tms/entity/StudentEntity.java @@ -30,4 +30,6 @@ public class StudentEntity { private Integer roleId; //是否删除(0未删除,1已删除) private Integer isdel; + //是否禁用账号(0禁用,1不禁用,默认0) + private Integer disableAccount; } diff --git a/src/main/java/com/msdw/tms/entity/vo/CheckVo.java b/src/main/java/com/msdw/tms/entity/vo/CheckVo.java new file mode 100644 index 0000000..541e370 --- /dev/null +++ b/src/main/java/com/msdw/tms/entity/vo/CheckVo.java @@ -0,0 +1,29 @@ +package com.msdw.tms.entity.vo; + +import lombok.Data; +import lombok.experimental.Accessors; + +@Data +@Accessors(chain = true) +public class CheckVo { + //用户id + private Integer userId; + //用户的邮箱地址 + private String email; + //验证码 + private String checkCode; + //用户电话号码 + private String phone; + //验证码存入时间(实际时间-允许验证时间) + private String setTime; + //验证码类型(1邮箱验证码,2手机验证码) + private Integer types; + + //用户输入的验证码 + private String code; + //用户设置的新邮箱地址 + private String emailAddress; + //用户设置的新手机号 + private String mobile; + +} diff --git a/src/main/java/com/msdw/tms/entity/vo/StudentVo.java b/src/main/java/com/msdw/tms/entity/vo/StudentVo.java index ae21864..5d4fe9d 100644 --- a/src/main/java/com/msdw/tms/entity/vo/StudentVo.java +++ b/src/main/java/com/msdw/tms/entity/vo/StudentVo.java @@ -7,6 +7,9 @@ import lombok.experimental.Accessors; import java.util.List; +/** + * 学生管理 + */ @Data @Accessors(chain = true) public class StudentVo extends StudentEntity { @@ -23,6 +26,9 @@ public class StudentVo extends StudentEntity { private Integer logInNumber; //最后登陆时间 private String lastLoginTime; - + //电话 + private String phone; + //邮箱 + private String email; } diff --git a/src/main/java/com/msdw/tms/service/StudentService.java b/src/main/java/com/msdw/tms/service/StudentService.java index 0ff0daa..1e03410 100644 --- a/src/main/java/com/msdw/tms/service/StudentService.java +++ b/src/main/java/com/msdw/tms/service/StudentService.java @@ -10,9 +10,17 @@ import java.util.List; public interface StudentService extends IService{ + Integer queryStudentIdNumber(String studentIdNumber); + boolean saveStudent(StudentVo studentvo); PageUtils queryStudent(Integer schoolId,String searchContent, Integer page, Integer size); boolean deleteStudent(List studentId); + + boolean updateStudent(StudentVo studentVo); + + boolean initialPassword(Integer userId); + + boolean disableAccount(Integer studentId); } diff --git a/src/main/java/com/msdw/tms/service/UserService.java b/src/main/java/com/msdw/tms/service/UserService.java index eda8181..36f122b 100644 --- a/src/main/java/com/msdw/tms/service/UserService.java +++ b/src/main/java/com/msdw/tms/service/UserService.java @@ -2,14 +2,17 @@ package com.msdw.tms.service; import com.baomidou.mybatisplus.extension.service.IService; import com.msdw.tms.common.utils.PageUtils; +import com.msdw.tms.common.utils.R; import com.msdw.tms.entity.PersonalFileEntity; import com.msdw.tms.entity.UserEntity; import com.msdw.tms.entity.UserInfoEntity; import com.msdw.tms.entity.UserPersonalFileEntity; +import com.msdw.tms.entity.vo.CheckVo; import com.msdw.tms.entity.vo.UserEntityVo; import com.msdw.tms.entity.vo.UserProsonalEntityVo; import java.util.HashMap; +import java.util.List; import java.util.Map; public interface UserService extends IService { @@ -24,4 +27,27 @@ public interface UserService extends IService { HashMap queryUserInfo(Integer id); void insertPersonalInfo(UserPersonalFileEntity personalFile); + + boolean bindEmail(CheckVo checkVo); + + List findEmailCode(String email,Integer types); + + List findEmailSetTime(String email,String checkCode,Integer types); + + boolean bindPhone(CheckVo checkVo); + + List findPhoneCode(String phone,Integer types); + + List findPhoneSetTime(String email,String checkCode,Integer types); + + boolean queryEmial(CheckVo checkVo); + + boolean queryPhone(CheckVo checkVo); + + boolean saveCheckCode(CheckVo checkVo); + + boolean sendCheckPhoneCode(CheckVo checkVo); + + //清空过期验证码 + boolean cleanUp(); } diff --git a/src/main/java/com/msdw/tms/service/impl/StudentServiceImpl.java b/src/main/java/com/msdw/tms/service/impl/StudentServiceImpl.java index 3a70348..e2aede5 100644 --- a/src/main/java/com/msdw/tms/service/impl/StudentServiceImpl.java +++ b/src/main/java/com/msdw/tms/service/impl/StudentServiceImpl.java @@ -22,6 +22,20 @@ public class StudentServiceImpl extends ServiceImpl im @Autowired private StudentDao studentDao; + /** + * 初始密码 + */ + private static final String INITIAL_PASSWORD = "huoran123"; + + @Override + public Integer queryStudentIdNumber(String studentIdNumber) { + List result = studentDao.queryStudentIdNumber(studentIdNumber); + if (result.size()==1){ + return 1; + } + return 0; + } + @Transactional @Override public boolean saveStudent(StudentVo studentvo) { @@ -46,4 +60,26 @@ public class StudentServiceImpl extends ServiceImpl im return delete; } + @Transactional + @Override + public boolean updateStudent(StudentVo studentVo) { + boolean update = studentDao.updateStudent(studentVo); + return update; + } + + @Transactional + @Override + public boolean initialPassword(Integer userId) { + + boolean initial = studentDao.initialPassword(userId,INITIAL_PASSWORD); + return initial; + } + + @Transactional + @Override + public boolean disableAccount(Integer studentId) { + boolean disable = studentDao.disableAccount(studentId); + return disable; + } + } diff --git a/src/main/java/com/msdw/tms/service/impl/UserServiceImpl.java b/src/main/java/com/msdw/tms/service/impl/UserServiceImpl.java index 0293b9f..7adf2f7 100644 --- a/src/main/java/com/msdw/tms/service/impl/UserServiceImpl.java +++ b/src/main/java/com/msdw/tms/service/impl/UserServiceImpl.java @@ -6,15 +6,20 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.msdw.tms.common.utils.Constant; import com.msdw.tms.common.utils.PageUtils; import com.msdw.tms.common.utils.Query; +import com.msdw.tms.common.utils.R; +import com.msdw.tms.config.CheckCodeUtils; +import com.msdw.tms.config.EmailUtils; +import com.msdw.tms.config.SendSMS; import com.msdw.tms.dao.UserDao; import com.msdw.tms.entity.UserEntity; -import com.msdw.tms.entity.UserInfoEntity; import com.msdw.tms.entity.UserPersonalFileEntity; +import com.msdw.tms.entity.vo.CheckVo; import com.msdw.tms.entity.vo.UserEntityVo; import com.msdw.tms.entity.vo.UserProsonalEntityVo; import com.msdw.tms.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.HashMap; @@ -25,6 +30,13 @@ import java.util.Map; public class UserServiceImpl extends ServiceImpl implements UserService { @Autowired private UserDao userMapper; + + @Override + public boolean cleanUp(){ + boolean clean = userMapper.cleanUp(); + return clean; + } + @Override public PageUtils queryPage(Map params) { @@ -58,9 +70,6 @@ public class UserServiceImpl extends ServiceImpl implements List userInfo = userMapper.queryUserInfo(id); map.put("userInfo",userInfo); - //个人档案sql -// List userEntityVos = userMapper.queryPersonalFile(id);//档案里有学校信息时 -// map.put("userPersonalFile",userEntityVos); List prosonalEntityESVos = userMapper.queryPersonalFileES(id);//档案里无学校信息时 map.put("userPersonalFile",prosonalEntityESVos); @@ -71,4 +80,87 @@ public class UserServiceImpl extends ServiceImpl implements public void insertPersonalInfo(UserPersonalFileEntity personalFile) { userMapper.insertPersonalInfo(personalFile); } + + @Transactional + @Override + public boolean bindEmail(CheckVo checkVo) { + boolean bind = userMapper.bindEmail(checkVo); + return bind; + } + @Transactional + @Override + public List findEmailCode(String email,Integer types) { + List bind = userMapper.findEmailCode(email,types); + return bind; + } + @Transactional + @Override + public List findEmailSetTime(String email,String checkCode,Integer types) { + List bind = userMapper.findEmailSetTime(email,checkCode,types); + return bind; + } + + @Override + public boolean queryEmial(CheckVo checkVo){ + List re = userMapper.queryEmial(checkVo); + if (re.size()>0){ + return true; + }else{ + return false; + } + } + @Override + public boolean queryPhone(CheckVo checkVo){ + List re = userMapper.queryPhone(checkVo); + if (re.size()>0){ + return true; + }else{ + return false; + } + } + + @Override + public boolean saveCheckCode(CheckVo checkVo) { + CheckCodeUtils checkCodeUtils = new CheckCodeUtils(); + //获取六位随机数验证码 + String code = checkCodeUtils.sendCode(); + EmailUtils.sendEmail(checkVo.getEmail(),code);//发送邮箱验证码 + + String letTimes = checkCodeUtils.letTimes(5);//设定验证码5分钟内有效 + checkVo.setCheckCode(code).setSetTime(letTimes); + boolean save = userMapper.saveCheckCode(checkVo); + return save; + } + + @Override + public boolean sendCheckPhoneCode(CheckVo checkVo) { + CheckCodeUtils checkCodeUtils = new CheckCodeUtils(); + //获取六位随机数验证码 + String code = checkCodeUtils.sendCode(); + SendSMS.sendCode(checkVo.getPhone(),code);//发送邮箱验证码 + + String letTimes = checkCodeUtils.letTimes(5);//设定验证码5分钟内有效 + checkVo.setCheckCode(code).setSetTime(letTimes); + boolean save = userMapper.saveCheckPhoneCode(checkVo); + return save; + } + + @Transactional + @Override + public boolean bindPhone(CheckVo checkVo) { + boolean bind = userMapper.bindPhone(checkVo); + return bind; + } + @Transactional + @Override + public List findPhoneCode(String phone,Integer types) { + List bind = userMapper.findPhoneCode(phone,types); + return bind; + } + @Transactional + @Override + public List findPhoneSetTime(String phone,String checkCode,Integer types) { + List bind = userMapper.findPhoneSetTime(phone,checkCode,types); + return bind; + } } diff --git a/src/main/resources/mapper/tms/StudentDao.xml b/src/main/resources/mapper/tms/StudentDao.xml index d98a74e..3b05e44 100644 --- a/src/main/resources/mapper/tms/StudentDao.xml +++ b/src/main/resources/mapper/tms/StudentDao.xml @@ -26,6 +26,15 @@ lastLoginTime DESC + + INSERT INTO tms_student ( schoolId, userId, studentIdNumber, professionalId, gradeId, classId, roleId, isdel ) VALUES @@ -40,4 +49,45 @@ #{studentId} ) + + UPDATE + tms_student s,hr_user_info u + + s.schoolId = #{schoolId}, + s.studentIdNumber = #{studentIdNumber}, + s.professionalId = #{professionalId}, + s.gradeId = #{gradeId}, + s.classId = #{classId}, + u.userName = #{userName}, + u.schoolId = #{schoolId}, + u.account = #{account}, + u.phone = #{phone}, + u.phone = NULL, + u.email = #{email} + u.email = NULL + + WHERE + u.userId = s.userId + AND + u.userId = #{userId} + AND + s.studentId = #{studentId} + + + UPDATE + hr_user_info + SET + password = #{password} + WHERE + userId = #{userId} + + + + UPDATE + tms_student + SET + disableAccount = 1 + WHERE + studentId = #{studentId} + \ No newline at end of file diff --git a/src/main/resources/mapper/tms/UserDao.xml b/src/main/resources/mapper/tms/UserDao.xml index e2d078d..d969685 100644 --- a/src/main/resources/mapper/tms/UserDao.xml +++ b/src/main/resources/mapper/tms/UserDao.xml @@ -2,9 +2,11 @@ + + DELETE FROM tms_checkcode WHERE now() > setTime + - - SELECT u.userId, userName, @@ -83,8 +85,74 @@ WHERE userId = #{userId} + + + + + + - + UPDATE hr_personal_file per @@ -112,10 +180,37 @@ AND per.personalFileId = #{personalFileId} + + UPDATE + hr_user_info + SET + email = #{email} + WHERE + userId = #{userId} + - + + UPDATE + hr_user_info + SET + phone = #{phone} + WHERE + userId = #{userId} + + + INSERT INTO hr_personal_file ( userId, personalCareerId, schoolId, schoolName, disciplineId, disciplineName, professionalId, professionalName, professionalClassId, professionalClassName, updatetime ) VALUES (#{userId},#{personalCareerId},#{schoolId},#{schoolName},#{disciplineId},#{disciplineName},#{professionalId},#{professionalName},#{professionalClassId},#{professionalClassName},NOW()) + + INSERT INTO tms_checkcode ( userId, email, types, checkCode, setTime ) + VALUES + (#{userId},#{email},#{types},#{checkCode},#{setTime}) + + + INSERT INTO tms_checkcode ( userId, phone, types, checkCode, setTime ) + VALUES + (#{userId},#{phone},#{types},#{checkCode},#{setTime}) + \ No newline at end of file diff --git a/src/main/resources/mapper/tms/UserInfoDao.xml b/src/main/resources/mapper/tms/UserInfoDao.xml index fba849b..60ccddf 100644 --- a/src/main/resources/mapper/tms/UserInfoDao.xml +++ b/src/main/resources/mapper/tms/UserInfoDao.xml @@ -61,8 +61,8 @@ - INSERT INTO hr_user_info ( uniqueIdentificationAccount, schoolId, account, roleId,isdel) + INSERT INTO hr_user_info ( userName,email,phone,uniqueIdentificationAccount, schoolId, account, roleId,isdel) VALUES - ( #{uniqueIdentificationAccount}, #{schoolId}, #{account}, 4 ,#{isdel}) + ( #{userName},#{email},#{phone},#{uniqueIdentificationAccount}, #{schoolId}, #{account}, 4 ,#{isdel}) \ No newline at end of file diff --git a/src/test/java/com/msdw/tms/EmaiTest.java b/src/test/java/com/msdw/tms/EmaiTest.java new file mode 100644 index 0000000..16c710a --- /dev/null +++ b/src/test/java/com/msdw/tms/EmaiTest.java @@ -0,0 +1,27 @@ +package com.msdw.tms; + +import com.msdw.tms.config.EmailUtils; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.Random; + +@SpringBootTest +public class EmaiTest { + + @Test + public void sendEmailTest(){ + String emailaddress = "820631607@qq.com"; + + //获取六位随机数验证码 + Random random = new Random(); + String code=""; + for (int i=0;i<6;i++) + { + code+=random.nextInt(10); + } + System.out.println(code); + boolean b = EmailUtils.sendEmail(emailaddress, code); + System.out.println(b?"发送成功":"发送失败"); + } +} \ No newline at end of file diff --git a/src/test/java/com/msdw/tms/service/StudentTest.java b/src/test/java/com/msdw/tms/service/StudentTest.java index 10d0446..3fbe752 100644 --- a/src/test/java/com/msdw/tms/service/StudentTest.java +++ b/src/test/java/com/msdw/tms/service/StudentTest.java @@ -30,7 +30,7 @@ public class StudentTest { Integer a = 1; UserInfoEntity userInfoEntity = new UserInfoEntity(); userInfoEntity.setAccount("123456").setUserName("michonne").setSchoolId(a); - studentVo.setSchoolId(a).setUserId(257).setStudentIdNumber("888888").setRoleId(4).setProfessionalId(1).setGradeId(1).setClassId(1); + studentVo.setSchoolId(a).setStudentIdNumber("888888").setRoleId(4).setProfessionalId(1).setGradeId(1).setClassId(1); studentVo.setUserInfo(userInfoEntity); boolean aa= studentService.saveStudent(studentVo); // boolean bb = userInfoService.saveUserInfo(userInfoEntity); @@ -53,14 +53,14 @@ public class StudentTest { /** * 删除学生 */ -// @Test -// public void deleteStudent(){ + @Test + public void deleteStudent(){ // List studentId = {1,2}; -//// List studentId = new ArrayList<>(); -//// studentId.add(1); -//// studentId.add(2); -// -// boolean b = studentService.deleteStudent(studentId); -// System.out.println(b ? "删除成功" : "删除失败"); -// } + List studentId = new ArrayList<>(); + studentId.add(1); + studentId.add(2); + + boolean b = studentService.deleteStudent(studentId); + System.out.println(b ? "删除成功" : "删除失败"); + } }