minigame-std
Version:
Cross-platform standard library for WeChat minigame and web browsers with unified APIs for crypto, fs, fetch, storage, and more.
320 lines (319 loc) • 8.39 kB
JavaScript
import { Lazy, Ok, OnceAsync } from "happy-rusty";
import { asyncIOResultify } from "./utils.mjs";
//#region src/std/platform/base.ts
/**
* 获取当前的平台类型。
* @returns 返回当前的运行环境类型,可能是 'minigame' 或 'web'。
* @since 1.0.0
* @example
* ```ts
* const type = getTargetType();
* console.log('当前平台:', type); // 'minigame' 或 'web'
* ```
*/
function getTargetType() {
return "wx" in globalThis ? "minigame" : "web";
}
/**
* 判断当前是否在 Web 环境中。
* @returns 如果在 Web 环境中返回 true,否则返回 false。
* @since 1.0.0
* @example
* ```ts
* if (isWeb()) {
* console.log('当前在浏览器环境');
* }
* ```
*/
function isWeb() {
return getTargetType() === "web";
}
/**
* 判断当前是否在小游戏环境中。
* @returns 如果在小游戏环境中返回 true,否则返回 false。
* @since 1.0.0
* @example
* ```ts
* if (isMiniGame()) {
* console.log('当前在小游戏环境');
* }
* ```
*/
function isMiniGame() {
return getTargetType() === "minigame";
}
//#endregion
//#region src/macros/env.ts
/**
* 如果在小游戏环境中返回 true,否则返回 false。
*/
const IS_MINA = __MINIGAME_STD_MINA__;
//#endregion
//#region src/std/platform/user_agent.ts
/**
* 解析 userAgent 获取设备信息。
* @param ua - 要解析的 userAgent 字符串,默认为 navigator.userAgent。
* @returns 解析后的设备信息,包含 model、platform 和 system。
* @internal
*/
function parseUserAgent(ua = navigator.userAgent) {
const iosMatch = ua.match(/\((iPhone|iPad|iPod);.*?OS (\d+[_\d]*)/);
if (iosMatch) return {
model: iosMatch[1],
platform: "ios",
system: `iOS ${iosMatch[2].replace(/_/g, ".")}`
};
const androidMatch = ua.match(/Android\s+([\d.]+);(?:.*?;\s*)?([^;]+)\s+Build/);
if (androidMatch) {
const version = androidMatch[1];
return {
model: androidMatch[2].trim(),
platform: "android",
system: `Android ${version}`
};
}
const macMatch = ua.match(/Mac OS X (\d+[_\d]*)/);
if (macMatch) return {
model: "Mac",
platform: "mac",
system: `macOS ${macMatch[1].replace(/_/g, ".")}`
};
const winMatch = ua.match(/Windows NT ([\d.]+)/);
if (winMatch) return {
model: "PC",
platform: "windows",
system: `Windows NT ${winMatch[1]}`
};
if (ua.includes("Linux")) return {
model: "Linux",
platform: "linux",
system: "Linux"
};
return {
model: "unknown",
platform: "unknown",
system: "unknown"
};
}
//#endregion
//#region src/std/platform/device.ts
const deviceInfo = /*#__PURE__*/ Lazy(() => IS_MINA ? wx.getDeviceInfo ? wx.getDeviceInfo() : wx.getSystemInfoSync() : getWebDeviceInfo());
const benchmarkLevel = /*#__PURE__*/ OnceAsync();
/**
* 获取设备信息。
* @returns 返回小游戏的设备信息对象。
* @since 1.0.0
* @example
* ```ts
* const info = getDeviceInfo();
* console.log('设备平台:', info.platform);
* console.log('设备品牌:', info.brand);
* console.log('设备型号:', info.model);
* ```
*/
function getDeviceInfo() {
return deviceInfo.force();
}
/**
* 获取设备性能等级, web 环境返回 -2。
* @returns 返回设备性能等级。
* @since 1.10.0
* @example
* ```ts
* const result = await getDeviceBenchmarkLevel();
* if (result.isOk()) {
* const level = result.unwrap();
* if (level >= 30) {
* console.log('高性能设备');
* } else if (level >= 20) {
* console.log('中等性能设备');
* } else {
* console.log('低性能设备');
* }
* }
* ```
*/
async function getDeviceBenchmarkLevel() {
if (!IS_MINA) return Ok(-2);
if (benchmarkLevel.isInitialized()) return Ok(benchmarkLevel.get().unwrap());
if (typeof wx.getDeviceBenchmarkInfo === "function") return await benchmarkLevel.getOrTryInit(async () => {
return (await asyncIOResultify(wx.getDeviceBenchmarkInfo)()).map((x) => x.benchmarkLevel);
});
const level = getDeviceInfo().benchmarkLevel;
benchmarkLevel.set(level);
return Ok(level);
}
/**
* 获取窗口信息。
* @returns 包含窗口和屏幕相关信息的对象。
* @since 1.7.0
* @example
* ```ts
* const info = getWindowInfo();
* console.log('窗口尺寸:', info.windowWidth, 'x', info.windowHeight);
* console.log('屏幕尺寸:', info.screenWidth, 'x', info.screenHeight);
* console.log('设备像素比:', info.pixelRatio);
* ```
*/
function getWindowInfo() {
return IS_MINA ? wx.getWindowInfo() : getWebWindowInfo();
}
/**
* 获取 Web 环境下的设备信息。
*/
function getWebDeviceInfo() {
const { model, platform, system } = parseUserAgent();
return {
benchmarkLevel: -2,
brand: "",
memorySize: (navigator.deviceMemory ?? 0) * 1024,
model,
platform,
system
};
}
/**
* 获取 Web 环境下的窗口信息。
*/
function getWebWindowInfo() {
return {
pixelRatio: devicePixelRatio,
screenHeight: screen.height,
screenTop,
screenWidth: screen.width,
windowHeight: innerHeight,
windowWidth: innerWidth,
statusBarHeight: 0,
safeArea: {
left: 0,
right: innerWidth,
top: 0,
bottom: innerHeight,
width: innerWidth,
height: innerHeight
}
};
}
//#endregion
//#region src/std/platform/target.ts
/**
* 判断当前是否在小游戏的运行时环境中。
* @returns 如果在小游戏的运行时环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameRuntime()) {
* console.log('在小游戏真机环境中');
* }
* ```
*/
function isMiniGameRuntime() {
return isMiniGame() && getPlatform() !== "devtools";
}
/**
* 判断当前是否在小游戏的开发者工具中。
* @returns 如果在小游戏的开发者工具中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameDevtools()) {
* console.log('在开发者工具中');
* }
* ```
*/
function isMiniGameDevtools() {
return isMiniGame() && getPlatform() === "devtools";
}
/**
* 判断当前是否在小游戏的 iOS 环境中。
* @returns 如果在小游戏的 iOS 环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameIOS()) {
* console.log('在 iOS 设备上运行');
* }
* ```
*/
function isMiniGameIOS() {
return isMiniGame() && getPlatform() === "ios";
}
/**
* 判断当前是否在小游戏的 Android 环境中。
* @returns 如果在小游戏的 Android 环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameAndroid()) {
* console.log('在 Android 设备上运行');
* }
* ```
*/
function isMiniGameAndroid() {
return isMiniGame() && getPlatform() === "android";
}
/**
* 判断当前是否在小游戏的 Windows 环境中。
* @returns 如果在小游戏的 Windows 环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameWin()) {
* console.log('在 Windows 设备上运行');
* }
* ```
*/
function isMiniGameWin() {
return isMiniGame() && getPlatform() === "windows";
}
/**
* 判断当前是否在小游戏的 Mac 环境中。
* @returns 如果在小游戏的 Mac 环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameMac()) {
* console.log('在 Mac 设备上运行');
* }
* ```
*/
function isMiniGameMac() {
return isMiniGame() && getPlatform() === "mac";
}
/**
* 判断当前是否在小游戏的 HarmonyOS 环境中。
* @returns 如果在小游戏的 HarmonyOS 环境中返回 true,否则返回 false。
* @since 1.9.0
* @example
* ```ts
* if (isMiniGameHarmonyOS()) {
* console.log('在 HarmonyOS 设备上运行');
* }
* ```
*/
function isMiniGameHarmonyOS() {
return isMiniGame() && getPlatform() === "ohos";
}
/**
* 判断当前是否在小游戏的 HarmonyOS PC 环境中。
* @returns 如果在小游戏的 HarmonyOS PC 环境中返回 true,否则返回 false。
* @since 2.4.0
* @example
* ```ts
* if (isMiniGameHarmonyPC()) {
* console.log('在 HarmonyOS PC 设备上运行');
* }
* ```
*/
function isMiniGameHarmonyPC() {
return isMiniGame() && getPlatform() === "ohos_pc";
}
/**
* 获取当前平台类型。
*/
function getPlatform() {
return getDeviceInfo().platform.toLowerCase();
}
//#endregion
export { getDeviceBenchmarkLevel, getDeviceInfo, getTargetType, getWindowInfo, isMiniGame, isMiniGameAndroid, isMiniGameDevtools, isMiniGameHarmonyOS, isMiniGameHarmonyPC, isMiniGameIOS, isMiniGameMac, isMiniGameRuntime, isMiniGameWin, isWeb };
//# sourceMappingURL=platform.mjs.map