Explorar el Código

新增阿里云短信发送功能、定时任务(每周查询证书、并判断是否需要发送短信)

yjj hace 1 año
padre
commit
a670adf0cd

+ 10 - 0
pom.xml

@@ -84,6 +84,16 @@
             <artifactId>hutool-all</artifactId>
             <artifactId>hutool-all</artifactId>
             <version>${hutool.version}</version>
             <version>${hutool.version}</version>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>aliyun-java-sdk-core</artifactId>
+            <version>4.5.0</version>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>dysmsapi20170525</artifactId>
+            <version>2.0.24</version>
+        </dependency>
         <!-- 分页插件 -->
         <!-- 分页插件 -->
 <!--        <dependency>-->
 <!--        <dependency>-->
 <!--            <groupId>com.github.pagehelper</groupId>-->
 <!--            <groupId>com.github.pagehelper</groupId>-->

+ 2 - 0
src/main/java/com/example/demo/DemoApplication.java

@@ -4,9 +4,11 @@ import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.scheduling.annotation.EnableScheduling;
 
 
 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
 @MapperScan(basePackages = {"com.example.demo.**.mapper"})
 @MapperScan(basePackages = {"com.example.demo.**.mapper"})
+@EnableScheduling
 public class DemoApplication {
 public class DemoApplication {
 
 
     public static void main(String[] args) {
     public static void main(String[] args) {

+ 20 - 0
src/main/java/com/example/demo/demos/web/constant/Constants.java

@@ -10,6 +10,26 @@ import java.util.Locale;
  */
  */
 public class Constants
 public class Constants
 {
 {
+    /**
+     * 阿里云登录id
+     */
+    public final static String ACCESS_KEY_ID = "LTAI5tG1HJiv9X9zwwkuh6Av";
+
+    /**
+     * 阿里云登录密码
+     */
+    public final static String ACCESS_KEY_SECRET = "zAtrMWNfg1ZwaZkLA6PI2xn0G28vbJ";
+
+    /**
+     * 服务接入点
+     */
+    public final static String END_POINT = "dysmsapi.aliyuncs.com";
+
+    /**
+     * 发送短信的电话数组
+     */
+    public final static String[] SEND_MSG_NUMBER = {""};
+
     /**
     /**
      * UTF-8 字符集
      * UTF-8 字符集
      */
      */

+ 10 - 0
src/main/java/com/example/demo/demos/web/controller/EcsController.java

@@ -5,6 +5,8 @@ import com.example.demo.demos.web.service.EcsInfoService;
 import com.example.demo.demos.web.service.EcsService;
 import com.example.demo.demos.web.service.EcsService;
 import com.example.demo.demos.web.domain.EcsInfo;
 import com.example.demo.demos.web.domain.EcsInfo;
 import com.example.demo.demos.web.domain.Result;
 import com.example.demo.demos.web.domain.Result;
+import com.example.demo.demos.web.service.impl.EcsInfoServiceImpl;
+import com.example.demo.demos.web.task.SendMsgTask;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
 
 
@@ -35,4 +37,12 @@ public class EcsController {
         Page<EcsInfo> infoPage = ecsInfoService.page(pageInfo);
         Page<EcsInfo> infoPage = ecsInfoService.page(pageInfo);
         return Result.success(infoPage);
         return Result.success(infoPage);
     }
     }
+
+    /**
+     * 测试短信发送
+     */
+    @GetMapping("/test")
+    public void testCC(){
+        ecsInfoService.getInfos();
+    }
 }
 }

+ 2 - 5
src/main/java/com/example/demo/demos/web/service/EcsInfoService.java

@@ -6,9 +6,6 @@ import com.example.demo.demos.web.domain.EcsInfo;
 import java.util.List;
 import java.util.List;
 
 
 public interface EcsInfoService extends IService<EcsInfo> {
 public interface EcsInfoService extends IService<EcsInfo> {
-    /**
-     * 获取快过期的ssl证书
-     * @return
-     */
-    List<EcsInfo> getInfos();
+
+    void getInfos();
 }
 }

+ 20 - 3
src/main/java/com/example/demo/demos/web/service/impl/EcsInfoServiceImpl.java

@@ -3,11 +3,14 @@ package com.example.demo.demos.web.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.example.demo.demos.web.constant.Constants;
 import com.example.demo.demos.web.mapper.EcsInfoMapper;
 import com.example.demo.demos.web.mapper.EcsInfoMapper;
 import com.example.demo.demos.web.service.EcsInfoService;
 import com.example.demo.demos.web.service.EcsInfoService;
 import com.example.demo.demos.web.domain.EcsInfo;
 import com.example.demo.demos.web.domain.EcsInfo;
 import com.example.demo.demos.web.util.DateUtil;
 import com.example.demo.demos.web.util.DateUtil;
+import com.example.demo.demos.web.util.SMSUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import java.util.ArrayList;
 import java.util.ArrayList;
@@ -15,12 +18,11 @@ import java.util.List;
 
 
 @Service
 @Service
 public class EcsInfoServiceImpl extends ServiceImpl<EcsInfoMapper, EcsInfo> implements EcsInfoService {
 public class EcsInfoServiceImpl extends ServiceImpl<EcsInfoMapper, EcsInfo> implements EcsInfoService {
-
     @Autowired
     @Autowired
     private EcsInfoMapper mapper;
     private EcsInfoMapper mapper;
 
 
     @Override
     @Override
-    public List<EcsInfo> getInfos() {
+    public void getInfos() {
         LambdaQueryWrapper<EcsInfo> wrapper = Wrappers.lambdaQuery();
         LambdaQueryWrapper<EcsInfo> wrapper = Wrappers.lambdaQuery();
         List<EcsInfo> ecsInfos = mapper.selectList(wrapper);
         List<EcsInfo> ecsInfos = mapper.selectList(wrapper);
 
 
@@ -37,6 +39,21 @@ public class EcsInfoServiceImpl extends ServiceImpl<EcsInfoMapper, EcsInfo> impl
             }
             }
         });
         });
 
 
-        return list;
+        /*
+          发送短信
+         */
+        if (!list.isEmpty()) {
+            for (String number : Constants.SEND_MSG_NUMBER) {
+                list.forEach(i -> {
+                    String str  = "{\"name\": \""+i.getSerialNumber()+"\"}";
+                    try {
+                        SMSUtil.sendMsg(number,"天目智能科技","SMS_475345133",str);
+                    } catch (Exception e) {
+                        log.error("短信发送失败:{}",e);
+                        throw new RuntimeException(e);
+                    }
+                });
+            }
+        }
     }
     }
 }
 }

+ 39 - 0
src/main/java/com/example/demo/demos/web/task/SendMsgTask.java

@@ -0,0 +1,39 @@
+package com.example.demo.demos.web.task;
+
+import cn.hutool.extra.spring.SpringUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.example.demo.demos.web.constant.Constants;
+import com.example.demo.demos.web.domain.EcsInfo;
+import com.example.demo.demos.web.mapper.EcsInfoMapper;
+import com.example.demo.demos.web.service.EcsInfoService;
+import com.example.demo.demos.web.util.DateUtil;
+import com.example.demo.demos.web.util.SMSUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Component
+@Slf4j
+public class SendMsgTask {
+
+    @Autowired
+    private EcsInfoService ecsInfoService;
+
+    /**
+     * 每周五执行一次定时任务,查看是否有证书要过期的
+     */
+    @Scheduled(cron = "0 0 9 ? * 6 ")
+//    @Scheduled(cron = "10 * * ? * * ")
+    public void sendMsg(){
+        if (ecsInfoService == null){
+            ecsInfoService = SpringUtil.getBean(EcsInfoService.class);
+        }
+        System.out.println("执行定时任务");
+        ecsInfoService.getInfos();
+    }
+}

+ 54 - 0
src/main/java/com/example/demo/demos/web/util/SMSUtil.java

@@ -0,0 +1,54 @@
+package com.example.demo.demos.web.util;
+
+import cn.hutool.json.JSONObject;
+import com.aliyun.dysmsapi20170525.Client;
+import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
+import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
+import com.aliyun.teaopenapi.models.Config;
+import com.aliyun.teautil.models.RuntimeOptions;
+import com.example.demo.demos.web.constant.Constants;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * 阿里云发送短信的工具类
+ */
+@Slf4j
+public class SMSUtil {
+
+    /**
+     * 初始化登录阿里云的Client
+     * @return Client
+     */
+    public static Client createClient() throws Exception {
+        Config config = new Config()
+                .setAccessKeyId(Constants.ACCESS_KEY_ID)
+                .setAccessKeySecret(Constants.ACCESS_KEY_SECRET)
+                .setEndpoint(Constants.END_POINT);
+        return new Client(config);
+    }
+
+    /**
+     * 发送短信的方法
+     * @param phoneNumber 电话
+     * @param signName 签名名称
+     * @param templateCode 模板code
+     * @param param 模板变量值 {"name":"张三","number":"1390000****"}
+     * @throws Exception
+     */
+    public static void sendMsg(String phoneNumber,String signName,String templateCode,String param) throws Exception {
+        Client client = SMSUtil.createClient();
+        SendSmsRequest sendSmsRequest = new SendSmsRequest()
+                .setPhoneNumbers(phoneNumber)
+                .setSignName(signName)
+                .setTemplateCode(templateCode)
+                .setTemplateParam(param);
+
+        SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest,new RuntimeOptions());
+        log.info("短信发送给:{}成功,返回结果是:{}", phoneNumber,sendSmsResponse);
+    }
+
+
+    public static void main(String[] args) throws Exception {
+        SMSUtil.sendMsg("17673986122","天目智能科技","SMS_475345133","{\"name\":\"7777\"}");
+    }
+}