微信完整分享js

/**
 * 微信网页端调用JS
 * @author dodge
 * @contact dodgepudding@gmail.com
 * @link http://blog.4wer.com/wechat-timeline-share
 * @version 1.1
 * 
 * 自定义分享使用:
 * WeixinJS.hideOptionMenu() 隐藏右上角按钮
 * WeixinJS.hideToolbar() 隐藏工具栏
 * 自定义分享内容数据格式:
 * var dataForWeixin={
	   appId:"",
	   MsgImg:"消息图片路径",
	   TLImg:"时间线图路径",
	   url:"分享url路径",
	   title:"标题",
	   desc:"描述",
	   fakeid:"",
	   prepare:function(argv){
	   if (typeof argv.shareTo!='undefined') 
	   	switch(argv.shareTo) {
	   		case 'friend':
	   		//发送给朋友
	   		alert(argv.scene); //friend
	   		break;
	   		case 'timeline':
	   		//发送给朋友
	   		break;
	   		case 'weibo':
	   		//发送到微博
	   		alert(argv.url);
	   		break;
	   		case 'favorite':
	   		//收藏
	   		alert(argv.scene);//favorite
	   		break;
	   		case 'connector':
	   		//分享到第三方应用
	   		alert(argv.scene);//connector
	   		break;
	   		default:
	   	}
	   },
	   callback:function(res){
	   	//发送给好友或应用
	   	if (res.err_msg=='send_app_msg:confirm') {
	   		//todo:func1();
	   		alert(res.err_desc);
	   	}
	   	if (res.err_msg=='send_app_msg:cancel') {
	   		//todo:func2();
	   		alert(res.err_desc);
	   	}
	   	//分享到朋友圈
	   	if (res.err_msg=='share_timeline:confirm') {
	   		//todo:func1();
	   		alert(res.err_desc);
	   	}
	   	if (res.err_msg=='share_timeline:cancel') {
	   		//todo:func1();
	   		alert(res.err_desc);
	   	}
	   	//分享到微博
	   	if (res.err_msg=='share_weibo:confirm') {
	   		//todo:func1();
	   		alert(res.err_desc);
	   	}
	   	if (res.err_msg=='share_weibo:cancel') {
	   		//todo:func1();
	   		alert(res.err_desc);
	   	}
	   	//收藏或分享到应用
	   	if (res.err_msg=='send_app_msg:ok') {
	   		//todo:func1();
	   		alert(res.err_desc);
	   	}   	
	   }
	};
 */

WeixinJS = typeof WeixinJS!='undefined' || {};
WeixinJS.hideOptionMenu = function() {
	document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
		if (typeof WeixinJSBridge!='undefined')	WeixinJSBridge.call('hideOptionMenu');
	});
};
WeixinJS.hideToolbar = function() {
	document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
		if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.call('hideToolbar');
	});
};
WeixinJS.getNetworkType = function(callback) {
	document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
		if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke('getNetworkType',{},
		function(res){
			//result: network_type:wifi,network_type:edge,network_type:fail,network_type:wwan
			callback(res.err_msg);
	    });
	});
};

WeixinJS.closeWindow = function() {
	if (typeof WeixinJSBridge!='undefined') WeixinJSBridge.invoke("closeWindow", {});
};

WeixinJS.payCallback = function(appId,package,timeStamp,nonceStr,signType,paySign,callback){
	if (typeof WeixinJSBridge!='undefined')
	WeixinJSBridge.invoke('getBrandWCPayRequest',{
        "appId" : appId.toString(),
        "timeStamp" : timeStamp.toString(),
        "nonceStr" : nonceStr.toString(),
        "package" : package.toString(),
        "signType" : signType.toString(),
        "paySign" : paySign.toString()
        
    },function(res){
    	// res.err_msg == "get_brand_wcpay_request:ok" return true;
    	// res.err_msg == "get_brand_wcpay_request:cancel" return false;
    	callback(res);
    });
};

WeixinJS.editAddress = function(appId,addrSign,timeStamp,nonceStr,callback){
	var postdata = {
			"appId" : appId.toString(),
            "scope" : "jsapi_address",
            "signType" : "sha1",
            "addrSign" : addrSign.toString(),
            "timeStamp" : timeStamp.toString(),
            "nonceStr" : nonceStr.toString()
	};
	if (typeof WeixinJSBridge!='undefined')
	WeixinJSBridge.invoke('editAddress',postdata, function(res){
	//return res.proviceFirstStageName,res.addressCitySecondStageName,res.addressCountiesThirdStageName,res.addressDetailInfo,res.userName,res.addressPostalCode,res.telNumber
	//error return res.err_msg
	callback(res);
	});
};

(function(){
   var onBridgeReady=function(){
   WeixinJSBridge.on('menu:share:appmessage', function(argv){
	  (dataForWeixin.prepare)(argv);
      WeixinJSBridge.invoke('sendAppMessage',{
         "appid":dataForWeixin.appId,
         "img_url":dataForWeixin.MsgImg,
         "img_width":"120",
         "img_height":"120",
         "link":dataForWeixin.url,
         "desc":dataForWeixin.desc,
         "title":dataForWeixin.title
      }, function(res){(dataForWeixin.callback)(res);});
   });
   WeixinJSBridge.on('menu:share:timeline', function(argv){
	  (dataForWeixin.prepare)(argv);
      WeixinJSBridge.invoke('shareTimeline',{
         "img_url":dataForWeixin.TLImg,
         "img_width":"120",
         "img_height":"120",
         "link":dataForWeixin.url,
         "desc":dataForWeixin.desc,
         "title":dataForWeixin.title
      }, function(res){(dataForWeixin.callback)(res);});
   });
   WeixinJSBridge.on('menu:share:weibo', function(argv){
	  (dataForWeixin.prepare)(argv);
      WeixinJSBridge.invoke('shareWeibo',{
         "content":dataForWeixin.title,
         "url":dataForWeixin.url
      }, function(res){(dataForWeixin.callback)(res);});
   });
   WeixinJSBridge.on('menu:share:facebook', function(argv){
	  (dataForWeixin.prepare)(argv);
      WeixinJSBridge.invoke('shareFB',{
         "img_url":dataForWeixin.TLImg,
         "img_width":"120",
         "img_height":"120",
         "link":dataForWeixin.url,
         "desc":dataForWeixin.desc,
         "title":dataForWeixin.title
      }, function(res){(dataForWeixin.callback)(res);});
   });
};
if(document.addEventListener){
   document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if(document.attachEvent){
   document.attachEvent('WeixinJSBridgeReady'   , onBridgeReady);
   document.attachEvent('onWeixinJSBridgeReady' , onBridgeReady);
}
})();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值