Blame view

parking-module-pay/src/main/java/com/zteits/pay/weixinpublic/util/WeixinUtil.java 4.39 KB
55f68655   王彪总   新项目
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  package com.zteits.pay.weixinpublic.util;
  
  import java.io.UnsupportedEncodingException;
  import java.net.URISyntaxException;
  import java.text.MessageFormat;
  import java.util.HashMap;
  import java.util.Map;
  
  import com.clouds.common.utils.wx.AccessToken;
  import com.clouds.common.utils.wx.AccessTokenUtil;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import org.apache.commons.lang3.StringUtils;
  import com.alibaba.fastjson.JSONObject;
  import com.zteits.pay.util.HttpClientTutorial;
  
  public class WeixinUtil {
  
  
      /**
       *   * 主动推送信息接口  
       */
      private static String sendMsgUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}";
      private static Logger log = LoggerFactory.getLogger(WeixinUtil.class);
  
      /**
       * 发送模板消息前获取token
       *
       * @param wxTemplate
       * @throws UnsupportedEncodingException
       */
      public static Integer sendMessageBefore(WxTemplate wxTemplate) throws Exception {
          AccessToken accessToken = AccessTokenUtil.getToken(wxTemplate.getAppid(), wxTemplate.getAppsecret());
          log.info("微信 accessToken :" + JSONObject.toJSONString(accessToken));
        /*  log.info("微信公共号推送返回1:"+JSONObject.toJSONString(TokenThread.accessToken));
          if(TokenThread.accessToken==null || TokenThread.accessToken.getToken()==""){
          	log.info("微信公共号获取token开始:"+wxTemplate.getAppid()+"   "+wxTemplate.getAppsecret());
              log.info("微信公共号获取token结束:"+JSONObject.toJSONString(accessToken));
          }else{
              accessToken = TokenThread.accessToken;
          }*/
  //        if(template_id_short!=null&&!"".equals(template_id_short)){
  //        	WxTemplate  template = WeixinUtil.getTemplate(template_id_short,accessToken.getToken());
  //        	log.info("WeixinUtil.getTemplate:"+JSONObject.toJSONString(template));
  //        	wxTemplate.setTemplate_id(template.getTemplate_id());
  //        }
          int i = -1;
          if (accessToken != null && StringUtils.isNotEmpty(accessToken.getAccessToken())) {
              log.info(accessToken.getAccessToken() + "发起推送:" + JSONObject.toJSONString(wxTemplate));
              i = WeixinUtil.sendMessage(wxTemplate, accessToken.getAccessToken());
          }
          return i;
      }
  
      /**
       * 获得模板编号获得模板ID
       *
       * @param template_id_short
       * @return
       */
      public final static String template_id_url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN";
  
      public static WxTemplate getTemplate(String template_id_short, String accessToken) throws UnsupportedEncodingException, URISyntaxException {
          WxTemplate _template = new WxTemplate();
          String url = template_id_url.replace("ACCESS_TOKEN", accessToken);
          Map<String, Object> map = new HashMap<>();
          map.put("template_id_short", template_id_short);
          String jsonObject = HttpClientTutorial.httpPostRequest(url, JSONObject.toJSONString(map));//   httpRequest(url, "POST", jsonInfo);  
          if (null != jsonObject) {
              JSONObject _jsonObject = JSONObject.parseObject(jsonObject);
              if (0 == _jsonObject.getInteger("errcode")) {
                  _template.setTemplate_id(_jsonObject.getString("template_id"));
              } else {
                  log.error("获取模板ID失败 errcode:{} errmsg:{}", _jsonObject.getInteger("errcode"), _jsonObject.getString("errmsg"));
              }
          }
          return _template;
      }
  
      /**
       * 发送模板消息
       *
       * @param t
       * @param accessToken
       * @return
       * @throws UnsupportedEncodingException
       */
      public static int sendMessage(WxTemplate t, String accessToken) throws UnsupportedEncodingException {
          int result = 0;
          // 拼装创建菜单的url
          String url = MessageFormat.format(sendMsgUrl, accessToken);
          // 调用接口创建菜单
          String rs = HttpClientTutorial.httpPostRequest(url, JSONObject.toJSONString(t));
          if (null != rs) {
              JSONObject obj = JSONObject.parseObject(rs);
              log.info("微信公共号推送返回:" + obj);
              if (0 != obj.getInteger("errcode")) {
                  result = obj.getInteger("errcode");
                  log.error("发送模板消息失败 errcode:{} errmsg:{}", obj.getInteger("errcode"), obj.getString("errmsg"));
              }
          }
          return result;
      }
  }