html5+ share-分享

share-分享

Share模塊管理客戶端的社交分享功能,提供調用終端社交軟件的分享能力。通過plus.share可獲取社交分享管理對象。

方法:

對象:

回調方法:

權限:

permissions

{
// ...
"permissions":{
	// ...
	"Share": {
		"description": "分享"
	}
}
}

ShareService

分享服務對象

interface plus.share.ShareService {
	// Attributes
	attribute DOMString id;
	attribute DOMString description;
	attribute Boolean authenticated;
	attribute DOMString accessToken;
	attribute Boolean nativeClient;
	
	// Methods
	function void authorize( successCallback, errorCallback );
	function void forbid();
	function void send( message );
}

說明:

ShareService對象用於表示分享服務,在JS中爲對象,用於向系統請求分享操作。

屬性:

方法:

ShareServerIdentity

分享服務標識

常量:

  • sinaweibo: _(DOMString 類型 )_新浪微博

  • tencentweibo: _(DOMString 類型 )_騰訊微博

  • weixin: _(DOMString 類型 )_微信

ShareMessage

JSON對象,分享消息對象

interface plus.share.ShareMessage {
	attribute String content;
	attribute String\[\] thumbs;
	attribute String\[\] pictures;
	attribute String href;
	attribute String title;
	attribute JSON extra;
	attribute GEOPosition geo;
	attribute ShareMessageExtra extra;
	attribute String interface;
}

說明:

ShareMessage對象用於表示分享消息內容,在JS中爲JSON對象,用於向系統發送分享信息操作。

屬性:

  • content: _(String 類型 )_分享消息的文字內容

  • pictures: _(Array[ String ] 類型 )_分享消息的圖片

    分享消息中包含的圖片路徑,僅支持本地路徑。 若分享平臺僅支持提交一張圖片,傳入多張圖片則僅提交第一張圖片。 如果不能同時支持其它內容信息,優先級順序爲:pictures>content。

  • thumbs: _(Array[ Stromg ] 類型 )_分享消息的縮略圖

    分享消息中包含的縮略圖路徑; 若分享平臺僅支持提交一張圖片,傳入多張圖片則僅提交第一張圖片; 如果分享平臺的信息不支持縮略圖,若沒有設置消息的圖片(pictures)則使用縮略圖,否則忽略其屬性值。 注意:圖片有大小限制,推薦圖片小於20Kb。

  • href: _(String 類型 )_分享獨立的鏈接

    分享獨立鏈接地址,僅支持網絡地址(以http://或https://開頭)。 如果不能同時支持其它內容信息,優先級順序爲:href>pictures>content。

  • title: _(String 類型 )_分享消息的標題

    目前僅微信分享獨立鏈接消息時支持。

  • geo: _(GeoPosition 類型 )_分享消息中包含的用戶地理信息數據

  • extra: _(ShareMessageExtra 類型 )_分享消息擴展參數

  • interface: _(String 類型 )_分享消息的模式

    可取值: “auto” - 自動選擇,如果已經安裝微博客戶端則採用編輯界面進行分享,否則採用無界面分享; “slient” - 靜默分享,採用無界面模式進行分享; “editable” - 進入編輯界面,用戶確認分享內容後發送,如果當前未安裝微博客戶端則觸發錯誤回調。 默認值爲"auto"。 (僅新浪微博分享時生效)

GeoPosition

JSON對象,用戶位置信息

interface plus.share.GeoPosition {
	attribute Number latitude;
	attribute Number longitude;
}

說明:

GeoPosition對象用於表示用戶分享消息時的位置信息。用於標識分享操作時用戶的位置信息。

屬性:

  • latitude: _(Number 類型 )_用戶位置的緯度座標信息

  • longitude: _(Number 類型 )_用戶位置的經度座標信息

ShareMessageExtra

JSON對象,保存分享消息擴展信息

interface plus.share.ShareMessageExtra {
	attribute String scene;
}

說明:

ShareMessageExtra對象用於保存各分享平臺擴展的參數,用於自定義分享功能。

屬性:

  • scene: _(String 類型 )_微信分享場景,僅微信分享平臺有效

    可取值: "WXSceneSession"分享到微信的“我的好友”; "WXSceneTimeline"分享到微信的“朋友圈”中; “WXSceneFavorite"分享到微信的“我的收藏”中。 默認值爲"WXSceneSession”。

Authorize

分享授權控件對象

interface plus.share.Authorize {
	// Methods
	function void load( id );
	function void setVisible( visible );

	// Events
	function void onloaded();
	function void onauthenticated();
	function void onerror();
}

說明:

Authorize對象表示分享控件對象,用於在窗口中顯示分享控件,使用此對象可自定義分享授權界面。

構造:

方法:

  • load: 加載分享授權頁面
  • setVisible: 設置分享授權控件是否可見

事件:

ServicesSuccessCallback

獲取分享服務成功回調

void ServicesSuccessCallback( services ) {
	// Get share services success code
}

說明:

當獲取分享服務列表成功時的回調函數,用於返回終端支持的分享服務列表。

參數:

  • services: ( Array[ ShareService ] ) 必選 數組,運行環境支持的分享服務列表
    運行環境支持的分享服務列表數組,可通過services.length獲取分享服務列表的數目。

返回值:

void : 無

AuthorizeSuccessCallback

分享授權認證成功回調

void AuthorizeSuccessCallback(services){
	  // Authorize success code
}

說明:

分享服務授權認證操作成功時調用。

參數:

  • service: ( ShareService ) 必選 授權認證操作的分享服務對象

返回值:

void : 無

ShareSuccessCallback

分享操作成功回調

void ShareSuccessCallback () {
	// Share success code
}

說明:

分享操作成功回調函數,當分享操作成功時調用。

參數:

返回值:

void : 無

ShareErrorCallback

分享操作失敗回調

void ShareErrorCallback ( error ) {
	// Error 
	var code = error.code; 			// 錯誤編碼
	var message = error.message;	// 錯誤描述信息
}

說明:

當分享操作失敗時的回調函數,用於返回分享相關操作失敗的錯誤信息。

參數:

  • error: ( DOMException ) 必選 分享操作失敗錯誤信息
    可通過error.code(Number類型)獲取錯誤編碼; 可通過error.message(String類型)獲取錯誤描述信息。

返回值:

void : 無

<!DOCTYPE HTML>
<html>

	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="HandheldFriendly" content="true" />
		<meta name="MobileOptimized" content="320" />
		<title>Hello H5+</title>

		<script type="text/javascript">
			var shares = null;
			// H5 plus事件處理
			function plusReady() {
				updateSerivces();
			}
			if (window.plus) {
				plusReady();
			} else {
				document.addEventListener('plusready', plusReady, false);
			}
			/**
			 * 更新分享服務
			 */
			function updateSerivces() {
				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 shareSystem() {
				console.log('調用系統分享');
				var msg = {
					content: sharecontent.value
				};
				if (pic && pic.realUrl) {
					msg.pictures = [pic.realUrl];
				}
				plus.share.sendWithSystem ? plus.share.sendWithSystem(msg, function () {
					outLine('Success');
					console.log('Success');
				}, function (e) {
					outLine('Failed: ' + JSON.stringify(e));
					console.log('Failed: ' + JSON.stringify(e));
				}) : shareSystemNativeJS();
			}

			function shareSystemNativeJS() {
				if (plus.os.name !== 'Android') {
					plus.nativeUI.alert('此平臺暫不支持系統分享功能!');
					return;
				}
				var intent = new Intent(Intent.ACTION_SEND);
				if (pic && pic.realUrl) {
					var p = '';
					p = pic.realUrl;
					if (p.substr(0, 7) === 'file://') {
						p = p.substr(7);
					} else if (p.sub(0) !== '/') {
						p = plus.io.convertLocalFileSystemURL(p);
					}
				}
				var f = new File(p);
				var uri = Uri.fromFile(f);
				if (f.exists() && f.isFile()) {
					console.log('image/*');
					intent.setType('image/*');
					intent.putExtra(Intent.EXTRA_STREAM, uri);
				} else {
					console.log('text/plain');
					intent.setType('text/plain');
				}
				intent.putExtra(Intent.EXTRA_SUBJECT, 'HelloH5');
				intent.putExtra(Intent.EXTRA_TEXT, sharecontent.value);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				main.startActivity(Intent.createChooser(intent, '系統分享HelloH5'));
			}
			/**
			 * 分享操作
			 * @param {JSON} sb 分享操作對象s.s爲分享通道對象(plus.share.ShareService)
			 * @param {Boolean} bh 是否分享鏈接
			 */
			function shareAction(sb, bh) {
				console.log('分享操作:');
				if (!sb || !sb.s) {
					outLine('無效的分享服務!');
					return;
				}
				var msg = {
					content: sharecontent.value,
					extra: {
						scene: sb.x
					}
				};
				if (bh) {
					msg.href = sharehref.value;
					if (sharehrefTitle && sharehrefTitle.value != '') {
						msg.title = sharehrefTitle.value;
					}
					if (sharehrefDes && sharehrefDes.value != '') {
						msg.content = sharehrefDes.value;
					}
					msg.thumbs = ['_www/logo.png'];
					msg.pictures = ['_www/logo.png'];
				} else {
					if (pic && pic.realUrl) {
						msg.pictures = [pic.realUrl];
					}
				}
				// 發送分享
				if (sb.s.authenticated) {
					outLine('---已授權---');
					shareMessage(msg, sb.s);
				} else {
					outLine('---未授權---');
					sb.s.authorize(function () {
						shareMessage(msg, sb.s);
					}, function (e) {
						outLine('認證授權失敗:' + e.code + ' - ' + e.message);
					});
				}
			}
			/**
			 * 發送分享消息
			 * @param {JSON} msg
			 * @param {plus.share.ShareService} s
			 */
			function shareMessage(msg, s) {
				outLine(JSON.stringify(msg));
				s.send(msg, function () {
					outLine('分享到"' + s.description + '"成功!');
				}, function (e) {
					outLine('分享到"' + s.description + '"失敗: ' + JSON.stringify(e));
				});
			}
			/**
			 * 解除所有分享服務的授權
			 */
			function cancelAuth() {
				try {
					console.log('解除授權:');
					for (var i in shares) {
						var s = shares[i];
						if (s.authenticated) {
							outLine('取消"' + s.description + '"');
						}
						s.forbid();
					}
					// 取消授權後需要更新服務列表
					updateSerivces();
					outLine('操作成功!');
				} catch (e) {
					alert(e);
				}
			}
			// 拍照添加圖片分享
			function shareCameraPicture() {
				console.log('拍照添加分享圖片:');
				var cmr = plus.camera.getCamera();
				cmr.captureImage(function (p) {
					plus.io.resolveLocalFileSystemURL(p, function (entry) {
						pic.src = entry.toLocalURL();
						pic.realUrl = p;
						outLine('拍照圖片:' + pic.realUrl);
					}, function (e) {
						outLine('讀取拍照文件錯誤:' + e.message);
					});
				}, function (e) {
					outLine('拍照失敗:' + e.message);
				});
			}
			// 從相冊添加圖片分享
			function shareGalleryPicture() {
				console.log('從相冊添加分享圖片:');
				plus.gallery.pick(function (p) {
					// 從相冊返回的路徑不需要轉換可以直接使用
					pic.src = p;
					pic.realUrl = pic.src;
					outLine('選擇圖片:' + pic.realUrl);
					//      plus.io.resolveLocalFileSystemURL(p,function(entry){
					//			pic.src=entry.toLocalURL();
					//			pic.realUrl=pic.src;
					//			outLine('選擇圖片:'+pic.realUrl);
					//		},function(e){
					//			outLine('讀取拍照文件錯誤:'+e.message);
					//		} );
				});
			}
			// 使用Logo圖片分享
			function shareLogoPicture() {
				console.log('使用Logo分享圖片:');
				var url = '_www/logo.png';
				plus.io.resolveLocalFileSystemURL(url, function (entry) {
					pic.src = entry.toLocalURL();
					pic.realUrl = url;
				}, function (e) {
					outLine('讀取Logo文件錯誤:' + e.message);
				});
			}
			// 打開分享
			function shareShow() {
				var shareBts = [];
				// 更新分享列表
				var ss = shares['weixin'];
				if (navigator.userAgent.indexOf('qihoo') < 0) { //在360流應用中微信不支持分享圖片
					ss && ss.nativeClient && (shareBts.push({
							title: '微信朋友圈',
							s: ss,
							x: 'WXSceneTimeline'
						}),
						shareBts.push({
							title: '微信好友',
							s: ss,
							x: 'WXSceneSession'
						}));
				}
				ss = shares['sinaweibo'];
				ss && shareBts.push({
					title: '新浪微博',
					s: ss
				});
				ss = shares['qq'];
				ss && ss.nativeClient && shareBts.push({
					title: 'QQ',
					s: ss
				});
				// 彈出分享列表
				shareBts.length > 0 ? plus.nativeUI.actionSheet({
					title: '分享',
					cancel: '取消',
					buttons: shareBts
				}, function (e) {
					(e.index > 0) && shareAction(shareBts[e.index - 1], false);
				}) : plus.nativeUI.alert('當前環境無法支持分享操作!');
			}
			// 分析鏈接
			function shareHref() {
				var shareBts = [];
				// 更新分享列表
				var ss = shares['weixin'];
				ss && ss.nativeClient && (shareBts.push({
						title: '微信朋友圈',
						s: ss,
						x: 'WXSceneTimeline'
					}),
					shareBts.push({
						title: '微信好友',
						s: ss,
						x: 'WXSceneSession'
					}));
				ss = shares['qq'];
				ss && ss.nativeClient && shareBts.push({
					title: 'QQ',
					s: ss
				});
				// 彈出分享列表
				shareBts.length > 0 ? plus.nativeUI.actionSheet({
					title: '分享鏈接',
					cancel: '取消',
					buttons: shareBts
				}, function (e) {
					(e.index > 0) && shareAction(shareBts[e.index - 1], true);
				}) : plus.nativeUI.alert('當前環境無法支持分享鏈接操作!');
			}

		</script>

		<style type="text/css">
			#sharecontent {
				width: 80%;
				-webkit-user-select: text;
				border: 1px solid #6C6C6C;
				-webkit-border-radius: 2px;
				border-radius: 2px;
			}

			#pic {
				width: 100px;
				height: 100px;
				/*border: 1px dashed #CCCCCC;*/
			}

			.sharehref {
				width: 80%;
				-webkit-user-select: text;
				border: 1px solid #6C6C6C;
				-webkit-border-radius: 2px;
				border-radius: 2px;
				margin-bottom: .5em;
			}

		</style>
	</head>

	<body>
		<header id="header">
			<div class="nvbt iback" onclick="back()"></div>
			<div class="nvtt">Share</div>
			<div class="nvbt idoc" onclick="openDoc('Share Document','/doc/share.html')"></div>
		</header>
		<div id="dcontent" class="dcontent">
			<br />
			<p class="heading">分享內容:</p>
			<textarea id="sharecontent" rows="3">我正在使用HBuilder+HTML5開發移動應用,趕緊跟我一起來體驗!</textarea>
			<br /><br />
			<p class="heading">分享圖片:</p>
			<table style="width:100%;">
				<tbody>
					<tr>
						<td style="width:30%">
							<div class="button button-select" onclick="shareCameraPicture()">拍照</div>
						</td>
						<td style="width:30%">
							<div class="button button-select" onclick="shareGalleryPicture()">相冊選取</div>
						</td>
						<td style="width:30%">
							<div class="button button-select" onclick="shareLogoPicture()">使用logo圖</div>
						</td>
					</tr>
				</tbody>
			</table>
			<img id="pic" src="../img/add.png" />
			<br />
			<div class="button" onclick="shareShow()">分 享</div>
			<div class="button" onclick="shareSystem()">系統分享</div>
			<div style="padding: 0.5em 1em;">
				<hr color="#EEE" />
			</div>
			<p class="heading">鏈接地址:</p>
			<input id="sharehref" class="sharehref" type="url" value="http://www.dcloud.io/" placeholder="請輸入要分享的鏈接地址" />
			<p class="heading">鏈接標題:</p>
			<input id="sharehrefTitle" class="sharehref" type="text" value="DCloud HBuilder-做最好的HTML5開發工具" placeholder="請輸入要分享的鏈接標題" />
			<p class="heading">鏈接描述:</p>
			<input id="sharehrefDes" class="sharehref" type="text" value="我正在使用HBuilder+HTML5開發移動應用,趕緊跟我一起來體驗!" placeholder="請輸入要分享的鏈接描述" />
			<div class="button" onclick="shareHref()">分享鏈接</div>
			<br /><br />
			<div style="padding: 0.5em 1em;">
				<hr color="#EEE" />
			</div>
			<p class="des">如果需要解除分享中綁定的用戶信息,請點擊解除授權:</p>
			<div class="button" onclick="cancelAuth()">解除授權</div>
		</div>
		<div id="output">
			Share模塊管理客戶端的社交分享功能,提供調用終端社交軟件的分享能力。通過plus.share可獲取社交分享管理對象。
		</div>
	</body>

</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章