/** * 员工端二维码弹窗 — 展示员工端登录二维码,支持选择员工生成一次性自动登录链接 */ import { useState, useEffect } from 'react' import { QRCodeSVG } from 'qrcode.react' import { Smartphone, Copy, Check, Search, Loader2, User, ChevronRight } from 'lucide-react' import Modal from './ui/Modal' import { employeeApi, portalApi } from '../lib/api-services' interface Employee { id: string name: string phone: string department: string | null } export default function PortalQRModal({ open, onClose }: { open: boolean; onClose: () => void }) { const [copied, setCopied] = useState(false) const [employees, setEmployees] = useState([]) const [loading, setLoading] = useState(false) const [search, setSearch] = useState('') const [selectedEmployee, setSelectedEmployee] = useState(null) const [autoLoginUrl, setAutoLoginUrl] = useState('') const [generating, setGenerating] = useState(false) const portalUrl = `${window.location.origin}/portal/login` /** 加载员工列表 */ useEffect(() => { if (!open) return setLoading(true) employeeApi.list({ status: 'ACTIVE' }).then((list: any) => { setEmployees(list.map((e: any) => ({ id: e.id, name: e.name, phone: e.phone, department: e.department, }))) }).catch(() => {}).finally(() => setLoading(false)) }, [open]) /** 选择员工后生成一次性自动登录链接 */ const handleSelectEmployee = async (emp: Employee) => { setSelectedEmployee(emp) setGenerating(true) setAutoLoginUrl('') try { const res = await portalApi.generateAutoLoginToken(emp.id) as any const token = res?.token if (token) { setAutoLoginUrl(`${window.location.origin}/portal/auto-login?token=${token}`) } } catch { setAutoLoginUrl(portalUrl) } finally { setGenerating(false) } } const handleCopy = () => { navigator.clipboard.writeText(autoLoginUrl || portalUrl) setCopied(true) setTimeout(() => setCopied(false), 2000) } const handleReset = () => { setSelectedEmployee(null) setAutoLoginUrl('') setSearch('') } const filteredEmployees = search ? employees.filter(e => e.name.includes(search) || e.phone.includes(search)) : employees const displayUrl = autoLoginUrl || portalUrl return ( { handleReset(); onClose() }} title="员工端入口" size="sm">
{/* 未选择员工时:展示员工列表 */} {!selectedEmployee ? ( <>
选择员工后生成专属登录二维码
{/* 搜索框 */}
setSearch(e.target.value)} placeholder="搜索姓名或手机号..." className="w-full pl-9 pr-3 py-2 text-sm border rounded-lg focus:outline-none focus:ring-2 focus:ring-primary" />
{/* 员工列表 */}
{loading ? (
) : filteredEmployees.length === 0 ? (
未找到员工
) : ( filteredEmployees.map(emp => ( )) )}
) : ( /* 已选择员工:展示二维码 */ <> {/* 员工信息 + 重选按钮 */}
{selectedEmployee.name}
{selectedEmployee.phone}
{/* 二维码 */}
{generating ? (
) : ( )}
{/* 提示信息 */} {autoLoginUrl ? (

扫码后自动登录,链接 10 分钟内有效

) : (

员工用手机扫码即可进入员工端

)} {/* 链接地址 */}
{displayUrl}
{/* 功能说明 */}

查看工资条并确认

查看劳动合同信息

阅读并签收规章制度

)}
) }