Prechádzať zdrojové kódy

feat: 封装使用联通集团能力商店发布的能力向钉钉群推送消息接口

weijianghai 1 rok pred
rodič
commit
881af79222

+ 9 - 18
pom.xml

@@ -75,28 +75,19 @@
             <version>5.8.10</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.nokia</groupId>
+            <artifactId>push-message-starter</artifactId>
+            <version>1.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-validation</artifactId>
+        </dependency>
     </dependencies>
     <build>
         <finalName>sms_server</finalName>
-<!--        <resources>-->
-<!--            <resource>-->
-<!--                <directory>${project.basedir}/src/main/resources</directory>-->
-<!--                <targetPath></targetPath>-->
-<!--                <includes>-->
-<!--                    <include>**/*.*</include>-->
-<!--                </includes>-->
-<!--            </resource>-->
-<!--        </resources>-->
-<!--        <testSourceDirectory>src/test</testSourceDirectory>-->
         <plugins>
-<!--            <plugin>-->
-<!--                <groupId>org.apache.maven.plugins</groupId>-->
-<!--                <artifactId>maven-compiler-plugin</artifactId>-->
-<!--                <configuration>-->
-<!--                    <source>1.8</source>-->
-<!--                    <target>1.8</target>-->
-<!--                </configuration>-->
-<!--            </plugin>-->
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>

+ 1 - 2
src/main/java/com/nokia/sms/config/web/RequestLogHandlerInterceptor.java

@@ -5,7 +5,6 @@ import org.slf4j.MDC;
 import org.springframework.lang.Nullable;
 import org.springframework.util.StopWatch;
 import org.springframework.util.StreamUtils;
-import org.springframework.util.StringUtils;
 import org.springframework.web.servlet.HandlerInterceptor;
 
 import javax.servlet.http.HttpServletRequest;
@@ -52,7 +51,7 @@ public class RequestLogHandlerInterceptor implements HandlerInterceptor {
 //        log.info("查询参数: {}", JSON.toJSONString(parameters));
         // 请求体参数
         String body = StreamUtils.copyToString(request.getInputStream(), Charset.forName(request.getCharacterEncoding()));
-        log.info("请求参数: {}", StringUtils.trimAllWhitespace(body));
+        log.info("请求参数: \n{}", body);
         return true;
     }
 

+ 38 - 0
src/main/java/com/nokia/sms/controller/DingTalkController.java

@@ -0,0 +1,38 @@
+package com.nokia.sms.controller;
+
+import com.nokia.common.http.R;
+import com.nokia.sms.dto.SendDingTalkMessageDTO;
+import com.nokia.sms.service.DingTalkService;
+import lombok.extern.slf4j.Slf4j;
+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;
+
+import javax.validation.Valid;
+
+/**
+ * 使用联通集团能力商店发布的能力向钉钉群推送消息
+ *
+ */
+@Slf4j
+@RestController
+@RequestMapping("/api/dingtalk")
+public class DingTalkController {
+    private final DingTalkService dingTalkService;
+
+    public DingTalkController(DingTalkService dingTalkService) {
+        this.dingTalkService = dingTalkService;
+    }
+
+    /**
+     * 发送普通文本消息
+     *
+     * @param dto dto
+     * @return {@link R}
+     */
+    @PostMapping("/sendTextMessage")
+    public R sendTextMessage(@Valid @RequestBody SendDingTalkMessageDTO dto) {
+        return dingTalkService.sendTextMessage(dto);
+    }
+}

+ 28 - 0
src/main/java/com/nokia/sms/dto/SendDingTalkMessageDTO.java

@@ -0,0 +1,28 @@
+package com.nokia.sms.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.validation.constraints.NotBlank;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class SendDingTalkMessageDTO {
+    /**
+     * 消息
+     */
+    @NotBlank(message = "message不能为空")
+    private String message;
+    /**
+     * 访问令牌
+     */
+    @NotBlank(message = "accessToken不能为空")
+    private String accessToken;
+    /**
+     * 自定义关键词
+     */
+    @NotBlank(message = "prefix不能为空")
+    private String prefix;
+}

+ 20 - 0
src/main/java/com/nokia/sms/service/DingTalkService.java

@@ -0,0 +1,20 @@
+package com.nokia.sms.service;
+
+import com.nokia.common.http.R;
+import com.nokia.pushmessage.service.PushMessageService;
+import com.nokia.sms.dto.SendDingTalkMessageDTO;
+import org.springframework.stereotype.Service;
+
+@Service
+public class DingTalkService {
+    private final PushMessageService pushMessageService;
+
+    public DingTalkService(PushMessageService pushMessageService) {
+        this.pushMessageService = pushMessageService;
+    }
+
+    public R sendTextMessage(SendDingTalkMessageDTO dto) {
+        pushMessageService.sendTextMessage(dto.getAccessToken(), dto.getPrefix(), dto.getMessage());
+        return R.ok();
+    }
+}

+ 8 - 0
src/main/resources/application.properties

@@ -9,3 +9,11 @@ spring.datasource.url=jdbc:postgresql://192.168.70.109:5432/sqmmt
 spring.datasource.username=sqmdb
 spring.datasource.password=sqmdb_1QAZ
 sms.max-retry=1
+
+pushmessage.enable=true
+pushmessage.url=http://10.244.18.105:8000/api/chinaUnicom/microservice/notice/pushMessage/v1
+pushmessage.appId=ENWaB7YdUD
+pushmessage.appSecret=oz4OgKBaMNwi4LWfLPbhrPbbuCS8T0Rb
+pushmessage.systemId=10000078
+pushmessage.moduleId=20000156
+pushmessage.busiCode=30000111

+ 32 - 0
src/test/com/nokia/sms/service/DingTalkTest.java

@@ -0,0 +1,32 @@
+package com.nokia.sms.service;
+
+import com.nokia.pushmessage.service.PushMessageService;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+/**
+ * 使用联通集团能力商店发布的能力向钉钉群推送消息测试
+ *
+ * @author x
+ * @date 2023/07/13
+ */
+@SpringBootTest
+class DingTalkTest {
+    String accessToken = "1339c4484acb660527acedea99419473f4529eb345982a96371c1b1f5c69cfa7";
+    String prefix = "alert: ";
+    @Autowired
+    PushMessageService pushMessageService;
+
+    @Test
+    void sendTextMessageTest() {
+        String message = "纯文本消息";
+        pushMessageService.sendTextMessage(accessToken, prefix, message);
+    }
+
+    @Test
+    void sendTextMessageAllTest() {
+        String message = "纯文本消息@所有人";
+        pushMessageService.sendTextMessage(accessToken, prefix, message, true);
+    }
+}