第三方集成iframe 嵌入

iframe 嵌入

把电子签页面嵌进集成方自己的网页:先联系运营给宿主域名加白,然后用 OpenAPI 返回的链接做 iframe。

接入前提:宿主域名加白

把 OAuth 应用的 client_id 和宿主域名(如 https://app.your-company.com,支持 https://*.your-company.com)发给电子签运营 / 对接人加白,随版本发布生效。未加白的域名,iframe 会白屏。

可以嵌入的页面

场景获取方式
发起签署 / 签署状态列表获取内嵌页 URL(request_signatures / signature_status)
签署获取签署链接(embedded signing)

链接直接用 OpenAPI 返回的原值:不要增删参数、不要换域名,client_id 丢了就匹配不到你的加白名单。

宿主页面写法

<iframe
  src="OpenAPI 返回的链接"
  style="width:100%;height:900px;border:0"
  sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-downloads allow-modals"
></iframe>

allow-same-origin 必须保留(少了 iframe 调接口会带 Origin: null 被拒);iframe 内不会跳转宿主页面,不需要 allow-top-navigation。

接收签署结果

iframe 内签署完成 / 拒签会给宿主发 postMessage,宿主校验来源后再处理:

window.addEventListener('message', (e) => {
  if (e.origin !== 'https://www.tencent-esign.com') return; // 按实际环境域名校验
  if (!e.data || e.data.source !== 'tencent-esign') return;
  // e.data.type: embed.ready | signing.complete | signing.decline
  // 终态消息另带 event / envelopeId / recipientId / returnUrl
});

消息体的精确字段如下:

字段类型说明
sourcestring固定为 tencent-esign
typestringembed.ready、signing.complete 或 signing.decline
clientIdstring嵌入链接中携带的 OAuth 应用 client_id
envelopeIdstring终态消息中的信封 ID
recipientIdstring终态消息中的签署人 ID
eventstring终态结果:signing_complete 或 decline
returnUrlstring集成方传入且已通过校验的回跳地址,存在时返回
typeevent
embed.ready不设置
signing.completesigning_complete
signing.declinedecline

消息只用于界面联动,信封最终状态以 Webhook 或查询信封详情为准。

常见问题

现象处理
iframe 白屏宿主域名没加白,或链接里的 client_id 被改写/去掉
收不到 postMessage宿主没有按精确 origin 校验、iframe 没有高度、sandbox 少了 allow-same-origin
下载 / 弹窗无反应sandbox 缺少 allow-downloads / allow-popups
打开链接报 TOKEN_INVALID链接已过期,或同一签署人重新签发导致旧链接失效,重新调用接口签发
签署人被要求登录普通嵌入签署的正常行为(登录卡片在 iframe 内);如需免登录可联系运营了解 Trusted 签署