机器设备名

This commit is contained in:
chen wei bo
2025-08-18 10:58:44 +08:00
parent e32c3321cf
commit 0a1e33bd55
3 changed files with 130 additions and 3 deletions
+8 -3
View File
@@ -36,6 +36,9 @@ import DeviceIdService from "db://assets/Scripts/chat18x/foundation/identity/Dev
import { AuthService } from "db://assets/Scripts/chat18x/network/services/AuthService";
import { ApiCode } from "db://assets/Scripts/chat18x/network/client/types";
import MachineInfoService from "db://assets/Scripts/chat18x/foundation/identity/MachineInfoService";
const { ccclass, property } = _decorator;
@ccclass("PreloadUI")
@@ -256,15 +259,17 @@ export class PreloadUI extends Component {
// 游客登录
const deviceId = DeviceIdService.I.id;
const machineIdentifier = MachineInfoService.I.getMachineIdentifier();
const reqData = {
GameName: ConfigManager.tables.TbGlobalConfig.GameName,
Identifier: deviceId,
IdentityType: 1,
OperateType: 1,
ShareDeposit: "",
MachineIdentifier: "aaa",
SdkUid: "",
ShareDeposit: '',
MachineIdentifier: machineIdentifier,
SdkUid: '',
};
console.log("登录请求数据:", reqData);
let res = await AuthService.I.login(reqData);
console.log("登录响应数据:", res);
if (res && res.code === ApiCode.OK) {
@@ -0,0 +1,113 @@
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;
@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "332c833c-26d7-42b6-bb1b-09af0650d8e3",
"files": [],
"subMetas": {},
"userData": {}
}