CocosCreator微信小游戏带参数分享 |
您所在的位置:网站首页 › 微信小程序分享按钮带参数怎么弄 › CocosCreator微信小游戏带参数分享 |
微信小游戏分享分为被动分享和主动分享。官方文档:https://developers.weixin.qq.com/minigame/dev/tutorial/open-ability/share/share.html 被动分享被动分享为点击小程序右上角,在弹出的菜单栏中选择分享按钮进行分享。菜单栏要显示分享按钮,必须先开启分享wx.showShareMenu();,然后定义wx.onShareAppMessage事件,此事件需要 return 一个 Object,用于自定义转发内容。 Cocos中代码如下: private openWXShareMenu(): void { wx.showShareMenu(); cc.loader.loadRes("texture/shareImg", function (error, data) { wx.onShareAppMessage(function (res) { return { title: "不服来战啊", imageUrl: data.url, success(res) { console.log("转发成功"); }, fail(res) { console.log("转发失败"); } } }); }); } 主动分享主动分享为通过其它按钮调用微信的分享功能。核心API为:wx.shareAppMessage 传参只需要设置上述API中的query参数。 CocosCreator中: if (cc.sys.isMobile && typeof (wx) !== "undefined") { //WX小游戏移动设备环境下 cc.loader.loadRes("texture/shareImg", function (error, data) { wx.shareAppMessage({ title: "嘿嘿,我做了一个关卡,不服来战!", imageUrl: data.url, query: "diy=" + jsonStr, success(res) { wx.showToast({ title: "分享成功" }); console.log("分享成功" + res); } fail(res) { console.log("分享失败" + res); } }); }); }texture/shareImg为放在CocosCreator中resources目录下的图片文件。 取参数: wx.getLaunchOptionsSync()可以获得小游戏启动相关参数。 if (cc.sys.isMobile && typeof (wx) !== "undefined") { //WX小游戏移动设备环境下 var launch = wx.getLaunchOptionsSync(); var queryData = launch.query; var diyData = queryData.diy; if (diyData != null && diyData != "") { console.log("微信透传参数:" + diyData); } else { console.log("微信平台无透传参数"); } } |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |