From 0a1e33bd55ecccf50b9d8deaf4061a0210889004 Mon Sep 17 00:00:00 2001 From: chen wei bo <1025839511@qq.com> Date: Mon, 18 Aug 2025 10:58:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=BA=E5=99=A8=E8=AE=BE=E5=A4=87=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/Scripts/Sub/UI/PreloadUI.ts | 11 +- .../foundation/identity/MachineInfoService.ts | 113 ++++++++++++++++++ .../identity/MachineInfoService.ts.meta | 9 ++ 3 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 assets/Scripts/chat18x/foundation/identity/MachineInfoService.ts create mode 100644 assets/Scripts/chat18x/foundation/identity/MachineInfoService.ts.meta diff --git a/assets/Scripts/Sub/UI/PreloadUI.ts b/assets/Scripts/Sub/UI/PreloadUI.ts index a4fc4c2e..e9bdf0d4 100644 --- a/assets/Scripts/Sub/UI/PreloadUI.ts +++ b/assets/Scripts/Sub/UI/PreloadUI.ts @@ -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) { diff --git a/assets/Scripts/chat18x/foundation/identity/MachineInfoService.ts b/assets/Scripts/chat18x/foundation/identity/MachineInfoService.ts new file mode 100644 index 00000000..8d65abdb --- /dev/null +++ b/assets/Scripts/chat18x/foundation/identity/MachineInfoService.ts @@ -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.MODEL;iOS 使用硬件码(如 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; diff --git a/assets/Scripts/chat18x/foundation/identity/MachineInfoService.ts.meta b/assets/Scripts/chat18x/foundation/identity/MachineInfoService.ts.meta new file mode 100644 index 00000000..d3723c77 --- /dev/null +++ b/assets/Scripts/chat18x/foundation/identity/MachineInfoService.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "332c833c-26d7-42b6-bb1b-09af0650d8e3", + "files": [], + "subMetas": {}, + "userData": {} +}