import { useEffect } from 'react' import { useNavigate } from 'react-router-dom' import { Toast } from 'antd-mobile' import { useUserStore } from '@/stores/user' import styles from './login.module.css' export default function Login() { const navigate = useNavigate() const { login } = useUserStore() useEffect(() => { // 检查 URL 中是否有 auth_code 参数(飞书授权回调) const urlParams = new URLSearchParams(window.location.search) const authCode = urlParams.get('auth_code') if (authCode) { // 有授权码,执行登录 handleFeishuLogin(authCode) } else { // 没有授权码,跳转到飞书授权页面 redirectToFeishu() } }, []) const redirectToFeishu = () => { // 飞书应用配置 const appId = 'cli_a8033d607cba500d' // 当前页面 URL 作为回调地址 const redirectUri = encodeURIComponent(window.location.origin + '/login') // 租户密钥(新友合公司) const tenantKey = '14e7bc0fe5985740' // 构建飞书授权 URL const authUrl = `https://open.feishu.cn/open-apis/authen/v1/oidc/authorize?app_id=${appId}&redirect_uri=${redirectUri}&scope=&state=${tenantKey}` // 跳转到飞书授权页面 window.location.href = authUrl } const handleFeishuLogin = async (authCode: string) => { try { const tenantKey = '14e7bc0fe5985740' // 调用后端登录接口 const response = await fetch('/api/v1/auth/feishu-login', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ auth_code: authCode, tenant_key: tenantKey, }), }) const result = await response.json() if (result.code === 0) { // 登录成功,保存到 store login(result.data.user, result.data.token) Toast.show('登录成功') // 跳转到首页 navigate('/dashboard') } else { Toast.show(result.message || '登录失败') // 登录失败,重新跳转到飞书授权 setTimeout(() => { redirectToFeishu() }, 2000) } } catch (error: any) { console.error('登录失败:', error) Toast.show('登录失败,请重试') setTimeout(() => { redirectToFeishu() }, 2000) } } return (

企业工具台

正在跳转到飞书授权...

) }