diff --git a/cocoscreator_projects/YouleNexus/assets/framework/config/sources/query-string.ts b/cocoscreator_projects/YouleNexus/assets/framework/config/sources/query-string.ts new file mode 100644 index 0000000..22a6fc6 --- /dev/null +++ b/cocoscreator_projects/YouleNexus/assets/framework/config/sources/query-string.ts @@ -0,0 +1,14 @@ +import type { ChannelIdentity, IdentitySource } from '../identity.ts'; + +/** H5 模式:从 location.search 解析渠道身份覆盖。URLSearchParams 在浏览器/Cocos/Node 均可用。 */ +export function queryStringSource(search: string): IdentitySource { + return () => { + const q = new URLSearchParams(search); + const out: Partial = {}; + const agentid = q.get('agentid'); if (agentid) out.agentid = agentid; + const channelid = q.get('channelid'); if (channelid) out.channelid = channelid; + const marketid = q.get('marketid'); if (marketid) out.marketid = marketid; + const version = q.get('version'); if (version) out.version = version; + return out; + }; +} diff --git a/cocoscreator_projects/framework-tests/config/sources/query-string.test.ts b/cocoscreator_projects/framework-tests/config/sources/query-string.test.ts new file mode 100644 index 0000000..40657bb --- /dev/null +++ b/cocoscreator_projects/framework-tests/config/sources/query-string.test.ts @@ -0,0 +1,21 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { queryStringSource } from '../../../YouleNexus/assets/framework/config/sources/query-string.ts'; + +test('解析 agentid/channelid/marketid/version', () => { + const src = queryStringSource('?agentid=A1&channelid=C1&marketid=2&version=9.9'); + assert.deepEqual(src(), { agentid: 'A1', channelid: 'C1', marketid: '2', version: '9.9' }); +}); + +test('缺省键不出现在结果里', () => { + const src = queryStringSource('?agentid=A1'); + assert.deepEqual(src(), { agentid: 'A1' }); +}); + +test('空 query → 空对象', () => { + assert.deepEqual(queryStringSource('')(), {}); +}); + +test('不含前导 ? 也能解析', () => { + assert.deepEqual(queryStringSource('agentid=A1')(), { agentid: 'A1' }); +});