kunpolibrary/src/minigame/bytedance/BytedanceCommon.ts

125 lines
3.3 KiB
TypeScript
Raw Normal View History

/**
* @Author: Gongxh
* @Date: 2025-04-11
* @Description:
*/
import { LaunchParams } from "@douyin-microapp/typings/types/app";
import { warn } from "../../tool/log";
import { IMiniCommon } from "../interface/IMiniCommon";
export class BytedanceCommon implements IMiniCommon {
private _launchOptions: BytedanceMiniprogram.LaunchParams = null;
private _systemInfo: BytedanceMiniprogram.SystemInfo = null;
private _envInfo: BytedanceMiniprogram.EnvInfo = null;
/**
* @internal
*/
constructor() {
this._launchOptions = tt.getLaunchOptionsSync();
}
/**
*
*/
public getLaunchOptions(): LaunchParams {
return this._launchOptions;
}
/**
*
*/
public getHotLaunchOptions(): LaunchParams {
warn("字节跳动小游戏未提供热启动参数获取方式,请在 onShow 中获取");
return null;
}
/**
*
*/
public getLibVersion(): string {
return this.getSystemInfo()?.SDKVersion || "0.0.1";
}
/**
* 宿 ()
*/
public getHostVersion(): string {
return this.getSystemInfo()?.version || "0.0.1";
}
/**
* 宿 APP "Toutiao"
* [https://developer.open-douyin.com/docs/resource/zh-CN/mini-game/develop/api/system/system-information/tt-get-system-info-sync]
*/
public getHostName(): string {
return this.getSystemInfo()?.appName || "unknown";
}
/**
*
*/
public getPlatform(): 'ios' | 'android' | 'ohos' | 'windows' | 'mac' | 'devtools' {
return this.getSystemInfo().platform as ('ios' | 'android' | 'ohos' | 'windows' | 'mac' | 'devtools');
}
/**
*
*/
public getEnvType(): 'release' | 'debug' {
return this.getEnvInfo().microapp.envType == "production" ? "release" : "debug";
}
/**
* 退
*/
public exitMiniProgram(): void {
tt.exitMiniProgram?.({});
}
public getScreenSize(): { width: number, height: number } {
const systemInfo = this.getSystemInfo();
return {
width: systemInfo.screenWidth,
height: systemInfo.screenHeight,
};
}
/**
*
*/
public setClipboardData(text: string): void {
tt.setClipboardData({
data: text,
fail: (res: { errMsg: string, errNo?: number }) => {
warn(`复制到剪切板失败 errCode:${res.errNo} errMsg:${res.errMsg}`);
}
});
}
private getEnvInfo(): BytedanceMiniprogram.EnvInfo {
if (this._envInfo) {
return this._envInfo;
}
if (tt.getEnvInfoSync) {
this._envInfo = tt.getEnvInfoSync();
return this._envInfo;
}
warn("getEnvInfo 失败");
return null;
}
private getSystemInfo(): BytedanceMiniprogram.SystemInfo {
if (this._systemInfo) {
return this._systemInfo;
}
if (tt.getSystemInfoSync) {
this._systemInfo = tt.getSystemInfoSync();
return this._systemInfo;
}
warn("getSystemInfo 失败");
return null;
}
}