diff --git a/pom.xml b/pom.xml index df0fcb6..0721092 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,11 @@ mirai-core-jvm 2.99.0-local + + net.mamoe + mirai-logging-log4j2 + 2.16.0 + com.baomidou mybatis-plus-boot-starter @@ -79,6 +84,11 @@ thumbnailator 0.4.20 + + + org.springframework.boot + spring-boot-starter-mail + diff --git a/src/main/java/com/linxyun/homework/bot/MyBot.java b/src/main/java/com/linxyun/homework/bot/MyBot.java index bb381d2..a7239ee 100644 --- a/src/main/java/com/linxyun/homework/bot/MyBot.java +++ b/src/main/java/com/linxyun/homework/bot/MyBot.java @@ -3,19 +3,18 @@ package com.linxyun.homework.bot; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.linxyun.homework.bot.handler.MyEventHandler; import com.linxyun.homework.domain.dto.ClassGroupTeacher; -import com.linxyun.homework.domain.po.Class; -import com.linxyun.homework.domain.po.ClassQqGroupTeacher; +import com.linxyun.homework.domain.po.Class;; import com.linxyun.homework.mapper.ClassMapper; import com.linxyun.homework.mapper.ClassQqGroupTeacherMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import net.mamoe.mirai.Bot; import net.mamoe.mirai.BotFactory; import net.mamoe.mirai.auth.BotAuthorization; import net.mamoe.mirai.event.EventChannel; import net.mamoe.mirai.event.events.BotEvent; import net.mamoe.mirai.event.events.GroupMessageEvent; import net.mamoe.mirai.utils.BotConfiguration; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; @@ -70,7 +69,7 @@ public class MyBot implements ApplicationRunner { } public void runBot() { - net.mamoe.mirai.Bot bot = BotFactory.INSTANCE.newBot(qq, BotAuthorization.byQRCode(), configuration -> { + Bot bot = BotFactory.INSTANCE.newBot(qq, BotAuthorization.byQRCode(), configuration -> { configuration.setProtocol(BotConfiguration.MiraiProtocol.MACOS); // 指定设备信息文件路径,文件不存在将自动生成一个默认的,存在就读取 configuration.fileBasedDeviceInfo("deviceInfo_" + qq + ".json"); diff --git a/src/main/java/com/linxyun/homework/common/exception/GlobalExceptionHandler.java b/src/main/java/com/linxyun/homework/common/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..df31a1f --- /dev/null +++ b/src/main/java/com/linxyun/homework/common/exception/GlobalExceptionHandler.java @@ -0,0 +1,24 @@ +package com.linxyun.homework.common.exception; + +import com.linxyun.homework.utils.EmailUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.annotation.Resource; + +@Slf4j +@ControllerAdvice +public class GlobalExceptionHandler { + + @Resource + private EmailUtils emailUtils; + + @ExceptionHandler(value = Exception.class) + @ResponseBody + public void exceptionHandler(Exception e) { + emailUtils.sendEmail("异常捕获", e.getMessage()); + log.error("ExceptionHandler exception: {}", e.getMessage()); + } +} \ No newline at end of file diff --git a/src/main/java/com/linxyun/homework/utils/EmailUtils.java b/src/main/java/com/linxyun/homework/utils/EmailUtils.java new file mode 100644 index 0000000..b791fbb --- /dev/null +++ b/src/main/java/com/linxyun/homework/utils/EmailUtils.java @@ -0,0 +1,30 @@ +package com.linxyun.homework.utils; + +import lombok.extern.slf4j.Slf4j; +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.stereotype.Component; + +import javax.annotation.Resource; +import java.util.concurrent.CompletableFuture; + +@Slf4j +@Component +public class EmailUtils { + @Resource + private JavaMailSender javaMailSender; + + @Value("${spring.mail.to-address}") + private String toAddress; + + public void sendEmail(String subject, String text) { + SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); + simpleMailMessage.setFrom("wenxin_web@163.com"); + simpleMailMessage.setTo(toAddress); + simpleMailMessage.setSubject(subject); + simpleMailMessage.setText(text); + CompletableFuture.runAsync(() -> javaMailSender.send(simpleMailMessage)); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index a5592e0..8b74d3e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -16,6 +16,14 @@ spring: #springboot的配置 # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver + mail: + host: smtp.163.com + port: 465 + protocol: smtps + username: wenxin_web@163.com + password: YCOUPMDBKDTQRAJR + default-encoding: UTF-8 + to-address: 1731551615@qq.com # mybatis-plus相关配置 mybatis-plus: @@ -36,19 +44,20 @@ mybatis-plus: # 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段 call-setters-on-nulls: true # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 - log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl + logging: file: # 输出的log文件名 - name: slf4j-test + name: application.log # 输出的文件的路径 path: ./logs level: # 输出级别 - root: debug + root: info # 特定的mapper下的输出级别 - com.example.demo.mapper: debug + com.linxyun.homework.mapper: debug # 设置 MyBatis-Plus 日志级别为 DEBUG # xml配置文件 config: classpath:logback.xml