|
@@ -1,15 +1,22 @@
|
|
package com.fjhx.service.impl;
|
|
package com.fjhx.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
-import cn.hutool.core.lang.Assert;
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
-import com.fjhx.config.http.EmailEngineUtil;
|
|
|
|
|
|
+import com.fjhx.config.TaskPoolConfig;
|
|
import com.fjhx.entity.EmailInfo;
|
|
import com.fjhx.entity.EmailInfo;
|
|
|
|
+import com.fjhx.entity.EmailMailbox;
|
|
import com.fjhx.service.IAccountService;
|
|
import com.fjhx.service.IAccountService;
|
|
import com.fjhx.service.IEmailInfoService;
|
|
import com.fjhx.service.IEmailInfoService;
|
|
-import com.fjhx.vo.*;
|
|
|
|
|
|
+import com.fjhx.service.IEmailMailboxService;
|
|
|
|
+import com.fjhx.utils.EmailEngineUtil;
|
|
|
|
+import com.fjhx.vo.BindingVo;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.concurrent.Executor;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
public class AccountServiceImpl implements IAccountService {
|
|
public class AccountServiceImpl implements IAccountService {
|
|
@@ -17,110 +24,55 @@ public class AccountServiceImpl implements IAccountService {
|
|
@Autowired
|
|
@Autowired
|
|
private IEmailInfoService emailInfoService;
|
|
private IEmailInfoService emailInfoService;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private IEmailMailboxService emailMailboxService;
|
|
|
|
+
|
|
|
|
+ @Qualifier(TaskPoolConfig.taskExecutor)
|
|
|
|
+ @Autowired
|
|
|
|
+ private Executor executor;
|
|
|
|
+
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
@Override
|
|
@Override
|
|
public EmailInfo binding(BindingVo bindingVo) {
|
|
public EmailInfo binding(BindingVo bindingVo) {
|
|
|
|
|
|
- EmailInfo emailInfo = emailInfoService.getOne(Wrappers.<EmailInfo>lambdaQuery()
|
|
|
|
- .eq(EmailInfo::getEmail, bindingVo.getEmail()));
|
|
|
|
|
|
+ String email = bindingVo.getEmail();
|
|
|
|
+
|
|
|
|
+ EmailInfo emailInfo = emailInfoService.getOne(Wrappers.<EmailInfo>lambdaQuery().eq(EmailInfo::getEmail, email));
|
|
// 如果存在,直接返回邮箱信息
|
|
// 如果存在,直接返回邮箱信息
|
|
if (emailInfo != null) {
|
|
if (emailInfo != null) {
|
|
return emailInfo;
|
|
return emailInfo;
|
|
}
|
|
}
|
|
|
|
|
|
- // 验证邮件配置参数
|
|
|
|
- VerifyVo verifyVo = createVerifyVo(bindingVo);
|
|
|
|
- // 验证邮件配置是否正确
|
|
|
|
- VerifyResult verifyResult = EmailEngineUtil.post("v1/verifyAccount", verifyVo, VerifyResult.class);
|
|
|
|
- // 解析验证结果
|
|
|
|
- parsingVerifyResult(verifyResult);
|
|
|
|
- // 生成添加账号入参
|
|
|
|
- AddAccountVo addAccountVo = createAddAccountVo(verifyVo, bindingVo.getEmail());
|
|
|
|
- // 添加邮箱
|
|
|
|
- AddAccountResult addAccountResult = EmailEngineUtil.post("v1/account", addAccountVo, AddAccountResult.class);
|
|
|
|
- // 验证邮箱添加状态
|
|
|
|
- parsingAddAccountResult(addAccountResult);
|
|
|
|
- // mysql添加邮箱
|
|
|
|
- emailInfo = addEmailInfo(bindingVo);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+ // 添加账号
|
|
|
|
+ EmailEngineUtil.createAccount(bindingVo);
|
|
|
|
+ // 查询邮箱
|
|
|
|
+ List<EmailMailbox> mailboxList = EmailEngineUtil.getMailboxList(email);
|
|
|
|
+ // 保存账号数据
|
|
|
|
+ emailInfo = saveEmailInfo(bindingVo);
|
|
|
|
+ // 添加邮箱文件夹
|
|
|
|
+ saveEmailMailbox(mailboxList, emailInfo.getId(), email);
|
|
return emailInfo;
|
|
return emailInfo;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
- private VerifyVo createVerifyVo(BindingVo bindingVo) {
|
|
|
|
- VerifyVo verifyVo = new VerifyVo();
|
|
|
|
-
|
|
|
|
- VerifyVo.Auth auth = new VerifyVo.Auth();
|
|
|
|
- auth.setUser(bindingVo.getEmail());
|
|
|
|
- auth.setPass(bindingVo.getSecretKey());
|
|
|
|
-
|
|
|
|
- VerifyVo.Tls tls = new VerifyVo.Tls();
|
|
|
|
- tls.setRejectUnauthorized(true);
|
|
|
|
- tls.setMinVersion("TLSv1.2");
|
|
|
|
-
|
|
|
|
- VerifyVo.Imap imap = new VerifyVo.Imap();
|
|
|
|
- imap.setAuth(auth);
|
|
|
|
- imap.setPort(bindingVo.getImapPort());
|
|
|
|
- imap.setHost(bindingVo.getImapHost());
|
|
|
|
- imap.setTls(tls);
|
|
|
|
- imap.setSecure(true);
|
|
|
|
- verifyVo.setImap(imap);
|
|
|
|
-
|
|
|
|
- VerifyVo.Smtp smtp = new VerifyVo.Smtp();
|
|
|
|
- smtp.setAuth(auth);
|
|
|
|
- smtp.setPort(bindingVo.getSmtpPort());
|
|
|
|
- smtp.setHost(bindingVo.getSmtpHost());
|
|
|
|
- smtp.setTls(tls);
|
|
|
|
- smtp.setSecure(true);
|
|
|
|
- verifyVo.setSmtp(smtp);
|
|
|
|
-
|
|
|
|
- return verifyVo;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 解析验证结果
|
|
|
|
- */
|
|
|
|
- private void parsingVerifyResult(VerifyResult verifyResult) {
|
|
|
|
- VerifyResult.Status imap = verifyResult.getImap();
|
|
|
|
- VerifyResult.Status smtp = verifyResult.getSmtp();
|
|
|
|
-
|
|
|
|
- Assert.notNull(imap, "imap解析结果为空");
|
|
|
|
- Assert.notNull(smtp, "smtp解析结果为空");
|
|
|
|
-
|
|
|
|
- Assert.isTrue(imap.getSuccess(), "imap解析失败:error:{} ;code:{};", imap.getError(), imap.getCode());
|
|
|
|
- Assert.isTrue(smtp.getSuccess(), "smtp解析失败:error:{} ;code:{};", smtp.getError(), smtp.getCode());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 生成添加账号入参
|
|
|
|
- */
|
|
|
|
- private AddAccountVo createAddAccountVo(VerifyVo verifyVo, String email) {
|
|
|
|
- AddAccountVo addAccountVo = BeanUtil.copyProperties(verifyVo, AddAccountVo.class);
|
|
|
|
- addAccountVo.setAccount(email);
|
|
|
|
- addAccountVo.setName(email);
|
|
|
|
- addAccountVo.setEmail(email);
|
|
|
|
- addAccountVo.setPath("*");
|
|
|
|
- return addAccountVo;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 验证添加状态
|
|
|
|
- */
|
|
|
|
- private void parsingAddAccountResult(AddAccountResult addAccountResult) {
|
|
|
|
- String state = addAccountResult.getState();
|
|
|
|
-
|
|
|
|
- Assert.isTrue("new".equals(state),
|
|
|
|
- "邮箱添加失败,account:{},state:{}",
|
|
|
|
- addAccountResult.getAccount(), addAccountResult.getState());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 添加邮件信息
|
|
* 添加邮件信息
|
|
*/
|
|
*/
|
|
- private EmailInfo addEmailInfo(BindingVo bindingVo) {
|
|
|
|
|
|
+ private EmailInfo saveEmailInfo(BindingVo bindingVo) {
|
|
EmailInfo emailInfo = BeanUtil.copyProperties(bindingVo, EmailInfo.class);
|
|
EmailInfo emailInfo = BeanUtil.copyProperties(bindingVo, EmailInfo.class);
|
|
emailInfoService.save(emailInfo);
|
|
emailInfoService.save(emailInfo);
|
|
return emailInfo;
|
|
return emailInfo;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 添加邮箱文件夹
|
|
|
|
+ */
|
|
|
|
+ private void saveEmailMailbox(List<EmailMailbox> mailboxList, Long emailInfoId, String email) {
|
|
|
|
+ for (EmailMailbox emailMailbox : mailboxList) {
|
|
|
|
+ emailMailbox.setEmailInfoId(emailInfoId);
|
|
|
|
+ emailMailbox.setEmail(email);
|
|
|
|
+ emailMailbox.setIsShow(true);
|
|
|
|
+ }
|
|
|
|
+ emailMailboxService.saveBatch(mailboxList);
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|