(function() {"use strict";var __module = CC_EDITOR ? module : {exports:{}};var __filename = 'preview-scripts/assets/Script/Tools/wx.js';var __require = CC_EDITOR ? function (request) {return cc.require(request, require);} : function (request) {return cc.require(request, __filename);};function __define (exports, require, module) {"use strict";
cc._RF.push(module, '57711CNOOhFU66yj7mhrSk7', 'wx', __filename);
// Script/Tools/wx.ts

var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var ImportCDNJS_1 = require("./ImportCDNJS");
var axios_1 = require("./axios");
var queryString = require('./queryString');
var tokenName = 'relexToken';
var userInfoKey = 'relexUserInfo';
exports.default = (function (_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.title, title = _c === void 0 ? '西瓜PK赛，悦克免费烟弹送一年' : _c, _d = _b.desc, desc = _d === void 0 ? 'RELX悦刻新口味烟弹上市啦！冰镇西瓜、清爽黄瓜、清香绿茶、点击快速领取' : _d, _e = _b.link, link = _e === void 0 ? location.href : _e, _f = _b.imgUrl, imgUrl = _f === void 0 ? 'https://h5.parsec.com.cn/common/relexlogo1.png' : _f, _g = _b.openid, openid = _g === void 0 ? queryString('openid') || localStorage[tokenName] : _g, _h = _b.appId, appId = _h === void 0 ? 'wx56b01188481383cb' : _h, _j = _b.isNeedLogin, isNeedLogin = _j === void 0 ? true : _j;
    return ImportCDNJS_1.default('//res.wx.qq.com/open/js/jweixin-1.2.0.js', 'wx')
        .then(function (wx) { return __awaiter(_this, void 0, void 0, function () {
        var doLogin, href;
        var _this = this;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    window['openid'] = openid;
                    if (!((openid === 'undefined' || !openid) && isNeedLogin)) return [3 /*break*/, 2];
                    doLogin = function () { return __awaiter(_this, void 0, void 0, function () {
                        return __generator(this, function (_a) {
                            switch (_a.label) {
                                case 0: return [4 /*yield*/, axios_1.default.post('https://third.parsec.com.cn/api/wxuser/auth', {
                                        appid: appId,
                                        redirect_uri: location.href,
                                        return_token: 0,
                                    }).then(function (_a) {
                                        var url = _a.data.result.url;
                                        window.location.href = url;
                                        return Promise.reject();
                                    })];
                                case 1:
                                    _a.sent();
                                    return [2 /*return*/];
                            }
                        });
                    }); };
                    return [4 /*yield*/, doLogin()];
                case 1:
                    _a.sent();
                    return [3 /*break*/, 3];
                case 2:
                    localStorage[tokenName] = openid;
                    href = window.location.href;
                    href = href.replace("openid=" + openid + "&appid=" + appId, '');
                    if (window.location.href !== href) {
                        window.location.href = href;
                        return [2 /*return*/, Promise.resolve()];
                    }
                    _a.label = 3;
                case 3: return [4 /*yield*/, axios_1.default.post('https://third.parsec.com.cn/api/js_ticket', {
                        appid: appId,
                        t: 4,
                        url: location.href,
                    }, { headers: { 'content-type': 'application/json' } }).then(function (_a) {
                        var config = _a.data.result;
                        wx.config(__assign({ debug: false, jsApiList: Object.keys(wx) }, config));
                    })];
                case 4:
                    _a.sent();
                    // if (openid) {
                    //     await axios.get('https://games.parsec.com.cn/game-0011/user_info', {
                    //         params: {
                    //             appid: appId,
                    //             openid,
                    //         }
                    //     })
                    //         .then(({data}) => {
                    //             localStorage[userInfoKey] = JSON.stringify(data);
                    //             window['nickname'] = data.nickname;
                    //         });
                    // }
                    wx.error(function (e) {
                        console.log('wx sdk errors:', e);
                    });
                    wx.ready(function () {
                        console.log('wx ready');
                        share(title, desc, link, imgUrl, wx);
                    });
                    return [2 /*return*/];
            }
        });
    }); });
});
function share(title, desc, link, imgUrl, wx) {
    // 朋友圈
    wx.onMenuShareTimeline({
        title: title,
        link: link,
        imgUrl: imgUrl,
    });
    // 分享给朋友
    wx.onMenuShareAppMessage({
        title: title,
        desc: desc,
        link: link,
        imgUrl: imgUrl,
    });
    // QQ
    wx.onMenuShareQQ({
        title: title,
        desc: desc,
        link: link,
        imgUrl: imgUrl,
    });
    // 腾讯微博
    wx.onMenuShareWeibo({
        title: title,
        desc: desc,
        link: link,
        imgUrl: imgUrl,
    });
    // QQ空间
    wx.onMenuShareQZone({
        title: title,
        desc: desc,
        link: link,
        imgUrl: imgUrl,
    });
}

cc._RF.pop();
        }
        if (CC_EDITOR) {
            __define(__module.exports, __require, __module);
        }
        else {
            cc.registerModuleFunc(__filename, function () {
                __define(__module.exports, __require, __module);
            });
        }
        })();
        //# sourceMappingURL=wx.js.map
        