Files
18xchat/assets/Scripts/chat18x/foundation/identity/MachineInfoService.ts
T
2025-08-18 10:58:44 +08:00

114 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { sys, native } from "cc";
import { EDITOR, PREVIEW } from "cc/env";
function isWeChatMiniGame(): boolean {
const g: any = globalThis as any;
return !!g.wx?.getSystemInfoSync;
}
function isTTMiniGame(): boolean {
const g: any = globalThis as any;
return !!g.tt?.getSystemInfoSync;
}
/** 小游戏平台:优先 brand + model */
function tryMiniGameModel(): string | null {
const g: any = globalThis as any;
if (g.tt?.getSystemInfoSync) {
const info = g.tt.getSystemInfoSync();
const s = `${info.brand ?? ""} ${info.model ?? ""}`.trim();
return s || null;
}
if (g.wx?.getSystemInfoSync) {
const info = g.wx.getSystemInfoSync();
const s = `${info.brand ?? ""} ${info.model ?? ""}`.trim();
return s || null;
}
return null;
}
/** 原生:Android 使用 Build.MANUFACTURER + Build.MODELiOS 使用硬件码(如 iPhone14,5 */
function tryNativeModel(): string | null {
// if (!sys.isNative) return null;
// try {
// if (sys.platform === sys.Platform.ANDROID) {
// const s = native.reflection.callStaticMethod(
// "org/cocos2dx/javascript/AppActivity",
// "getDeviceModel",
// "()Ljava/lang/String;"
// );
// return (s && String(s).trim()) || null;
// }
// if (sys.platform === sys.Platform.IOS) {
// const s = native.reflection.callStaticMethod("AppController", "getHardwareModel");
// return (s && String(s).trim()) || null;
// }
// } catch {
// // 反射失败直接兜底
// }
return null;
}
/** 浏览器:拿不到“设备名”,仅做极简标识(避免泄露 UA 细节) */
function tryWebHint(): string {
// 可按需增强:navigator.userAgentData?.platform
// 这里仅返回平台信息,合规且稳定
// @ts-ignore
const plat = (navigator as any)?.userAgentData?.platform || (navigator as any)?.platform || "Web";
return String(plat);
}
export class MachineInfoService {
private static _I: MachineInfoService;
static get I() { return this._I ?? (this._I = new MachineInfoService()); }
private _cached: string | null = null;
/** 运行时标签,方便排查:editor / editor-preview / native-android / native-ios / minigame-wechat / minigame-tt / web */
public runtimeTag(): string {
if (EDITOR) return PREVIEW ? "editor-preview" : "editor";
if (sys.isNative) {
if (sys.platform === sys.Platform.ANDROID) return "native-android";
if (sys.platform === sys.Platform.IOS) return "native-ios";
return "native";
}
if (isWeChatMiniGame()) return "minigame-wechat";
if (isTTMiniGame()) return "minigame-tt";
return "web";
}
/** 获取“机器标识”(推荐:品牌+型号/硬件码;编辑器返回固定标识;浏览器返回平台提示) */
public getMachineIdentifier(): string {
if (this._cached) return this._cached;
let id: string | null = null;
// 1) 编辑器环境:不要做任何原生/平台调用,返回明确标签
if (EDITOR) {
id = PREVIEW ? `EditorPreview-${sys.os}` : `Editor-${sys.os}`;
this._cached = id;
return id;
}
// 浏览器/H5
id = tryWebHint();
return (this._cached = this.trimSafe(id || "Unknown"));
// 小游戏
id = tryMiniGameModel();
if (id) return (this._cached = this.trimSafe(id));
// 原生
id = tryNativeModel();
if (id) return (this._cached = this.trimSafe(id));
}
private trimSafe(s: string): string {
// 限长,去掉不可见字符,避免异常字符串
s = s.replace(/[^\x20-\x7E\u4e00-\u9fa5]/g, "").trim();
if (s.length > 64) s = s.slice(0, 64);
return s || "Unknown";
}
}
export default MachineInfoService;