Files
x-server-mobile/frontend/src/pages/login/index.tsx
T

212 lines
6.3 KiB
TypeScript
Raw Normal View History

2026-02-26 09:58:19 +08:00
import { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { Toast } from 'antd-mobile'
2026-02-25 23:54:38 +08:00
import { useUserStore } from '@/stores/user'
2026-02-26 09:58:19 +08:00
import QRCodeLogin from './QRCodeLogin'
import styles from './login.module.css'
2026-02-26 09:58:19 +08:00
// 声明 ttjsBridge 类型
declare global {
interface Window {
ttjsBridge?: {
getAuthCode: (params: any) => void
}
}
}
export default function Login() {
const navigate = useNavigate()
2026-02-25 23:54:38 +08:00
const { login } = useUserStore()
2026-02-26 09:58:19 +08:00
const [loading, setLoading] = useState(true)
const [environment, setEnvironment] = useState<'feishu' | 'external' | null>(null)
const [showQRCode, setShowQRCode] = useState(false)
useEffect(() => {
// 监听飞书登录成功事件
const handleFeishuLoginSuccess = ((e: any) => {
console.log('🎉 收到飞书登录成功事件:', e.detail)
const { user, token } = e.detail
login(user, token)
}) as EventListener
window.addEventListener('feishu-login-success', handleFeishuLoginSuccess)
return () => {
window.removeEventListener('feishu-login-success', handleFeishuLoginSuccess)
}
}, [])
useEffect(() => {
2026-02-26 09:58:19 +08:00
// 1. 检测是否在飞书客户端内
const ua = navigator.userAgent.toLowerCase()
const isInFeishu = ua.includes('feishu') || ua.includes('lark') || (window as any).ttjsBridge !== undefined
2026-02-26 09:58:19 +08:00
console.log('🔍 Environment check:', { ua, isInFeishu, hasBridge: !!(window as any).ttjsBridge })
if (isInFeishu) {
// 飞书内:静默授权
setEnvironment('feishu')
handleFeishuAutoLogin()
} else {
2026-02-26 09:58:19 +08:00
// 飞书外:检查 URL 中是否有 auth_code 参数(飞书授权回调)
const urlParams = new URLSearchParams(window.location.search)
const authCode = urlParams.get('auth_code')
const qrcodeId = urlParams.get('qrcode_id')
if (qrcodeId) {
// 扫码登录回调
setEnvironment('external')
handleQrcodeCallback(qrcodeId)
} else if (authCode) {
// 直接访问授权页面(不太可能)
setEnvironment('external')
handleFeishuCallback(authCode)
} else {
setEnvironment('external')
setLoading(false)
}
}
}, [])
2026-02-26 09:58:19 +08:00
// 飞书内静默授权
const handleFeishuAutoLogin = () => {
console.log('🚀 Feishu auto login started')
if ((window as any).ttjsBridge) {
(window as any).ttjsBridge.getAuthCode({
success: (authCode: string) => {
console.log('✅ Got auth code from ttjsBridge:', authCode)
handleFeishuLogin(authCode)
},
fail: (error: any) => {
console.error('❌ Get auth code failed:', error)
setLoading(false)
Toast.show('获取授权失败,请重试')
},
})
} else {
console.warn('⚠️ ttjsBridge not available')
setLoading(false)
}
}
// 跳转到飞书授权页面(飞书外扫码登录)
const redirectToFeishu = () => {
2026-02-26 09:58:19 +08:00
// 直接打开扫码登录弹窗
setShowQRCode(true)
}
2026-02-26 09:58:19 +08:00
// 处理飞书回调(飞书外扫码后回调)
const handleFeishuCallback = async (authCode: string) => {
try {
await handleFeishuLogin(authCode)
} catch (error) {
console.error('登录失败:', error)
setLoading(false)
}
}
2026-02-26 09:58:19 +08:00
// 处理扫码登录回调
const handleQrcodeCallback = async (qrcodeId: string) => {
try {
// 飞书扫码后会带着 qrcode_id 参数跳转回来
// 这个时候前端需要显示 loading 状态
// 后端会处理 qrcodeLoginCallback,更新 session 状态
// 前端只需要轮询状态即可
setShowQRCode(true)
setLoading(false)
2026-02-26 09:58:19 +08:00
// 设置轮询 session_id
// 实际上这个逻辑在 QRCodeLogin 组件中处理
} catch (error) {
console.error('处理扫码回调失败:', error)
setLoading(false)
}
}
2026-02-26 09:58:19 +08:00
// 处理飞书登录
const handleFeishuLogin = async (authCode: string) => {
try {
const tenantKey = '14e7bc0fe5985740'
const response = await fetch('/api/v1/auth/feishu-login', {
method: 'POST',
2026-02-26 09:58:19 +08:00
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
auth_code: authCode,
tenant_key: tenantKey,
}),
})
const result = await response.json()
if (result.code === 0) {
2026-02-25 23:54:38 +08:00
login(result.data.user, result.data.token)
Toast.show('登录成功')
navigate('/dashboard')
} else {
Toast.show(result.message || '登录失败')
2026-02-26 09:58:19 +08:00
setLoading(false)
}
} catch (error: any) {
console.error('登录失败:', error)
Toast.show('登录失败,请重试')
2026-02-26 09:58:19 +08:00
setLoading(false)
}
}
2026-02-26 09:58:19 +08:00
// 邮箱密码登录
const handlePasswordLogin = () => {
navigate('/login-simple')
}
if (loading) {
return (
<div className={styles.login}>
<div className={styles.container}>
<h1>企业工具台</h1>
<p>正在登录...</p>
</div>
</div>
)
}
return (
<div className={styles.login}>
<div className={styles.container}>
<h1>企业工具台</h1>
2026-02-26 09:58:19 +08:00
<p className={styles.subtitle}>请选择登录方式</p>
<div className={styles.loginMethods}>
<button className={styles.methodBtn} onClick={handlePasswordLogin}>
<div className={styles.methodIcon}>📧</div>
<div className={styles.methodText}>
<div className={styles.methodTitle}>邮箱密码登录</div>
<div className={styles.methodDesc}>使用企业邮箱登录</div>
</div>
</button>
<button className={styles.methodBtn} onClick={redirectToFeishu}>
<div className={styles.methodIcon}>📱</div>
<div className={styles.methodText}>
<div className={styles.methodTitle}>飞书扫码登录</div>
<div className={styles.methodDesc}>使用飞书 APP 扫码登录</div>
</div>
</button>
</div>
{environment === 'external' && (
<p className={styles.tip}>
💡 提示:在飞书客户端内打开可自动登录
</p>
)}
<QRCodeLogin
visible={showQRCode}
onClose={() => setShowQRCode(false)}
/>
</div>
</div>
)
}