diff --git a/frontend/src/pages/login/index.tsx b/frontend/src/pages/login/index.tsx index 8fad2de..daf4b8b 100644 --- a/frontend/src/pages/login/index.tsx +++ b/frontend/src/pages/login/index.tsx @@ -76,23 +76,52 @@ export default function Login() { // 飞书内免登 const attemptFeishuAutoLogin = async (companyParam?: string) => { - // 如果 h5sdk 存在,使用 h5sdk.ready() 等待环境就绪 - if (window.h5sdk?.ready) { - window.h5sdk.ready(() => { - console.log('h5sdk ready, tt对象:', !!window.tt) - if (window.tt) { - performFeishuLogin(companyParam) - } else { - console.error('h5sdk ready 后 tt 对象仍不存在') - setLoading(false) + // 先获取 appId + let appId: string | undefined | null + const urlParams = new URLSearchParams(window.location.search) + appId = urlParams.get('app_id') + + if (!appId && companyParam) { + try { + const response = await fetch(`/api/v1/auth/feishu-config?company=${companyParam}`) + const result = await response.json() + if (result.code === 0 && result.data.app_id) { + appId = result.data.app_id } - }) + } catch (e) { + console.error('获取 appId 失败:', e) + } + } + + if (!appId) { + setLoading(false) + Toast.show('飞书应用配置错误') return } + // 如果 h5sdk 存在,先配置再等待 ready + if (window.h5sdk?.config) { + // 配置 h5sdk + window.h5sdk.config({ + appId: appId, + timestamp: Date.now(), + nonceStr: Math.random().toString(36).substring(2), + signature: '', // 网页应用模式下不需要签名 + }) + + // 等待环境就绪 + if (window.h5sdk.ready) { + window.h5sdk.ready(() => { + console.log('h5sdk ready, 调用免登') + performFeishuLogin(companyParam, appId) + }) + return + } + } + // 如果 tt 对象已存在,直接使用 if (window.tt) { - performFeishuLogin(companyParam) + performFeishuLogin(companyParam, appId) return } @@ -104,7 +133,7 @@ export default function Login() { attempts++ if (window.tt) { clearInterval(checkInterval) - performFeishuLogin(companyParam) + performFeishuLogin(companyParam, appId) } else if (attempts >= maxAttempts) { clearInterval(checkInterval) console.error('等待 tt 对象超时') @@ -114,37 +143,20 @@ export default function Login() { } // 执行飞书免登 - const performFeishuLogin = async (companyParam?: string) => { + const performFeishuLogin = async (companyParam?: string, appId?: string | null) => { if (!window.tt) { setLoading(false) Toast.show('飞书环境异常,请刷新重试') return } + if (!appId) { + setLoading(false) + Toast.show('飞书应用配置错误') + return + } + try { - // 获取 appId - let appId: string | undefined | null - - // 优先从 URL 参数获取 - const urlParams = new URLSearchParams(window.location.search) - appId = urlParams.get('app_id') - - // 如果没有,从后端获取 - if (!appId && companyParam) { - const response = await fetch(`/api/v1/auth/feishu-config?company=${companyParam}`) - const result = await response.json() - if (result.code === 0 && result.data.app_id) { - appId = result.data.app_id - } - } - - if (!appId) { - console.error('appId 获取失败') - setLoading(false) - Toast.show('飞书应用配置错误') - return - } - console.log('开始免登,appId:', appId) // 调用飞书免登