miniapp-web-jsapi
Version:
JSAPI/View adapter for miniprogram running on the web
74 lines • 2.57 kB
JavaScript
import { asyncRun } from "../../../common";
import { getAppName } from "../../utils/web-utils";
import { AppNameEnums } from "../../site";
export function getSystemInfoSync() {
return buildSystemInfo();
}
export function getSystemInfo(options) {
asyncRun(function () {
try {
var _options$success;
(_options$success = options.success) === null || _options$success === void 0 ? void 0 : _options$success.call(options, buildSystemInfo());
} catch (e) {
var _options$fail;
(_options$fail = options.fail) === null || _options$fail === void 0 ? void 0 : _options$fail.call(options);
} finally {
var _options$complete;
(_options$complete = options.complete) === null || _options$complete === void 0 ? void 0 : _options$complete.call(options);
}
});
}
var __systemInfo;
function buildSystemInfo() {
if (!__systemInfo) {
__systemInfo = buildSystemInfoInternal();
}
return __systemInfo;
}
function buildSystemInfoInternal() {
var titleBarHeight = 44;
var statusBarHeight = 20;
var windowWidth = 375;
var screenHeight = Math.ceil(windowWidth / window.screen.width * window.screen.height);
var app = getAppName();
return {
platform: getPlatform(),
pixelRatio: window.devicePixelRatio,
windowWidth: windowWidth,
windowHeight: screenHeight - titleBarHeight - statusBarHeight,
titleBarHeight: titleBarHeight,
statusBarHeight: statusBarHeight,
screenWidth: windowWidth,
screenHeight: screenHeight,
currentBattery: '100%',
brand: 'UNKNOWN',
model: 'UNKNOWN',
system: 'UNKNOWN',
version: getAppVersion(app),
storage: '',
app: app || '',
fontSizeSetting: 16,
language: navigator.language
};
}
function getPlatform() {
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
return 'iOS';
} else if (/(Android)/i.test(navigator.userAgent)) {
return 'Android';
} else {
return 'UNKNOWN';
}
}
function getAppVersion(app) {
if (app === AppNameEnums.kakaopay) {
// iOS: Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 KAKAOTALK 9.3.0; KT/9.3.0 Io/14.6 (zh-Hant)
// Android: Mozilla/5.0 (Linux; Android 11; SM-G975F Build/RP1A.200720.012; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.120 Mobile Safari/537.36;KAKAOTALK 2309350; KT/9.3.5 An/11 (zh_CN_#Hans)
//
var matches = navigator.userAgent.match(/.+\sKT\/([0-9|.]+).*/);
if (matches && matches.length >= 2) {
return matches[1];
}
}
return '';
}