24282 2 年 前
コミット
147d29f8c1

+ 2 - 2
src/main/java/com/fjhx/config/code/CodeGeneration.java → src/main/java/com/fjhx/config/CodeGeneration.java

@@ -1,4 +1,4 @@
-package com.fjhx.config.code;
+package com.fjhx.config;
 
 import com.baomidou.mybatisplus.generator.FastAutoGenerator;
 import com.fjhx.base.BaseEntity;
@@ -25,7 +25,7 @@ public class CodeGeneration {
                 })
                 .strategyConfig(builder -> builder
                         // 设置需要生成的表名
-                        .addInclude("email_info")
+                        .addInclude("email_mailbox")
                         .entityBuilder()
                         .disableSerialVersionUID()
                         .superClass(BaseEntity.class)

+ 41 - 0
src/main/java/com/fjhx/config/TaskPoolConfig.java

@@ -0,0 +1,41 @@
+package com.fjhx.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.ThreadPoolExecutor;
+
+/**
+ * 线程池配置
+ */
+@EnableAsync
+@Configuration
+public class TaskPoolConfig {
+
+    public static final String taskExecutor = "taskExecutor";
+
+    @Bean(name = taskExecutor)
+    public Executor taskExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        //配置核心线程数
+        executor.setCorePoolSize(5);
+        //配置最大线程数
+        executor.setMaxPoolSize(20);
+        //配置队列大小
+        executor.setQueueCapacity(500);
+        // 缓冲队列大小
+        executor.setKeepAliveSeconds(100);
+        //配置线程池中的线程的名称前缀
+        executor.setThreadNamePrefix("async-service:");
+        // 线程池对拒绝任务的处理策略:由调用线程(提交任务的线程)处理该任务
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+
+        //执行初始化
+        executor.initialize();
+        return executor;
+    }
+
+}

+ 0 - 30
src/main/java/com/fjhx/config/http/EmailEngineUtil.java

@@ -1,30 +0,0 @@
-package com.fjhx.config.http;
-
-import cn.hutool.http.HttpUtil;
-import com.alibaba.fastjson.JSONObject;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Component;
-import org.springframework.web.client.RestTemplate;
-
-@Component
-public class EmailEngineUtil {
-
-    private static String urlPrefix;
-
-    private static final RestTemplate restTemplate = new RestTemplate();
-
-    @Value("${config.urlPrefix}")
-    public void setUrlPrefix(String urlPrefix) {
-        EmailEngineUtil.urlPrefix = urlPrefix;
-    }
-
-    public static <T> T get(String url, Class<T> cls) {
-        String result = HttpUtil.get(urlPrefix + url);
-        return JSONObject.parseObject(result).toJavaObject(cls);
-    }
-
-    public static <T> T post(String url, Object paramObj, Class<T> cls) {
-        return restTemplate.postForObject(urlPrefix + url, paramObj, cls);
-    }
-
-}

+ 0 - 1
src/main/java/com/fjhx/controller/AccountController.java

@@ -28,5 +28,4 @@ public class AccountController {
 
 
 
-
 }

+ 20 - 0
src/main/java/com/fjhx/controller/EmailMailboxController.java

@@ -0,0 +1,20 @@
+package com.fjhx.controller;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.stereotype.Controller;
+
+/**
+ * <p>
+ *  前端控制器
+ * </p>
+ *
+ * @author zlj
+ * @since 2022-12-20
+ */
+@Controller
+@RequestMapping("/emailMailbox")
+public class EmailMailboxController {
+
+
+
+}

+ 0 - 6
src/main/java/com/fjhx/entity/EmailInfo.java

@@ -48,10 +48,4 @@ public class EmailInfo extends BaseEntity {
      */
     private String smtpHost;
 
-    /**
-     * 删除标志(0存在 1删除)
-     */
-    private Integer delFlag;
-
-
 }

+ 53 - 0
src/main/java/com/fjhx/entity/EmailMailbox.java

@@ -0,0 +1,53 @@
+package com.fjhx.entity;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.fjhx.base.BaseEntity;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * <p>
+ *
+ * </p>
+ *
+ * @author zlj
+ * @since 2022-12-20
+ */
+@Getter
+@Setter
+@TableName("email_mailbox")
+public class EmailMailbox extends BaseEntity {
+
+    /**
+     * 电子邮件id
+     */
+    private Long emailInfoId;
+
+    /**
+     * 电子邮件
+     */
+    private String email;
+
+    private String path;
+
+    private String delimiter;
+
+    private Boolean listed;
+
+    private String specialUse;
+
+    private String name;
+
+    private Boolean subscribed;
+
+    private Integer messages;
+
+    private Boolean uidNext;
+
+    /**
+     * 是否展示(1是 0否)
+     */
+    private Boolean isShow;
+
+
+}

+ 16 - 0
src/main/java/com/fjhx/mapper/EmailMailboxMapper.java

@@ -0,0 +1,16 @@
+package com.fjhx.mapper;
+
+import com.fjhx.entity.EmailMailbox;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author zlj
+ * @since 2022-12-20
+ */
+public interface EmailMailboxMapper extends BaseMapper<EmailMailbox> {
+
+}

+ 5 - 0
src/main/java/com/fjhx/mapper/xml/EmailMailboxMapper.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.fjhx.mapper.EmailMailboxMapper">
+
+</mapper>

+ 16 - 0
src/main/java/com/fjhx/service/IEmailMailboxService.java

@@ -0,0 +1,16 @@
+package com.fjhx.service;
+
+import com.fjhx.entity.EmailMailbox;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author zlj
+ * @since 2022-12-20
+ */
+public interface IEmailMailboxService extends IService<EmailMailbox> {
+
+}

+ 42 - 90
src/main/java/com/fjhx/service/impl/AccountServiceImpl.java

@@ -1,15 +1,22 @@
 package com.fjhx.service.impl;
 
 import cn.hutool.core.bean.BeanUtil;
-import cn.hutool.core.lang.Assert;
 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.EmailMailbox;
 import com.fjhx.service.IAccountService;
 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.Qualifier;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+import java.util.concurrent.Executor;
 
 @Service
 public class AccountServiceImpl implements IAccountService {
@@ -17,110 +24,55 @@ public class AccountServiceImpl implements IAccountService {
     @Autowired
     private IEmailInfoService emailInfoService;
 
+    @Autowired
+    private IEmailMailboxService emailMailboxService;
+
+    @Qualifier(TaskPoolConfig.taskExecutor)
+    @Autowired
+    private Executor executor;
+
+    @Transactional(rollbackFor = Exception.class)
     @Override
     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) {
             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;
     }
 
-
-    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);
         emailInfoService.save(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);
+    }
+
 }

+ 20 - 0
src/main/java/com/fjhx/service/impl/EmailMailboxServiceImpl.java

@@ -0,0 +1,20 @@
+package com.fjhx.service.impl;
+
+import com.fjhx.entity.EmailMailbox;
+import com.fjhx.mapper.EmailMailboxMapper;
+import com.fjhx.service.IEmailMailboxService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author zlj
+ * @since 2022-12-20
+ */
+@Service
+public class EmailMailboxServiceImpl extends ServiceImpl<EmailMailboxMapper, EmailMailbox> implements IEmailMailboxService {
+
+}

+ 132 - 0
src/main/java/com/fjhx/utils/EmailEngineUtil.java

@@ -0,0 +1,132 @@
+package com.fjhx.utils;
+
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.lang.Assert;
+import com.alibaba.fastjson.JSONObject;
+import com.fjhx.entity.EmailMailbox;
+import com.fjhx.vo.*;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.List;
+
+@Component
+public class EmailEngineUtil {
+
+    private static String urlPrefix;
+
+    private static final RestTemplate restTemplate = new RestTemplate();
+
+
+    @Value("${config.urlPrefix}")
+    public void setUrlPrefix(String urlPrefix) {
+        EmailEngineUtil.urlPrefix = urlPrefix;
+    }
+
+    /**
+     * 生成邮箱
+     */
+    public static void createAccount(BindingVo bindingVo) {
+        // 验证邮件配置参数
+        VerifyVo verifyVo = createVerifyVo(bindingVo);
+        // 验证邮件配置是否正确
+        VerifyResult verifyResult = post("v1/verifyAccount", verifyVo, VerifyResult.class);
+        // 解析验证结果
+        parsingVerifyResult(verifyResult);
+        // 生成添加账号入参
+        AddAccountVo addAccountVo = createAddAccountVo(verifyVo, bindingVo.getEmail());
+        // 添加邮箱
+        AddAccountResult addAccountResult = post("v1/account", addAccountVo, AddAccountResult.class);
+        // 验证邮箱添加状态
+        parsingAddAccountResult(addAccountResult);
+    }
+
+    /**
+     * 查看邮箱所有文件夹
+     */
+    public static List<EmailMailbox> getMailboxList(String email) {
+        String result = get("v1/account/" + email + "/mailboxes?counters=false", String.class);
+        return BeanUtil.copyToList(JSONObject.parseObject(result).getJSONArray("mailboxes"), EmailMailbox.class);
+    }
+
+
+    /**
+     * 生成验证邮箱配置实体
+     */
+    private static 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 static 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 static 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 static void parsingAddAccountResult(AddAccountResult addAccountResult) {
+        String state = addAccountResult.getState();
+
+        Assert.isTrue("new".equals(state) || "existing".equals(state),
+                "邮箱添加失败,account:{},state:{}",
+                addAccountResult.getAccount(), addAccountResult.getState());
+    }
+
+    public static <T> T get(String url, Class<T> cls) {
+        return restTemplate.getForObject(urlPrefix + url, cls);
+    }
+
+    public static <T> T post(String url, Object paramObj, Class<T> cls) {
+        return restTemplate.postForObject(urlPrefix + url, paramObj, cls);
+    }
+
+}

+ 7 - 0
src/main/java/com/fjhx/vo/Mailbox.java

@@ -0,0 +1,7 @@
+package com.fjhx.vo;
+
+public class Mailbox {
+
+
+
+}