From 551c68c456b1e9092b1632d253b6301edb4dd1b0 Mon Sep 17 00:00:00 2001 From: kx_guo Date: Sat, 5 Sep 2026 17:47:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=92=89=E9=92=89=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E5=8F=B0=E5=85=8D=E7=99=BB=E7=8E=AF=E5=A2=83=E8=AF=86?= =?UTF-8?q?=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/index.html | 2 +- frontend/src/main.jsx | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/frontend/index.html b/frontend/index.html index a40b320..32c33c2 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -7,7 +7,7 @@
- + diff --git a/frontend/src/main.jsx b/frontend/src/main.jsx index 3bddd7e..9423235 100644 --- a/frontend/src/main.jsx +++ b/frontend/src/main.jsx @@ -88,11 +88,35 @@ function LoginPage({ onLogin }) { const [autoDingtalkLogin, setAutoDingtalkLogin] = useState(false); const autoLoginStarted = useRef(false); const query = new URLSearchParams(window.location.search); - const inDingtalkWorkbench = /DingTalk|AliApp\(DT/i.test(navigator.userAgent || "") || query.get("from") === "dingtalk"; - function requestDingTalkAuthCode(corpId) { + const inDingtalkWorkbench = /DingTalk|AliApp\(DT/i.test(navigator.userAgent || "") || query.get("from") === "dingtalk" || Boolean(window.dd || window.DingTalkPC); + function loadDingTalkSdk() { + if (window.dd) return Promise.resolve(window.dd); return new Promise((resolve, reject) => { - const ding = window.dd; - if (!ding) return reject(new Error("未检测到钉钉工作台环境,请从钉钉工作台打开")); + const existing = document.querySelector('script[data-dingtalk-sdk="true"]'); + const finish = () => { + if (window.dd) resolve(window.dd); + else reject(new Error("钉钉JSAPI未加载,请确认工作台应用类型为H5微应用,并将 ask.dieya.net.cn 加入应用安全域名")); + }; + if (existing) { + existing.addEventListener("load", finish, { once: true }); + existing.addEventListener("error", finish, { once: true }); + window.setTimeout(finish, 5000); + return; + } + const script = document.createElement("script"); + script.src = "https://g.alicdn.com/dingding/dingtalk-jsapi/2.15.15/dingtalk.open.js"; + script.async = true; + script.dataset.dingtalkSdk = "true"; + script.onload = finish; + script.onerror = finish; + document.head.appendChild(script); + window.setTimeout(finish, 8000); + }); + } + async function requestDingTalkAuthCode(corpId) { + const ding = (await loadDingTalkSdk()) || window.dd || window.DingTalkPC; + return new Promise((resolve, reject) => { + if (!ding) return reject(new Error("未检测到钉钉JSAPI,请将当前地址配置为钉钉H5微应用首页,并加入应用安全域名")); let settled = false; const finish = (callback) => (value) => { if (settled) return; @@ -106,7 +130,9 @@ function LoginPage({ onLogin }) { const failure = finish((error) => reject(new Error(error?.errorMessage || error?.message || "钉钉免登授权失败"))); const invoke = () => { try { - ding.runtime.permission.requestAuthCode({ corpId, onSuccess: success, onFail: failure }); + const requestAuthCode = ding.runtime?.permission?.requestAuthCode || ding.permission?.requestAuthCode; + if (!requestAuthCode) throw new Error("当前钉钉版本不支持工作台免登JSAPI"); + requestAuthCode.call(ding.runtime?.permission || ding.permission, { corpId, onSuccess: success, onFail: failure }); } catch (error) { failure(error); }