很奇怪的plus.share.getServices 获取的无论是分享还是打开小程序都无法调起微信,没有任何反应,但是直接调用plus.share.sendWithSystem却是能够调起分享功能,
这里的代码能正确的调起微信分享功能
let msg = {
content: "DCloud HBuilder-做最好的HTML5开发工具",
href: "http://www.dcloud.io/",
extra: {
scene: "WXSceneSession"
} // "WXSceneSession"分享给好友,"WXSceneTimeline"分享到朋友圈,"WXSceneFavorite"分享到微信收藏
}
plus.share.sendWithSystem(msg, function() {
console.log('分享成功');
}, function(e) {
alert('分享失败:' + JSON.stringify(e))
console.log('分享失败:' + JSON.stringify(e));
})
但是用plus.share.getServices下面的代码调用分享却报错,appid确实是用的微信开放平台申请的app的appid,universal links配置的也没问题,浏览器打开会提示打开app。
{
"code": -100,
"message": "[Share微信:-99]appid无效或配置错误,https://ask.dcloud.net.cn/article/287"
}
let shares = null;
let sweixin = null; // 需调用plus.share.getServices获取微信分享服务对象
// 获取分享服务列表
plus.share.getServices(function(res) {
shares = res; // 这个回调函数的参数 res 就包含了 所有对象的数组
console.log('shares', shares);
// 在这个数组里 找到属于微信的对象 循环匹配查找
for (let i = 0; i < res.length; i++) {
if ("weixin" == res[i].id) {
sweixin = res[i]; // 保存到变量里 (之后即可使用该对象发起分享)
}
}
let msg = {
content: "DCloud HBuilder-做最好的HTML5开发工具",
href: "http://www.dcloud.io/",
extra: {
scene: "WXSceneTimeline"
} // "WXSceneSession"分享给好友,"WXSceneTimeline"分享到朋友圈,"WXSceneFavorite"分享到微信收藏
}
console.log('sweixin', sweixin);
// msg对象的属性和参数 官方文档里有说明
// 这里就用到了上面获取到的 微信对象 使用send发起分享
sweixin.send(msg, function() {
console.log("分享成功");
}, function(error) {
console.log("分享失败", error);
})
}, function(error) {
console.log("获取分享服务列表失败:" + JSON.stringify(error));
});
然后调用这里的唤起微信小程序更是到sweixin.launchMiniProgram没有任何报错信息,也调不起来微信小程序
let shares = null;
let sweixin = null; // 需调用plus.share.getServices获取微信分享服务对象
// 获取分享服务列表
plus.share.getServices(function(res) {
shares = res; // 这个回调函数的参数 res 就包含了 所有对象的数组
console.log('shares', shares);
// 在这个数组里 找到属于微信的对象 循环匹配查找
for (let i = 0; i < res.length; i++) {
if ("weixin" == res[i].id) {
sweixin = res[i]; // 保存到变量里 (之后即可使用该对象发起分享)
}
}
const miniProgramParams = {
id: 'gh_07fe923xxxx',
path: ``,
type: 0
};
sweixin.launchMiniProgram(miniProgramParams,
function(success) {
console.log('launchMiniProgram成功:', success);
uni.showToast({
title: '跳转成功',
icon: 'success'
});
},
function(err) {
console.log('launchMiniProgram失败:', err);
uni.showToast({
title: '跳转失败:' + (err.message ||
'未知错误'),
icon: 'none'
});
});
}, function(error) {
console.log("获取分享服务列表失败:" + JSON.stringify(error));
});
Acefzp (作者)
图片上传上去了
2025-06-09 11:25