微信js分享朋友圈(二)

 

  近期又用到微信分享的功能了。虽然不是第一次用了,依然我又有幸踩到了一个坑,所以分享一下吧。

  根据微信sdk写的代码一步步很顺利,但是后面就是获取微信返回的分享结果的回调的时候IOS老是有问题,然后就网上各种搜,但是我得到的大部分答案是分享链接url的限制即:该分享链接域名或路径必须与当前页面对应的公众号JS安全域名一致

  but我的url符合要求就还是拿不到分享后的回调,然后经过一番折腾最后发现在微信分享成功回调的方法里加一个定时器就ok了。

 →提示:

  1. 这里我使用的还是旧的版本(1.3.2)这里就暂且不改了
  2. 官方已更至1.4.0详情参考:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

 ☞更多微信分享内容可移步:https://www.cnblogs.com/imelemon/p/7088556.html

 

<script type="text/javascript" src="https://res2.wx.qq.com/open/js/jweixin-1.3.2.js "></script> 

  

function wxShare(){

			// 1 判断当前版本是否支持指定 JS 接口,支持批量判断
			wx.ready(function () {
					
				wx.checkJsApi({
				      jsApiList: [
				        'onMenuShareAppMessage',
				        'onMenuShareTimeline'
				      ],
				      success: function (res) {
				        console.log("验证通过:"+JSON.stringify(res));
				      }
				 });   
				
					/* 分享好友1.3*/
					 wx.onMenuShareAppMessage({ 
						type: 'link', // 分享类型,music、video或link,不填默认为link
				        title: '【春节】2019你好!', // 分享标题
				        desc: 'hello。', // 分享描述
				        link:'https://'+location.hostname+'/web',
				        imgUrl: '', // 分享图标
				        success: function (res) {
				          // 设置成功
				           if(res.errMsg.indexOf(':ok')!=-1){
				        	   shareCallback();//分享成功的回调
				           }else if(res.errMsg.indexOf(':cancel')!=-1){
				        	   mui.toast('分享已经取消');
				           }else{
				        	   mui.toast('分享失败');
				           }
				         },
					 cancel: function () {
				            alert("分享已经取消");
				         }
					 });
					
					 /* 分享朋友圈1.3 */
						wx.onMenuShareTimeline({ 
						  type: 'link', // 分享类型,music、video或link,不填默认为link
					          title: '【春节】2019你好!', // 分享标题
					          link:'https://'+location.hostname+'/web',
					          imgUrl: '', // 分享图标
					          success: function(res){
					        	setTimeout(function(){
					        		shareCallback();//分享成功的回调
					        	},500);
					           
					          },
						   cancel: function () {
					              alert("分享已经取消");
					          }
						});
			})
			
			wx.error(function(res){
		  	 	mui.toast("wx:"+res.errMsg);
			});
		}

 

/* 分享成功的回调 */
		function shareCallback(){
			
			$.ajax({
  				 type : 'post',					 
  				 url : '/web/tic/shareCallback',
  				 dataType:'json',
  				 beforeSend: function() { 
					 $('.mui-loading').show();
					 mask.show();//显示遮罩层 
				 },
  				 success: function(json){
  					 mask.close();//关闭遮罩层
  					 console.log(json);
  					  
  					 if(json.status=='fail'){
  						 mui.toast(json.msg);
  					     return;
  					 }else{
  						 mui.toast('分享成功!');
  						setTimeout(function(){
  							 if(json.getRed==1){
  								$('.shareModal').hide()
  								$('.pop_wrap').show();
  							 }else{
  								 window.location.reload();
  							 }
					    	
					     },800);
  					 }
  				 },
  				 error:function(XMLHttpRequest, textStatus, errorThrown){
  					mask.close();//关闭遮罩层
  					alert('error:'+textStatus+"***"+JSON.stringfy(XMLHttpRequest.readyState))
  					mui.toast('请求失败(500)');
  				 }
  		 	});
		}

  

 

posted @ 2019-02-26 18:42  小鱼海棠  阅读(204)  评论(0编辑  收藏  举报