cocos creator 微信小游戏接口

/*
 * @Description:微信小游戏
 * @version: 
 * @Author: lvpeijun
 * @Date: 2019-12-02 10:43:41
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-08-06 20:59:33
 */
export class WXMiniGame{
    advert = null;
    advertCb = null;
    isSharing = false;
    init = false;
    constructor() {
        super();
        wx.onShow(this.wxShowBack.bind(this));
    }

    /**
     * 检查是否授权 授权后才能获取玩家信息
     * @param cb  回调
     */
    checkAuthorized(cb) {
        let self = this;
        wx.getSetting({
            success(res) {
                // console.log('xxxxxxxxxxxxxx getSetting ', res);
                if (!res.authSetting['scope.userInfo']) {
                    //没有授权
                    cb.call(null, false);
                } else {
                    //授权过
                    self.getUserInfo();
                    cb.call(null, true);
                }
            }
        })
        return true;
    }

    /**
     * 获取玩家信息
     */
    getUserInfo() {
        let self = this;
        wx.getUserInfo({
            success: function (res) {
                var userInfo = res.userInfo
                // console.log('xxxxxxxx wx game user info', userInfo);
                self.userInfo = userInfo;
            },
        })
    }

    /**
     * 创建授权按钮进行授权
     * @param node 授权按钮的位置大小与传过来的node一致
     * @param cb 回调
     */
    private createAuthorizeBtn(node: cc.Node, cb) {
        let btnSize = cc.size(node.width, node.height);
        let frameSize = cc.view.getFrameSize();
        let winSize = cc.director.getWinSize();
        // console.log("winSize: ",winSize);
        // console.log("frameSize: ",frameSize);
        //适配不同机型来创建微信授权按钮
        let left = (winSize.width * 0.5 + node.x - btnSize.width * 0.5) / winSize.width * frameSize.width;
        let top = (winSize.height * 0.5 - node.y - btnSize.height * 0.5) / winSize.height * frameSize.height;
        let width = btnSize.width / winSize.width * frameSize.width;
        let height = btnSize.height / winSize.height * frameSize.height;
        // console.log("button pos: ",cc.v2(left,top));
        // console.log("button size: ",cc.size(width,height));
        let self = this;
        let btnAuthorize = wx.createUserInfoButton({
            type: 'text',
            text: '',
            style: {
                left: left,
                top: top,
                width: width,
                height: height,
                lineHeight: 0,
                backgroundColor: '',
                color: '#ffffff',
                textAlign: 'center',
                fontSize: 16,
                borderRadius: 4
            }
        })

        btnAuthorize.onTap((uinfo) => {
            console.log("onTap uinfo: ", uinfo);
            if (uinfo.userInfo) {
                //授权成功
                // console.log('xxxxxxxx wx game user info', userInfo);
                // wx.showToast({ title: "授权成功" });
                self.userInfo  = uinfo.userInfo
                btnAuthorize.destroy();
                cb.call(null, true);
            } else {
                //授权失败
                // console.log('xxxxxxxx wx game user fail info');
                // wx.showToast({ title: "授权失败" });
                cb.call(null, false);
            }
        });
    }

    /**
     * 获取设备信息
     */
    getAreaHeight() {
        wx.getSystemInfo({
            success(res) {
                // console.log('xxxxx weChatMiniGame  area height' + JSON.stringify(res));
            }
        })
    }

    /**
     * 能不能看视频
     */
    canShowAdvert() {
        return false;
    }

    /**
     * 能不能分享
     */
    canShowShare() {
        return true;
    }

    /**
     * 初始化激励视频
     */
    initAdvert() {
        let self = this;
        this.advert = null;
        let rewardedVideoAd = null;
        // console.log('xxxxxxxxx wx minigame initAdvert');
        if (wx.createRewardedVideoAd) {
            rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: 'xxxx' })
            rewardedVideoAd.onLoad(() => {
                // console.log('xxxxxxxxx onLoad event emit');
                this.advert = rewardedVideoAd;
            })
            rewardedVideoAd.onError((err) => {
                // console.log('xxxxxxxxx onError event emit', err)
            })
            rewardedVideoAd.onClose((res) => {
                if (res && res.isEnded) {
                    self.videoCb(true);
                    // console.log('xxxxxxxxx advert onClose event emit true');
                    // 正常播放结束,可以下发游戏奖励
                } else {
                    self.videoCb(false);
                    // console.log('xxxxxxxxx advert onClose event emit false');
                    // 播放中途退出,不下发游戏奖励
                }
            })
        }
    }

    /**
     * 观看激烈视频的回调
     * @param success 
     */
    videoCb(success) {
        if (this.advertCb) {
            this.advertCb.call(null, success, 'video_weChat');
        }
        this.advertCb = null;
    }

    /**
     * 观看激烈视频
     * @param cb 
     */
    showAdvert(cb) {
        if (!this.advert) {
            console.log('xxxxxxxxx wx advert null!');
            cb.call(null, false);
            return;
        }
        this.advertCb = cb;
        this.advert.show();
    }
    /**
     * 是否使用超链接
     */
    useSSL() {
        return false;
    }

    /**
     * 分享
     * @param param
     */
    share(param) {
        this.isSharing = true;
        //原来微信分享
        // wx.shareAppMessage({
        //     title: title,
        //     imageUrl: "https://abc.com/" + icon,//cc.url.raw('img.png'),        // 分享图片要放在 wechatgame/res/raw-assers 路径下
        //     query: 'param'//'分享卡片上所带的信息'  // query最大长度(length)为2048
        // });

        //接入umeng后微信分享
        var data = wx.uma.trackShare({
            query: 'param',
            title: title,
            imageUrl: "https://abc.com" + icon,
        });
        wx.shareAppMessage(data);

    }

    /**
     * 分享回调
     * @param data 
     */
    wxShowBack(data) {
        if (this.isSharing) {
            //分享回调
            // cc.log('xxxxxxxxxxx weChat show back', data);
            this.isSharing = false;
        } else {
            if (this.init && data.query && data.query.param) {
                // console.log('xxxxxxxxxxx weChat show back param:', param);
            }
        }
    }

    /**
     * 获取登录参数
     */
    getLaunchParam() {
        let data = wx.getLaunchOptionsSync();
        this.init = true;
        if (data.query && data.query.param) {
            // console.log('xxxxxxxxxxxxxxxxxxx param:', data.query.param);
            return data.query.param;
        }
        return null;
    }

    //设置umeng openid
    setOpenId(id){
        // cc.log('xxxxxxxxxxx weChat open Id:',id);
        wx.uma.setOpenid(id);
    }
}
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐