

var shareConfig = {
    gameId: "100019",
    shareLine: "zDLsruVI",
    EK:"hui231%1"
  };

// 定义微信配置数据的接口
interface IWeChatConfig {
    appId: string;
    timestamp: number;
    nonceStr: string;
    signature: string;
    jsApiList: [];
}

// 微信操作类
export class WeChat {
    static setShare(url) {
        var urlTemp = this.removeQueryParams(url);
        shareConfig.shareLine = urlTemp;
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            WeChat.getSignature(url);
        }
   
    }

    static getResult(res){
        if(res){
            var data = res.data;
            wx.config({
                debug: false,
                appId: data.appId,
                timestamp: data.timestamp,
                nonceStr: data.nonceStr,
                signature: data.signature,
                jsApiList: ['onMenuShareTimeline','updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareAppMessage']
            });
            wx.checkJsApi({
                jsApiList: ['updateAppMessageShareData'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
                success: function(res) {
                    setTimeout(() => {  
                        WeChat.changeShare();
                    }, 100);
                    setTimeout(() => {
                        WeChat.changeShare();
                    }, 200);
                }
            });
        }
    }

    static changeShare(){
        wx.ready(() => {
            wx.updateAppMessageShareData({ 
                title: '逻辑性认知测评', // 分享标题
                desc: '你的认知灵活性和选择注意有问题吗', // 分享描述
                link: shareConfig.shareLine, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: 'https://static.sparkus.cn/public/shennong.jpg', // 分享图标
                success: function () {
                  // 设置成功
                  console.log("分享好友成功回调");
                }
            });
            setTimeout(() => {
                wx.updateTimelineShareData({ 
                    title: '逻辑性认知测评', // 分享标题
                    link: shareConfig.shareLine, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                    imgUrl: 'https://static.sparkus.cn/public/shennong.jpg', // 分享图标
                    success: function () {
                      // 设置成功
                      console.log("分享朋友圈成功回调");
                    }
                  })
            }, 500);

        }); 
    }

    static getSignature(url: string): Promise<IWeChatConfig> {
        return new Promise((resolve) => {
            WeChat.getShareInfo((encodeURIComponent(url)),WeChat.getResult);
        });
    }
    static async getShareInfo(shareUrl: string, callback:Function): Promise<any> {
        const time = Math.floor((new Date().getTime()) / 1000)
        const url = cc.fx.HttpUtil.apiSign(`/api/share/cfg?gameId=${shareConfig.gameId}&time=${time}&url=${shareUrl}`,{})
        return cc.fx.HttpUtil.get(url,callback,0);
    }

    
    static containsNanana(str) {
        return /test/i.test(str);
    }

    static removeQueryParams(url) {
        return url.replace(/\?.*$/, '');
    }

}
