@dajiaoai/algeo-sdk 2.16.1 → 2.16.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  大角几何内嵌画板 SDK,支持在任意网页中嵌入几何画板,通过 postMessage 与内嵌页通信。
4
4
 
5
- 当前版本:**2.16.1**。版本变更与升级注意事项见 [GitHub Release](https://github.com/dajiaoai/algeo-sdk/releases/tag/2.16.1)。
5
+ 当前版本:**2.16.2**。版本变更与升级注意事项见 [GitHub Release](https://github.com/dajiaoai/algeo-sdk/releases/tag/2.16.2)。
6
6
 
7
7
  ## 交流与支持
8
8
 
@@ -35,9 +35,9 @@ npm install @dajiaoai/algeo-sdk
35
35
 
36
36
  ## 使用方式
37
37
 
38
- ### 本地 HTML 文件
38
+ ### 本地使用
39
39
 
40
- 通过 `file://` 直接打开 HTML 时,演示和编辑模式都可以省略 `auth` 或将 `appId` 留空。
40
+ 通过 `file://` 直接打开本地文件,或通过 `localhost`、`127.0.0.1` 访问本地服务时,演示和编辑模式都可以省略 `auth`。本地联调应用授权时,请使用本地 HTTP 服务并配置 `auth.appId` 和域名白名单。详见 [SDK 鉴权说明](https://open.dajiaoai.com/sdk/2/auth)。
41
41
 
42
42
  ```javascript
43
43
  const presentation = await createPresentation(container, { shareId: '33TA3484' });
@@ -301,7 +301,7 @@ create(container, {
301
301
 
302
302
  演示模式快捷入口。该函数负责真正创建并初始化 `EmbeddedPresentation`。
303
303
 
304
- 创建成功后,SDK 会额外调用 `https://open.dajiaoai.com/console/api/whitelist/check`,使用 `options.auth.appId` 与当前页面 `hostname` 做白名单校验。若未命中白名单,iframe 仍会完成初始化并呈现页面,`createPresentation` 仍会成功返回实例,但后续 `loadShareById`、`loadFile`、`switchSlide`、`getSlideCount`、`repl` 等演示模式 API 会被拒绝,并在控制台输出包含方法名的错误,便于接入方感知并处理。
304
+ `createPresentation` 初始化时会加载画板。首次调用 `switchSlide` 等需要授权的方法时,SDK 才校验应用和宿主主机名。校验失败时,该方法返回错误。
305
305
 
306
306
  ```javascript
307
307
  create(container, {
@@ -1,5 +1,33 @@
1
1
  'use strict';
2
2
 
3
+ const SDK_LOGO = `<svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" fill="none" viewBox="0 0 36 36">
4
+ <g clip-path="url(#a)">
5
+ <path stroke="#A668B8" stroke-width="3.819" d="m30.261 29.85-5.368-23.9c-.163-.723-1.126-.875-1.503-.237l-9.082 15.349a.818.818 0 0 0 .288 1.12l14.45 8.551a.818.818 0 0 0 1.215-.884Z"/>
6
+ <path stroke="#01B88B" stroke-width="3.819" d="M5.335 11.746V30.12c6.605.315 14.644.647 15.468-8.963.165-1.922-.34-4.035-.824-5.9-.851-3.276-3.854-5.913-12.59-5.913"/>
7
+ <path fill="#A668B8" d="m16.483 13.62 2.182-3.682 3.273 1.91-3.682 6.137-1.773-4.364Z"/>
8
+ </g>
9
+ <defs>
10
+ <clipPath id="a">
11
+ <path fill="#fff" d="M0 0h36v36H0z"/>
12
+ </clipPath>
13
+ </defs>
14
+ </svg>`;
15
+ const BADGE_STYLE = 'background-color: #f1faf7; background-position: 9px center;' +
16
+ 'background-repeat: no-repeat; background-size: 24px 24px;' +
17
+ 'border: 1px solid #9cd4c5; border-radius: 4px; padding: 6px 12px 6px 42px;' +
18
+ 'color: #123e35; font-size: 14px; font-weight: bold;';
19
+ const TITLE_STYLE = 'background-color: #174e42; color: #fff; font-weight: bold; padding: 2px 8px;';
20
+ const DIVIDER = '+------------------------------------------------------+';
21
+ function logSdk(...entry) {
22
+ if (entry[0] === 'debug') {
23
+ const [, message, ...details] = entry;
24
+ console.debug('[Algeo SDK]', message, ...details);
25
+ return;
26
+ }
27
+ const [, { title, lines }] = entry;
28
+ console.info(`%cALGEO SDK / 大角几何%c\n\n${DIVIDER}\n| %c%s%c\n%s\n${DIVIDER}`, `background-image: url(data:image/svg+xml;base64,${btoa(SDK_LOGO)});${BADGE_STYLE}`, '', TITLE_STYLE, title, '', ['|', ...lines.map((line) => (line ? `| ${line}` : '|'))].join('\n'));
29
+ }
30
+
3
31
  var util;
4
32
  (function (util) {
5
33
  util.assertEqual = (_) => { };
@@ -4011,7 +4039,7 @@ function convertToLatest(content) {
4011
4039
  }
4012
4040
 
4013
4041
  /** SDK 版本号,构建时由 rollup 注入 */
4014
- const VERSION = '2.16.1';
4042
+ const VERSION = '2.16.2';
4015
4043
  const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
4016
4044
  const DEFAULT_PRESENTATION_PATH = '/embed/present';
4017
4045
  const DEFAULT_EDITOR_PATH = '/embed/edit';
@@ -4019,6 +4047,34 @@ const EMBED_TIMEOUT_MS = 30000;
4019
4047
  function isLocalFileHost() {
4020
4048
  return typeof window !== 'undefined' && window.location.protocol === 'file:';
4021
4049
  }
4050
+ function isLocalLoopbackHost() {
4051
+ if (typeof window === 'undefined')
4052
+ return false;
4053
+ const { protocol, hostname } = window.location;
4054
+ return ((protocol === 'http:' || protocol === 'https:') &&
4055
+ ['localhost', '127.0.0.1'].includes(hostname));
4056
+ }
4057
+ function readEmbedAppId(auth) {
4058
+ const appId = auth?.appId?.trim() ?? '';
4059
+ if (!appId) {
4060
+ logSdk('notice', {
4061
+ title: '未配置应用 ID(auth.appId)',
4062
+ lines: [
4063
+ '注册应用 > 配置域名白名单 > 填写 auth.appId',
4064
+ '开放平台 https://open.dajiaoai.com/console',
4065
+ '使用许可 https://open.dajiaoai.com/sdk/2/auth',
4066
+ '',
4067
+ '版权 © 大角几何',
4068
+ ],
4069
+ });
4070
+ }
4071
+ return appId;
4072
+ }
4073
+ function requireHttpHostForAppId(appId) {
4074
+ if (appId && isLocalFileHost()) {
4075
+ throw new AlgeoError('file:// 没有可校验的宿主域名。请通过本地 HTTP 服务打开页面,将该主机加入应用白名单后重试。', EMBED_ERROR_CODES.BAD_REQUEST);
4076
+ }
4077
+ }
4022
4078
  let requestIdCounter = 0;
4023
4079
  function generateRequestId() {
4024
4080
  return `req-${Date.now()}-${++requestIdCounter}`;
@@ -4133,10 +4189,13 @@ function buildEmbedSrc(options) {
4133
4189
  const baseUrl = normalizeBaseUrl(options.baseUrl ?? DEFAULT_EMBED_BASE);
4134
4190
  const mode = normalizeMode(options.mode);
4135
4191
  const path = getEmbedPath(mode);
4136
- const localFile = isLocalFileHost();
4137
- const appId = options.auth?.appId?.trim() || (localFile ? 'local' : '');
4192
+ const appId = options.auth?.appId?.trim();
4193
+ const localFile = !appId && isLocalFileHost();
4194
+ const localLoopback = !appId && isLocalLoopbackHost();
4138
4195
  const initialId = options.initialId?.trim();
4139
- const src = `${baseUrl}${path}/${encodeURIComponent(appId)}${initialId ? `/${encodeURIComponent(initialId)}` : ''}`;
4196
+ const initialPath = initialId ? `/${encodeURIComponent(initialId)}` : '';
4197
+ const routeAppId = appId || (localFile || localLoopback ? 'local' : 'public');
4198
+ const src = `${baseUrl}${path}/${encodeURIComponent(routeAppId)}${initialPath}`;
4140
4199
  const params = new URLSearchParams();
4141
4200
  if (mode === 'editor' && options.cameraMode) {
4142
4201
  params.set('cameraMode', options.cameraMode);
@@ -4144,6 +4203,9 @@ function buildEmbedSrc(options) {
4144
4203
  if (mode === 'editor' && localFile) {
4145
4204
  params.set('localFile', '1');
4146
4205
  }
4206
+ if (mode === 'editor' && localLoopback) {
4207
+ params.set('localLoopback', '1');
4208
+ }
4147
4209
  return params.size ? `${src}?${params}` : src;
4148
4210
  }
4149
4211
  const MASTER_TEMPLATE_OPERATIONS = [
@@ -4243,6 +4305,7 @@ class EmbeddedTarget {
4243
4305
  ...options,
4244
4306
  mode: this.embedMode,
4245
4307
  });
4308
+ const iframeOrigin = new URL(src, window.location.href).origin;
4246
4309
  let settled = false;
4247
4310
  let initTimeoutId;
4248
4311
  const finalizeFailure = (error) => {
@@ -4295,6 +4358,19 @@ class EmbeddedTarget {
4295
4358
  iframe.contentWindow?.postMessage({ type: 'localFileHostConfirmed', requestId: data.requestId }, '*');
4296
4359
  return;
4297
4360
  }
4361
+ if (data?.type === 'localLoopbackHostCheck' &&
4362
+ typeof data.requestId === 'string' &&
4363
+ !options.auth?.appId?.trim() &&
4364
+ isLocalLoopbackHost()) {
4365
+ iframe.contentWindow?.postMessage({ type: 'localLoopbackHostConfirmed', requestId: data.requestId }, iframeOrigin);
4366
+ return;
4367
+ }
4368
+ if (this.embedMode === 'editor' &&
4369
+ e.origin === iframeOrigin &&
4370
+ data?.type === 'embedEditAuthorizationFailed') {
4371
+ finalizeFailure(new AlgeoError('白名单校验未通过。请在开放平台将当前主机加入应用白名单后重试。', EMBED_ERROR_CODES.BAD_REQUEST));
4372
+ return;
4373
+ }
4298
4374
  if (isReadyMessage(data)) {
4299
4375
  if (this._ready) {
4300
4376
  return;
@@ -4427,6 +4503,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4427
4503
  super(container, 'presentation');
4428
4504
  this.currentSlideIndex = 0;
4429
4505
  this.slideCount = 0;
4506
+ this.appId = '';
4430
4507
  this.uiConfig = {
4431
4508
  logo: true,
4432
4509
  slidePanel: true,
@@ -4436,19 +4513,30 @@ class EmbeddedPresentation extends EmbeddedTarget {
4436
4513
  this.mode = {
4437
4514
  getUiConfig: () => ({ ...this.uiConfig }),
4438
4515
  setUiConfig: async (config) => {
4516
+ await this.ensureWhitelistAccess('setUiConfig');
4439
4517
  await this.post('setUiConfig', { config });
4440
4518
  this.uiConfig = {
4441
4519
  ...this.uiConfig,
4442
4520
  ...config,
4443
4521
  };
4444
4522
  },
4445
- setMasterTemplate: (template) => {
4446
- this.ensureWhitelistAccess('setMasterTemplate');
4523
+ setMasterTemplate: async (template) => {
4524
+ await this.ensureWhitelistAccess('setMasterTemplate');
4447
4525
  return this.post('setMasterTemplate', { template });
4448
4526
  },
4449
4527
  };
4450
4528
  }
4451
4529
  async initialize(options = {}, baseUrl) {
4530
+ const appId = readEmbedAppId(options.auth);
4531
+ if (!this.iframe) {
4532
+ this.appId = appId;
4533
+ this.whitelistCheck = undefined;
4534
+ }
4535
+ // 未配置应用的部署页面按默认样式展示公开分享。
4536
+ if (!appId && !isLocalFileHost() && !isLocalLoopbackHost()) {
4537
+ await this.init({ baseUrl, initialId: options.shareId });
4538
+ return;
4539
+ }
4452
4540
  this.uiConfig = {
4453
4541
  logo: true,
4454
4542
  slidePanel: true,
@@ -4467,7 +4555,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4467
4555
  if (options.fonts) {
4468
4556
  await this.post('setFontConfig', { config: options.fonts });
4469
4557
  }
4470
- await this.mode.setUiConfig(this.uiConfig);
4558
+ await this.post('setUiConfig', { config: this.uiConfig });
4471
4559
  }
4472
4560
  acceptsEventMessage() {
4473
4561
  return false;
@@ -4475,16 +4563,44 @@ class EmbeddedPresentation extends EmbeddedTarget {
4475
4563
  setWhitelistError(error) {
4476
4564
  this.whitelistError = error;
4477
4565
  }
4478
- ensureWhitelistAccess(methodName) {
4479
- if (!this.whitelistError) {
4480
- return;
4566
+ async checkWhitelist(appId) {
4567
+ if (!appId) {
4568
+ if (isLocalFileHost() || isLocalLoopbackHost())
4569
+ return;
4570
+ throw new AlgeoError('正式接入演示模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
4571
+ }
4572
+ requireHttpHostForAppId(appId);
4573
+ const host = window.location.hostname;
4574
+ const url = new URL('https://open.dajiaoai.com/console/api/whitelist/check');
4575
+ url.searchParams.set('appId', appId);
4576
+ url.searchParams.set('host', host);
4577
+ try {
4578
+ const response = await fetch(url.toString(), { cache: 'no-store' });
4579
+ if (response.ok && (await response.json()).matched === true)
4580
+ return;
4581
+ }
4582
+ catch {
4583
+ // 网络或响应解析失败时按鉴权失败处理。
4584
+ }
4585
+ throw new AlgeoError(`白名单校验未通过,appId="${appId}",host="${host}"。请在开放平台将该主机加入应用白名单后重试。`, EMBED_ERROR_CODES.BAD_REQUEST);
4586
+ }
4587
+ async ensureWhitelistAccess(methodName) {
4588
+ try {
4589
+ if (this.whitelistError)
4590
+ throw this.whitelistError;
4591
+ this.whitelistCheck ?? (this.whitelistCheck = this.checkWhitelist(this.appId));
4592
+ await this.whitelistCheck;
4593
+ }
4594
+ catch (cause) {
4595
+ if (!(cause instanceof AlgeoError))
4596
+ throw cause;
4597
+ const error = new AlgeoError(`演示模式调用 ${methodName} 被白名单限制。${cause.message}`, cause.code, cause.details);
4598
+ console.error(error);
4599
+ throw error;
4481
4600
  }
4482
- const error = new AlgeoError(`演示模式调用 ${methodName} 被白名单限制。${this.whitelistError.message}`, this.whitelistError.code, this.whitelistError.details);
4483
- console.error(error);
4484
- throw error;
4485
4601
  }
4486
4602
  async loadShareById(id) {
4487
- this.ensureWhitelistAccess('loadShareById');
4603
+ await this.ensureWhitelistAccess('loadShareById');
4488
4604
  const result = await this.post('loadShareById', {
4489
4605
  id,
4490
4606
  });
@@ -4494,7 +4610,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4494
4610
  return result;
4495
4611
  }
4496
4612
  async loadFile(content) {
4497
- this.ensureWhitelistAccess('loadFile');
4613
+ await this.ensureWhitelistAccess('loadFile');
4498
4614
  const latestContent = convertToLatest(content);
4499
4615
  const result = await this.post('loadContent', {
4500
4616
  content: latestContent,
@@ -4505,19 +4621,19 @@ class EmbeddedPresentation extends EmbeddedTarget {
4505
4621
  return result;
4506
4622
  }
4507
4623
  async switchSlide(index) {
4508
- this.ensureWhitelistAccess('switchSlide');
4624
+ await this.ensureWhitelistAccess('switchSlide');
4509
4625
  const result = await this.post('switchSlide', { index });
4510
4626
  this.currentSlideIndex = index;
4511
4627
  return result;
4512
4628
  }
4513
4629
  async getSlideCount() {
4514
- this.ensureWhitelistAccess('getSlideCount');
4630
+ await this.ensureWhitelistAccess('getSlideCount');
4515
4631
  const result = await this.post('getSlideCount', {});
4516
4632
  this.slideCount = result.count;
4517
4633
  return result;
4518
4634
  }
4519
4635
  async repl(command) {
4520
- this.ensureWhitelistAccess('repl');
4636
+ await this.ensureWhitelistAccess('repl');
4521
4637
  return this.post('repl', { command });
4522
4638
  }
4523
4639
  }
@@ -4705,7 +4821,9 @@ class EmbeddedEditor extends EmbeddedTarget {
4705
4821
  this.historyCurrentIndex = state.historyCurrentIndex;
4706
4822
  }
4707
4823
  async initialize(options = {}, baseUrl) {
4708
- if (!options.auth?.appId?.trim() && !isLocalFileHost()) {
4824
+ const appId = readEmbedAppId(options.auth);
4825
+ requireHttpHostForAppId(appId);
4826
+ if (!appId && !isLocalFileHost() && !isLocalLoopbackHost()) {
4709
4827
  throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
4710
4828
  }
4711
4829
  this.uiConfig = options.ui ? { ...options.ui } : {};
@@ -5215,35 +5333,6 @@ class EmbeddedEditor extends EmbeddedTarget {
5215
5333
  }
5216
5334
  }
5217
5335
 
5218
- const WHITELIST_CHECK_BASE_URL = 'https://open.dajiaoai.com/console';
5219
- const WHITELIST_CHECK_PATH = '/api/whitelist/check';
5220
- async function checkPresentationWhitelist(options) {
5221
- if (isLocalFileHost())
5222
- return null;
5223
- const appId = options.auth?.appId?.trim() ?? '';
5224
- const host = window.location.hostname.trim();
5225
- const url = new URL(`${normalizeBaseUrl(WHITELIST_CHECK_BASE_URL)}${WHITELIST_CHECK_PATH}`);
5226
- url.searchParams.set('appId', appId);
5227
- url.searchParams.set('host', host);
5228
- let matched = false;
5229
- try {
5230
- const response = await fetch(url.toString(), {
5231
- method: 'GET',
5232
- cache: 'no-store',
5233
- });
5234
- if (response.ok) {
5235
- const payload = (await response.json());
5236
- matched = payload.matched === true;
5237
- }
5238
- }
5239
- catch {
5240
- matched = false;
5241
- }
5242
- if (matched) {
5243
- return null;
5244
- }
5245
- return new AlgeoError(`白名单校验未通过,appId="${appId || '(empty)'}",host="${host || '(empty)'}"。`, EMBED_ERROR_CODES.BAD_REQUEST);
5246
- }
5247
5336
  async function createEditorInstance(container, options = {}, baseUrl) {
5248
5337
  const editor = new EmbeddedEditor(container);
5249
5338
  await editor.initialize(options, baseUrl);
@@ -5252,10 +5341,6 @@ async function createEditorInstance(container, options = {}, baseUrl) {
5252
5341
  async function createPresentationInstance(container, options = {}, baseUrl) {
5253
5342
  const presentation = new EmbeddedPresentation(container);
5254
5343
  await presentation.initialize(options, baseUrl);
5255
- const whitelistError = await checkPresentationWhitelist(options);
5256
- if (whitelistError) {
5257
- presentation.setWhitelistError(whitelistError);
5258
- }
5259
5344
  return presentation;
5260
5345
  }
5261
5346
  function createEmbeddedInstance(container, options) {
@@ -1,3 +1,31 @@
1
+ const SDK_LOGO = `<svg xmlns="http://www.w3.org/2000/svg" width="36" height="36" fill="none" viewBox="0 0 36 36">
2
+ <g clip-path="url(#a)">
3
+ <path stroke="#A668B8" stroke-width="3.819" d="m30.261 29.85-5.368-23.9c-.163-.723-1.126-.875-1.503-.237l-9.082 15.349a.818.818 0 0 0 .288 1.12l14.45 8.551a.818.818 0 0 0 1.215-.884Z"/>
4
+ <path stroke="#01B88B" stroke-width="3.819" d="M5.335 11.746V30.12c6.605.315 14.644.647 15.468-8.963.165-1.922-.34-4.035-.824-5.9-.851-3.276-3.854-5.913-12.59-5.913"/>
5
+ <path fill="#A668B8" d="m16.483 13.62 2.182-3.682 3.273 1.91-3.682 6.137-1.773-4.364Z"/>
6
+ </g>
7
+ <defs>
8
+ <clipPath id="a">
9
+ <path fill="#fff" d="M0 0h36v36H0z"/>
10
+ </clipPath>
11
+ </defs>
12
+ </svg>`;
13
+ const BADGE_STYLE = 'background-color: #f1faf7; background-position: 9px center;' +
14
+ 'background-repeat: no-repeat; background-size: 24px 24px;' +
15
+ 'border: 1px solid #9cd4c5; border-radius: 4px; padding: 6px 12px 6px 42px;' +
16
+ 'color: #123e35; font-size: 14px; font-weight: bold;';
17
+ const TITLE_STYLE = 'background-color: #174e42; color: #fff; font-weight: bold; padding: 2px 8px;';
18
+ const DIVIDER = '+------------------------------------------------------+';
19
+ function logSdk(...entry) {
20
+ if (entry[0] === 'debug') {
21
+ const [, message, ...details] = entry;
22
+ console.debug('[Algeo SDK]', message, ...details);
23
+ return;
24
+ }
25
+ const [, { title, lines }] = entry;
26
+ console.info(`%cALGEO SDK / 大角几何%c\n\n${DIVIDER}\n| %c%s%c\n%s\n${DIVIDER}`, `background-image: url(data:image/svg+xml;base64,${btoa(SDK_LOGO)});${BADGE_STYLE}`, '', TITLE_STYLE, title, '', ['|', ...lines.map((line) => (line ? `| ${line}` : '|'))].join('\n'));
27
+ }
28
+
1
29
  var util;
2
30
  (function (util) {
3
31
  util.assertEqual = (_) => { };
@@ -4009,7 +4037,7 @@ function convertToLatest(content) {
4009
4037
  }
4010
4038
 
4011
4039
  /** SDK 版本号,构建时由 rollup 注入 */
4012
- const VERSION = '2.16.1';
4040
+ const VERSION = '2.16.2';
4013
4041
  const DEFAULT_EMBED_BASE = 'https://dajiaoai.com';
4014
4042
  const DEFAULT_PRESENTATION_PATH = '/embed/present';
4015
4043
  const DEFAULT_EDITOR_PATH = '/embed/edit';
@@ -4017,6 +4045,34 @@ const EMBED_TIMEOUT_MS = 30000;
4017
4045
  function isLocalFileHost() {
4018
4046
  return typeof window !== 'undefined' && window.location.protocol === 'file:';
4019
4047
  }
4048
+ function isLocalLoopbackHost() {
4049
+ if (typeof window === 'undefined')
4050
+ return false;
4051
+ const { protocol, hostname } = window.location;
4052
+ return ((protocol === 'http:' || protocol === 'https:') &&
4053
+ ['localhost', '127.0.0.1'].includes(hostname));
4054
+ }
4055
+ function readEmbedAppId(auth) {
4056
+ const appId = auth?.appId?.trim() ?? '';
4057
+ if (!appId) {
4058
+ logSdk('notice', {
4059
+ title: '未配置应用 ID(auth.appId)',
4060
+ lines: [
4061
+ '注册应用 > 配置域名白名单 > 填写 auth.appId',
4062
+ '开放平台 https://open.dajiaoai.com/console',
4063
+ '使用许可 https://open.dajiaoai.com/sdk/2/auth',
4064
+ '',
4065
+ '版权 © 大角几何',
4066
+ ],
4067
+ });
4068
+ }
4069
+ return appId;
4070
+ }
4071
+ function requireHttpHostForAppId(appId) {
4072
+ if (appId && isLocalFileHost()) {
4073
+ throw new AlgeoError('file:// 没有可校验的宿主域名。请通过本地 HTTP 服务打开页面,将该主机加入应用白名单后重试。', EMBED_ERROR_CODES.BAD_REQUEST);
4074
+ }
4075
+ }
4020
4076
  let requestIdCounter = 0;
4021
4077
  function generateRequestId() {
4022
4078
  return `req-${Date.now()}-${++requestIdCounter}`;
@@ -4131,10 +4187,13 @@ function buildEmbedSrc(options) {
4131
4187
  const baseUrl = normalizeBaseUrl(options.baseUrl ?? DEFAULT_EMBED_BASE);
4132
4188
  const mode = normalizeMode(options.mode);
4133
4189
  const path = getEmbedPath(mode);
4134
- const localFile = isLocalFileHost();
4135
- const appId = options.auth?.appId?.trim() || (localFile ? 'local' : '');
4190
+ const appId = options.auth?.appId?.trim();
4191
+ const localFile = !appId && isLocalFileHost();
4192
+ const localLoopback = !appId && isLocalLoopbackHost();
4136
4193
  const initialId = options.initialId?.trim();
4137
- const src = `${baseUrl}${path}/${encodeURIComponent(appId)}${initialId ? `/${encodeURIComponent(initialId)}` : ''}`;
4194
+ const initialPath = initialId ? `/${encodeURIComponent(initialId)}` : '';
4195
+ const routeAppId = appId || (localFile || localLoopback ? 'local' : 'public');
4196
+ const src = `${baseUrl}${path}/${encodeURIComponent(routeAppId)}${initialPath}`;
4138
4197
  const params = new URLSearchParams();
4139
4198
  if (mode === 'editor' && options.cameraMode) {
4140
4199
  params.set('cameraMode', options.cameraMode);
@@ -4142,6 +4201,9 @@ function buildEmbedSrc(options) {
4142
4201
  if (mode === 'editor' && localFile) {
4143
4202
  params.set('localFile', '1');
4144
4203
  }
4204
+ if (mode === 'editor' && localLoopback) {
4205
+ params.set('localLoopback', '1');
4206
+ }
4145
4207
  return params.size ? `${src}?${params}` : src;
4146
4208
  }
4147
4209
  const MASTER_TEMPLATE_OPERATIONS = [
@@ -4241,6 +4303,7 @@ class EmbeddedTarget {
4241
4303
  ...options,
4242
4304
  mode: this.embedMode,
4243
4305
  });
4306
+ const iframeOrigin = new URL(src, window.location.href).origin;
4244
4307
  let settled = false;
4245
4308
  let initTimeoutId;
4246
4309
  const finalizeFailure = (error) => {
@@ -4293,6 +4356,19 @@ class EmbeddedTarget {
4293
4356
  iframe.contentWindow?.postMessage({ type: 'localFileHostConfirmed', requestId: data.requestId }, '*');
4294
4357
  return;
4295
4358
  }
4359
+ if (data?.type === 'localLoopbackHostCheck' &&
4360
+ typeof data.requestId === 'string' &&
4361
+ !options.auth?.appId?.trim() &&
4362
+ isLocalLoopbackHost()) {
4363
+ iframe.contentWindow?.postMessage({ type: 'localLoopbackHostConfirmed', requestId: data.requestId }, iframeOrigin);
4364
+ return;
4365
+ }
4366
+ if (this.embedMode === 'editor' &&
4367
+ e.origin === iframeOrigin &&
4368
+ data?.type === 'embedEditAuthorizationFailed') {
4369
+ finalizeFailure(new AlgeoError('白名单校验未通过。请在开放平台将当前主机加入应用白名单后重试。', EMBED_ERROR_CODES.BAD_REQUEST));
4370
+ return;
4371
+ }
4296
4372
  if (isReadyMessage(data)) {
4297
4373
  if (this._ready) {
4298
4374
  return;
@@ -4425,6 +4501,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4425
4501
  super(container, 'presentation');
4426
4502
  this.currentSlideIndex = 0;
4427
4503
  this.slideCount = 0;
4504
+ this.appId = '';
4428
4505
  this.uiConfig = {
4429
4506
  logo: true,
4430
4507
  slidePanel: true,
@@ -4434,19 +4511,30 @@ class EmbeddedPresentation extends EmbeddedTarget {
4434
4511
  this.mode = {
4435
4512
  getUiConfig: () => ({ ...this.uiConfig }),
4436
4513
  setUiConfig: async (config) => {
4514
+ await this.ensureWhitelistAccess('setUiConfig');
4437
4515
  await this.post('setUiConfig', { config });
4438
4516
  this.uiConfig = {
4439
4517
  ...this.uiConfig,
4440
4518
  ...config,
4441
4519
  };
4442
4520
  },
4443
- setMasterTemplate: (template) => {
4444
- this.ensureWhitelistAccess('setMasterTemplate');
4521
+ setMasterTemplate: async (template) => {
4522
+ await this.ensureWhitelistAccess('setMasterTemplate');
4445
4523
  return this.post('setMasterTemplate', { template });
4446
4524
  },
4447
4525
  };
4448
4526
  }
4449
4527
  async initialize(options = {}, baseUrl) {
4528
+ const appId = readEmbedAppId(options.auth);
4529
+ if (!this.iframe) {
4530
+ this.appId = appId;
4531
+ this.whitelistCheck = undefined;
4532
+ }
4533
+ // 未配置应用的部署页面按默认样式展示公开分享。
4534
+ if (!appId && !isLocalFileHost() && !isLocalLoopbackHost()) {
4535
+ await this.init({ baseUrl, initialId: options.shareId });
4536
+ return;
4537
+ }
4450
4538
  this.uiConfig = {
4451
4539
  logo: true,
4452
4540
  slidePanel: true,
@@ -4465,7 +4553,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4465
4553
  if (options.fonts) {
4466
4554
  await this.post('setFontConfig', { config: options.fonts });
4467
4555
  }
4468
- await this.mode.setUiConfig(this.uiConfig);
4556
+ await this.post('setUiConfig', { config: this.uiConfig });
4469
4557
  }
4470
4558
  acceptsEventMessage() {
4471
4559
  return false;
@@ -4473,16 +4561,44 @@ class EmbeddedPresentation extends EmbeddedTarget {
4473
4561
  setWhitelistError(error) {
4474
4562
  this.whitelistError = error;
4475
4563
  }
4476
- ensureWhitelistAccess(methodName) {
4477
- if (!this.whitelistError) {
4478
- return;
4564
+ async checkWhitelist(appId) {
4565
+ if (!appId) {
4566
+ if (isLocalFileHost() || isLocalLoopbackHost())
4567
+ return;
4568
+ throw new AlgeoError('正式接入演示模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
4569
+ }
4570
+ requireHttpHostForAppId(appId);
4571
+ const host = window.location.hostname;
4572
+ const url = new URL('https://open.dajiaoai.com/console/api/whitelist/check');
4573
+ url.searchParams.set('appId', appId);
4574
+ url.searchParams.set('host', host);
4575
+ try {
4576
+ const response = await fetch(url.toString(), { cache: 'no-store' });
4577
+ if (response.ok && (await response.json()).matched === true)
4578
+ return;
4579
+ }
4580
+ catch {
4581
+ // 网络或响应解析失败时按鉴权失败处理。
4582
+ }
4583
+ throw new AlgeoError(`白名单校验未通过,appId="${appId}",host="${host}"。请在开放平台将该主机加入应用白名单后重试。`, EMBED_ERROR_CODES.BAD_REQUEST);
4584
+ }
4585
+ async ensureWhitelistAccess(methodName) {
4586
+ try {
4587
+ if (this.whitelistError)
4588
+ throw this.whitelistError;
4589
+ this.whitelistCheck ?? (this.whitelistCheck = this.checkWhitelist(this.appId));
4590
+ await this.whitelistCheck;
4591
+ }
4592
+ catch (cause) {
4593
+ if (!(cause instanceof AlgeoError))
4594
+ throw cause;
4595
+ const error = new AlgeoError(`演示模式调用 ${methodName} 被白名单限制。${cause.message}`, cause.code, cause.details);
4596
+ console.error(error);
4597
+ throw error;
4479
4598
  }
4480
- const error = new AlgeoError(`演示模式调用 ${methodName} 被白名单限制。${this.whitelistError.message}`, this.whitelistError.code, this.whitelistError.details);
4481
- console.error(error);
4482
- throw error;
4483
4599
  }
4484
4600
  async loadShareById(id) {
4485
- this.ensureWhitelistAccess('loadShareById');
4601
+ await this.ensureWhitelistAccess('loadShareById');
4486
4602
  const result = await this.post('loadShareById', {
4487
4603
  id,
4488
4604
  });
@@ -4492,7 +4608,7 @@ class EmbeddedPresentation extends EmbeddedTarget {
4492
4608
  return result;
4493
4609
  }
4494
4610
  async loadFile(content) {
4495
- this.ensureWhitelistAccess('loadFile');
4611
+ await this.ensureWhitelistAccess('loadFile');
4496
4612
  const latestContent = convertToLatest(content);
4497
4613
  const result = await this.post('loadContent', {
4498
4614
  content: latestContent,
@@ -4503,19 +4619,19 @@ class EmbeddedPresentation extends EmbeddedTarget {
4503
4619
  return result;
4504
4620
  }
4505
4621
  async switchSlide(index) {
4506
- this.ensureWhitelistAccess('switchSlide');
4622
+ await this.ensureWhitelistAccess('switchSlide');
4507
4623
  const result = await this.post('switchSlide', { index });
4508
4624
  this.currentSlideIndex = index;
4509
4625
  return result;
4510
4626
  }
4511
4627
  async getSlideCount() {
4512
- this.ensureWhitelistAccess('getSlideCount');
4628
+ await this.ensureWhitelistAccess('getSlideCount');
4513
4629
  const result = await this.post('getSlideCount', {});
4514
4630
  this.slideCount = result.count;
4515
4631
  return result;
4516
4632
  }
4517
4633
  async repl(command) {
4518
- this.ensureWhitelistAccess('repl');
4634
+ await this.ensureWhitelistAccess('repl');
4519
4635
  return this.post('repl', { command });
4520
4636
  }
4521
4637
  }
@@ -4703,7 +4819,9 @@ class EmbeddedEditor extends EmbeddedTarget {
4703
4819
  this.historyCurrentIndex = state.historyCurrentIndex;
4704
4820
  }
4705
4821
  async initialize(options = {}, baseUrl) {
4706
- if (!options.auth?.appId?.trim() && !isLocalFileHost()) {
4822
+ const appId = readEmbedAppId(options.auth);
4823
+ requireHttpHostForAppId(appId);
4824
+ if (!appId && !isLocalFileHost() && !isLocalLoopbackHost()) {
4707
4825
  throw new AlgeoError('编辑模式需要提供 auth.appId。', EMBED_ERROR_CODES.MISSING_APP_ID);
4708
4826
  }
4709
4827
  this.uiConfig = options.ui ? { ...options.ui } : {};
@@ -5213,35 +5331,6 @@ class EmbeddedEditor extends EmbeddedTarget {
5213
5331
  }
5214
5332
  }
5215
5333
 
5216
- const WHITELIST_CHECK_BASE_URL = 'https://open.dajiaoai.com/console';
5217
- const WHITELIST_CHECK_PATH = '/api/whitelist/check';
5218
- async function checkPresentationWhitelist(options) {
5219
- if (isLocalFileHost())
5220
- return null;
5221
- const appId = options.auth?.appId?.trim() ?? '';
5222
- const host = window.location.hostname.trim();
5223
- const url = new URL(`${normalizeBaseUrl(WHITELIST_CHECK_BASE_URL)}${WHITELIST_CHECK_PATH}`);
5224
- url.searchParams.set('appId', appId);
5225
- url.searchParams.set('host', host);
5226
- let matched = false;
5227
- try {
5228
- const response = await fetch(url.toString(), {
5229
- method: 'GET',
5230
- cache: 'no-store',
5231
- });
5232
- if (response.ok) {
5233
- const payload = (await response.json());
5234
- matched = payload.matched === true;
5235
- }
5236
- }
5237
- catch {
5238
- matched = false;
5239
- }
5240
- if (matched) {
5241
- return null;
5242
- }
5243
- return new AlgeoError(`白名单校验未通过,appId="${appId || '(empty)'}",host="${host || '(empty)'}"。`, EMBED_ERROR_CODES.BAD_REQUEST);
5244
- }
5245
5334
  async function createEditorInstance(container, options = {}, baseUrl) {
5246
5335
  const editor = new EmbeddedEditor(container);
5247
5336
  await editor.initialize(options, baseUrl);
@@ -5250,10 +5339,6 @@ async function createEditorInstance(container, options = {}, baseUrl) {
5250
5339
  async function createPresentationInstance(container, options = {}, baseUrl) {
5251
5340
  const presentation = new EmbeddedPresentation(container);
5252
5341
  await presentation.initialize(options, baseUrl);
5253
- const whitelistError = await checkPresentationWhitelist(options);
5254
- if (whitelistError) {
5255
- presentation.setWhitelistError(whitelistError);
5256
- }
5257
5342
  return presentation;
5258
5343
  }
5259
5344
  function createEmbeddedInstance(container, options) {