H5+ 分享到微信、朋友圈代碼示例


h5+分享到微信、朋友圈代碼示例

在使用分享功能的時候會莫名的分享失敗,debug時發現是圖片過大的問題。
圖片過大時ios平台上返回錯誤碼-8,安卓上返回錯誤碼-3(我測試是這樣)
因此如果第一次分享失敗時遞歸重新獲取默認圖片進行分享,這樣就可以分享成功了。
如果使用七牛等雲服務器存放圖片,可以設置下載圖片的大小,下載圖片時在鏈接后加"!(你定義的大小或名稱)",建議微信分享圖片大小60*60,一般這樣就能分享成功了

http://www.cnblogs.com/phillyx/

(function(window) {
	var Share={};
	Share.info = {
		id: '',
		name: '',
		head_image: "_www/images/icon/A/144.png",
		introduce: ''
	};
	/**
	 * 更新分享服務
	 */
	var shares = null;

	function getSerivces() {		
		plus.share.getServices(function(s) {
			
			shares = {};
			for (var i in s) {
				var t = s[i];
				shares[t.id] = t;
			}
		}, function(e) {
			console.log("獲取分享服務列表失敗:" + e.message);
		});
	};

	function shareAction(id, ex) {
		var s = null;
		
		if (!id || !(s = shares[id])) {
			console.log("無效的分享服務!");
			return;
		}
		if (s.authenticated) {
			console.log("---已授權---");
			shareMessage(s, ex);
		} else {
			console.log("---未授權---");
			//TODO 授權無法回調,有bug
			s.authorize(function() {	
				console.log('授權成功...')				
				shareMessage(s, ex);
			}, function(e) {		
				console.log("認證授權失敗:" + e.code + " - " + e.message);
			});
		}
	};
	var sharecount = 0;
	/**
	 * 發送分享消息
	 * @param
	 */
	function shareMessage(s, ex) {
		plus.nativeUI.showWaiting();
                setTimeout(plus.nativeUI.closeWaiting,5000);//TODO 5秒后自動關閉等待,否則如果用戶分享出去后選擇‘留在微信’,再手動回到app的時候,waiting無法關閉
		var msg = {
			extra: {
				scene: ex
			}
		};
		msg.href = "分享的網址" + "share?hid=" + Share.info.id;
		msg.title = "我在xxxx等你——" + Share.info._name;
		msg.content = Share.info.introduce;
        //取本地圖片
		var img = plus.io.convertAbsoluteFileSystem(Share.info.head_image.replace('file://', ''));
		console.log(img);
		msg.thumbs = [img];
		if (sharecount > 0) {
            //如果本地圖片過大,導致分享失敗,遞歸時重新分享獲取默認圖片
			msg.thumbs = ["_www/images/icon/A/144.png"];
		}
		console.log(JSON.stringify(msg));
		s.send(msg, function() {
			plus.nativeUI.closeWaiting();
			var strtmp = "分享到\"" + s.description + "\"成功! ";
			console.log(strtmp);
			plus.nativeUI.toast(strtmp, {
				verticalAlign: 'center'
			});
			sharecount = 0;
		}, function(e) {
			plus.nativeUI.closeWaiting();			
			if (e.code == -2) {
				plus.nativeUI.toast('已取消分享', {
					verticalAlign: 'center'
				});
				sharecount = 0;
			} else if (e.code == -3 || e.code == -8) {
				console.log(e.code);
				if (++sharecount < 2) {
					//TODO 分享失敗可能是圖片過大的問題,遞歸取默認圖片重新分享
					shareMessage(s, ex);
				} else {
					sharecount = 0;
					plus.nativeUI.toast('分享失敗', {
						verticalAlign: 'center'
					});
				}
			}else{
				console.error('分享失敗:'+JSON.stringify(e))
			}
			console.log("分享到\"" + s.description + "\"失敗: " + e.code + " - " + e.message);
		});
	};

	function share() {
		bhref = true;
		var ids = [{
				id: "weixin",
				ex: "WXSceneSession"
			}, {
				id: "weixin",
				ex: "WXSceneTimeline"
			}],
			bts = [{
				title: "發送給微信好友"
			}, {
				title: "分享到微信朋友圈"
			}];
		plus.nativeUI.actionSheet({
				cancel: "取消",
				buttons: bts
			},
			function(e) {
				var i = e.index;
				if (i > 0) {
					shareAction(ids[i - 1].id, ids[i - 1].ex);
				}
			}
		);
	};
	Share.share=share;
	window.Share = Share;
	mui.plusReady(function() {		
		getSerivces();
	}); 
})(window)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM