UNPKG

miniapp-web-jsapi

Version:

JSAPI/View adapter for miniprogram running on the web

126 lines 5.32 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import JsUtils from "../../utils/js-utils"; import { EnvironmentEnums, getEnvironment, getWebPolyfillOptions, getWebQueryParams } from "../index"; import { appEnumsMap, AppNameEnums } from "../site"; export function getSiteName() { var _getWebPolyfillOption; var siteName = (_getWebPolyfillOption = getWebPolyfillOptions()) === null || _getWebPolyfillOption === void 0 ? void 0 : _getWebPolyfillOption.siteName; if (JsUtils.isNotEmpty(siteName)) { return siteName; } siteName = getWebQueryParams().site; if (JsUtils.isNotEmpty(siteName)) { return siteName; } console.error('no siteName! please specify siteName in initWebPolyfill(...)'); return undefined; } export function getAppName() { var _getWebPolyfillOption2; var appName = (_getWebPolyfillOption2 = getWebPolyfillOptions()) === null || _getWebPolyfillOption2 === void 0 ? void 0 : _getWebPolyfillOption2.appName; if (JsUtils.isNotEmpty(appName)) { return appName; } appName = getWebQueryParams().app; if (JsUtils.isNotEmpty(appName)) { return appName; } // 通过 SiteName 映射一个 AppName var siteName = getSiteName(); appName = appEnumsMap[siteName || '']; if (JsUtils.isNotEmpty(appName)) { return appName; } console.error('no appName! please specify appName in initWebPolyfill(...)'); return undefined; } export function getAppId() { var _getWebPolyfillOption3, _window$location$path; var appId = (_getWebPolyfillOption3 = getWebPolyfillOptions()) === null || _getWebPolyfillOption3 === void 0 ? void 0 : _getWebPolyfillOption3.appId; if (JsUtils.isNotEmpty(appId)) { return appId; } // /page/aplus-miniapps/2102000024800001/index.html var matches = (_window$location$path = window.location.pathname) === null || _window$location$path === void 0 ? void 0 : _window$location$path.match(/^\/page\/aplus-miniapps\/(\d{8,}).*/); if ((matches === null || matches === void 0 ? void 0 : matches.length) === 2) { return matches[1]; } console.error('no AppId! please specify AppId in initWebPolyfill(...)'); return undefined; } export function getRpcOptions() { var _getWebPolyfillOption4; var rpcOptions = _objectSpread({}, (_getWebPolyfillOption4 = getWebPolyfillOptions()) === null || _getWebPolyfillOption4 === void 0 ? void 0 : _getWebPolyfillOption4.rpcOptions); if (!rpcOptions.gatewayUrl) { rpcOptions.gatewayUrl = getGatewayUrlFromOrigin(); } if (!rpcOptions.workspaceId) { if (rpcOptions.gatewayUrl.indexOf('pre') > 0) { rpcOptions.workspaceId = 'pre'; } else { rpcOptions.workspaceId = 'default'; } } if (!rpcOptions.appId) { rpcOptions.appId = getRpcAppIdFromApp(); } return rpcOptions; } function getGatewayUrlFromOrigin() { var environment = getEnvironment(); if (environment === EnvironmentEnums.STABLE) { return 'http://imgs-2.sggz00b.stable.alipay.net/imgw.htm'; } else if (environment === EnvironmentEnums.TEST) { return 'http://imgs-ztt-0.sggz00a.test.alipay.net/mgw.htm'; } else if (environment === EnvironmentEnums.PRE) { return 'https://imgs-sea-pre.alipay.com/imgw.htm'; } else { return 'https://imgs-ac.alipay.com/imgw.htm'; } } function getRpcAppIdFromApp() { var appName = getAppName(); if (appName === AppNameEnums.alipayhk) { return 'BE9C457241710'; } else if (appName === AppNameEnums.alipaymo) { return 'ALIPAY_WALLET_MO'; } else if (appName === AppNameEnums.gcash) { return 'GCASH_WALLET_PH'; } else if (appName === AppNameEnums.tngd) { return 'TNGD_WALLET_MY'; } else if (appName === AppNameEnums.dana) { return 'DANA_WALLET_ID'; } else if (appName === AppNameEnums.kakaopay) { return 'KAKAOPAY_WALLET_KR'; } console.error("Cannot find Rpc AppId for ".concat(appName, "!")); return 'UNKNOWN_APP_ID'; } export function parseQueryParams(str) { var _str$split; var queryParams = {}; if (JsUtils.isEmpty(str)) { return queryParams; } var index = str.indexOf('?'); if (index >= 0) { str = str.substring(index + 1); } (_str$split = str.split('&')) === null || _str$split === void 0 ? void 0 : _str$split.forEach(function (item) { var list = item.split('='); if (list.length < 2) { return; } var key = decodeURIComponent(list[0]); queryParams[key] = decodeURIComponent(list[1]); }); return queryParams; } /** * 是否为本地预览地址 */ export function isLocalhost(host) { return host === 'localhost' || /^localhost:[0-9]+$/.test(host) || /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:[0-9]+$/.test(host); }