fix: 添加 QR SDK 加载失败的降级方案
1. 优先尝试加载飞书官方 QR SDK 2. 如果 SDK 加载超时(5秒)或失败,自动降级到普通二维码 3. 添加详细的加载日志,方便调试 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { useState, useEffect, useRef } from 'react'
|
import { useState, useEffect, useRef } from 'react'
|
||||||
import { Dialog, Toast } from 'antd-mobile'
|
import { Dialog, Toast } from 'antd-mobile'
|
||||||
|
import { QRCodeSVG } from 'qrcode.react'
|
||||||
import { useNavigate } from 'react-router-dom'
|
import { useNavigate } from 'react-router-dom'
|
||||||
import styles from './QRCodeLogin.module.css'
|
import styles from './QRCodeLogin.module.css'
|
||||||
|
|
||||||
@@ -11,7 +12,6 @@ declare global {
|
|||||||
goto: string
|
goto: string
|
||||||
width?: string
|
width?: string
|
||||||
height?: string
|
height?: string
|
||||||
style?: string
|
|
||||||
}) => {
|
}) => {
|
||||||
matchOrigin: (origin: string) => boolean
|
matchOrigin: (origin: string) => boolean
|
||||||
matchData: (data: any) => boolean
|
matchData: (data: any) => boolean
|
||||||
@@ -25,26 +25,47 @@ interface QRCodeLoginProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 动态加载飞书 QR SDK
|
// 动态加载飞书 QR SDK
|
||||||
const loadFeishuQRSDK = (): Promise<void> => {
|
const loadFeishuQRSDK = (): Promise<boolean> => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve) => {
|
||||||
|
// 检查是否已经加载
|
||||||
if (window.QR) {
|
if (window.QR) {
|
||||||
resolve()
|
console.log('✅ Feishu QR SDK already loaded')
|
||||||
|
resolve(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否已经加载过
|
console.log('📦 Loading Feishu QR SDK...')
|
||||||
const existingScript = document.getElementById('feishu-qr-sdk')
|
|
||||||
if (existingScript) {
|
|
||||||
existingScript.addEventListener('load', () => resolve())
|
|
||||||
existingScript.addEventListener('error', () => reject(new Error('Failed to load Feishu QR SDK')))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const script = document.createElement('script')
|
const script = document.createElement('script')
|
||||||
script.id = 'feishu-qr-sdk'
|
script.id = 'feishu-qr-sdk'
|
||||||
script.src = 'https://lf-package-cn.feishucdn.com/obj/feishu-static/lark/passport/qrcode/LarkSSOSDKWebQRCode-1.0.3.js'
|
script.src = 'https://lf-package-cn.feishucdn.com/obj/feishu-static/lark/passport/qrcode/LarkSSOSDKWebQRCode-1.0.3.js'
|
||||||
script.onload = () => resolve()
|
script.async = true
|
||||||
script.onerror = () => reject(new Error('Failed to load Feishu QR SDK'))
|
script.timeout = 5000 // 5秒超时
|
||||||
|
|
||||||
|
const timeoutId = setTimeout(() => {
|
||||||
|
console.warn('⚠️ Feishu QR SDK load timeout, using fallback')
|
||||||
|
resolve(false)
|
||||||
|
}, 5000)
|
||||||
|
|
||||||
|
script.onload = () => {
|
||||||
|
clearTimeout(timeoutId)
|
||||||
|
setTimeout(() => {
|
||||||
|
if (window.QR) {
|
||||||
|
console.log('✅ Feishu QR SDK loaded successfully')
|
||||||
|
resolve(true)
|
||||||
|
} else {
|
||||||
|
console.warn('⚠️ QR SDK loaded but window.QR not found, using fallback')
|
||||||
|
resolve(false)
|
||||||
|
}
|
||||||
|
}, 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
script.onerror = () => {
|
||||||
|
clearTimeout(timeoutId)
|
||||||
|
console.warn('⚠️ Failed to load Feishu QR SDK, using fallback')
|
||||||
|
resolve(false) // 不 reject,而是返回 false 使用降级方案
|
||||||
|
}
|
||||||
|
|
||||||
document.head.appendChild(script)
|
document.head.appendChild(script)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -52,6 +73,8 @@ const loadFeishuQRSDK = (): Promise<void> => {
|
|||||||
export default function QRCodeLogin({ visible, onClose }: QRCodeLoginProps) {
|
export default function QRCodeLogin({ visible, onClose }: QRCodeLoginProps) {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const [status, setStatus] = useState<'loading' | 'pending' | 'scanning' | 'success'>('loading')
|
const [status, setStatus] = useState<'loading' | 'pending' | 'scanning' | 'success'>('loading')
|
||||||
|
const [useFallback, setUseFallback] = useState(false)
|
||||||
|
const [qrcodeUrl, setQrcodeUrl] = useState('')
|
||||||
const qrContainerRef = useRef<HTMLDivElement>(null)
|
const qrContainerRef = useRef<HTMLDivElement>(null)
|
||||||
const qrObjRef = useRef<any>(null)
|
const qrObjRef = useRef<any>(null)
|
||||||
const pollTimerRef = useRef<NodeJS.Timeout | null>(null)
|
const pollTimerRef = useRef<NodeJS.Timeout | null>(null)
|
||||||
@@ -70,18 +93,20 @@ export default function QRCodeLogin({ visible, onClose }: QRCodeLoginProps) {
|
|||||||
if (pollTimerRef.current) {
|
if (pollTimerRef.current) {
|
||||||
clearTimeout(pollTimerRef.current)
|
clearTimeout(pollTimerRef.current)
|
||||||
}
|
}
|
||||||
|
// 重置状态
|
||||||
|
setUseFallback(false)
|
||||||
|
setQrcodeUrl('')
|
||||||
}
|
}
|
||||||
}, [visible])
|
}, [visible])
|
||||||
|
|
||||||
const initQRCode = async () => {
|
const initQRCode = async () => {
|
||||||
setStatus('loading')
|
setStatus('loading')
|
||||||
try {
|
setUseFallback(false)
|
||||||
// 加载飞书 QR SDK
|
setQrcodeUrl('')
|
||||||
await loadFeishuQRSDK()
|
|
||||||
|
|
||||||
if (!window.QR) {
|
try {
|
||||||
throw new Error('QR SDK not loaded')
|
// 尝试加载飞书 QR SDK
|
||||||
}
|
const sdkLoaded = await loadFeishuQRSDK()
|
||||||
|
|
||||||
// 生成登录会话
|
// 生成登录会话
|
||||||
const response = await fetch('/api/v1/auth/qrcode-login', {
|
const response = await fetch('/api/v1/auth/qrcode-login', {
|
||||||
@@ -98,12 +123,12 @@ export default function QRCodeLogin({ visible, onClose }: QRCodeLoginProps) {
|
|||||||
const authUrl = result.data.auth_url
|
const authUrl = result.data.auth_url
|
||||||
sessionIdRef.current = result.data.session_id
|
sessionIdRef.current = result.data.session_id
|
||||||
|
|
||||||
// 清空容器
|
if (sdkLoaded && window.QR) {
|
||||||
|
// 使用飞书官方 QR SDK
|
||||||
if (qrContainerRef.current) {
|
if (qrContainerRef.current) {
|
||||||
qrContainerRef.current.innerHTML = ''
|
qrContainerRef.current.innerHTML = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
// 使用飞书官方 QR SDK 创建二维码
|
|
||||||
qrObjRef.current = window.QR({
|
qrObjRef.current = window.QR({
|
||||||
id: 'feishu-qrcode-container',
|
id: 'feishu-qrcode-container',
|
||||||
goto: authUrl,
|
goto: authUrl,
|
||||||
@@ -112,8 +137,15 @@ export default function QRCodeLogin({ visible, onClose }: QRCodeLoginProps) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
setStatus('pending')
|
setStatus('pending')
|
||||||
// 开始轮询登录状态
|
|
||||||
startPolling(result.data.session_id)
|
startPolling(result.data.session_id)
|
||||||
|
} else {
|
||||||
|
// SDK 加载失败,使用普通二维码
|
||||||
|
console.log('📱 Using fallback QR code')
|
||||||
|
setQrcodeUrl(authUrl)
|
||||||
|
setUseFallback(true)
|
||||||
|
setStatus('pending')
|
||||||
|
startPolling(result.data.session_id)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Toast.show(result.message || '生成二维码失败')
|
Toast.show(result.message || '生成二维码失败')
|
||||||
}
|
}
|
||||||
@@ -194,11 +226,43 @@ export default function QRCodeLogin({ visible, onClose }: QRCodeLoginProps) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleRefresh = () => {
|
||||||
|
if (pollTimerRef.current) {
|
||||||
|
clearTimeout(pollTimerRef.current)
|
||||||
|
}
|
||||||
|
initQRCode()
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog
|
<Dialog
|
||||||
visible={visible}
|
visible={visible}
|
||||||
content={
|
content={
|
||||||
<div className={styles.qrcodeContainer}>
|
<div className={styles.qrcodeContainer}>
|
||||||
|
{status === 'loading' && <div className={styles.loading}>正在生成二维码...</div>}
|
||||||
|
|
||||||
|
{useFallback && qrcodeUrl && (
|
||||||
|
<>
|
||||||
|
<div className={styles.qrcode}>
|
||||||
|
<QRCodeSVG
|
||||||
|
value={qrcodeUrl}
|
||||||
|
size={200}
|
||||||
|
level="M"
|
||||||
|
includeMargin={false}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className={styles.status} style={{ color: getStatusColor() }}>
|
||||||
|
{getStatusText()}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p className={styles.tip} style={{ fontSize: '12px', color: '#999' }}>
|
||||||
|
💡 使用飞书扫码登录
|
||||||
|
</p>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!useFallback && status !== 'loading' && (
|
||||||
|
<>
|
||||||
<div id="feishu-qrcode-container" ref={qrContainerRef} className={styles.qrcode}></div>
|
<div id="feishu-qrcode-container" ref={qrContainerRef} className={styles.qrcode}></div>
|
||||||
|
|
||||||
<div className={styles.status} style={{ color: getStatusColor() }}>
|
<div className={styles.status} style={{ color: getStatusColor() }}>
|
||||||
@@ -208,6 +272,8 @@ export default function QRCodeLogin({ visible, onClose }: QRCodeLoginProps) {
|
|||||||
<p className={styles.tip}>
|
<p className={styles.tip}>
|
||||||
💡 打开飞书 APP,扫一扫登录
|
💡 打开飞书 APP,扫一扫登录
|
||||||
</p>
|
</p>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
onClose={onClose}
|
onClose={onClose}
|
||||||
|
|||||||
Reference in New Issue
Block a user