mui-微信分享功能

分享按钮触发

document.getElementById('openShare').addEventListener('tap', function() {
			console.log("wo idanji");
			mui.openWindow({
				id:'share',
				url:'../share/shareTest.html',
				extras:{//传参数。输入的内容
					//userid:json_data.user.id,
					//shareurl:'www.t*****/share.do',、后台的地址,需要被分享的人所访问到的网页内容(可包含软件的下载链接)
					shareurl:url+'/share.do',
					shareid:houseId,
					sharetype:1,
					sharetitle:title,
					sharedes:miaoshu,
				}
			});
	})

  分享事件

<!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 src="../js/mui.min.js"></script>
        <script src="../js/jquery.min.js"></script>
        <script type="text/javascript" src="../js/common.js"></script>
        <script type="text/javascript">
var shareurl;
var shareid;
var sharetitle;
var sharedes;
var sharetype;

var shares=null;
var sweixin=null;
var buttons=[
  {title:'我的好友',extra:{scene:'WXSceneSession'}},
  {title:'朋友圈',extra:{scene:'WXSceneTimeline'}},
  {title:'我的收藏',extra:{scene:'WXSceneFavorite'}}
];
// H5 plus事件处理
function plusReady(){
    updateSerivces();
    shareurl = plus.webview.currentWebview().shareurl;
    console.log(shareurl);
    //localStorage.setItem("shareurl",shareurl);
    
    shareid = plus.webview.currentWebview().shareid;
    //localStorage.setItem("shareid",shareid);
    sharetype = plus.webview.currentWebview().sharetype;
    //localStorage.setItem("sharetype",sharetype);
    //sharetitle
    sharetitle = plus.webview.currentWebview().sharetitle;
    //localStorage.setItem("sharetitle",sharetitle);
    //sharedes
    sharedes = plus.webview.currentWebview().sharedes;
    //localStorage.setItem("sharedes",sharedes);
            
}
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;
        }
    sweixin=shares['weixin'];
    }, function(e){
        outSet('获取分享服务列表失败:'+e.message);
    });
}

// 分享网页
function shareWeb(){
  var msg={type:'web',thumbs:['../images/morentouxiang.png']};
  shareurl = shareurl+"?id="+shareid+"&sharetype="+sharetype;
  msg.href=shareurl;
  msg.title=sharetitle;
  msg.content=sharedes;
  sweixin?plus.nativeUI.actionSheet({title:'Share a web page to WeChat',cancel:'Cancel',buttons:buttons}, function(e){
      (e.index>0)&&share(sweixin, msg, buttons[e.index-1]);
  }):plus.nativeUI.alert('WeChat sharing is not supported in the current environment!'); 
}
// 分享
function share(srv, msg, button){
    outSet('分享操作:');
  if(!srv){
    outLine('无效的分享服务!');
    return;
  }
  button&&(msg.extra=button.extra);
    // 发送分享
    if(srv.authenticated){
        outLine('---已授权---');
        doShare(srv, msg);
    }else{
        outLine('---未授权---');
        srv.authorize(function(){
            doShare(srv, msg);
        }, function(e){
            outLine('认证授权失败:'+JSON.stringify(e));
        });
    }  
}
// 发送分享
function doShare(srv, msg){
    outLine(JSON.stringify(msg));
    srv.send(msg, function(){
        outLine('分享到"'+srv.description+'"成功!');
        mui.toast("share success!");
    }, function(e){
        outLine('分享到"'+srv.description+'"失败: '+JSON.stringify(e));
    });
}
// 解除授权
function cancelAuth(){
    outSet('解除授权:');
  if(sweixin){
    if(sweixin.authenticated){
        outLine('取消"'+sweixin.description+'"');
    }
    sweixin.forbid();
  }else{
    outLine('当前环境不支持微信分享操作!');
  }
}
        </script>
        <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
        <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')" style="display: none;"></div>
        </header>
        <div id="dcontent" class="dcontent">
            <br/>
            <div class="button" onclick="shareWeb()">WeChat Share</div>
            <br/>
        </div>
        <div id="output" style="display: none;">

        </div>
    </body>
    <script type="text/javascript" src="../js/immersed.js" ></script>
</html>

注意:需要申请微信开发者,申请appid,才能授权访问微信接口

 

posted @ 2019-09-06 15:43  小白咚  阅读(997)  评论(0编辑  收藏  举报