haha 2 anni fa
commit
8c9d85e68a

+ 38 - 0
.gitignore

@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store

+ 63 - 0
pom.xml

@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.seecoder</groupId>
+    <artifactId>seec-message</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.7.3</version>
+        <relativePath/>
+    </parent>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-configuration-processor</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-mail</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+
+        <!--内部依赖-->
+        <dependency>
+            <groupId>com.seecoder</groupId>
+            <artifactId>common-core</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+
+        <!--阿里云依赖-->
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>aliyun-java-sdk-core</artifactId>
+            <version>4.5.20</version>
+        </dependency>
+    </dependencies>
+
+</project>

+ 17 - 0
src/main/java/com/seecoder/AppApplication.java

@@ -0,0 +1,17 @@
+package com.seecoder;
+
+
+import com.seecoder.config.property.AliProperties;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.scheduling.annotation.EnableScheduling;
+
+@SpringBootApplication
+@EnableScheduling
+@EnableConfigurationProperties({AliProperties.class})
+public class AppApplication {
+    public static void main(String[] args) {
+        SpringApplication.run(AppApplication.class, args);
+    }
+}

+ 22 - 0
src/main/java/com/seecoder/config/property/AliProperties.java

@@ -0,0 +1,22 @@
+package com.seecoder.config.property;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+@Data
+@ConfigurationProperties("aliyun")
+public class AliProperties {
+    private Profile profile;
+
+    @Data
+    public static class Profile {
+        private String accessKeyId;
+        private String accessKeySecret;
+        private String regionId;
+        private String sysDomain;
+        private String sysVersion;
+        private String signName;
+        private String templateCode;
+    }
+
+}

+ 31 - 0
src/main/java/com/seecoder/controller/MessageController.java

@@ -0,0 +1,31 @@
+package com.seecoder.controller;
+
+import com.seecoder.commoncore.util.Response;
+import com.seecoder.entity.req.SendByMailReq;
+import com.seecoder.entity.req.SendByPhoneReq;
+import com.seecoder.service.MessageService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+
+@RestController
+@RequestMapping("/message")
+@RequiredArgsConstructor
+public class MessageController {
+    private final MessageService messageService;
+
+    @PostMapping("/phone")
+    public Response<Void> sendByPhone(@RequestBody SendByPhoneReq req) {
+        messageService.sendByPhone(req.getPhone(), req.getText());
+        return Response.ok();
+    }
+
+    @PostMapping("/mail")
+    public Response<Void> sendByMail(@RequestBody SendByMailReq req) {
+        messageService.sendMail(req.getReceivers(), req.getSubject(), req.getText());
+        return Response.ok();
+    }
+}

+ 12 - 0
src/main/java/com/seecoder/entity/req/SendByMailReq.java

@@ -0,0 +1,12 @@
+package com.seecoder.entity.req;
+
+import lombok.Data;
+
+import java.util.Set;
+
+@Data
+public class SendByMailReq {
+    Set<String> receivers;
+    String subject;
+    String text;
+}

+ 12 - 0
src/main/java/com/seecoder/entity/req/SendByPhoneReq.java

@@ -0,0 +1,12 @@
+package com.seecoder.entity.req;
+
+import lombok.Data;
+
+import java.util.Map;
+
+@Data
+public class SendByPhoneReq {
+    String phone;
+    String text;
+    Map<String,String> params;
+}

+ 9 - 0
src/main/java/com/seecoder/service/MessageService.java

@@ -0,0 +1,9 @@
+package com.seecoder.service;
+
+import java.util.Set;
+
+public interface MessageService {
+    void sendMail(Set<String> receivers, String subject, String text);
+
+    void sendByPhone(String phone,String text);
+}

+ 38 - 0
src/main/java/com/seecoder/service/impl/MessageServiceImpl.java

@@ -0,0 +1,38 @@
+package com.seecoder.service.impl;
+
+import com.seecoder.service.MessageService;
+import com.seecoder.thirdParty.AliService;
+import lombok.AllArgsConstructor;
+import org.springframework.core.env.Environment;
+import org.springframework.mail.SimpleMailMessage;
+import org.springframework.mail.javamail.JavaMailSender;
+import org.springframework.stereotype.Service;
+
+import java.util.Date;
+import java.util.Objects;
+import java.util.Set;
+
+@Service
+@AllArgsConstructor
+public class MessageServiceImpl implements MessageService {
+    private final JavaMailSender javaMailSender;
+    private final Environment environment;
+    private final AliService aliService;
+
+    @Override
+    public void sendMail(Set<String> receivers, String subject, String text) {
+        SimpleMailMessage message = new SimpleMailMessage();
+        message.setSubject(subject);
+        message.setFrom(Objects.requireNonNull(environment.getProperty("spring.mail.username")));
+        message.setTo(receivers.toArray(new String[0]));
+        message.setSentDate(new Date());
+        message.setText(text);
+        // 发送邮件
+        javaMailSender.send(message);
+    }
+
+    @Override
+    public void sendByPhone(String phone, String text) {
+        aliService.sendMessage(phone,text);
+    }
+}

+ 56 - 0
src/main/java/com/seecoder/thirdParty/AliService.java

@@ -0,0 +1,56 @@
+package com.seecoder.thirdParty;
+
+import com.aliyuncs.CommonRequest;
+import com.aliyuncs.CommonResponse;
+import com.aliyuncs.DefaultAcsClient;
+import com.aliyuncs.IAcsClient;
+import com.aliyuncs.exceptions.ClientException;
+import com.aliyuncs.http.MethodType;
+import com.aliyuncs.profile.DefaultProfile;
+import com.seecoder.config.property.AliProperties;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+@Service
+public class AliService {
+    private static final Logger log = LoggerFactory.getLogger(AliService.class);
+
+    private final AliProperties.Profile aliProfile;
+    private final IAcsClient client;
+
+    public AliService(AliProperties aliProperties) {
+        this.aliProfile = aliProperties.getProfile();
+        DefaultProfile profile = DefaultProfile.getProfile(
+                aliProfile.getRegionId(),
+                aliProfile.getAccessKeyId(),
+                aliProfile.getAccessKeySecret());
+        client = new DefaultAcsClient(profile);
+    }
+
+    @Async
+    public void sendMessage(String phone, String text) {
+        sendSms(phone, text);
+    }
+
+    private void sendSms(String phone, String code) {
+        CommonRequest request = new CommonRequest();
+        request.setSysMethod(MethodType.POST);
+        request.setSysDomain(aliProfile.getSysDomain());
+        request.setSysVersion(aliProfile.getSysVersion());
+        request.setSysAction("SendSms");
+        request.putQueryParameter("RegionId", aliProfile.getRegionId());
+        request.putQueryParameter("PhoneNumbers", phone);
+        request.putQueryParameter("SignName", aliProfile.getSignName());
+        request.putQueryParameter("TemplateCode", aliProfile.getTemplateCode());
+        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
+        try {
+            CommonResponse response = client.getCommonResponse(request);
+            log.info(response.getData());
+        } catch (ClientException e) {
+            log.error("Send sms error", e);
+        }
+    }
+
+}

+ 31 - 0
src/main/resources/application.yml

@@ -0,0 +1,31 @@
+server:
+  # 端口
+  port: 8090
+
+spring:
+  mail:
+    host: smtp.163.com
+    username: seecoder@163.com
+    password: NJU67seecoder
+    default-encoding: UTF-8
+    protocol: smtp
+    properties:
+      mail.smtp.auth: true
+      mail.smtp.starttls.enable: true
+      mail.smtp.starttls.required: true
+      mail.smtp.socketFactory.port: 465
+      mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
+      mail.smtp.socketFactory.fallback: false
+
+aliyun:
+  profile:
+    access-key-id: LTAI5t5ZwNZ6a4ptUXuiy7of
+    access-key-secret: zC4VEHInTiGDNWGakVvkUPrZSzS1P2
+    region-id: cn-hangzhou
+    sys-domain: dysmsapi.aliyuncs.com
+    sys-version: 2017-05-25
+    sign-name: SEECODER
+    template-code: SMS_253920073
+
+
+