diff --git a/cocoscreator_projects/YouleNexus/assets/framework/config/sources/native-settings.ts b/cocoscreator_projects/YouleNexus/assets/framework/config/sources/native-settings.ts new file mode 100644 index 0000000..cb37491 --- /dev/null +++ b/cocoscreator_projects/YouleNexus/assets/framework/config/sources/native-settings.ts @@ -0,0 +1,40 @@ +import type { ChannelIdentity, IdentitySource } from '../identity.ts'; + +/** 原生注入宿主(≈ window)。settings 由原生注入;全局 app_* 为 uAgent_3 路径回退。 */ +export interface NativeHost { + settings?: { + getothername?(name: string): string | number; + getchannelName?(): string | number; + getmarketname?(): string | number; + }; + [global: string]: unknown; // app_agent / app_market 等 +} + +function tryRead(primary: () => unknown, fallback: () => unknown): unknown { + try { + const v = primary(); + if (v !== undefined && v !== null && v !== '') return v; + } catch { /* 原生桥不存在,走回退 */ } + try { return fallback(); } catch { return undefined; } +} + +function has(v: unknown): boolean { return v !== undefined && v !== null && v !== ''; } + +/** + * 原生模式:同步读取渠道身份。逐字对齐原 05_Func.js: + * agent → window.settings.getothername('agent') | window.app_agent + * channel → window.settings.getchannelName() + * market → window.settings.getmarketname() | window.app_market + */ +export function nativeSettingsSource(win: NativeHost): IdentitySource { + return () => { + const out: Partial = {}; + const agentid = tryRead(() => win.settings?.getothername?.('agent'), () => win['app_agent']); + if (has(agentid)) out.agentid = agentid as string | number; + const channelid = tryRead(() => win.settings?.getchannelName?.(), () => undefined); + if (has(channelid)) out.channelid = channelid as string | number; + const marketid = tryRead(() => win.settings?.getmarketname?.(), () => win['app_market']); + if (has(marketid)) out.marketid = marketid as string | number; + return out; + }; +} diff --git a/cocoscreator_projects/framework-tests/config/sources/native-settings.test.ts b/cocoscreator_projects/framework-tests/config/sources/native-settings.test.ts new file mode 100644 index 0000000..432c773 --- /dev/null +++ b/cocoscreator_projects/framework-tests/config/sources/native-settings.test.ts @@ -0,0 +1,29 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { nativeSettingsSource } from '../../../YouleNexus/assets/framework/config/sources/native-settings.ts'; + +test('优先从 window.settings 读取 agentid/channelid/marketid', () => { + const win = { + settings: { + getothername: (name: string) => (name === 'agent' ? 'NA' : ''), + getchannelName: () => 'NC', + getmarketname: () => 7, + }, + }; + assert.deepEqual(nativeSettingsSource(win)(), { agentid: 'NA', channelid: 'NC', marketid: 7 }); +}); + +test('settings 抛错时回退到全局 app_agent / app_market', () => { + const win: any = { + settings: { getothername: () => { throw new Error('no bridge'); }, + getchannelName: () => { throw new Error('no bridge'); }, + getmarketname: () => { throw new Error('no bridge'); } }, + app_agent: 'GA', + app_market: 5, + }; + assert.deepEqual(nativeSettingsSource(win)(), { agentid: 'GA', marketid: 5 }); +}); + +test('完全无原生注入 → 空对象', () => { + assert.deepEqual(nativeSettingsSource({})(), {}); +});