1. 程式人生 > >解決mui-app微信二維碼分享

解決mui-app微信二維碼分享

var Intent = null,
 	File = null,
 	Uri = null,
 	main = null;
 var shares = null;
 var shareImageUrl = '';
 mui.plusReady(function() {
 	updateSerivces();
 	if(plus.os.name == "Android") {
 		Intent = plus.android.importClass("android.content.Intent");
 		File = plus.android.importClass("java.io.File");
 		Uri = plus.android.importClass("android.net.Uri");
 		main = plus.android.runtimeMainActivity();
 	}
 })
 /**
  * 更新分享服務
  */
 function updateSerivces() {
 	plus.share.getServices(function(s) {
 		shares = {};
 		for(var i in s) {
 			var t = s[i];
 			shares[t.id] = t;
 		}
 		outSet("獲取分享服務列表成功");
 	}, function(e) {
 		outSet("獲取分享服務列表失敗:" + e.message);
 	});
 }
 /**
  * 分享操作
  */
 function shareAction(id, ex) {
 	var s = null;
 	if(!id || !(s = shares[id])) {
 		outLine("無效的分享服務!");
 		return;
 	}
 	if(s.authenticated) {
 		outSet("---已授權---");
 		shareMessage(s, ex);
 	} else {
 		outSet("---未授權---");
 		s.authorize(function() {
 			shareMessage(s, ex);
 		}, function(e) {
 			outLine("認證授權失敗");
 		});
 	}
 }
 /**
  * 傳送分享訊息
  */
 function shareMessage(s, ex) {
 			var msg = {
 				content: ' ',
 				//href: '',
 				//thumbs: [],
 				pictures: [],
 				extra: {
 					scene: ex
 				}
 			};
 			s.send(msg, function() {
 				outLine("分享成功!");
 			}, function(e) {
 				outLine("分享失敗!");
 			});
/**
  * 分享按鈕點選事件
  */
 function shareHref() {
 	var ids = [{
 				id: "weixin",
 				ex: "WXSceneSession" /*微信好友*/
 			},
 			{
 				id: "weixin",
 				ex: "WXSceneTimeline" /*微信朋友圈*/
 			}
 		],
 		bts = [{
 				title: "傳送給微信好友"
 			}, {
 				title: "分享到微信朋友圈"
 			}
		];
 	//開啟彈出sheet
 	plus.nativeUI.actionSheet({
 			cancel: "取消",
 			buttons: bts
 		},
 		//點選分享的回撥函式
 		function(e) {
 			var i = e.index;
 			if(i > 0) {
 				shareAction(ids[i - 1].id, ids[i - 1].ex);
 			}
 		}
 	);
 }
 // 控制檯輸出分享日誌
 function outSet(msg) {
 	console.log(msg);
 }
 // 介面彈出分享成功失敗提示
 function outLine(msg) {
 	mui.toast(msg);
 }