Procházet zdrojové kódy

feats: add wx subcribe message

张延森 před 5 roky
rodič
revize
49558a0765

+ 118
- 35
src/main/java/com/huiju/estateagents/service/impl/MiniAppServiceImpl.java Zobrazit soubor

@@ -76,57 +76,95 @@ 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
-        List<WxMaTemplateData> data = new ArrayList<>();
80
-        data.add(new WxMaTemplateData("keyword1", name, "#fffff"));
81
-        data.add(new WxMaTemplateData("keyword2", phone, "#fffff"));
82
-        data.add(new WxMaTemplateData("keyword3", content, "#fffff"));
83
-        data.add(new WxMaTemplateData("keyword4", DateUtils.cutSecond(dt), "#fffff"));
84
-
85
-        sendMessage(CommConstant.MINIAPP_TPL_NOTICE, toUser, link, data);
79
+        List<String> data = new ArrayList<String>(){{
80
+            add(name);
81
+            add(phone);
82
+            add(content);
83
+            add(DateUtils.cutSecond(dt));
84
+        }};
85
+
86
+        ResponseBean res = sendSubscribeMessage(CommConstant.MINIAPP_TPL_NOTICE, toUser, link, data);
87
+        if (res.getCode() != ResponseBean.CODE_SUCCESS) {
88
+            // TODO
89
+            // 使用短信发送
90
+        }
86 91
     }
87 92
     
88 93
     @Override
89 94
     public void sendHelpMessage(TaPerson toUser, String link, String result, String activityName, LocalDateTime dt) {
90
-        List<WxMaTemplateData> data = new ArrayList<>();
91
-        data.add(new WxMaTemplateData("keyword1", result, "#fffff"));
92
-        data.add(new WxMaTemplateData("keyword2", activityName, "#fffff"));
93
-        data.add(new WxMaTemplateData("keyword3", DateUtils.cutSecond(dt), "#fffff"));
94
-        
95
-        sendMessage(CommConstant.MINIAPP_TPL_HELP, toUser, link, data);
95
+//        List<WxMaSubscribeData> data = new ArrayList<>();
96
+//        data.add(new WxMaSubscribeData("keyword1", result, "#fffff"));
97
+//        data.add(new WxMaSubscribeData("keyword2", activityName, "#fffff"));
98
+//        data.add(new WxMaSubscribeData("keyword3", DateUtils.cutSecond(dt), "#fffff"));
99
+
100
+        // WARN: break change
101
+        List<String> data = new ArrayList<String>(){{
102
+            add(activityName);
103
+            add(result);
104
+            add(DateUtils.cutSecond(dt));
105
+        }};
106
+
107
+        ResponseBean res = sendSubscribeMessage(CommConstant.MINIAPP_TPL_HELP, toUser, link, data);
108
+        if (res.getCode() != ResponseBean.CODE_SUCCESS) {
109
+            // TODO
110
+            // 使用短信发送
111
+        }
96 112
     }
97 113
     
98 114
     @Override
99 115
     public void sendGroupMessage(TaPerson toUser, String link, String result, String activityName, LocalDateTime dt) {
100
-        List<WxMaTemplateData> data = new ArrayList<>();
101
-        data.add(new WxMaTemplateData("keyword1", result, "#fffff"));
102
-        data.add(new WxMaTemplateData("keyword2", activityName, "#fffff"));
103
-        data.add(new WxMaTemplateData("keyword3", DateUtils.cutSecond(dt), "#fffff"));
104
-        
105
-        sendMessage(CommConstant.MINIAPP_TPL_GROUP, toUser, link, data);
116
+//        List<WxMaSubscribeData> data = new ArrayList<>();
117
+//        data.add(new WxMaSubscribeData("keyword1", result, "#fffff"));
118
+//        data.add(new WxMaSubscribeData("keyword2", activityName, "#fffff"));
119
+//        data.add(new WxMaSubscribeData("keyword3", DateUtils.cutSecond(dt), "#fffff"));
120
+
121
+        // WARN: break change
122
+        List<String> data = new ArrayList<String>(){{
123
+            add(activityName);
124
+            add(result);
125
+            add(DateUtils.cutSecond(dt));
126
+        }};
127
+
128
+
129
+        ResponseBean res = sendSubscribeMessage(CommConstant.MINIAPP_TPL_GROUP, toUser, link, data);
130
+        if (res.getCode() != ResponseBean.CODE_SUCCESS) {
131
+            // TODO
132
+            // 使用短信发送
133
+        }
106 134
     }
107 135
 
108 136
     @Override
109 137
     public void sendMainBizMessage(TaPerson toUser, String link, String custName, String phone, String sex, String fromName, String bizType, String content) {
110
-        List<WxMaTemplateData> data = new ArrayList<>();
111
-        data.add(new WxMaTemplateData("keyword1", custName, "#fffff"));
112
-        data.add(new WxMaTemplateData("keyword2", StringUtils.ifNull(phone, "未知"), "#fffff"));
113
-        data.add(new WxMaTemplateData("keyword3",StringUtils.ifNull(sex, "未知"), "#fffff"));
114
-        data.add(new WxMaTemplateData("keyword4", fromName, "#fffff"));
115
-        data.add(new WxMaTemplateData("keyword5", bizType, "#fffff"));
116
-        data.add(new WxMaTemplateData("keyword6", content, "#fffff"));
117
-
118
-        sendMessage("mainbiz", toUser, link, data);
138
+        List<String> data = new ArrayList<String>(){{
139
+            add(custName);
140
+            add(StringUtils.ifNull(phone, "未知"));
141
+            add(StringUtils.ifNull(sex, "未知"));
142
+            add(fromName);
143
+            add(bizType);
144
+            add(content);
145
+        }};
146
+
147
+        ResponseBean res = sendSubscribeMessage("mainbiz", toUser, link, data);
148
+        if (res.getCode() != ResponseBean.CODE_SUCCESS) {
149
+            // TODO
150
+            // 使用短信发送
151
+        }
119 152
     }
120 153
 
121 154
     @Override
122 155
     public void sendNewCustomerMessage(TaPerson toUser, String link, String custName, String phone, String sex, String fromName) {
123
-        List<WxMaTemplateData> data = new ArrayList<>();
124
-        data.add(new WxMaTemplateData("keyword1", custName, "#fffff"));
125
-        data.add(new WxMaTemplateData("keyword2", StringUtils.ifNull(phone, "未知"), "#fffff"));
126
-        data.add(new WxMaTemplateData("keyword3",StringUtils.ifNull(sex, "未知"), "#fffff"));
127
-        data.add(new WxMaTemplateData("keyword4", fromName, "#fffff"));
128
-
129
-        sendMessage("newCustomer", toUser, link, data);
156
+        List<String> data = new ArrayList<String>(){{
157
+            add(custName);
158
+            add(StringUtils.ifNull(phone, "未知"));
159
+            add(StringUtils.ifNull(sex, "未知"));
160
+            add(fromName);
161
+        }};
162
+
163
+        ResponseBean res = sendSubscribeMessage("newCustomer", toUser, link, data);
164
+        if (res.getCode() != ResponseBean.CODE_SUCCESS) {
165
+            // TODO
166
+            // 使用短信发送
167
+        }
130 168
     }
131 169
 
132 170
     @Override
@@ -220,6 +258,7 @@ public class MiniAppServiceImpl implements IMiniAppService {
220 258
         return taMiniappList;
221 259
     }
222 260
 
261
+    @Deprecated
223 262
     private void sendMessage(String tplName, TaPerson toUser, String link, List<WxMaTemplateData> data) {
224 263
         String appid = toUser.getMiniappId();
225 264
         if (StringUtils.isEmpty(appid)) {
@@ -250,6 +289,50 @@ public class MiniAppServiceImpl implements IMiniAppService {
250 289
         }
251 290
     }
252 291
 
292
+    private ResponseBean sendSubscribeMessage(String tplName, TaPerson toUser, String link, List<String> data) {
293
+        String appid = toUser.getMiniappId();
294
+        if (StringUtils.isEmpty(appid)) {
295
+            log.error("人员 {} 无对应来源小程序信息", toUser);
296
+            return ResponseBean.error("消息接收人无 appid 信息", ResponseBean.ERROR_ILLEGAL_PARAMS);
297
+        }
298
+
299
+        TaMiniappTemplate tpl = getTplByType(tplName, toUser.getMiniappId());
300
+        if (null == tpl) {
301
+            log.error("模板 {} 不存在", tplName);
302
+            return ResponseBean.error("消息模板不存在", ResponseBean.ERROR_ILLEGAL_PARAMS);
303
+        }
304
+
305
+        List<WxMaSubscribeData> sendData = createMessageData(data, tpl.getFieldNum());
306
+
307
+        WxMaSubscribeMessage message = WxMaSubscribeMessage.builder()
308
+                .templateId(tpl.getTplId())
309
+                .data(sendData)
310
+                .page(StringUtils.ifNull(link, CommConstant.MINIAPP_INDEX))
311
+                .toUser(toUser.getMiniOpenid())
312
+                .build();
313
+
314
+        try {
315
+            WxMaService wxService = wxUtils.getMiniApp(appid);
316
+            wxService.getMsgService().sendSubscribeMsg(message);
317
+
318
+            return ResponseBean.success("消息发送成功");
319
+        } catch (WxErrorException e) {
320
+            e.printStackTrace();
321
+            return ResponseBean.error(e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
322
+        }
323
+    }
324
+
325
+    private List<WxMaSubscribeData> createMessageData(List<String> list, Integer count) {
326
+        List<WxMaSubscribeData> data = new ArrayList<>();
327
+        if (list == null || list.size() == 0 || count == null || count == 0) return data;
328
+
329
+        int length = count > list.size() ? list.size() : count;
330
+        for (int i = 0; i < length; i++) {
331
+            data.add(new WxMaSubscribeData("keyword"+ (i+1), list.get(i), "#fffff"));
332
+        }
333
+        return data;
334
+    }
335
+
253 336
     private TaMiniappTemplate getTplByType(String type, String appid) {
254 337
         TaMiniappTemplate tpl = taMiniappTemplateMapper.selectTplByType(type, appid);
255 338
         return tpl;