YhEmailServiceImpl.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.cyksj.service.mail.impl;
  2. import cn.hutool.core.util.StrUtil;
  3. import cn.hutool.http.HttpRequest;
  4. import cn.hutool.http.HttpStatus;
  5. import cn.hutool.http.HttpUtil;
  6. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.cyksj.common.exception.BusinessRuntimeException;
  9. import com.cyksj.common.util.StringUtil;
  10. import com.cyksj.common.util.TicketCodeCommonUtil;
  11. import com.cyksj.mapper.sys.SysEmailMapper;
  12. import com.cyksj.model.entity.SysEmail;
  13. import com.cyksj.service.mail.YhMailService;
  14. import com.cyksj.service.relation.GroupsRelationNetflixService;
  15. import lombok.RequiredArgsConstructor;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.stereotype.Service;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. /**
  21. * 项目名: yhlxj2
  22. * 文件名: YhEmailServiceImpl
  23. * 创建者: JavaZou
  24. * 创建时间:2024/8/19 15:40
  25. */
  26. @Service
  27. @RequiredArgsConstructor
  28. @Slf4j
  29. public class YhEmailServiceImpl implements YhMailService {
  30. private final GroupsRelationNetflixService groupsRelationNetflixService;
  31. private final SysEmailMapper sysEmailMapper;
  32. @Override
  33. public String getVerifyCode(String account, String relateEmail, Long goodsId, Integer type, Integer linkType, Boolean isReturnDeviceUrl) throws Exception {
  34. String body = readEmail(StrUtil.isNotEmpty(relateEmail) ? relateEmail : account);
  35. String code = getDifferentGoodsCode(goodsId, body, type, linkType, isReturnDeviceUrl);
  36. if (StrUtil.isEmpty(code)) {
  37. throw BusinessRuntimeException.getInstance("请重新发送对应邮件");
  38. }
  39. return code;
  40. }
  41. @Override
  42. public String readResetPwdResetLink(String account) {
  43. String body = readEmail(account);
  44. String prefix = "https://auth0.openai.com/u/reset-verify";
  45. if (body.contains(prefix)) {
  46. String query = StrUtil.subBetween(body, "<" + prefix, ">");
  47. if(StringUtils.isBlank(query)){
  48. query = StrUtil.subBetween(body, "(" + prefix, ")");
  49. if(StringUtils.isBlank(query)){
  50. query = StrUtil.subBetween(body, "( " + prefix, " )");
  51. }
  52. }
  53. String link = prefix + query;
  54. if (link.contains("\uE53B")) {
  55. link = link.replace("\uE53B", "=");
  56. }
  57. if (!link.contains("=")) {
  58. link = link.replaceAll("(ticket)[^=]", "$1=");
  59. }
  60. return link;
  61. }
  62. throw BusinessRuntimeException.getInstance("读取重置密码邮件失败或被新邮件覆盖");
  63. }
  64. @Override
  65. public String getGptCode(String account) {
  66. String body = readEmail(account);
  67. String prefix = "Enter this temporary verification";
  68. if (body.contains(prefix)) {
  69. String str = StrUtil.subAfter(body, prefix, true);
  70. String code = StringUtil.getVerifyCodePattern(str, 6);
  71. return code;
  72. }
  73. throw BusinessRuntimeException.getInstance("获取验证码失败或被新邮件覆盖");
  74. }
  75. public String readEmail(String account) {
  76. //获取邮箱密码
  77. SysEmail sysEmail = sysEmailMapper.selectOne(Wrappers.lambdaQuery(SysEmail.class)
  78. .eq(SysEmail::getEmail, account)
  79. .last("limit 1"));
  80. if (sysEmail == null) {
  81. log.info("获取账号:{}验证码异常,未配置对应系统邮箱", account);
  82. throw BusinessRuntimeException.getInstance("对应邮箱配置不存在...");
  83. }
  84. String url = "http://yinhelx.com/api/controller.php";
  85. Map<String, Object> params = new HashMap<>();
  86. params.put("imap_key", "imap_56568niua@@");
  87. params.put("fun", "imap_read");
  88. params.put("imap_ip", "yinhelx.com");
  89. params.put("port", "143");
  90. params.put("username", account);
  91. params.put("password", sysEmail.getPassword());
  92. params.put("mbox_id", "0");
  93. params.put("delete", "0");
  94. HttpRequest post = HttpUtil.createPost(url);
  95. post.form(params);
  96. cn.hutool.http.HttpResponse execute = post.execute();
  97. if (execute.getStatus() != HttpStatus.HTTP_OK) {
  98. throw BusinessRuntimeException.getInstance("获取验证码异常,请联系客服...");
  99. }
  100. return execute.body();
  101. }
  102. public String getDifferentGoodsCode(Long goodsId, String body, Integer type, Integer linkType, Boolean isReturnDeviceUrl) {
  103. String code = null;
  104. //奈飞
  105. if (goodsId == 1) {
  106. if (type != null && type == 1) {
  107. if (body.contains("輸入此代碼登入") || body.contains("输入此代码登录") || body.contains("Enter this code to sign in")) {
  108. code = StrUtil.subBetween(body, "輸入此代碼登入", "請在裝置上輸入上");
  109. if (StrUtil.isEmpty(code)) {
  110. code = StrUtil.subAfter(body, "输入此代码登录", true);
  111. }
  112. if (StrUtil.isEmpty(code)) {
  113. code = StrUtil.subAfter(body, "Enter this code to sign in", true);
  114. }
  115. }
  116. if (StrUtil.isEmpty(code)) {
  117. throw BusinessRuntimeException.getInstance("查询失败,请检查是否发送了登陆码..");
  118. }
  119. } else {
  120. String urlPref;
  121. //更新装置
  122. if (linkType == 1) {
  123. urlPref = "https://www.netflix.com/account/update-primary-location";
  124. code = StrUtil.subBetween(body, "[" + urlPref, "]");
  125. } else {
  126. //验证链接
  127. urlPref = "https://www.netflix.com/account/travel/verify";
  128. code = StrUtil.subBetween(body, "[" + urlPref, "]");
  129. }
  130. if (StrUtil.isEmpty(code)) {
  131. throw BusinessRuntimeException.getInstance("请在奈飞端点击“传送电子邮件”");
  132. }
  133. String url = urlPref + code;
  134. //非点击装置同步更新按钮 直接返回链接
  135. if (linkType == 2) return url;
  136. //直接返回同步更新装置url
  137. if (isReturnDeviceUrl) {
  138. return url;
  139. }
  140. //失败返回验证链接
  141. if (!groupsRelationNetflixService.confirmUpdate(url)) {
  142. return url;
  143. }
  144. return StrUtil.EMPTY;
  145. }
  146. }
  147. //除奈飞
  148. if (StrUtil.isEmpty(code)) {
  149. code = TicketCodeCommonUtil.getTicketCodeStr(body, goodsId);
  150. }
  151. return code;
  152. }
  153. }