UNPKG

gamelet-pixui-frame

Version:

pixui开发者框架

493 lines 24.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UnityGameletAPI = exports.UnityGameletAPIImpl = void 0; var unityrpc_mgr_1 = require("./unityrpc_mgr"); var local_dispatcher_1 = require("../../utilities/local_dispatcher"); var utilities_1 = require("../../utilities/utilities"); var unitytools_1 = require("./unitytools"); var global_data_1 = require("../../global_data"); var logger_1 = require("./logger"); var faas_tdm_reporter_1 = require("../../utilities/faas_tdm_reporter"); var UnityGameletAPIImpl = /** @class */ (function () { function UnityGameletAPIImpl() { var _this = this; this.dispatchEvent = function (evt) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } unityrpc_mgr_1.UnityGameletAdapter.callLua.apply(unityrpc_mgr_1.UnityGameletAdapter, __spreadArray(["EventDispatcher.DispatchEvent", evt], args, false)); }; global_data_1.GlobalData.runtimeEnv = "unity"; local_dispatcher_1.LocalDispatcher.getInstance().init(); var sdk_info = unitytools_1.UnityTools.getQueryParam("_sdk_info"); if (sdk_info === "openplatform_unity") { //确保是在untiy中才加appid头 if (typeof gamelet == 'object' && (typeof gamelet.appId == 'number' || typeof gamelet.appId == 'string')) { (0, logger_1.loggerInit)(gamelet.appId); } addEventListener("error", function (event) { var e = event; _this.reportJSExceptionStringToTDM("AppPage Error: + ".concat(e.filename, " ").concat(e.lineno, " ").concat(e.colno, " \nmessage: ").concat(e.message)); }); } } UnityGameletAPIImpl.prototype.canUsePlatformAPI = function () { var isInPixUI = window.navigator.userAgent.search('PixUI') >= 0; var sdk_info = unitytools_1.UnityTools.getQueryParam("_sdk_info"); if (sdk_info === "openplatform_unity" && isInPixUI) { return true; } return true; }; UnityGameletAPIImpl.prototype.getRuntimeEnv = function () { return global_data_1.GlobalData.runtimeEnv; }; UnityGameletAPIImpl.prototype.genAppScopeEventName = function (evt) { return ""; }; UnityGameletAPIImpl.prototype.getAppID = function () { return gamelet.appId; }; UnityGameletAPIImpl.prototype.getAppKey = function () { return gamelet.appKey; }; UnityGameletAPIImpl.prototype.getAppName = function () { return gamelet.appName; }; UnityGameletAPIImpl.prototype.getAppVersion = function () { return gamelet.appVersion; }; UnityGameletAPIImpl.prototype.queryAppStatus = function (appID) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, Promise.resolve({})]; }); }); }; UnityGameletAPIImpl.prototype.getAppEntranceConfig = function (key) { if (gamelet.entranceConfig != undefined) { try { var entranceObj = JSON.parse(gamelet.entranceConfig); return entranceObj[key]; } catch (e) { return undefined; } } return undefined; }; UnityGameletAPIImpl.prototype.getOpenArgs = function () { return gamelet.openMessage; }; UnityGameletAPIImpl.prototype.getIsProductEnvironment = function () { return gamelet.isProductEnv; }; UnityGameletAPIImpl.prototype.getIsHitBackendWhitelist = function () { if (gamelet.isApplicationDebug) { return gamelet.isApplicationDebug; } return false; }; UnityGameletAPIImpl.prototype.getIsSupportCallbroker = function () { return this.canUsePlatformAPI(); }; UnityGameletAPIImpl.prototype.getPlatformDesc = function () { if (window['platform'] && typeof window['platform'] == 'string') { return window['platform']; } return "unknown"; }; UnityGameletAPIImpl.prototype.getEntryInfo = function () { return 'unity'; }; UnityGameletAPIImpl.prototype.getFaasAddr = function () { return gamelet.faasAddress ? gamelet.faasAddress : ""; }; UnityGameletAPIImpl.prototype.getUserData = function () { return __awaiter(this, void 0, void 0, function () { var udObject; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.GetUserDataTable")]; case 1: udObject = _a.sent(); return [2 /*return*/, udObject]; } }); }); }; UnityGameletAPIImpl.prototype.refreshUserDataTokens = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.RefreshUserDataTokens")]; }); }); }; UnityGameletAPIImpl.prototype.reportJSExceptionStringToTDM = function (content) { unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.ReportJSExceptionToTdm", content, gamelet.appId, gamelet.appKey); }; UnityGameletAPIImpl.prototype.reportStringToTDM = function (content, tag) { unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.ReportToTdm", content, tag, 2, gamelet.appId, gamelet.appKey); }; UnityGameletAPIImpl.prototype.reportNumberToTDM = function (tag) { unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.ReportToTdm", "", tag, 0, gamelet.appId, gamelet.appKey); }; UnityGameletAPIImpl.prototype.faasReportStringToTDM = function (content, tag) { return __awaiter(this, void 0, void 0, function () { var ud; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getUserData()]; case 1: ud = _a.sent(); return [2 /*return*/, (0, faas_tdm_reporter_1.FaasReportToTdm)(content, tag, 2, this.getAppID(), "index", this.getAppVersion(), ud, this.getIsProductEnvironment())]; } }); }); }; UnityGameletAPIImpl.prototype.faasReportNumberToTDM = function (tag) { return __awaiter(this, void 0, void 0, function () { var ud; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getUserData()]; case 1: ud = _a.sent(); return [2 /*return*/, (0, faas_tdm_reporter_1.FaasReportToTdm)("", tag, 0, this.getAppID(), "index", this.getAppVersion(), ud, this.getIsProductEnvironment())]; } }); }); }; UnityGameletAPIImpl.prototype.setDataStash = function (dataKey, dataValue) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.SetDataStash", gamelet.appId, gamelet.appKey, dataKey, dataValue)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; UnityGameletAPIImpl.prototype.getDataStash = function (dataKey) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.GetDataStash", gamelet.appId, dataKey)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; UnityGameletAPIImpl.prototype.clearDataStash = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.ClearDataStash", gamelet.appId, gamelet.appKey)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; UnityGameletAPIImpl.prototype.writeCookie = function (customerName, content) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.WriteCookie", customerName + "_" + this.getAppID(), content)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; UnityGameletAPIImpl.prototype.writeRoleCookie = function (customerName, content) { return __awaiter(this, void 0, void 0, function () { var ud, openid, roleid, k; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getUserData()]; case 1: ud = _a.sent(); openid = ud.sOpenId || ud.openID || ""; roleid = ud.sRoleId || ud.roleID || ""; k = customerName + openid + roleid; return [4 /*yield*/, unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.WriteCookie", k + "_" + this.getAppID(), content)]; case 2: _a.sent(); return [2 /*return*/]; } }); }); }; UnityGameletAPIImpl.prototype.readCookie = function (customerName) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.ReadCookie", customerName + "_" + this.getAppID())]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; UnityGameletAPIImpl.prototype.readRoleCookie = function (customerName) { return __awaiter(this, void 0, void 0, function () { var ud, openid, roleid, k; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.getUserData()]; case 1: ud = _a.sent(); openid = ud.sOpenId || ud.openID || ""; roleid = ud.sRoleId || ud.roleID || ""; k = customerName + openid + roleid; return [4 /*yield*/, unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.ReadCookie", k + "_" + this.getAppID())]; case 2: return [2 /*return*/, _a.sent()]; } }); }); }; UnityGameletAPIImpl.prototype.deleteCookie = function (customerName) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { console.error("PandoraUnitySDK \u4E2D\u4E0D\u8981\u8C03\u7528deleteCookie | key:".concat(customerName)); return [2 /*return*/, Promise.reject('not support')]; }); }); }; UnityGameletAPIImpl.prototype.addOnGameCommandListener = function (func) { var eventKey = "BROADCAST_MSG_FROM_GAME"; this.addEventListener(eventKey, func); }; UnityGameletAPIImpl.prototype.removeOnGameCommandListener = function (func) { var eventKey = "BROADCAST_MSG_FROM_GAME"; this.removeEventListener(eventKey, func); }; UnityGameletAPIImpl.prototype.addOnSrvPushDataListener = function (func) { var eventKey = "PUSH_TO_APP_" + this.getAppID(); this.addEventListener(eventKey, func); }; UnityGameletAPIImpl.prototype.removeOnSrvPushDataListener = function (func) { var eventKey = "PUSH_TO_APP_" + this.getAppID(); this.removeEventListener(eventKey, func); }; UnityGameletAPIImpl.prototype.addEventListener = function (evt, func) { unityrpc_mgr_1.UnityGameletAdapter.callLua("EventDispatcher.AddEventListener", evt, func); }; UnityGameletAPIImpl.prototype.removeEventListener = function (evt, func) { unityrpc_mgr_1.UnityGameletAdapter.callLua("EventDispatcher.RemoveEventListener", evt, func); }; UnityGameletAPIImpl.prototype.addLocalEventListener = function (evt, func) { local_dispatcher_1.LocalDispatcher.getInstance().add(evt, func); }; UnityGameletAPIImpl.prototype.removeLocalEventListener = function (evt, func) { local_dispatcher_1.LocalDispatcher.getInstance().remove(evt, func); }; UnityGameletAPIImpl.prototype.dispatchLocalEvent = function (evt) { var _a; var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } (_a = local_dispatcher_1.LocalDispatcher.getInstance()).dispatch.apply(_a, __spreadArray([evt], args, false)); }; UnityGameletAPIImpl.prototype.callGame = function (jsonStr) { unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.CallGame", jsonStr); }; UnityGameletAPIImpl.prototype.open = function (appID, features) { features = features || {}; var openMsgObject = { type: "open", appId: String(appID), appPage: features["appPage"] || "index", appName: features["appName"] || "", }; for (var key in features) { if (Object.prototype.hasOwnProperty.call(features, key)) { openMsgObject[key] = features[key]; } } this.dispatchEvent("PIXUI_COMMAND_FROM_JS", JSON.stringify(openMsgObject)); }; UnityGameletAPIImpl.prototype.close = function (appID, features) { if (appID == undefined || appID == this.getAppID()) { window.close(); return; } features = features || {}; var openMsgObject = { type: "close", appId: String(appID), appPage: features["appPage"] || "index", }; for (var key in features) { if (Object.prototype.hasOwnProperty.call(features, key)) { openMsgObject[key] = features[key]; } } this.dispatchEvent("PIXUI_COMMAND_FROM_JS", JSON.stringify(openMsgObject)); }; UnityGameletAPIImpl.prototype.reportStats = function (userdata, iModule, iChannelId, iType, iActId, iJumpType, sJumpUrl, sRecommendId, sChangjingId, iGoodsId, iCountId, iFee, iActStyle, iFlowId, sExtendList) { unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.OpenPlatformReportStats", iModule, iChannelId, iType, iActId, iJumpType, sJumpUrl, sRecommendId, sChangjingId, iGoodsId, iCountId, iFee, "openplat", iActStyle, iFlowId, sExtendList); }; UnityGameletAPIImpl.prototype.callApplicationBroker = function (commandId, subCommandId, requestBodyObject, callback) { return unityrpc_mgr_1.UnityGameletAdapter.callLua("Common.CallApplicationBroker", commandId, subCommandId, this.getAppID(), this.getAppKey(), requestBodyObject, callback); }; UnityGameletAPIImpl.prototype.gameCustomFunction = function (functionName) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, unityrpc_mgr_1.UnityGameletAdapter.callLua.apply(unityrpc_mgr_1.UnityGameletAdapter, __spreadArray([functionName], args, false))]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; UnityGameletAPIImpl.prototype.setGameletAppEnv = function (appInfo, _isProductEnvironment) { return __awaiter(this, void 0, void 0, function () { var appID, appName, appVersion, appKey, isProductEnvironment, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: appID = parseInt(appInfo['appID'] || appInfo['appId']) || -1; appName = appInfo['appName'] != undefined ? appInfo['appName'] : ''; appVersion = appInfo['appVersion'] != undefined ? appInfo['appVersion'] : ''; appKey = appInfo['appKey'] != undefined ? appInfo['appKey'] : ''; if (!(_isProductEnvironment != undefined)) return [3 /*break*/, 1]; _a = _isProductEnvironment; return [3 /*break*/, 3]; case 1: return [4 /*yield*/, this.gameCustomFunction('Common.IsProductEnvironment')]; case 2: _a = _b.sent(); _b.label = 3; case 3: isProductEnvironment = _a; if (appID < 0 && !appName) { return [2 /*return*/]; } if (!(appID > 0 && !appName)) return [3 /*break*/, 5]; return [4 /*yield*/, this.gameCustomFunction('PxFrame.GetAppNameByAppId', appID)]; case 4: appName = _b.sent(); _b.label = 5; case 5: if (!(appName && appID < 0)) return [3 /*break*/, 7]; return [4 /*yield*/, this.gameCustomFunction('PxFrame.GetAppIdByAppName', appName)]; case 6: appID = _b.sent(); _b.label = 7; case 7: if (!!appVersion) return [3 /*break*/, 9]; return [4 /*yield*/, this.gameCustomFunction('Common.GetAppVersion', appID)]; case 8: appVersion = _b.sent(); _b.label = 9; case 9: if (!!appKey) return [3 /*break*/, 11]; return [4 /*yield*/, this.gameCustomFunction('CSharpInterface.GetAppKey', appID)]; case 10: appKey = _b.sent(); _b.label = 11; case 11: globalThis.gamelet = { appId: appID, appName: appName, appKey: appKey, appVersion: appVersion, isProductEnv: isProductEnvironment }; (0, logger_1.loggerInit)(gamelet.appId); return [2 /*return*/]; } }); }); }; UnityGameletAPIImpl.prototype.reportStatsV2 = function (iType, iChangjingId, iGoodsId, iCountId, iFee, iFlowId, extendList, appId) { console.error("PandoraUnitySDK \u4E2D\u4E0D\u8981\u8C03\u7528reportStatsV2 | iType:".concat(iType)); }; UnityGameletAPIImpl.prototype.reportMonitorV2 = function (tag, content, itype) { console.error("PandoraUnitySDK \u4E2D\u4E0D\u8981\u8C03\u7528reportMonitorV2 | tag:".concat(tag, ", content:").concat(content)); }; UnityGameletAPIImpl.prototype.cacheAsset = function (url, callback) { }; UnityGameletAPIImpl.prototype.isAssetCached = function (url) { return new Promise(function (resolve, reject) { resolve(false); }); }; UnityGameletAPIImpl.prototype.getAssetCachePath = function (url) { return new Promise(function (resolve, reject) { resolve(''); }); }; UnityGameletAPIImpl.prototype.storeAsset = function (url, callback) { }; UnityGameletAPIImpl.prototype.isAssetStored = function (url) { return new Promise(function (resolve, reject) { resolve(false); }); }; UnityGameletAPIImpl.prototype.getAssetStorePath = function (url) { return new Promise(function (resolve, reject) { resolve(''); }); }; UnityGameletAPIImpl.prototype.getJSSDKVersion = function () { return ''; }; return UnityGameletAPIImpl; }()); exports.UnityGameletAPIImpl = UnityGameletAPIImpl; exports.UnityGameletAPI = (0, utilities_1.singleton)(UnityGameletAPIImpl); //# sourceMappingURL=unitygameletapi.js.map