MailLuServiceImpl.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package com.cyksj.service.mail.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import com.cyksj.common.constant.Constant;
  6. import com.cyksj.common.exception.BusinessRuntimeException;
  7. import com.cyksj.common.util.StringUtil;
  8. import com.cyksj.common.util.TicketCodeCommonUtil;
  9. import com.cyksj.service.mail.MailLuService;
  10. import com.cyksj.service.relation.GroupsRelationNetflixService;
  11. import lombok.RequiredArgsConstructor;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.stereotype.Service;
  14. import javax.mail.*;
  15. import javax.mail.search.AndTerm;
  16. import javax.mail.search.ComparisonTerm;
  17. import javax.mail.search.ReceivedDateTerm;
  18. import javax.mail.search.SearchTerm;
  19. import java.util.Arrays;
  20. import java.util.Comparator;
  21. import java.util.Properties;
  22. /**
  23. * 项目名: yhlxj2
  24. * 文件名: MailLuServiceImpl
  25. * 创建者: JavaZou
  26. * 创建时间:2024/12/23 11:14
  27. */
  28. @Service
  29. @RequiredArgsConstructor
  30. @Slf4j
  31. public class MailLuServiceImpl implements MailLuService {
  32. //mail lu邮箱统一密码
  33. private final static String UNIFIED_PWD = "Wp112233";
  34. private final GroupsRelationNetflixService groupsRelationNetflix;
  35. @Override
  36. public boolean isMailLuEmail(String account) {
  37. for (String domain : Constant.MAIL_LU_DOMAIN) {
  38. if (account.contains(domain)) {
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. @Override
  45. public String getVerifyCode(String account, String relateEmail, Long goodsId, Integer type, Integer linkType, boolean isReturnDeviceUrl) throws Exception {
  46. Properties props = new Properties();
  47. props.put("mail.imap.host", "mail.yxzh8.com"); // 替换为你的IMAP服务器
  48. props.put("mail.imap.port", "993"); // IMAP服务的端口号,通常是993
  49. props.put("mail.imap.ssl.enable", "true"); // 启用SSL
  50. Session session = Session.getInstance(props, new javax.mail.Authenticator() {
  51. protected PasswordAuthentication getPasswordAuthentication() {
  52. return new PasswordAuthentication(StrUtil.isNotBlank(relateEmail) ? relateEmail : account, UNIFIED_PWD);
  53. }
  54. });
  55. Store store = session.getStore("imap");
  56. store.connect();
  57. // 访问收件箱
  58. Folder inbox = store.getFolder("INBOX");
  59. inbox.open(Folder.READ_ONLY);
  60. // 设置时间范围
  61. DateTime now = DateTime.now();
  62. //10分钟
  63. DateTime differ = DateUtil.offsetMinute(now, -10);
  64. // 构建搜索条件
  65. SearchTerm searchTerm = new AndTerm(
  66. new ReceivedDateTerm(ComparisonTerm.GE, differ),
  67. new ReceivedDateTerm(ComparisonTerm.LE, now)
  68. );
  69. // 执行搜索
  70. Message[] messages = inbox.search(searchTerm);
  71. // 根据接收日期对邮件进行排序
  72. Arrays.sort(messages, new Comparator<Message>() {
  73. public int compare(Message m1, Message m2) {
  74. try {
  75. return m2.getReceivedDate().compareTo(m1.getReceivedDate());
  76. } catch (MessagingException e) {
  77. log.error("将邮件按时间排序错误:{}", StringUtil.getErrorText(e));
  78. throw BusinessRuntimeException.getInstance("获取异常,请联系客服");
  79. }
  80. }
  81. });
  82. String code = null;
  83. for (int i = 0; i < messages.length; i++) {
  84. Message message = messages[i];
  85. //主题
  86. String subject = message.getSubject();
  87. //content
  88. String body = message.getContent().toString();
  89. if (goodsId == 1 && subject.contains(Constant.NETFLIX)) {
  90. if (type != null && type == 1) {
  91. String pre = "NetflixSans-Regular, Helvetica, Roboto, Segoe UI, sans-serif; font-weight";
  92. if (body.contains("輸入此代碼登入") || body.contains("输入此代码登录") || body.contains("Enter this code to sign in")) {
  93. code = StrUtil.subAfter(body, pre, false);
  94. if (StrUtil.isEmpty(code)) {
  95. code = StrUtil.subAfter(body, "Enter this code to sign in", true);
  96. }
  97. if (StrUtil.isEmpty(code)) {
  98. code = StrUtil.subAfter(body, "font-weight: 400; color: #232323; padding-top: 20px;", true);
  99. }
  100. }
  101. if (StrUtil.isEmpty(code)) {
  102. throw BusinessRuntimeException.getInstance("查询失败,请检查是否发送了登陆码..");
  103. }
  104. } else {
  105. String urlPref;
  106. String nfVerifyUrl;
  107. if (linkType == 1) {
  108. urlPref = "https://www.netflix.com/account/update-primary-location";
  109. nfVerifyUrl = TicketCodeCommonUtil.extractUrl(body, urlPref);
  110. } else {
  111. urlPref = "https://www.netflix.com/account/travel/verify";
  112. nfVerifyUrl = TicketCodeCommonUtil.extractUrl(body, urlPref);
  113. }
  114. if (StrUtil.isEmpty(nfVerifyUrl)) {
  115. throw BusinessRuntimeException.getInstance("请在奈飞端点击“传送电子邮件”");
  116. }
  117. //非点击装置同步更新按钮 直接返回链接
  118. if (linkType == 2) return nfVerifyUrl;
  119. //直接返回同步装置url
  120. if (isReturnDeviceUrl) {
  121. return nfVerifyUrl;
  122. }
  123. //失败返回验证链接
  124. if (!groupsRelationNetflix.confirmUpdate(nfVerifyUrl)) {
  125. return nfVerifyUrl;
  126. }
  127. return StrUtil.EMPTY;
  128. }
  129. }
  130. //PS、Disney
  131. code = TicketCodeCommonUtil.getTicketCodeStr(body, goodsId);
  132. if (StrUtil.isNotBlank(code)) {
  133. return code;
  134. }
  135. }
  136. inbox.close(false);
  137. store.close();
  138. if (StrUtil.isEmpty(code)) {
  139. throw BusinessRuntimeException.getInstance("请重新发送对应邮件");
  140. }
  141. return code;
  142. }
  143. }