Преглед изворни кода

微信小程序消息推送

liuyao пре 1 година
родитељ
комит
f87885ef86

+ 5 - 0
pom.xml

@@ -116,6 +116,11 @@
             <artifactId>weixin-java-miniapp</artifactId>
             <version>4.4.0</version>
         </dependency>
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.4.3</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 0 - 7
src/main/java/com/dk/mdm/controller/mst/CusFollowController.java

@@ -147,11 +147,4 @@ public class CusFollowController{
         cusFollowService.wxMessageTest(request, response);
     }
 
-    @ApiOperation(value = "小程序消息推送测试", notes = "小程序消息推送测试")
-    @PostMapping("wx_message_push")
-    @ResponseBody
-    public ResponseResultVO<?> wxMessagePush(HttpServletRequest request){
-        return cusFollowService.wxMessagePush(request);
-    }
-
 }

+ 95 - 41
src/main/java/com/dk/mdm/service/mst/CusFollowService.java

@@ -1,5 +1,6 @@
 package com.dk.mdm.service.mst;
 
+import cn.hutool.core.bean.BeanUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
@@ -427,27 +428,39 @@ public class CusFollowService extends BaseService<CusFollow> {
             Customer customer = customerMapper.selectById(follow.getCusId());
             if (customer != null) {
                 String loginType = "4";
-                List<String> list = new ArrayList<>();
+//                List<String> list = new ArrayList<>();
+                Map<String, String> keywordMap = new HashMap<>();
                 if (Constant.BasicDataConstant.FOLLOW_STATUS_4.getValue().equals(follow.getFollowStatus()) ||
-                        Constant.BasicDataConstant.FOLLOW_STATUS_1.getValue().equals(follow.getFollowStatus())) {
-                    list.add("您有客户需要跟进");
+                        Constant.BasicDataConstant.FOLLOW_STATUS_1.getValue().equals(follow.getFollowStatus()) ||
+                        Constant.BasicDataConstant.FOLLOW_STATUS_5.getValue().equals(follow.getFollowStatus())) {
+//                    list.add("您有客户需要跟进");
+                    keywordMap.put("remark", "您有客户需要跟进");
                     loginType = "4";
 
                 } else if (Constant.BasicDataConstant.FOLLOW_STATUS_6.getValue().equals(follow.getFollowStatus())) {
-                    list.add("您有客户需要量尺服务");
+//                    list.add("您有客户需要量尺服务");
+                    keywordMap.put("remark", "您有客户需要量尺服务");
                     loginType = "6";
 
                 } else if (Constant.BasicDataConstant.FOLLOW_STATUS_2.getValue().equals(follow.getFollowStatus())) {
-                    list.add("您有客户邀约进店");
+//                    list.add("您有客户邀约进店");
+                    keywordMap.put("remark", "您有客户邀约进店");
                     loginType = "2";
 
                 } else if (Constant.BasicDataConstant.FOLLOW_STATUS_3.getValue().equals(follow.getFollowStatus())) {
-                    list.add("您有客户需要约尺服务");
+//                    list.add("您有客户需要约尺服务");
+                    keywordMap.put("remark", "您有客户需要约尺服务");
                     loginType = "3";
                 }
-                list.add(customer.getCusName());
-                list.add(customer.getCusPhone());
-                // 查询跟进人的openid和publicOpenId
+//                list.add(customer.getCusName());
+//                list.add(customer.getCusPhone());
+                LocalDateTime followTime = follow.getFollowTime();
+                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+
+                keywordMap.put("cusName", customer.getCusName());
+                keywordMap.put("cusPhone", customer.getCusPhone());
+                keywordMap.put("followTime", followTime.format(formatter));
+                // 查询跟进人的openid和publicOpenId   7.19 加了userWxId 刘尧
                 ResponseResultVO<?> resultVO = userFeign.getUser(followStaff.getWxUserId());
                 // 如果没有成功返回,状态设置为待审
                 if (resultVO.getCode() != ResponseCodeEnum.SUCCESS.getCode()) {
@@ -460,11 +473,14 @@ public class CusFollowService extends BaseService<CusFollow> {
                         Map<String, Object> mapMessage = new HashMap<>();
                         mapMessage.put("loginType", loginType);
                         mapMessage.put("otherParam", follow);
-                        mapMessage.put("publicOpenId", user.get("publicOpenId"));
+//                        mapMessage.put("publicOpenId", user.get("publicOpenId"));
+                        mapMessage.put("userWxId", user.get("userWxId"));
                         mapMessage.put("first", "客户跟进提醒");
-                        mapMessage.put("keywordList", list);
+//                        mapMessage.put("keywordList", list);
+                        mapMessage.put("keywordMap", keywordMap);
                         mapMessage.put("remark", "客户跟进备注");
-                        sendUniformMessage(mapMessage);
+//                        sendUniformMessage(mapMessage);
+                        wxMessagePush(mapMessage);
                     }
                 }
 
@@ -538,6 +554,47 @@ public class CusFollowService extends BaseService<CusFollow> {
     }
 
     /**
+     * @desc : 向用户推送小程序订阅消息方法
+     * @author : 刘尧
+     * @date : 2024-07-19 08:56
+     * */
+    public ResponseResultVO<?> wxMessagePush(Map<String, Object> map) {
+        // 获取微信Token
+        String wxToken = getWxToken();
+        Map<String, Object> params = new HashMap<>();
+        // 接口调用凭证
+        params.put("access_token", wxToken);
+        // 所需下发的订阅模板id
+        params.put("template_id", "z3REuwft6NLPxVW8s5HN1UgWjFstWjD0QfCXNLTa0LA");
+        // 点击模板卡片后的跳转页面
+        if (map.containsKey("otherParam")) {
+            params.put("page", "pages/welcome/welcome?loginType=" + map.get("loginType").toString() + "&otherParam=" + map.get("otherParam").toString());
+        } else {
+            params.put("page", "pages/welcome/welcome?loginType=" + map.get("loginType").toString());
+        }
+        // 接收者(用户)的 openid
+        params.put("touser", map.get("userWxId"));
+        // 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;
+        params.put("miniprogram_state", "trial");
+
+        // 获取客户信息
+        Object keywordMap = map.get("keywordMap");
+        Map<String, Object> personMap = BeanUtil.beanToMap(keywordMap);
+
+        // 构造对应的显示信息 和模板数据结构对应
+        JSONObject data = new JSONObject();
+        data.put("time4", createDataItem("value", String.valueOf(personMap.get("followTime"))));
+        data.put("thing1", createDataItem("value", String.valueOf(personMap.get("cusName"))));
+        data.put("phone_number2", createDataItem("value", String.valueOf(personMap.get("cusPhone"))));
+        data.put("thing6", createDataItem("value", String.valueOf(personMap.get("remark"))));
+        data.put("thing5", createDataItem("value", String.valueOf(map.get("remark"))));
+        params.put("data", data);
+
+        ResponseResultVO<JSONObject> post = HttpUtils.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + wxToken, params);
+        return post;
+    }
+
+    /**
      * @author : jyh
      * @date : 2023-10-11 16:23
      * @desc : 调用中控服务获取公众号的token
@@ -800,40 +857,22 @@ public class CusFollowService extends BaseService<CusFollow> {
         return ResponseResultUtil.success(cusFollowMapper.selectCusFollow(cusFollowQuery));
     }
 
-    public ResponseResultVO<?> wxMessagePush(HttpServletRequest request) {
-        // 获取微信Token
-        String wxToken = getWxToken();
-        Map<String, Object> params = new HashMap<>();
-        // 接口调用凭证
-        params.put("access_token", wxToken);
-        // 所需下发的订阅模板id
-        params.put("template_id", "z3REuwft6NLPxVW8s5HN1RH_gVxN9KPH7boGh92shYs");
-        // 点击模板卡片后的跳转页面
-        params.put("page", "pages/login/login");
-        // 接收者(用户)的 openid
-        params.put("touser", "oNIC56z-NuTQYlzfnupF_ZEo9agc");
-        // 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;
-        params.put("miniprogram_state", "developer");
-
-        // 构造对应的显示信息 和模板数据结构对应
-        JSONObject data = new JSONObject();
-        data.put("time4", createDataItem("value", "2024年07月18日"));
-        data.put("thing1", createDataItem("value", "ly"));
-        data.put("phone_number2", createDataItem("value", "19841226666"));
-        data.put("time7", createDataItem("value", "2024年07月18日"));
-        data.put("thing5", createDataItem("value", "666"));
-        params.put("data", data);
-
-        ResponseResultVO<JSONObject> post = HttpUtils.post("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + wxToken, params);
-        return post;
-    }
-
+    /**
+     * @desc : 构造消息推送模板中data值
+     * @author : 刘尧
+     * @date : 2024-07-19 08:56
+     * */
     private static Map<String, Object> createDataItem(String name, String value) {
         Map<String, Object> item = new HashMap<>();
-        item.put("value", value);
+        item.put(name, value);
         return item;
     }
 
+    /**
+     * @desc : 微信公众平台后台配置测试用
+     * @author : 刘尧
+     * @date : 2024-07-19 08:56
+     * */
     public void wxMessageTest(HttpServletRequest request, HttpServletResponse response) {
         String signature = request.getParameter("signature");
         String timestamp = request.getParameter("timestamp");
@@ -869,6 +908,11 @@ public class CusFollowService extends BaseService<CusFollow> {
         }
     }
 
+    /**
+     * @desc : 获取微信小程序后台Token
+     * @author : 刘尧
+     * @date : 2024-07-19 08:56
+     * */
     private String getWxToken(){
         String appId = config.getAppId();
         String appSecret = config.getAppSecret();
@@ -877,6 +921,11 @@ public class CusFollowService extends BaseService<CusFollow> {
         return String.valueOf(data.get("access_token"));
     }
 
+    /**
+     * @desc : 将字节转化为十六进制字符串
+     * @author : 刘尧
+     * @date : 2024-07-19 08:56
+     * */
     private static String byteToStr(byte[] byteArray) {
         String strDigest = "";
         for (int i = 0; i < byteArray.length; i++) {
@@ -885,6 +934,11 @@ public class CusFollowService extends BaseService<CusFollow> {
         return strDigest;
     }
 
+    /**
+     * @desc : 将字节转换成对应的两位十六进制字符串
+     * @author : 刘尧
+     * @date : 2024-07-19 08:56
+     * */
     private static String byteToHexStr(byte mByte) {
         char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
                 'B', 'C', 'D', 'E', 'F' };