瀏覽代碼

add sms message

张延森 5 年之前
父節點
當前提交
8b5e08810f

+ 2
- 2
src/main/java/com/huiju/estateagents/common/SMSUtils.java 查看文件

@@ -94,7 +94,7 @@ public class SMSUtils {
94 94
         return String.join("", nums);
95 95
     }
96 96
 
97
-    private boolean sendMessage(String code, String tel, String[] params) {
97
+    public boolean sendMessage(String code, String tel, String[] params) {
98 98
         // 发送内容
99 99
         SMSProperties.Template message = new SMSProperties.Template();
100 100
         message.setCode(code);
@@ -118,7 +118,7 @@ public class SMSUtils {
118 118
         );
119 119
 
120 120
         if (((ResponseEntity<String>) response).getStatusCodeValue() != HttpStatus.OK.value()) {
121
-            log.error("请求发送短信验证码失败: {}", response.toString());
121
+            log.error("请求发送短信失败: {}", response.toString());
122 122
             return false;
123 123
         }
124 124
 

+ 56
- 0
src/main/java/com/huiju/estateagents/entity/SysSmsSetting.java 查看文件

@@ -0,0 +1,56 @@
1
+package com.huiju.estateagents.entity;
2
+
3
+import com.baomidou.mybatisplus.annotation.IdType;
4
+import com.baomidou.mybatisplus.annotation.TableId;
5
+import lombok.Data;
6
+import lombok.EqualsAndHashCode;
7
+import lombok.experimental.Accessors;
8
+
9
+import java.io.Serializable;
10
+
11
+/**
12
+ * <p>
13
+ * 短信消息模板
14
+ * </p>
15
+ *
16
+ * @author jobob
17
+ * @since 2019-09-11
18
+ */
19
+@Data
20
+@EqualsAndHashCode(callSuper = false)
21
+@Accessors(chain = true)
22
+public class SysSmsSetting implements Serializable {
23
+
24
+    private static final long serialVersionUID = 1L;
25
+
26
+    /**
27
+     * 序号
28
+     */
29
+    @TableId(value = "serial_no", type = IdType.AUTO)
30
+    private Integer serialNo;
31
+
32
+    /**
33
+     * 模板名称
34
+     */
35
+    private String smsName;
36
+
37
+    /**
38
+     * 模板类型
39
+     */
40
+    private String smsType;
41
+
42
+    /**
43
+     * 模板编号
44
+     */
45
+    private String tplCode;
46
+
47
+    /**
48
+     * 模板签名
49
+     */
50
+    private String tplSign;
51
+
52
+    /**
53
+     * 状态
54
+     */
55
+    private Integer status;
56
+}

+ 19
- 0
src/main/java/com/huiju/estateagents/mapper/SysSmsSettingMapper.java 查看文件

@@ -0,0 +1,19 @@
1
+package com.huiju.estateagents.mapper;
2
+
3
+
4
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5
+import com.huiju.estateagents.entity.SysSmsSetting;
6
+import org.apache.ibatis.annotations.Mapper;
7
+
8
+/**
9
+ * <p>
10
+ * 项目参数配置表  Mapper 接口
11
+ * </p>
12
+ *
13
+ * @author jobob
14
+ * @since 2019-08-01
15
+ */
16
+@Mapper
17
+public interface SysSmsSettingMapper extends BaseMapper<SysSmsSetting> {
18
+
19
+}

+ 5
- 5
src/main/java/com/huiju/estateagents/service/ISysProjectParamsService.java 查看文件

@@ -7,11 +7,11 @@ import com.huiju.estateagents.entity.SysProjectParams;
7 7
 /**
8 8
  * <p>
9 9
  * 项目参数配置表  服务类
10
- * </p>
11
- *
12
- * @author jobob
13
- * @since 2019-08-01
14
- */
10
+         * </p>
11
+        *
12
+        * @author jobob
13
+        * @since 2019-08-01
14
+        */
15 15
 public interface ISysProjectParamsService extends IService<SysProjectParams> {
16 16
 
17 17
 }

+ 17
- 0
src/main/java/com/huiju/estateagents/service/ISysSmsSettingService.java 查看文件

@@ -0,0 +1,17 @@
1
+package com.huiju.estateagents.service;
2
+
3
+
4
+import com.baomidou.mybatisplus.extension.service.IService;
5
+import com.huiju.estateagents.entity.SysSmsSetting;
6
+
7
+/**
8
+ * <p>
9
+ * 短信消息配置表  服务类
10
+         * </p>
11
+        *
12
+        * @author jobob
13
+        * @since 2019-08-01
14
+        */
15
+public interface ISysSmsSettingService extends IService<SysSmsSetting> {
16
+
17
+}

+ 54
- 14
src/main/java/com/huiju/estateagents/service/impl/MiniAppServiceImpl.java 查看文件

@@ -6,14 +6,8 @@ import com.alibaba.fastjson.JSONObject;
6 6
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
7 7
 import com.huiju.estateagents.base.ResponseBean;
8 8
 import com.huiju.estateagents.common.*;
9
-import com.huiju.estateagents.entity.TaMiniapp;
10
-import com.huiju.estateagents.entity.TaMiniappQrcode;
11
-import com.huiju.estateagents.entity.TaMiniappTemplate;
12
-import com.huiju.estateagents.entity.TaPerson;
13
-import com.huiju.estateagents.mapper.TaMiniFormidsMapper;
14
-import com.huiju.estateagents.mapper.TaMiniappMapper;
15
-import com.huiju.estateagents.mapper.TaMiniappQrcodeMapper;
16
-import com.huiju.estateagents.mapper.TaMiniappTemplateMapper;
9
+import com.huiju.estateagents.entity.*;
10
+import com.huiju.estateagents.mapper.*;
17 11
 import com.huiju.estateagents.service.IMiniAppService;
18 12
 import com.huiju.estateagents.service.ITaMiniFormidsService;
19 13
 import lombok.extern.slf4j.Slf4j;
@@ -45,9 +39,15 @@ public class MiniAppServiceImpl implements IMiniAppService {
45 39
     @Autowired
46 40
     TaMiniappTemplateMapper taMiniappTemplateMapper;
47 41
 
42
+    @Autowired
43
+    SysSmsSettingMapper sysSmsSettingMapper;
44
+
48 45
     @Autowired
49 46
     WxUtils wxUtils;
50 47
 
48
+    @Autowired
49
+    SMSUtils smsUtils;
50
+
51 51
     @Override
52 52
     public WxMaJscode2SessionResult getSessionInfo(String code, String appid) throws WxErrorException {
53 53
         WxMaService wxService = wxUtils.getMiniApp(appid);
@@ -76,20 +76,27 @@ public class MiniAppServiceImpl implements IMiniAppService {
76 76
 
77 77
     @Override
78 78
     public void sendNoticeMessage(TaPerson toUser, String link, String name, String phone, String content, LocalDateTime dt) {
79
+        TaMiniapp miniapp = taMiniappMapper.selectById(toUser.getMiniappId());
80
+        if (null == miniapp) {
81
+            log.error("小程序 {} 不存在", toUser.getMiniappId());
82
+            return ;
83
+        }
84
+        String[] smsParams = { name, miniapp.getName() + "小程序" };
85
+
79 86
         TaMiniappTemplate tpl = getTplByType(CommConstant.MINIAPP_TPL_NOTICE, toUser.getMiniappId());
80 87
         if (null == tpl) {
81 88
             log.error("小程序 {} 无通知消息模板 {}", toUser.getMiniappId(), CommConstant.MINIAPP_TPL_NOTICE);
82 89
 
83
-            // TODO
84 90
             // 短信发送
91
+            sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_NOTICE, smsParams);
85 92
         };
86 93
 
87 94
         if (((Boolean) true).equals(tpl.getIsSubscribe())) {
88 95
             String[] data = new String[] { name, content, DateUtils.cutSecond(dt) };
89 96
             ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
90 97
             if (res.getCode() != ResponseBean.CODE_SUCCESS) {
91
-                // TODO
92 98
                 // 使用短信发送
99
+                sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_NOTICE, smsParams);
93 100
             }
94 101
         } else {
95 102
             List<WxMaTemplateData> data = new ArrayList<>();
@@ -105,20 +112,27 @@ public class MiniAppServiceImpl implements IMiniAppService {
105 112
     
106 113
     @Override
107 114
     public void sendHelpMessage(TaPerson toUser, String link, String result, String activityName, LocalDateTime dt) {
115
+        TaMiniapp miniapp = taMiniappMapper.selectById(toUser.getMiniappId());
116
+        if (null == miniapp) {
117
+            log.error("小程序 {} 不存在", toUser.getMiniappId());
118
+            return ;
119
+        }
120
+        String[] smsParams = { miniapp.getName() + "小程序-" + activityName };
121
+
108 122
         TaMiniappTemplate tpl = getTplByType(CommConstant.MINIAPP_TPL_HELP, toUser.getMiniappId());
109 123
         if (null == tpl) {
110 124
             log.error("小程序 {} 无通知消息模板 {}", toUser.getMiniappId(), CommConstant.MINIAPP_TPL_HELP);
111 125
 
112
-            // TODO
113 126
             // 短信发送
127
+            sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_HELP, smsParams);
114 128
         };
115 129
 
116 130
         if (((Boolean) true).equals(tpl.getIsSubscribe())) {
117 131
             String[] data = new String[] { activityName, result };
118 132
             ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
119 133
             if (res.getCode() != ResponseBean.CODE_SUCCESS) {
120
-                // TODO
121 134
                 // 使用短信发送
135
+                sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_HELP, smsParams);
122 136
             }
123 137
         } else {
124 138
             List<WxMaTemplateData> data = new ArrayList<>();
@@ -132,20 +146,27 @@ public class MiniAppServiceImpl implements IMiniAppService {
132 146
 
133 147
     @Override
134 148
     public void sendGroupMessage(TaPerson toUser, String link, String result, String activityName, LocalDateTime dt) {
149
+        TaMiniapp miniapp = taMiniappMapper.selectById(toUser.getMiniappId());
150
+        if (null == miniapp) {
151
+            log.error("小程序 {} 不存在", toUser.getMiniappId());
152
+            return ;
153
+        }
154
+        String[] smsParams = { miniapp.getName() + "小程序-" + activityName };
155
+
135 156
         TaMiniappTemplate tpl = getTplByType(CommConstant.MINIAPP_TPL_GROUP, toUser.getMiniappId());
136 157
         if (null == tpl) {
137 158
             log.error("小程序 {} 无通知消息模板 {}", toUser.getMiniappId(), CommConstant.MINIAPP_TPL_GROUP);
138 159
 
139
-            // TODO
140 160
             // 短信发送
161
+            sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_GROUP, smsParams);
141 162
         };
142 163
 
143 164
         if (((Boolean) true).equals(tpl.getIsSubscribe())) {
144 165
             String[] data = new String[] { activityName, result };
145 166
             ResponseBean res = sendSubscribeMessage(tpl, toUser, link, data);
146 167
             if (res.getCode() != ResponseBean.CODE_SUCCESS) {
147
-                // TODO
148 168
                 // 使用短信发送
169
+                sendSmsMessage(StringUtils.ifNull(toUser.getTel(), toUser.getPhone()), CommConstant.MINIAPP_TPL_GROUP, smsParams);
149 170
             }
150 171
         } else {
151 172
             List<WxMaTemplateData> data = new ArrayList<>();
@@ -374,6 +395,25 @@ public class MiniAppServiceImpl implements IMiniAppService {
374 395
         }
375 396
     }
376 397
 
398
+    private String sendSmsMessage(String tel, String type, String[] params) {
399
+        if (StringUtils.isEmpty(tel) || StringUtils.isEmpty(type)) {
400
+            return "短信发送失败: 没有手机号或者短信模板";
401
+        }
402
+
403
+        QueryWrapper<SysSmsSetting> query = new QueryWrapper<>();
404
+        query.eq("sms_type", type);
405
+        query.eq("status", CommConstant.STATUS_NORMAL);
406
+        query.last("limit 1");
407
+
408
+        SysSmsSetting smsSetting = sysSmsSettingMapper.selectOne(query);
409
+        if (null == smsSetting) {
410
+            return "没有设置有效的【" + type + "】短信模板";
411
+        }
412
+
413
+        boolean success = smsUtils.sendMessage(smsSetting.getTplCode(), tel, params);
414
+        return success ? null : "短信发送失败";
415
+    }
416
+
377 417
     private List<WxMaSubscribeData> createMessageData(String[] list, String fieldsStr) {
378 418
         List<WxMaSubscribeData> data = new ArrayList<>();
379 419
         if (list == null || list.length == 0 || StringUtils.isEmpty(fieldsStr)) return data;

+ 24
- 0
src/main/java/com/huiju/estateagents/service/impl/SysSmsSettingImpl.java 查看文件

@@ -0,0 +1,24 @@
1
+package com.huiju.estateagents.service.impl;
2
+
3
+
4
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
5
+import com.huiju.estateagents.entity.SysProjectParams;
6
+import com.huiju.estateagents.entity.SysSmsSetting;
7
+import com.huiju.estateagents.mapper.SysProjectParamsMapper;
8
+import com.huiju.estateagents.mapper.SysSmsSettingMapper;
9
+import com.huiju.estateagents.service.ISysProjectParamsService;
10
+import com.huiju.estateagents.service.ISysSmsSettingService;
11
+import org.springframework.stereotype.Service;
12
+
13
+/**
14
+ * <p>
15
+ * 项目参数配置表  服务实现类
16
+ * </p>
17
+ *
18
+ * @author jobob
19
+ * @since 2019-08-01
20
+ */
21
+@Service
22
+public class SysSmsSettingImpl extends ServiceImpl<SysSmsSettingMapper, SysSmsSetting> implements ISysSmsSettingService {
23
+
24
+}

+ 5
- 0
src/main/resources/mapper/SysSmsSettingMapper.xml 查看文件

@@ -0,0 +1,5 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3
+<mapper namespace="com.huiju.estateagents.mapper.SysSmsSettingMapper">
4
+
5
+</mapper>