import { useEffect, useState } from "react"; import { Settings, Save, Loader2, Wifi, WifiOff } from "lucide-react"; import { getPrinterConfig, updatePrinterConfig, type PrinterConfig, } from "../../services/printApi"; import { useToast } from "../../components/ui/Toast"; export default function AdminPrintConfigPage() { const { toast } = useToast(); const [config, setConfig] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); // 表单字段 const [ip, setIp] = useState(""); const [port, setPort] = useState(9100); const [enabled, setEnabled] = useState(true); useEffect(() => { loadConfig(); }, []); async function loadConfig() { setLoading(true); try { const cfg = await getPrinterConfig(); setConfig(cfg); const lp = cfg.label_printer; setIp(lp.ip); setPort(lp.port); setEnabled(lp.enabled ?? true); } catch (err: any) { toast(err?.response?.data?.detail ?? "加载配置失败", "error"); } finally { setLoading(false); } } async function handleSave(e: React.FormEvent) { e.preventDefault(); if (!ip.trim()) { toast("请输入打印机 IP 地址", "error"); return; } setSaving(true); try { const result = await updatePrinterConfig(ip.trim(), port, enabled); toast(result.message, "success"); } catch (err: any) { toast(err?.response?.data?.detail ?? "保存失败", "error"); } finally { setSaving(false); } } if (loading) { return (
); } return (

打印机设置

配置标签打印机(热敏打标机)的 IP 地址和端口,协议: TSPL

{/* IP 地址 */}
setIp(e.target.value)} placeholder="如 192.168.9.221" className="w-full rounded-lg border border-gray-200 px-3 py-2.5 text-sm font-mono focus:border-blue-400 focus:outline-none focus:ring-2 focus:ring-blue-100" />
{/* 端口 */}
setPort(Number(e.target.value))} min={1} max={65535} className="w-full rounded-lg border border-gray-200 px-3 py-2.5 text-sm font-mono focus:border-blue-400 focus:outline-none focus:ring-2 focus:ring-blue-100" />

热敏打印机通常使用 9100 端口(Raw Socket)

{/* 启用状态 */} {/* 保存 */}
{/* 当前状态 */} {config?.label_printer && (

当前配置: {config.label_printer.ip}:{config.label_printer.port} {" · "} {config.label_printer.enabled ? ( 已启用 ) : ( 已禁用 )}

)}
); }