js 微信分享

一、

//js接口

var shareme;

var urls = window.location.href;
if(isWeiXin()){
  var weifileref=document.createElement('script')//创建标签
  weifileref.setAttribute("type","text/javascript")//定义属性type的值为text/javascript
  weifileref.setAttribute("src", "http://res.wx.qq.com/open/js/jweixin-1.0.0.js")//文件的地址
  document.getElementsByTagName("head")[0].appendChild(weifileref);
}
function isWeiXin(){
  var ua = window.navigator.userAgent.toLowerCase();
  if(ua.match(/MicroMessenger/i) == 'micromessenger'){
    return true;
  }else{
    return false;
  }
}
//修改jq  
$(window).load(function(){
 if(isWeiXin()){
      $.ajax({
        type: "POST",
        async: false,   //同步请求
        url: "",
        data:{
            urls:urls
        },
        jsonp: 'callback',
        dataType: 'jsonp',
        success:function(data){
          shareme = eval(data);
          wx.config({
              debug: false,
              appId: shareme.appId,
              timestamp: shareme.timestamp,
              nonceStr: shareme.nonceStr,
              signature: shareme.signature,
              jsApiList: [
              // 所有要调用的 API 都要加到这个列表中
              'checkJsApi',
              'onMenuShareTimeline',
              'onMenuShareAppMessage',
              'onMenuShareQQ',
              'onMenuShareWeibo',
              'hideMenuItems',
              'showMenuItems',
              'hideAllNonBaseMenuItem',
              'showAllNonBaseMenuItem'
            ]
          });
          
          wx.ready(function () {
            //判断当前版本是否支持指定 JS 接口,支持批量判断
              wx.checkJsApi({
                  jsApiList: [
                    'getNetworkType',
                    'previewImage',
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage'
                  ],
                  success: function (res) {

                  },
                  fail:function(){
                    alert('抱歉您的微信版本有问题不支持分享功能!');
                  }
              });

            //分享接口
            //监听“分享给朋友”
             wx.onMenuShareAppMessage({
                title: '热招岗位',
                desc: '感谢您的关注!',
                link: 'http:/',
                imgUrl: /share.png',
                trigger: function (res) {
                  //alert('用户点击发送给朋友');
                },
                success: function (res) {
                  //alert('已分享');
                },
                cancel: function (res) {
                  //alert('已取消');
                },
                fail: function (res) {
                  //alert(JSON.stringify(res));
                }
              });
             

            // 监听“分享到朋友圈”按钮点击
            wx.onMenuShareTimeline({
              title: '热招岗位',
                desc: '感谢您的关注!',
                link: 'http://',
                imgUrl: '/share.png',
                trigger: function (res) {
                  //alert('用户点击分享到朋友圈');
                },
                success: function (res) {
                  //alert('已分享');
                },
                cancel: function (res) {
                  //alert('已取消');
                },
                fail: function (res) {
                 // alert(JSON.stringify(res));
                }
              });                        
            });

            wx.error(function (res) {
              //alert(res.errMsg);
            });
        },
        error: function () {
         
        }
    });
  }else{
      //alert("对不起您不在微信浏览器中,不支持分享功能!");
      return false;
  }

});

//不过不知道为什么,试了半天这个方法我一直没有效果,好像接口链接不太对

二、

head里的title就是分享的问题

分享的图片,一般就是每页的body里面的第一张图片,300*300以上才行;

可以写个div,包含想要的img,然后设置div隐藏,img隐藏的话实现不了。

你可能感兴趣的:(js 微信分享)