From a2ac8e6dc06cabe98093e1bf2c39031c2ae6d604 Mon Sep 17 00:00:00 2001 From: cuibaocheng Date: Fri, 15 Apr 2022 17:39:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E6=97=A5=E7=A8=8B?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=AE=9A=E6=97=B6=E5=8F=91=E9=80=81=E7=9F=AD?= =?UTF-8?q?=E4=BF=A1=E9=80=9A=E7=9F=A5/=E9=82=AE=E4=BB=B6=E9=80=9A?= =?UTF-8?q?=E7=9F=A5/=E7=AB=99=E5=86=85=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../systemoa/task/ScheduleMessage.java | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 src/main/java/cn/com/tenlion/systemoa/task/ScheduleMessage.java diff --git a/src/main/java/cn/com/tenlion/systemoa/task/ScheduleMessage.java b/src/main/java/cn/com/tenlion/systemoa/task/ScheduleMessage.java new file mode 100644 index 0000000..3372e20 --- /dev/null +++ b/src/main/java/cn/com/tenlion/systemoa/task/ScheduleMessage.java @@ -0,0 +1,138 @@ +package cn.com.tenlion.systemoa.task; + +import cn.com.tenlion.schedule.pojo.dtos.schedule.ScheduleDTO; +import cn.com.tenlion.schedule.service.schedule.IScheduleService; +import cn.com.tenlion.schedule.util.SendShortMessage; +import cn.com.tenlion.systemoa.service.mail.IMailService; +import ink.wgink.interfaces.user.IUserBaseService; +import ink.wgink.module.sms.service.email.IEmailService; +import ink.wgink.pojo.dtos.user.UserDTO; +import org.quartz.DisallowConcurrentExecution; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * 日程管理 + * 轮巡向用户发送日程提醒 + * 2022年4月14日15:29:01 + * 崔宝铖 - 来自日程管理模块 + */ +@Component +@DisallowConcurrentExecution // 防止定时任务并行执行(防止1个没有执行完,另一个又开始了) +public class ScheduleMessage { + + @Autowired + private IScheduleService iScheduleService; + + @Autowired + private JavaMailSender javaMailSender; + + @Autowired + private IUserBaseService iUserBaseService; + + @Autowired + private IMailService iMailService; + + @Value("${spring.mail.username}") //发送人的邮箱 + private String from; + + @Scheduled(cron="0 0/4 * * * ?") // 4分钟执行1次 + public void sendMessage() throws Exception { + // 查出已经开始的日程 , 但没有通知的 + List list = iScheduleService.notNoticeList(); + for(ScheduleDTO dto : list) { + // 1:站内,2:短信,3:邮件 + // 站内通知 + if(dto.getScheduleMessageType().contains("1")) { + String username = dto.getCreatorName().split("\\|")[0]; + String name = dto.getCreatorName().split("\\|")[1]; + String time = dto.getScheduleStartTime().substring(0, 5); + iMailService.cbcSendMail("1", dto.getCreator(), username + "[" + name+ "]", "日程提醒", name + ",您的日程(" + dto.getScheduleTitle() + ")即将在 " + time + "开始" ); + } + // 短信通知 + if(dto.getScheduleMessageType().contains("2")) { + // 校验手机号是否正确 + String phone = dto.getCreatorName().split("\\|")[0]; + String name = dto.getCreatorName().split("\\|")[1]; + if(checkMobileNumber(phone)) { + String time = dto.getScheduleStartTime().substring(0, 5); + SendShortMessage.send(phone, name + ",您的日程(" + dto.getScheduleTitle() + ")即将在 " + time + "开始" , null); + } + } + // 邮箱通知 + if(dto.getScheduleMessageType().contains("3")) { + String name = dto.getCreatorName().split("\\|")[1]; + String time = dto.getScheduleStartTime().substring(0, 5); + UserDTO userDTO = iUserBaseService.get(dto.getCreator()); + String email = userDTO.getUserEmail(); + if(checkEmail(email)) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setText(name + ",您的日程(" + dto.getScheduleTitle() + ")即将在 " + time + "开始"); + message.setSubject("日程提醒"); + message.setTo(email); + message.setFrom(from); + javaMailSender.send(message); + } + } + // 更改提醒状态为已提醒 + iScheduleService.updateMessageStatus(dto.getScheduleId()); + } + } + + /** + * 验证邮箱 + * @param email + * @return + */ + + public static boolean checkEmail(String email) { + boolean flag = false; + try { + String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; + Pattern regex = Pattern.compile(check); + Matcher matcher = regex.matcher(email); + flag = matcher.matches(); + } catch (Exception e) { + flag = false; + } + return flag; + } + + /** + * 验证手机号码 + * @Title : checkMobileNumber + * @功能描述 : TODO + * @设定文件 : @param mobileNumber 手机号 + * @设定文件 : @return + * @返回类型 : boolean false 未通过 | true 通过 + * @throws : + */ + public boolean checkMobileNumber(String mobileNumber) { + boolean flag = false; + try { + // 移动 + String CM = "^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$"; + // 联通 + String CU = "^1(3[0-2]|4[5]|5[56]|7[0156]|8[56])\\d{8}$"; + // 电信 + String CT = "^1(3[3]|4[9]|53|7[037]|8[019])\\d{8}$"; + // 进行匹配 + if(mobileNumber.matches(CM)||mobileNumber.matches(CU)||mobileNumber.matches(CT)) { + flag = true; + } + } catch (Exception e) { + flag = false; + } + return flag; + } + +} +