share常见问题我要提意见

Share模块管理客户端的社交分享功能,提供调用终端社交软件的分享能力。通过plus.share可获取社交分享管理对象。

分享接口支持开发者获取设备上安装的社交App环境,调用社交App进行分享操作。若当前设备安装了对应的社交App,则调用此App的分享操作界面,否则调用WAP页面进行分享。

方法:

对象:

回调方法:

权限:

permissions


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

getServices

获取分享服务


void plus.share.getServices(successCB, errorCB);
				

说明:

获取终端支持的分享通道列表,可用于提示用户进行分享列表选择。 成功后通过successCB回调返回当前环境支持的分享服务列表,失败则通过errorCB回调返回错误信息。

参数:

返回值:

void : 无

示例:


var shares=null;
// 监听plusready事件  
document.addEventListener("plusready", function(){
	// 获取分享服务
	plus.share.getServices(function(s){
		shares = s;
	}, function(e){
		alert("获取分享服务列表失败: "+JSON.stringify(e));
	});
}, false);
				

uni-app使用plus注意事项

sendWithSystem

使用系统分享


void plus.share.sendWithSystem(msg, successCB, errorCB);
				

说明:

调用系统分享组件发送分享消息,msg参数中设置分享的内容。 分享成功后通过successCB回调函数通知操作完成,失败则通过errorCB回调返回。

参数:

返回值:

void : 无

平台支持:

示例:


// 使用系统分享发送分享消息 
function shareSystem(){
	plus.share.sendWithSystem({type:'text',content:'分享内容',href:'http://www.dcloud.io/'}, function(){
		console.log('分享成功');
	}, function(e){
		console.log('分享失败:'+JSON.stringify(e));
	});
}
				

uni-app使用plus注意事项

AuthOptions

JSON对象,分享授权认证参数选项


interface plus.share.AuthOptions {
	attribute String appid;
	attribute String appkey;
	attribute String appsecret;
	attribute String redirect_uri;
}
				

说明:

此对象支持的属性值由分享服务的授权认证模块定义。

属性:

示例:


var shares=null;
// 需调用plus.share.getServices获取分享服务列表  
// ...

// 分享操作判断是否认证,如果没有则需要授权认证
function shareAction(){
	var s = shares[0];
	if(!s.authenticated){
		s.authorize(functioin(){
			console.log("认证完成!");
		}, function(e){
			console.log("未进行认证");
		}, {
			"appid": "XXXXXX"
		});
	}
}
				

uni-app使用plus注意事项

GeoPosition

JSON对象,位置信息(将废弃)


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

说明:

GeoPosition对象保存位置信息,用于标识分享操作时用户的位置信息。 注意:目前主流App不再支持设置位置信息,将废弃。

属性:

示例:


/**
 * 发送分享消息
 * @param {plus.share.ShareService} s
 */
function shareMessage(s){
	s.send({content:"Hello",
	    geo: {   // 可以调用plus.geolocation.getCurrentPosition获取当前位置
	        latitude: 39.9074,
	        longitude: 116.3975
	    }
	}, function(){
		alert("分享成功!");
	},function(e){
		alert("分享失败:"+e.message);
	});
}
				

uni-app使用plus注意事项

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, options);
	function void forbid();
	function void send(message);
	function void launchMiniProgram(options);
	function void openCustomerServiceChat(options, successCB, errorCB);
}
				

说明:

ShareService对象用于表示分享服务,在JS中为对象,用于向系统请求分享操作。

属性:

方法:

id

分享服务标识

说明:

ShareServerIdentity 类型 只读属性

用于表示分享服务标识: "sinaweibo" - 表示新浪微博; "weixin" - 表示微信; "qq" - 表示腾讯QQ; "tencentweibo" - 表示腾讯微博(已废弃)。

description

分享服务描述

说明:

DOMString 类型 只读属性

用于描述分享服务的信息: 如“新浪微博”、“腾讯微博”、“微信”、“QQ”。

authenticated

是否授权认证

说明:

Boolean 类型 只读属性

用于标识此分享是否已经授权认证过,true表示已完成授权认证;false表示未完成授权认证。

accessToken

授权认证信息

说明:

DOMString 类型 只读属性

仅在authenticated为true时有效,标识客户认证标识信息,用于发送分享信息。

nativeClient

是否存在对应的分享客户端

说明:

Boolean 类型 只读属性

对于某些分享服务,必须依赖相应客户端App才能实现分享操作;有些分享服务则不依赖,客户端App不存在时调用WAP页面进行分享操作。
具体情况如下:

注意:提交AppStore审核时没有安装相应的客户端App可能导致无法通过审核,此时需要判断相应的客户端App是否安装,如果没有安装则不显示对应的分享入口。

authorize

授权认证操作


						
void obj.authorize(successCallback, errorCallback, options);
						
						

说明:

对指定的分享服务进行授权认证操作,在授权前可通过ShareService.authenticated属性判断是否已经授权过,通常只需要对没有进行过授权认证的服务进行授权认证操作。 授权认证操作成功后通过successCB回调函数通知操作完成,操作失败则通过errorCB回调返回。

参数:

返回值:

void : 无

示例:


var shares=null;
// 获取分享服务列表  
// ...

// 分享操作判断是否认证,如果没有则需要授权认证
function shareAction(){
	var s = shares[0];
	if(!s.authenticated){
		s.authorize(functioin(){
			console.log("认证完成!");
		}, function(e){
			console.log("未进行认证");
		}, {
			"appid": "XXXXXX"
		});
	}
}
				

uni-app使用plus注意事项

forbid

取消授权认证


void obj.forbid();
						

说明:

对指定的分享服务取消授权认证操作,取消授权认证后,再次分享时需重新进行授权操作。

参数:

返回值:

void : 无

示例:


var shares=null;
// 获取分享服务列表  
// ...

// 取消所有分享服务的
function cancelAuthorize(){
	for(var i in shares){
		var s = shares[i];
		if(s.authenticated){
			s.forbid();
		}
	}
}
						

uni-app使用plus注意事项

send

发送分享


void obj.send(msg, successCB, errorCB);
						

说明:

发送分享消息,分享消息的内容通过msg设置。 发送成功后通过successCB回调函数通知操作完成,发送失败则通过errorCB回调返回。若分享服务没有进行授权认证或授权认证失效则触发失败回调函数。

参数:

返回值:

void : 无

示例:


/**
 * 发送分享消息
 * @param {plus.share.ShareService} s
 */
function shareMessage(s){
	s.send({content:"Hello"}, function(){
		alert("分享成功!");
	},function(e){
		alert("分享失败:"+e.message);
	});
}
						

uni-app使用plus注意事项

launchMiniProgram

调用微信小程序


void obj.launchMiniProgram(options, successCB, errorCB);
						

说明:

在微信小程序中打开APP(使用button组件的open-type属性值设置为"launchApp")时,可通过plus.runtime.arguments获取小程序传入的参数(小程序中button组件的app-parameter属性值)。 注意:需在微信开放平台将应用关联小程序才能正常调用。

参数:

返回值:

void : 无

示例:


var sweixin = null; 
// 需调用plus.share.getServices获取微信分享服务对象
//...

/**
 * 调用微信小程序
 */
function launchMiniProgram(){
	sweixin?sweixin.launchMiniProgram({
		id:'gh_33446d7f7a26'
	}):plus.nativeUI.alert('当前环境不支持微信操作!');
}
						

uni-app使用plus注意事项

openCustomerServiceChat

打开微信客服


void obj.openCustomerServiceChat(options, successCB, errorCB);
						

说明:

切换到微信App中,打开微信客服聊天界面。 注意:需在微信开放平台开通微信客服功能。

参数:

返回值:

void : 无

示例:


var sweixin = null; 
// 需调用plus.share.getServices获取微信分享服务对象
//...

/**
 * 打开微信客服
 */
function openWeixinService(){
	sweixin?sweixin.openCustomerServiceChat({
		corpid:'111112222',
		url:'https://www.weixin.com//'
	}):plus.nativeUI.alert('当前环境不支持微信操作!');
}
						

uni-app使用plus注意事项

ShareServerIdentity

分享服务标识

常量:

ShareMessage

JSON对象,分享消息对象


interface plus.share.ShareMessage {
	attribute String type;
	attribute String content;
	attribute String[] thumbs;
	attribute String[] pictures;
	attribute String media;
	attribute String href;
	attribute String title;
	attribute GEOPosition geo;
	attribute ShareMessageExtra extra;
	attribute WeixinMiniProgramOptions miniProgram;
	attribute String interface;
}
				

说明:

ShareMessage对象用于表示分享消息内容,在JS中为JSON对象,用于向系统发送分享信息操作。

属性:

ShareMessageExtra

JSON对象,保存分享消息扩展信息


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

说明:

ShareMessageExtra对象用于保存各分享平台扩展的参数,用于自定义分享功能。

属性:

WeixinMiniProgramOptions

JSON对象,打开微信小程序的信息


interface plus.share.WeixinMiniProgramOptions {
	attribute String id;
	attribute String path;
	attribute Nnumber type;
	attribute webUrl;
}
				

说明:

用于配置分享小程序的参数,如小程序标识、页面路径等。 注意:分享的小程序需要在微信开放平台关联的开发者账号下,否则会分享失败。

属性:

示例:


var sweixin = null; 
// 需调用plus.share.getServices获取微信分享服务对象
//...

// 分享小程序
function shareMiniProgram(){
    if(!sweixin){
		plus.nativeUI.alert('当前环境不支持微信分享操作!');
    }
	var msg={type:'miniProgram',title:'分享小程序标题',thumbs:['_www/mp.png']};
	msg.content = '分享小程序描述内容。';
	msg.miniProgram={id:'g_XXXXXXX',	// 小程序的原始标识
    webUrl:'http://www.dcloud.io/'};
	// 发送分享
	if(sweixin.authenticated){
		doShare(sweixin, msg);
	}else{
		sweixin.authorize(function(){
			doShare(sweixin, msg);
		}, function(e){
			console.log('认证授权失败:'+JSON.stringify(e));
		});
	}
}
				

uni-app使用plus注意事项

WeixinCustomerServiceOptions

JSON对象,打开微信客服的信息


interface plus.share.WeixinCustomerServiceOptions {
	attribute String corpid
	attribute String url;
}
				

说明:

用于配置微信客服的参数,如客服标识、服务地址等。

属性:

ServicesSuccessCallback

获取分享服务成功回调


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

说明:

当获取分享服务列表成功时的回调函数,用于返回终端支持的分享服务列表。

参数:

返回值:

void : 无

AuthorizeSuccessCallback

分享授权认证成功回调


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

说明:

分享服务授权认证操作成功时调用。

参数:

返回值:

void : 无

ShareSuccessCallback

分享操作成功回调


void ShareSuccessCallback(){
	// Share success code
}
				

说明:

分享操作成功回调函数,当分享操作成功时调用。

参数:

返回值:

void : 无

ShareErrorCallback

分享操作失败回调


void ShareErrorCallback(error){
	// Error 
	var code = error.code; 			// 错误编码
	var message = error.message;	// 错误描述信息
}
				

说明:

当分享操作失败时的回调函数,用于返回分享相关操作失败的错误信息。

参数:

返回值:

void : 无