Files
ckb-SuperAdmin/Cunkebao/app/scenarios/phone/new/components/PhoneImportDialog.tsx
2025-06-03 16:37:39 +08:00

139 lines
4.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client"
import type React from "react"
import { useState } from "react"
import { Upload } from "lucide-react"
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { toast } from "@/components/ui/use-toast"
interface PhoneImportDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
onImport: (numbers: string[]) => void
}
export function PhoneImportDialog({ open, onOpenChange, onImport }: PhoneImportDialogProps) {
const [manualInput, setManualInput] = useState("")
const [importing, setImporting] = useState(false)
const handleManualImport = () => {
const numbers = manualInput
.split(/[\n,]/)
.map((n) => n.trim())
.filter((n) => n && /^1[3-9]\d{9}$/.test(n))
if (numbers.length === 0) {
toast({
title: "没有有效的电话号码",
description: "请输入正确格式的手机号码",
variant: "destructive",
})
return
}
onImport(numbers)
setManualInput("")
toast({
title: "导入成功",
description: `成功导入 ${numbers.length} 个电话号码`,
})
}
const handleFileUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0]
if (!file) return
setImporting(true)
try {
// 这里应该解析Excel/CSV文件
// 模拟导入过程
await new Promise((resolve) => setTimeout(resolve, 1000))
// 模拟导入的号码
const mockNumbers = ["13800138000", "13900139000", "13700137000"]
onImport(mockNumbers)
toast({
title: "导入成功",
description: `成功导入 ${mockNumbers.length} 个电话号码`,
})
} catch (error) {
toast({
title: "导入失败",
description: "文件解析失败,请检查文件格式",
variant: "destructive",
})
} finally {
setImporting(false)
}
}
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle></DialogTitle>
<DialogDescription>Excel/CSV文件或手动输入</DialogDescription>
</DialogHeader>
<Tabs defaultValue="manual" className="w-full">
<TabsList className="grid w-full grid-cols-2">
<TabsTrigger value="manual"></TabsTrigger>
<TabsTrigger value="file"></TabsTrigger>
</TabsList>
<TabsContent value="manual" className="space-y-4">
<div>
<label className="text-sm font-medium"></label>
<textarea
className="w-full mt-1 px-3 py-2 border rounded-md"
rows={6}
placeholder="每行一个号码,或用逗号分隔"
value={manualInput}
onChange={(e) => setManualInput(e.target.value)}
/>
<p className="text-xs text-gray-500 mt-1">13800138000</p>
</div>
<Button className="w-full" onClick={handleManualImport}>
</Button>
</TabsContent>
<TabsContent value="file" className="space-y-4">
<div className="border-2 border-dashed rounded-lg p-8 text-center">
<Upload className="h-12 w-12 mx-auto text-gray-400 mb-4" />
<p className="text-sm text-gray-600 mb-2"></p>
<p className="text-xs text-gray-500 mb-4"> Excel (.xlsx, .xls) CSV </p>
<input
type="file"
accept=".xlsx,.xls,.csv"
onChange={handleFileUpload}
className="hidden"
id="file-upload"
disabled={importing}
/>
<label htmlFor="file-upload">
<Button as="span" disabled={importing}>
{importing ? "导入中..." : "选择文件"}
</Button>
</label>
</div>
<div className="bg-gray-50 p-3 rounded-md">
<p className="text-xs text-gray-600 mb-2"></p>
<div className="bg-white p-2 rounded border text-xs font-mono">
<div>,,,</div>
<div>,13800138000,,</div>
<div>,13900139000,广,</div>
</div>
</div>
</TabsContent>
</Tabs>
</DialogContent>
</Dialog>
)
}