浏览代码

增加短信发送

Yansen 1年前
父节点
当前提交
980de80df9

+ 2
- 0
src/main/java/com/example/civilizedcity/controller/TaIssueProcessController.java 查看文件

@@ -6,8 +6,10 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
6 6
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
7 7
 import com.example.civilizedcity.common.*;
8 8
 
9
+import com.example.civilizedcity.entity.SysOrg;
9 10
 import com.example.civilizedcity.entity.SysUser;
10 11
 import com.example.civilizedcity.entity.TaIssue;
12
+import com.example.civilizedcity.service.SysOrgService;
11 13
 import com.example.civilizedcity.service.TaIssueService;
12 14
 import com.example.civilizedcity.service.TaOrgIssueService;
13 15
 import io.swagger.annotations.Api;

+ 4
- 4
src/main/java/com/example/civilizedcity/entity/SysOrg.java 查看文件

@@ -52,10 +52,10 @@ public class SysOrg implements Serializable,Cloneable{
52 52
     /** 创建时间 */
53 53
     @ApiModelProperty(name = "创建时间",notes = "")
54 54
     private LocalDateTime createDate ;
55
-//
56
-//     /** 短信接收者 */
57
-//     @ApiModelProperty(name = "短信接收者",notes = "")
58
-//     private String smsPson ;
55
+
56
+     /** 短信接收者 */
57
+     @ApiModelProperty(name = "短信接收者",notes = "")
58
+     private String smsPson ;
59 59
 
60 60
 
61 61
 }

+ 11
- 0
src/main/java/com/example/civilizedcity/event/MessagEvent.java 查看文件

@@ -2,12 +2,14 @@ package com.example.civilizedcity.event;
2 2
 
3 3
 import com.example.civilizedcity.entity.TaIssue;
4 4
 import com.example.civilizedcity.entity.TaIssueApply;
5
+import com.example.civilizedcity.entity.TaOrgIssue;
5 6
 import org.springframework.context.ApplicationEvent;
6 7
 
7 8
 public class MessagEvent extends ApplicationEvent {
8 9
 
9 10
     private TaIssueApply taIssueApply = null;
10 11
     private TaIssue taIssue = null;
12
+    private TaOrgIssue taOrgIssue = null;
11 13
 
12 14
     public MessagEvent(Object source, TaIssue taIssue) {
13 15
         super(source);
@@ -19,6 +21,11 @@ public class MessagEvent extends ApplicationEvent {
19 21
         this.taIssueApply = taIssueApply;
20 22
     }
21 23
 
24
+    public MessagEvent(Object source, TaOrgIssue taOrgIssue) {
25
+        super(source);
26
+        this.taOrgIssue = taOrgIssue;
27
+    }
28
+
22 29
     public TaIssue getTaIssue() {
23 30
         return taIssue;
24 31
     }
@@ -26,4 +33,8 @@ public class MessagEvent extends ApplicationEvent {
26 33
     public TaIssueApply getTaIssueApply() {
27 34
         return taIssueApply;
28 35
     }
36
+
37
+    public TaOrgIssue getTaOrgIssue() {
38
+        return taOrgIssue;
39
+    }
29 40
 }

+ 4
- 0
src/main/java/com/example/civilizedcity/event/listener/MessagEventListener.java 查看文件

@@ -20,5 +20,9 @@ public class MessagEventListener implements ApplicationListener<MessagEvent> {
20 20
         if (null != event.getTaIssueApply()) {
21 21
             taMessageService.sendMessage(event.getTaIssueApply());
22 22
         }
23
+
24
+        if (null != event.getTaOrgIssue()) {
25
+            taMessageService.sendMessage(event.getTaOrgIssue());
26
+        }
23 27
     }
24 28
 }

+ 5
- 0
src/main/java/com/example/civilizedcity/message/IMessage.java 查看文件

@@ -0,0 +1,5 @@
1
+package com.example.civilizedcity.message;
2
+
3
+public interface IMessage {
4
+    void send(String toUser, String code, String message) throws Exception;
5
+}

+ 120
- 0
src/main/java/com/example/civilizedcity/message/impl/SMS.java 查看文件

@@ -0,0 +1,120 @@
1
+package com.example.civilizedcity.message.impl;
2
+
3
+import com.example.civilizedcity.message.IMessage;
4
+import lombok.extern.slf4j.Slf4j;
5
+import org.springframework.beans.factory.annotation.Value;
6
+import org.springframework.stereotype.Component;
7
+
8
+import java.io.BufferedReader;
9
+import java.io.InputStream;
10
+import java.io.InputStreamReader;
11
+import java.net.HttpURLConnection;
12
+import java.net.URL;
13
+import java.security.MessageDigest;
14
+import java.security.NoSuchAlgorithmException;
15
+
16
+@Slf4j
17
+@Component
18
+public class SMS implements IMessage {
19
+
20
+    @Value("${message.sms.enable}")
21
+    boolean enable;
22
+
23
+    @Value("${message.sms.httpUrl}")
24
+    String httpUrl;
25
+
26
+    @Value("${message.sms.userName}")
27
+    String userName;
28
+
29
+    @Value("${message.sms.password}")
30
+    String password;
31
+
32
+    @Value("${message.sms.template}")
33
+    String template;
34
+
35
+    @Override
36
+    public void send(String toUser, String code, String message) throws Exception {
37
+        if (!enable) return;
38
+
39
+        String content = String.format(template, message);
40
+
41
+        String searchStr = "u=" + userName;
42
+        searchStr += "&p=" + md5(password);
43
+        searchStr += "&m=" + toUser;
44
+        searchStr += "&c=" + encodeUrlString(content, "UTF-8");
45
+
46
+        String resp = request(httpUrl, searchStr);
47
+        log.debug(String.format("Send sms to %s , that result is : [ %s ]", toUser, resp));
48
+
49
+        if (!"0".equals(resp)) {
50
+            SMSError error = SMSError.getEnum(resp);
51
+            String errMsg = null == error ? "未知错误" : error.getMessage();
52
+            String errorMessage = String.format("Send sms to %s , that result is : [ %s ]: %s", toUser, resp, errMsg);
53
+            throw new Exception(errorMessage);
54
+        }
55
+    }
56
+
57
+    private String request(String httpUrl, String httpArg) {
58
+        BufferedReader reader = null;
59
+        String result = null;
60
+        StringBuffer sbf = new StringBuffer();
61
+        httpUrl = httpUrl + "?" + httpArg;
62
+
63
+        try {
64
+            URL url = new URL(httpUrl);
65
+            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
66
+            connection.setRequestMethod("GET");
67
+            connection.connect();
68
+            InputStream is = connection.getInputStream();
69
+            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
70
+            String strRead = reader.readLine();
71
+            if (strRead != null) {
72
+                sbf.append(strRead);
73
+                while ((strRead = reader.readLine()) != null) {
74
+                    sbf.append("\n");
75
+                    sbf.append(strRead);
76
+                }
77
+            }
78
+            reader.close();
79
+            result = sbf.toString();
80
+        } catch (Exception e) {
81
+            e.printStackTrace();
82
+        }
83
+        return result;
84
+    }
85
+
86
+    private String md5(String plainText) {
87
+        StringBuffer buf = null;
88
+        try {
89
+            MessageDigest md = MessageDigest.getInstance("MD5");
90
+            md.update(plainText.getBytes());
91
+            byte b[] = md.digest();
92
+            int i;
93
+            buf = new StringBuffer("");
94
+            for (int offset = 0; offset < b.length; offset++) {
95
+                i = b[offset];
96
+                if (i < 0)
97
+                    i += 256;
98
+                if (i < 16)
99
+                    buf.append("0");
100
+                buf.append(Integer.toHexString(i));
101
+            }
102
+        } catch (NoSuchAlgorithmException e) {
103
+            e.printStackTrace();
104
+        }
105
+        return buf.toString();
106
+    }
107
+
108
+    private String encodeUrlString(String str, String charset) {
109
+        String strret = null;
110
+        if (str == null)
111
+            return str;
112
+        try {
113
+            strret = java.net.URLEncoder.encode(str, charset);
114
+        } catch (Exception e) {
115
+            e.printStackTrace();
116
+            return null;
117
+        }
118
+        return strret;
119
+    }
120
+}

+ 36
- 0
src/main/java/com/example/civilizedcity/message/impl/SMSError.java 查看文件

@@ -0,0 +1,36 @@
1
+package com.example.civilizedcity.message.impl;
2
+
3
+public enum SMSError {
4
+    SUCCESS("0", "成功"),
5
+    ERROR_PASSWORD("30", "错误密码"),
6
+    ERROR_ACCOUNT("40", "账号不存在"),
7
+    ERROR_BALANCE("41", "余额不足"),
8
+    ERROR_IP("43", "IP地址限制"),
9
+    ERROR_CONTENT("50", "内容含有敏感词"),
10
+    ERROR_PHONE("51", "手机号码不正确");
11
+
12
+    private String code;
13
+    private String message;
14
+
15
+    private SMSError(String code, String message) {
16
+        this.code = code;
17
+        this.message = message;
18
+    }
19
+
20
+    public String getCode() {
21
+        return code;
22
+    }
23
+
24
+    public String getMessage() {
25
+        return message;
26
+    }
27
+
28
+    public static SMSError getEnum(String code) {
29
+        for (SMSError smsError : SMSError.values()) {
30
+            if (smsError.getCode().equals(code)) {
31
+                return smsError;
32
+            }
33
+        }
34
+        return null;
35
+    }
36
+}

+ 4
- 1
src/main/java/com/example/civilizedcity/service/TaMessageService.java 查看文件

@@ -4,8 +4,9 @@ import com.baomidou.mybatisplus.extension.service.IService;
4 4
 import com.example.civilizedcity.entity.TaIssue;
5 5
 import com.example.civilizedcity.entity.TaIssueApply;
6 6
 import com.example.civilizedcity.entity.TaMessage;
7
+import com.example.civilizedcity.entity.TaOrgIssue;
7 8
 
8
- /**
9
+/**
9 10
  * 消息通知;(ta_message)表服务接口
10 11
  * @author : http://njyunzhi.com
11 12
  * @date : 2023-1-2
@@ -15,4 +16,6 @@ public interface TaMessageService extends IBaseService<TaMessage> {
15 16
      void sendMessage(TaIssue taIssue);
16 17
 
17 18
      void sendMessage(TaIssueApply taIssueApply);
19
+
20
+     void sendMessage(TaOrgIssue taOrgIssue);
18 21
  }

+ 39
- 3
src/main/java/com/example/civilizedcity/service/impl/TaMessageServiceImpl.java 查看文件

@@ -2,19 +2,21 @@ package com.example.civilizedcity.service.impl;
2 2
 
3 3
 import com.example.civilizedcity.common.Constants;
4 4
 import com.example.civilizedcity.common.StringUtils;
5
-import com.example.civilizedcity.entity.TaIssue;
6
-import com.example.civilizedcity.entity.TaIssueApply;
5
+import com.example.civilizedcity.entity.*;
6
+import com.example.civilizedcity.mapper.SysOrgMapper;
7
+import com.example.civilizedcity.mapper.SysUserMapper;
8
+import com.example.civilizedcity.message.impl.SMS;
7 9
 import org.apache.ibatis.annotations.Param;
8 10
 import org.springframework.beans.factory.annotation.Autowired;
9 11
 import org.springframework.scheduling.annotation.Async;
10 12
 import org.springframework.scheduling.annotation.EnableAsync;
11 13
 import org.springframework.stereotype.Service;
12
-import com.example.civilizedcity.entity.TaMessage;
13 14
 import com.example.civilizedcity.mapper.TaMessageMapper;
14 15
 import com.example.civilizedcity.service.TaMessageService;
15 16
 import org.springframework.transaction.annotation.Transactional;
16 17
 
17 18
 import java.time.LocalDateTime;
19
+import java.util.List;
18 20
 
19 21
 /**
20 22
  * 消息通知;(ta_message)表服务实现类
@@ -25,6 +27,15 @@ import java.time.LocalDateTime;
25 27
 @Service
26 28
 public class TaMessageServiceImpl extends BaseServiceImpl<TaMessageMapper, TaMessage> implements TaMessageService {
27 29
 
30
+    @Autowired
31
+    SysOrgMapper sysOrgMapper;
32
+
33
+    @Autowired
34
+    SysUserMapper sysUserMapper;
35
+
36
+    @Autowired
37
+    SMS sms;
38
+
28 39
     /**
29 40
      * 平台流程
30 41
      * @param taIssue
@@ -140,4 +151,29 @@ public class TaMessageServiceImpl extends BaseServiceImpl<TaMessageMapper, TaMes
140 151
 
141 152
         save(taMessage);
142 153
     }
154
+
155
+    @Async
156
+    @Override
157
+    public void sendMessage(TaOrgIssue taOrgIssue) {
158
+        SysOrg sysOrg = sysOrgMapper.selectById(taOrgIssue.getOrgId());
159
+        if (null == sysOrg || StringUtils.isEmpty(sysOrg.getSmsPson())) {
160
+            return ;
161
+        }
162
+
163
+        String[] smsPsons = sysOrg.getSmsPson().split(",");
164
+        for (String p : smsPsons) {
165
+            List<SysUser> userList = sysUserMapper.getListByPhone(p);
166
+            if (null == userList || userList.size() == 0) {
167
+                continue;
168
+            }
169
+
170
+            SysUser sysUser = userList.get(0);
171
+
172
+            try {
173
+                sms.send(sysUser.getPhone(), null, sysUser.getName());
174
+            } catch (Exception e) {
175
+                log.error("发送短信失败", e);
176
+            }
177
+        }
178
+    }
143 179
 }

+ 7
- 0
src/main/java/com/example/civilizedcity/service/impl/TaOrgIssueServiceImpl.java 查看文件

@@ -4,8 +4,10 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
4 4
 import com.example.civilizedcity.common.Constants;
5 5
 import com.example.civilizedcity.common.StringUtils;
6 6
 import com.example.civilizedcity.entity.*;
7
+import com.example.civilizedcity.event.MessagEvent;
7 8
 import com.example.civilizedcity.mapper.*;
8 9
 import org.springframework.beans.factory.annotation.Autowired;
10
+import org.springframework.context.ApplicationEventPublisher;
9 11
 import org.springframework.stereotype.Service;
10 12
 import com.example.civilizedcity.service.TaOrgIssueService;
11 13
 import org.springframework.transaction.annotation.Transactional;
@@ -38,6 +40,8 @@ public class TaOrgIssueServiceImpl extends BaseServiceImpl<TaOrgIssueMapper, TaO
38 40
     @Autowired
39 41
     TaAttachMapper taAttachMapper;
40 42
 
43
+    @Autowired
44
+    ApplicationEventPublisher applicationEventPublisher;
41 45
 
42 46
     private void transParams(Map<String, Object> params) {
43 47
         params.put("processNode", null);
@@ -104,6 +108,9 @@ public class TaOrgIssueServiceImpl extends BaseServiceImpl<TaOrgIssueMapper, TaO
104 108
         taIssueProcess.setCreateDate(LocalDateTime.now());
105 109
         taIssueProcess.setStatus(Constants.STATUS_NORMAL);
106 110
         taIssueProcessMapper.insert(taIssueProcess);
111
+
112
+        // 发送短信通知
113
+        applicationEventPublisher.publishEvent(new MessagEvent(this, taOrgIssue));
107 114
     }
108 115
 
109 116
     @Override

+ 9
- 0
src/main/resources/application.yml 查看文件

@@ -14,6 +14,15 @@ spring:
14 14
   profiles:
15 15
     active: @profileActive@
16 16
 
17
+### 仅 h5 版本用到
18
+message:
19
+  sms:
20
+    enable: true
21
+    httpUrl: http://api.smsbao.com/sms
22
+    userName: pzhgd
23
+    password: PZHdt@147293ac
24
+    template: 【文明创城办】%s,您有一条工作待办请前往{XXXAPP}及时处理
25
+
17 26
 ###
18 27
 sms:
19 28
   captcha: