miniapp-web-jsapi
Version:
JSAPI/View adapter for miniprogram running on the web
59 lines • 1.81 kB
JavaScript
import { asyncRun } from "../../../common";
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 = 0;
var statusBarHeight = 0;
var windowWidth = 375;
var screenHeight = Math.ceil(windowWidth / window.screen.width * window.screen.height);
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: 'UNKNOWN',
storage: '',
app: 'h5',
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';
}
}