Browse Source

安装发送卡片消息

seyason 2 years ago
parent
commit
a7b3494955

+ 42 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/controller/WeiXinCorpController.java

@@ -549,6 +549,7 @@ public class WeiXinCorpController {
                             }
                             }
                             userMapper.updateById(userUpdate);
                             userMapper.updateById(userUpdate);
                             System.out.println("======关注时更新人员======");
                             System.out.println("======关注时更新人员======");
+                            sendCardMsg(corpWxUserId);
                         }).start();
                         }).start();
                     }
                     }
                 } else if ("enter_agent".equals(event)) {
                 } else if ("enter_agent".equals(event)) {
@@ -673,6 +674,7 @@ public class WeiXinCorpController {
                                 }
                                 }
                                 userMapper.updateById(userUpdate);
                                 userMapper.updateById(userUpdate);
                                 System.out.println("======更新人员======");
                                 System.out.println("======更新人员======");
+                                sendCardMsg(authUserId);
                             }
                             }
                         }
                         }
                     }
                     }
@@ -1508,6 +1510,7 @@ public class WeiXinCorpController {
                     }else {
                     }else {
                         System.out.println("未查询到该新建公司有支付订单");
                         System.out.println("未查询到该新建公司有支付订单");
                     }
                     }
+                    sendCardMsg(openUserId);
                 } else {
                 } else {
                     //企业已经存在
                     //企业已经存在
                     Integer companyId = data.getCompanyId();
                     Integer companyId = data.getCompanyId();
@@ -1570,6 +1573,7 @@ public class WeiXinCorpController {
                                 .setJobNumber(openUserId.equals(userId)?null:userId);//工号就是企业微信的用户ID
                                 .setJobNumber(openUserId.equals(userId)?null:userId);//工号就是企业微信的用户ID
                         userMapper.insert(user);
                         userMapper.insert(user);
                     }
                     }
+                    sendCardMsg(openUserId);
                 }
                 }
             }
             }
         }
         }
@@ -3603,4 +3607,42 @@ public class WeiXinCorpController {
             }
             }
         }
         }
     }
     }
+
+    @RequestMapping("/sendCardMsg")
+    public HttpRespMsg sendCardMsg(String corpwxOpenId) {
+        User user = userMapper.selectOne(new QueryWrapper<User>().eq("corpwx_userid", corpwxOpenId));
+        WxCorpInfo wxCorpInfo = wxCorpInfoMapper.selectOne(new QueryWrapper<WxCorpInfo>().eq("company_id", user.getCompanyId()));
+        JSONObject data = new JSONObject();
+        data.put("card_type", "news_notice");
+        JSONObject mainTitle = new JSONObject();
+        mainTitle.put("title", "工时管家使用指南");
+        mainTitle.put("desc", "欢迎使用工时管家,点击查看工时管家使用指南");
+        data.put("main_title", mainTitle);
+        JSONObject cardImg = new JSONObject();
+        cardImg.put("url", "http://www.ttkuaiban.com/card_img.png");
+        cardImg.put("aspect_ratio", 2.23);
+        data.put("card_image", cardImg);
+        JSONArray array = new JSONArray();
+        JSONObject docItem = new JSONObject();
+        docItem.put("type", 1);
+        docItem.put("title", "使用指南");
+        docItem.put("url", "https://doc.weixin.qq.com/doc/w3_AQAACQauAEA1Ucywi0bQGy7XLaYYE?scode=AMsA1AfkAAgde2tFhrAFIAdQbyAPU");
+        array.add(docItem);
+        JSONObject enterItem = new JSONObject();
+        enterItem.put("type", 1);
+        enterItem.put("title", "进入应用");
+        enterItem.put("url", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww4e237fd6abb635af&redirect_uri=http://worktime.ttkuaiban.com/api/corpWXAuth&response_type=code&scope=snsapi_base&state=0#wechat_redirect");
+        array.add(enterItem);
+        data.put("jump_list", array);
+        JSONObject cardAction = new JSONObject();
+        cardAction.put("type", 1);
+        cardAction.put("url", "https://doc.weixin.qq.com/doc/w3_AQAACQauAEA1Ucywi0bQGy7XLaYYE?scode=AMsA1AfkAAgde2tFhrAFIAdQbyAPU");
+        data.put("card_action", cardAction);
+        if (!isDev) {
+            //正式环境下开启重复检查
+            data.put("enable_duplicate_check", 1);
+        }
+        wxCorpInfoService.sendWXCorpTemplateCardMsg(wxCorpInfo, corpwxOpenId, data);
+        return new HttpRespMsg();
+    }
 }
 }

+ 2 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/WxCorpInfoService.java

@@ -24,6 +24,8 @@ public interface WxCorpInfoService extends IService<WxCorpInfo> {
 
 
     public void sendWXCorpMsg(WxCorpInfo corpInfo, String corpUserid, String msg);
     public void sendWXCorpMsg(WxCorpInfo corpInfo, String corpUserid, String msg);
 
 
+    public void sendWXCorpTemplateCardMsg(WxCorpInfo corpInfo, String corpUserid, JSONObject msg);
+
     public void sendWXCorpTemplateMsg(WxCorpInfo corpInfo, String corpUserid, JSONObject msg);
     public void sendWXCorpTemplateMsg(WxCorpInfo corpInfo, String corpUserid, JSONObject msg);
 
 
     public String downloadFile(WxCorpInfo corpInfo, String mediaId);
     public String downloadFile(WxCorpInfo corpInfo, String mediaId);

+ 35 - 0
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/service/impl/WxCorpInfoServiceImpl.java

@@ -278,6 +278,41 @@ public class WxCorpInfoServiceImpl extends ServiceImpl<WxCorpInfoMapper, WxCorpI
 
 
     }
     }
 
 
+    @Override
+    public void sendWXCorpTemplateCardMsg(WxCorpInfo corpInfo, String corpUserid, JSONObject msg) {
+        try {
+//            if (isDev) return;
+            String accessToken = getCorpAccessToken(corpInfo);
+            String url=URL_SEND_WXCORP_MSG.replaceAll("ACCESS_TOKEN", accessToken);
+            HttpHeaders headers = new HttpHeaders();
+            headers.setContentType(MediaType.APPLICATION_JSON);
+            JSONObject reqParam = new JSONObject();
+            reqParam.put("touser", corpUserid);
+            reqParam.put("msgtype", "template_card");
+            reqParam.put("agentid", corpInfo.getAgentid());
+            reqParam.put("template_card", msg);
+
+            HttpEntity<String> requestEntity = new HttpEntity<String>(reqParam.toJSONString(), headers);
+            ResponseEntity<String> responseEntity = this.restTemplate.exchange(url,
+                    HttpMethod.POST, requestEntity, String.class);
+            if (responseEntity.getStatusCode() == HttpStatus.OK) {
+                String resp = responseEntity.getBody();
+                JSONObject json = JSONObject.parseObject(resp);
+                if (json.getIntValue("errcode") == 0) {
+                    //发送成功
+
+                } else {
+                    System.out.println("发送企业微信模板消息发生错误==" + resp);
+                    throw new Exception(json.toJSONString());
+                }
+            } else {
+                System.err.println("发送失败:" + responseEntity.getStatusCode() + ", " + responseEntity.getBody());
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
     @Override
     @Override
     public void sendWXCorpTemplateMsg(WxCorpInfo corpInfo, String corpUserid, JSONObject msg) {
     public void sendWXCorpTemplateMsg(WxCorpInfo corpInfo, String corpUserid, JSONObject msg) {
         try {
         try {