第一種(只分享給好友)
document.getElementById('friends').addEventListener('tap', function() {
var shares;
mui.plusReady(function() {
plus.share.getServices(function(list) {
for(var i = 0; i < list.length; i++) {
if(list[i].authenticated) {
shares = list[i];
}
}
if(shares != undefined) {
var msg = {
type: 'image',
pictures: ["_www/logo.png"],//這裏的pictures是要分享的圖片 該圖片放在項目根目錄下
extra: {
scene: "WXSceneSession"
}
};
shares.send(msg, function(success) {
mui.toast("轉發成功!");
}, function(error) {
//mui.toast("轉發失敗"+JSON.stringify(error));
console.log(JSON.stringify(error))
});
}
}, function(e) {
alert("獲取分享服務列表失敗:" + e.message);
});
});
});
第二種(分享到微信好友、微信朋友圈、qq好友)
<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);
});
}
/**
* 分享操作
* @param {JSON} sb 分享操作對象s.s爲分享通道對象(plus.share.ShareService)
* @param {Boolean} bh 是否分享鏈接
*/
function shareAction(sb,bh) {
console.log('heiheihei')
var msg={content:"你好啊",extra:{},"pictures":["_www/logo.png"]};
// 發送分享
if(sb.s.authenticated){
console.log('---已授權---');
shareMessage(msg, sb.s);
}else{
console.log('---未授權---');
sb.s.authorize(function(){
// shareMessage(msg,sb.s);
}, function(e){
console.log('認證授權失敗:'+e.code+' - '+e.message);
});
}
}
/**
* 發送分享消息
* @param {JSON} msg
* @param {plus.share.ShareService} s
*/
function shareMessage(msg, s){
console.log(JSON.stringify(msg));
s.send(msg, function(){
console.log('分享到"'+s.description+'"成功!');
}, function(e){console.log('分享到"'+s.description+'"失敗: '+JSON.stringify(e));
});
}
// 使用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){
console.log('讀取Logo文件錯誤:'+e.message);
});
}
// 打開分享
function shareShow(){
console.log("asdgjkhg");
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['qq'];
ss&&ss.nativeClient&&shareBts.push({title:'QQ',s:ss});
// shareAction(shareBts[e.index-1],false);
plus.nativeUI.actionSheet({title:'分享',cancel:'取消',buttons:shareBts}, function(e){
(e.index>0)&&shareAction(shareBts[e.index-1],false);
});
}
</script>
<div class="button" onclick="shareShow()">分 享</div>