t-comm
Version:
专业、稳定、纯粹的工具库
110 lines (107 loc) • 4.13 kB
JavaScript
import { b as __awaiter, c as __generator } from '../../tslib.es6-096fffdd.js';
import { isTestEnvInPixui } from '../env.mjs';
import { getGameletPixuiFrame } from '../gamelet.mjs';
// 加载 uid 和 aid
var initUserData = function initUserData(aegis) {
return __awaiter(void 0, void 0, void 0, function () {
var GameletAPI, _a, sOpenId, openID, thisAid, lastAid, aid;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
GameletAPI = getGameletPixuiFrame().GameletAPI;
return [4 /*yield*/, GameletAPI.getUserData()];
case 1:
_a = _c.sent(), sOpenId = _a.sOpenId, openID = _a.openID;
thisAid = (_b = window.localStorage) === null || _b === void 0 ? void 0 : _b.getItem('AEGIS_ID');
if (!GameletAPI.canUsePlatformAPI()) return [3 /*break*/, 3];
return [4 /*yield*/, GameletAPI.readCookie('AEGIS_ID')];
case 2:
lastAid = _c.sent();
_c.label = 3;
case 3:
aid = lastAid || thisAid;
if (!lastAid && thisAid) {
GameletAPI.writeCookie('AEGIS_ID', thisAid);
}
if (sOpenId || openID) {
aegis.setConfig({
uid: sOpenId || openID
});
}
if (aid) {
aegis.setConfig({
aid: aid
});
}
return [2 /*return*/];
}
});
});
};
// URL 处理,需要自定义,不同游戏不太一样
var urlHandler = function urlHandler() {
var _a;
// 原本的获取方式: let url: string = this.config.pageUrl || location.href || '';
var path = location.pathname.replace('/index.html', '');
var search = ((_a = location.search) === null || _a === void 0 ? void 0 : _a.indexOf('?')) > -1 || !location.search ? location.search || '' : "?".concat(location.search);
// 上报时获取最后 2 个路径,如 /xxxx/Client/Saved/Gamelet/extract/5535/gamelet5535social_bin/app/feedsHome,只会取到 /app/feedsHome
var reportPath = "/".concat(path.split('/').filter(function (p) {
return p !== '';
}).slice(-2).join('/'));
return reportPath + search;
};
// 返回码处理,一般可以复用
var retCodeHandler = function retCodeHandler(obj, _url, _xhr, _payload) {
var returnCode = "".concat(((obj === null || obj === void 0 ? void 0 : obj.returnCode) || (obj === null || obj === void 0 ? void 0 : obj.code) || (obj === null || obj === void 0 ? void 0 : obj.retcode) || (obj === null || obj === void 0 ? void 0 : obj.errcode) || 'unknown').toString());
return {
code: returnCode,
isErr: returnCode !== '0' && returnCode !== 'unknown'
};
};
function isTestEnvironment() {
return isTestEnvInPixui();
}
function initAegis(options) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var env, aegis;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
env = isTestEnvironment() ? options.Aegis.environment.TEST : options.Aegis.environment.PROD;
if (window.mAegis) {
return [2 /*return*/, window.mAegis];
}
(_a = options === null || options === void 0 ? void 0 : options.setupPolyfills) === null || _a === void 0 ? void 0 : _a.call(options);
aegis = new options.Aegis({
id: options.aegisKey,
env: env,
plugin: {
session: true,
spa: true,
api: {
apiDetail: true,
injectTraceHeader: 'traceparent',
reqHeaders: ['traceparent'],
retCodeHandler: retCodeHandler
},
assetSpeed: true
},
reportImmediately: false,
urlHandler: urlHandler,
hostUrl: options.url
});
return [4 /*yield*/, initUserData(aegis)];
case 1:
_b.sent();
return [4 /*yield*/, aegis.ready()];
case 2:
_b.sent();
window.mAegis = aegis;
return [2 /*return*/, aegis];
}
});
});
}
export { initAegis };