cocos-creator使用记录38_小游戏跳小游戏


1.前言
小游戏跳小游戏的微信接口是在2018.7.16左右开放的,只有微信6.7.1以上的版本才支持。
相互跳转的小游戏必须属于同一个公众号。

2.代码
common.js-------------
module.exports = {
    wechatVersion: null, //微信的版本
    getWechatVersion: function(){ //获得微信版本
        if(!(cc.sys.platform === cc.sys.WECHAT_GAME)) return;
        wx.getSystemInfo({
            success(res){
                this.wechatVersion = res.version;
            }
        });
    },
    compareVersion: function (v1, v2) {//比较版本
        v1 = v1.split('.');
        v2 = v2.split('.');
        var len = Math.max(v1.length, v2.length);
        while (v1.length < len) {
            v1.push('0');
        }
        while (v2.length < len) {
            v2.push('0');
        }
        for (var i = 0; i < len; i++) {
            var num1 = parseInt(v1[i]);
            var num2 = parseInt(v2[i]);
            if (num1 > num2) {
                return 1;
            } else if (num1 < num2) {
                return -1;
            }
        }
        return 0;
    },
    isBiggerThanWechatVersion: function(targetVersion){ //微信当前版本是否大于指定版本
        if(!this.wechatVersion || !targetVersion) return false;
        return this.compareVersion(this.wechatVersion, targetVersion) == 1;
    },
    toOtherSmallGame(targetAppId, targetImage, targetPath){ //跳转其他小游戏
        var image = targetImage;
        if(!wx.navigateToMiniProgram) {
            if(image){
                wx.previewImage({
                    current: image,  
                    urls: [image]
                });  
            }
            return;
        }
        var appid = "";
        var extra = "";
        var appname = "";
        var appstasticsname = "";
        var appversion = "";
        if(targetPath){
            appid = targetPath.split("appid=")[1].split("&")[0];
            extra = targetPath.split("extra=")[1].split("&")[0];
            appname = targetPath.split("appname=")[1].split("&")[0];
            appstasticsname = targetPath.split("appstasticsname=")[1].split("&")[0];
            appversion = targetPath.split("appversion=")[1].split("&")[0];
        }
        wx.navigateToMiniProgram({
            appId: targetAppId || "", //目标小游戏的appId,必须与当前小游戏属于同一个公众号
            path: targetPath || "",
            extraData: {
                appid: appid,
                extra: extra, //这里必须为json格式的字符串
                appname: appname,
                appstasticsname: appstasticsname,
                appversion: appversion,
                apppath: ""
            },
            envVersion: 'release',    //跳转的目标小游戏版本,develop(开发版),trial(体验版),release(正式版)
            success(res) {
                console.log("跳转XXXX", res);
            },
            fail(res){
                if(image){
                    wx.previewImage({
                        current: image,  
                        urls: [image]
                    });  
                }
            }
        });
    },
};
start.js-------------
onMoreGameBtn: function(){ //更多游戏按钮
    if(!(cc.sys.platform === cc.sys.WECHAT_GAME)) return;
    common.toOtherSmallGame(this.adsAppId, this.adsImageUrl, this.adsPath); 
},

3.注意
据说小程序API的跳转接口wx.navigateToMiniProgram即将弃用,不知道小游戏的会不会弃用,先用着吧。
另外,发现在Android下,不需要升级到6.7.1,也可以直接跳转。

猜你喜欢

转载自blog.csdn.net/haibo19981/article/details/81251928