用模拟器试了好几次,分享服务列表返回的都是空数组,需要用真机测试,最终用真机测试正常 HbuilderX配置如下
vue页面的 script import shareJS from "../request/share.js" var shareUrl = '', //分享链接 detail = {}; document.addEventListener("plusready",function(){ shareJS.service() }, false); vue分享页面 methods // 点击分享按钮 shareHref() { 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) { if (i > 0) { shareJS.shareAction(ids[i - 1].id, ids[i - 1].ex,shareUrl,detail); } } }) }, 封装的js share.js import Vue from 'vue' import { Toast } from 'vant' Vue.use(Toast); let Share = new Object(); var shares = null; Share.service = function(){ plus.share.getServices(function(s){ shares={}; for(var i in s){ var t=s[i]; shares[t.id]=t; } console.log(JSON.stringify(s)); }, function(e){ alert("获取分享服务列表失败: "+JSON.stringify(e)); }); } /** * 分享操作 */ Share.shareAction = function(id, ex,shareUrl,detail) { var s = null; if (!id || !(s = shares[id])) { Toast("无效的分享服务!"); return; } if (s.authenticated) { console.log("---已授权---"); shareMessage(s, ex,shareUrl,detail); } else { console.log("---未授权---"); s.authorize(function() { shareMessage(s, ex,shareUrl,detail); }, function(e) { Toast("认证授权失败"); }); } } /** * 发送分享消息 */ function shareMessage(s, ex,shareUrl,detail) { var msg = { href: shareUrl, title: detail.name, content: detail.name, thumbs: [detail.goods_image_text], pictures: [detail.goods_image_text], extra: { scene: ex } }; s.send(msg, function() { Toast("分享成功!"); }, function(e) { Toast("分享失败!"); }); } export default Share;