微信分享和模板消息中的经验

微信分享步骤

1、参考js-sdk准备好公众号等

2、 <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

3、$(document).ready(function(){ 
wx.config({
   debug: false, 
   appId: '${appId}', // 必填,公众号的唯一标识
   timestamp:'${timestamp}', // 必填,生成签名的时间戳
   nonceStr: '${nonceStr}', // 必填,生成签名的随机串
   signature: '${signature}',// 必填,签名,见附录1
   jsApiList: [
             'onMenuShareTimeline',
           'onMenuShareAppMessage',
         ] 
});
wx.ready(function(){
var shareData = {
    title: 'title',
    desc: 'content',
    link: 'url', // 分享链接
    imgUrl: 'imgurl', // 分享图标
    success: function () { 

alert("成功");
    //微信回调
    }
   };
wx.onMenuShareAppMessage(shareData);
  wx.onMenuShareTimeline(shareData);
});
wx.error(function (res) {
alert("出错");
});


模板消息步骤

1、

      public static TokenMessage getAccessToken(){
StringBuffer sb = new StringBuffer(ACCESS_TOKEN);
HashMap<String, String> map=new HashMap<String, String>();
sb.append("grant_type="+GRANT_TYPE);
sb.append("&appid="+APPID);
sb.append("&secret="+SECRET);
String result =  requestMethod(sb.toString(),"POST", null, null);
if(StringUtils.isNotEmpty(result)){
TokenMessage  message = new Gson().fromJson(result, TokenMessage.class);
if(message==null){
logger.error("=============无法获取授权====================");
}
/**
* 获取预支付订单
*/
if(message.getErrcode()==null || message.getErrcode().equals("")){
return  message;//获取用户的openId
}
}
return null;
}

2、
    public static String sendTextMessageToUser(String openId,String templateId,String url,String... contents){
       if(contents == null || contents.length == 0){
      return "failure";
       }
       String color = "#173177";
       StringBuilder json = new StringBuilder("{\"touser\": \""+openId+"\",\"template_id\": \""+templateId+"\", \"url\": "+"\""+url+"\","+"\"topcolor\":"
      +"\"#FF0000\"," +"\"data\":"
      +"{");
         for(int i = 0 ; i < contents.length ; i ++){
         if(i == 0){
         json.append("\"first\":");
         json.append("{");
         json.append("\"value\":"+"\""+contents[i]+"\",");
         json.append("\"color\":"+"\""+color+"\"");
         json.append("},");
          continue;
         }else if(i == contents.length-1){
         json.append("\"remark\":");
         json.append("{");
         json.append("\"value\":"+"\""+contents[i]+"\",");
         json.append("\"color\":"+"\""+color+"\"");
         json.append("}");
          continue;
         }else{
         json.append("\"keyword"+i+"\":");
     json.append("{");
     json.append("\"value\":"+"\""+contents[i]+"\",");
     json.append("\"color\":"+"\""+color+"\"");
     json.append("},");
         }
         }
      json.append( "}");
        json.append( "}");
       logger.info("sendTextMessageToUser json:"+json.toString());
       String interfaceUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+getAccessToken().getAccess_token();
       logger.info("sendTextMessageToUser interfaceUrl:"+interfaceUrl);
       System.out.println("json:"+json);
       try {
           connectWeiXinInterface(interfaceUrl,json.toString());
       } catch (Exception e) {
           e.printStackTrace();
       }
       return "success";
   }
    
3、
    public static  void connectWeiXinInterface(String interfaceUrl,String json){
        URL url;
        OutputStream os = null;
        InputStream is = null;
       try {
           url = new URL(interfaceUrl);
           HttpURLConnection http = (HttpURLConnection) url.openConnection();
           http.setRequestMethod("POST");
           http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
           http.setDoOutput(true);
           http.setDoInput(true);
           System.setProperty("sun.net.client.defaultConnectTimeout", "10000");// 连接超时30秒
           System.setProperty("sun.net.client.defaultReadTimeout", "10000"); // 读取超时30秒
           http.connect();
           os = http.getOutputStream();
           os.write(json.getBytes("UTF-8"));// 传入参数
           is = http.getInputStream();
           int size = is.available();
           byte[] jsonBytes = new byte[size];
           is.read(jsonBytes);
           String result = new String(jsonBytes, "UTF-8");
           logger.info("sendTextMessageToUser result:"+result);
           System.out.println("请求返回结果:"+result);
           os.flush();
       } catch (Exception e) {
           e.printStackTrace();
       } finally{
      try {
      os.close();
      is.close();
 } catch (IOException e) {
 e.printStackTrace();
}
       }
    }

微信分享所遇到问题

1、分享路径及参数过长,会导致出现state参数过长问题

2、弱网页面没有加载完会导致微信验证失败从而分享内容,链接改变失败

3、分享从一个人转发到另一个人参数问题

4、不同手机不同微信登陆问题

欢迎访问我的技术群425783133

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值