From 9debc1238d614e763861f512dac05dca1f60ca18 Mon Sep 17 00:00:00 2001 From: chen wei bo <1025839511@qq.com> Date: Fri, 15 Aug 2025 16:19:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/Scripts/Sub/UI/PreloadUI.ts | 7 ++++ assets/Scripts/chat18x/foundation.meta | 9 ++++ .../Scripts/chat18x/foundation/identity.meta | 9 ++++ .../chat18x/foundation/identity/DeviceId.ts | 17 ++++++++ .../foundation/identity/DeviceId.ts.meta | 9 ++++ .../foundation/identity/DeviceIdService.ts | 41 +++++++++++++++++++ .../identity/DeviceIdService.ts.meta | 9 ++++ .../Scripts/chat18x/foundation/storage.meta | 9 ++++ .../chat18x/foundation/storage/KVStore.ts | 10 +++++ .../foundation/storage/KVStore.ts.meta | 9 ++++ 10 files changed, 129 insertions(+) create mode 100644 assets/Scripts/chat18x/foundation.meta create mode 100644 assets/Scripts/chat18x/foundation/identity.meta create mode 100644 assets/Scripts/chat18x/foundation/identity/DeviceId.ts create mode 100644 assets/Scripts/chat18x/foundation/identity/DeviceId.ts.meta create mode 100644 assets/Scripts/chat18x/foundation/identity/DeviceIdService.ts create mode 100644 assets/Scripts/chat18x/foundation/identity/DeviceIdService.ts.meta create mode 100644 assets/Scripts/chat18x/foundation/storage.meta create mode 100644 assets/Scripts/chat18x/foundation/storage/KVStore.ts create mode 100644 assets/Scripts/chat18x/foundation/storage/KVStore.ts.meta diff --git a/assets/Scripts/Sub/UI/PreloadUI.ts b/assets/Scripts/Sub/UI/PreloadUI.ts index e5acd907..b179da6c 100644 --- a/assets/Scripts/Sub/UI/PreloadUI.ts +++ b/assets/Scripts/Sub/UI/PreloadUI.ts @@ -27,6 +27,9 @@ import { promptItem } from "../Item/promptItem"; import LanguageUtils, { LanguageType } from "../../Main/Common/LanguageUtils"; import { DataManager } from "../../chat18x/data/DataManager"; import ConfigManager from "../../chat18x/manager/ConfigManager"; +import { AppConfig } from "db://assets/Scripts/chat18x/config/env/appConfig"; +import DeviceIdService from "db://assets/Scripts/chat18x/foundation/identity/DeviceIdService"; + const { ccclass, property } = _decorator; @ccclass("PreloadUI") @@ -59,8 +62,12 @@ export class PreloadUI extends Component { this.jinduFilled.fillRange = 0; //this.btnLogin.node.active = false; + // 初始化环境配置 + AppConfig.I; // 初始化数据层 DataManager.I.init(); + // 设备号 + DeviceIdService.I.init(); this.preloadFiles(); diff --git a/assets/Scripts/chat18x/foundation.meta b/assets/Scripts/chat18x/foundation.meta new file mode 100644 index 00000000..53fa79dd --- /dev/null +++ b/assets/Scripts/chat18x/foundation.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.2.0", + "importer": "directory", + "imported": true, + "uuid": "f42171fd-943c-43f1-8e9b-a01c598a7968", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/chat18x/foundation/identity.meta b/assets/Scripts/chat18x/foundation/identity.meta new file mode 100644 index 00000000..f850c017 --- /dev/null +++ b/assets/Scripts/chat18x/foundation/identity.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.2.0", + "importer": "directory", + "imported": true, + "uuid": "c4d4cb16-e0f2-47ce-bb95-e432ef6a103b", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/chat18x/foundation/identity/DeviceId.ts b/assets/Scripts/chat18x/foundation/identity/DeviceId.ts new file mode 100644 index 00000000..7c51415d --- /dev/null +++ b/assets/Scripts/chat18x/foundation/identity/DeviceId.ts @@ -0,0 +1,17 @@ +export default class DeviceId { + static uuidv4(): string { + const hex: string[] = []; + for (let i = 0; i < 256; i++) hex[i] = (i < 16 ? "0" : "") + i.toString(16); + const r: number[] = []; + for (let i = 0; i < 16; i++) r[i] = Math.floor(Math.random() * 256); + r[6] = (r[6] & 0x0f) | 0x40; // version 4 + r[8] = (r[8] & 0x3f) | 0x80; // variant + return ( + hex[r[0]] + hex[r[1]] + hex[r[2]] + hex[r[3]] + "-" + + hex[r[4]] + hex[r[5]] + "-" + + hex[r[6]] + hex[r[7]] + "-" + + hex[r[8]] + hex[r[9]] + "-" + + hex[r[10]] + hex[r[11]] + hex[r[12]] + hex[r[13]] + hex[r[14]] + hex[r[15]] + ); + } + } \ No newline at end of file diff --git a/assets/Scripts/chat18x/foundation/identity/DeviceId.ts.meta b/assets/Scripts/chat18x/foundation/identity/DeviceId.ts.meta new file mode 100644 index 00000000..be49d446 --- /dev/null +++ b/assets/Scripts/chat18x/foundation/identity/DeviceId.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "b1f91060-ad0e-41a8-af88-04b18e36e9d1", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/chat18x/foundation/identity/DeviceIdService.ts b/assets/Scripts/chat18x/foundation/identity/DeviceIdService.ts new file mode 100644 index 00000000..dccb60fb --- /dev/null +++ b/assets/Scripts/chat18x/foundation/identity/DeviceIdService.ts @@ -0,0 +1,41 @@ +import KVStore from "../storage/KVStore"; +import DeviceId from "./DeviceId"; + +export class DeviceIdService { + private static _I: DeviceIdService | null = null; + static get I(): DeviceIdService { return this._I ?? (this._I = new DeviceIdService()); } + private constructor(private store = new KVStore("app:")) {} + + private static KEY = "device_id"; + private _id: string | null = null; + private _inited = false; + + /** 启动时调用:加载或生成并持久化 */ + init(): void { + if (this._inited) return; + const saved = this.store.get(DeviceIdService.KEY); + if (saved) { + this._id = saved; + } else { + // 如需原生ID,可先尝试原生,再用 UUID 兜底 + this._id = DeviceId.uuidv4(); + this.store.set(DeviceIdService.KEY, this._id); + } + this._inited = true; + } + + /** 获取设备号(若未 init,会懒初始化一次) */ + get id(): string { + if (!this._inited) this.init(); + // 理论上不为空,保险起见再兜底 + return this._id ?? DeviceId.uuidv4(); + } + + /** 重置(合规/调试用) */ + reset(): void { + this.store.remove(DeviceIdService.KEY); + this._id = null; + this._inited = false; + } +} +export default DeviceIdService; diff --git a/assets/Scripts/chat18x/foundation/identity/DeviceIdService.ts.meta b/assets/Scripts/chat18x/foundation/identity/DeviceIdService.ts.meta new file mode 100644 index 00000000..c297aca6 --- /dev/null +++ b/assets/Scripts/chat18x/foundation/identity/DeviceIdService.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "23dd5007-7e97-4e49-9035-8fbca04d0d03", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/chat18x/foundation/storage.meta b/assets/Scripts/chat18x/foundation/storage.meta new file mode 100644 index 00000000..236f48fa --- /dev/null +++ b/assets/Scripts/chat18x/foundation/storage.meta @@ -0,0 +1,9 @@ +{ + "ver": "1.2.0", + "importer": "directory", + "imported": true, + "uuid": "cd3361cc-4e2b-4bc0-8a6e-016111f5e5d8", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/Scripts/chat18x/foundation/storage/KVStore.ts b/assets/Scripts/chat18x/foundation/storage/KVStore.ts new file mode 100644 index 00000000..b72e49d4 --- /dev/null +++ b/assets/Scripts/chat18x/foundation/storage/KVStore.ts @@ -0,0 +1,10 @@ +import { sys } from "cc"; + +export default class KVStore { + constructor(private prefix = "app:") {} + private key(k: string) { return `${this.prefix}${k}`; } + + get(k: string): string | null { return sys.localStorage.getItem(this.key(k)); } + set(k: string, v: string): void { sys.localStorage.setItem(this.key(k), v); } + remove(k: string): void { sys.localStorage.removeItem(this.key(k)); } +} diff --git a/assets/Scripts/chat18x/foundation/storage/KVStore.ts.meta b/assets/Scripts/chat18x/foundation/storage/KVStore.ts.meta new file mode 100644 index 00000000..f16b8152 --- /dev/null +++ b/assets/Scripts/chat18x/foundation/storage/KVStore.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.24", + "importer": "typescript", + "imported": true, + "uuid": "be7e3168-3885-4ad8-8775-8e2f1fe957ce", + "files": [], + "subMetas": {}, + "userData": {} +}