wwl 3 lat temu
rodzic
commit
eb3eb61c7a

+ 3 - 1
twzd-admin/src/main/resources/application-dev.yml

@@ -112,9 +112,11 @@ file:
     ticketPath: /data/test/
 
 
-# 公众号上传素材url
+# 公众号
 wxgzh:
     # 上传永久素材
     materialUrl: https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=
     # 上传临时素材
     mediaUpload: https://api.weixin.qq.com/cgi-bin/media/upload?access_token=
+    # 客服接口-发消息
+    messageCustomSend: https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=

+ 3 - 1
twzd-admin/src/main/resources/application-prod.yml

@@ -109,9 +109,11 @@ file:
     ticketPath: /data/test/
 
 
-# 公众号上传素材url
+# 公众号
 wxgzh:
     # 上传永久素材
     materialUrl: https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=
     # 上传临时素材
     mediaUpload: https://api.weixin.qq.com/cgi-bin/media/upload?access_token=
+    # 客服接口-发消息
+    messageCustomSend: https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=

+ 60 - 0
twzd-common/src/main/java/com/miaxis/common/utils/http/HttpUtils.java

@@ -1,5 +1,6 @@
 package com.miaxis.common.utils.http;
 
+import com.alibaba.fastjson.JSONObject;
 import com.miaxis.common.constant.Constants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -320,4 +321,63 @@ public class HttpUtils
     }
 
 
+    /**
+     * 描述:  发起https请求并获取结果
+     * @param requestUrl 请求地址
+     * @param requestMethod 请求方式(GET、POST)
+     * @param outputStr 提交的数据
+     * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
+     */
+    public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
+        JSONObject jsonObject = null;
+        StringBuffer buffer = new StringBuffer();
+        try {
+            URL url = new URL(requestUrl);
+            HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
+
+            httpUrlConn.setDoOutput(true);
+            httpUrlConn.setDoInput(true);
+            httpUrlConn.setUseCaches(false);
+
+            // 设置请求方式(GET/POST)
+            httpUrlConn.setRequestMethod(requestMethod);
+
+            if ("GET".equalsIgnoreCase(requestMethod)){
+                httpUrlConn.connect();
+            }
+
+            // 当有数据需要提交时
+            if (null != outputStr) {
+                OutputStream outputStream = httpUrlConn.getOutputStream();
+                // 注意编码格式,防止中文乱码
+                outputStream.write(outputStr.getBytes("UTF-8"));
+                outputStream.close();
+            }
+
+            // 将返回的输入流转换成字符串
+            InputStream inputStream = httpUrlConn.getInputStream();
+            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
+            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
+
+            String str = null;
+            while ((str = bufferedReader.readLine()) != null) {
+                buffer.append(str);
+            }
+            bufferedReader.close();
+            inputStreamReader.close();
+            // 释放资源
+            inputStream.close();
+            inputStream = null;
+            httpUrlConn.disconnect();
+            jsonObject = JSONObject.parseObject(buffer.toString());
+        } catch (ConnectException ce) {
+            log.error("Weixin server connection timed out.");
+        } catch (Exception e) {
+            log.error("https request error:{}", e);
+        }
+        return jsonObject;
+    }
+
+
+
 }

+ 2 - 1
twzd-service/src/main/java/com/miaxis/wx/service/IWxMessageEvenService.java

@@ -15,9 +15,10 @@ public interface IWxMessageEvenService {
      * 扫码关注事件
      * @param fromUserName 发送方帐号(一个OpenID)
      * @param ticket 二维码的ticket
+     * @param token 凭证
      * @return
      */
-    String scanSubscribeEvent(String fromUserName,String ticket);
+    String scanSubscribeEvent(String fromUserName,String ticket,String token);
 
     /**
      * click获取分销二维码事件

+ 3 - 2
twzd-service/src/main/java/com/miaxis/wx/service/impl/WxGzhServiceImpl.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.miaxis.common.constant.Constants;
 import com.miaxis.common.core.domain.entity.UserInfo;
 import com.miaxis.common.utils.StringUtils;
+import com.miaxis.common.utils.http.HttpUtils;
 import com.miaxis.common.utils.wx.MessageUtil;
 import com.miaxis.feign.service.IWxSendService;
 import com.miaxis.system.service.ISysUserService;
@@ -138,8 +139,8 @@ public class WxGzhServiceImpl implements IWxGzhService {
                     //存在Ticket为扫码关注
                     if (org.apache.commons.lang3.StringUtils.isNotEmpty(jsonObjectData.getStr("Ticket"))){
                         log.info("3.1.2..");
-                        //根据Ticket  推送绑定信息到上级用户
-                        String subscribeEvent = wxMessageEvenService.scanSubscribeEvent(fromUserName, jsonObjectData.getStr("Ticket"));
+                        //根据Ticket  绑定信息到上级用户
+                        String subscribeEvent = wxMessageEvenService.scanSubscribeEvent(fromUserName, jsonObjectData.getStr("Ticket"),this.getGzhToken());
                         return MessageUtil.initText(fromUserName,toUserName,subscribeEvent);
 
                     }else {

+ 19 - 1
twzd-service/src/main/java/com/miaxis/wx/service/impl/WxMessageEvenServiceImpl.java

@@ -7,6 +7,7 @@ import cn.hutool.core.img.ImgUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.extra.qrcode.QrCodeUtil;
 import cn.hutool.extra.qrcode.QrConfig;
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.miaxis.common.constant.Constants;
@@ -26,6 +27,7 @@ import org.springframework.stereotype.Service;
 import java.awt.*;
 import java.io.File;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 
@@ -53,6 +55,9 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
     @Value("${wxgzh.mediaUpload}")
     private String mediaUpload;
 
+    @Value("${wxgzh.messageCustomSend}")
+    private String messageCustomSend;
+
     @Value("${cos.bucketName}")
     private String bucketName;
 
@@ -67,10 +72,11 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
      * 扫码关注事件
      * @param fromUserName 发送方帐号(一个OpenID)
      * @param ticket 二维码的ticket
+     * @param token 凭证
      * @return
      */
     @Override
-    public String scanSubscribeEvent(String fromUserName, String ticket) {
+    public String scanSubscribeEvent(String fromUserName, String ticket,String token) {
 
         String message = "";
         //判断是否存在父级推广关系
@@ -82,6 +88,18 @@ public class WxMessageEvenServiceImpl implements IWxMessageEvenService {
             wxSpreadRelation.setOpenid(fromUserName);
             wxSpreadRelation.setParentOpenid(openid);
             wxSpreadRelationService.save(wxSpreadRelation);
+
+            //推送绑定信息到上级
+            HashMap<String, Object> map = new HashMap<>();
+            map.put("touser",openid);
+            map.put("msgtype","text");
+            map.put("text",new HashMap<String,Object>()
+                    .put("content","用户openid:"+fromUserName+"\n内容:成功绑定下级"+"\n备注:测试文字链<a href=\"http://www.qq.com\" data-miniprogram-appid=\"appid\" data-miniprogram-path=\"pages/index/index\">点击跳小程序</a>"));
+            String jsonString = JSON.toJSONString(map);
+            String path = messageCustomSend + token;
+            JSONObject jsonObject = HttpUtils.httpRequest(path, "POST", jsonString);
+            log.info("回复客服消息:[{}],响应信息:[{}]",jsonString,jsonObject);
+
             message = "已成功绑定推广关系!";
         }else {
             message = "已存在推广关系!";