修复 xuanz

This commit is contained in:
menxipeng
2025-10-19 13:26:50 +08:00
parent ac28965614
commit e6255148a4

View File

@@ -16,7 +16,7 @@ import {
DialogTrigger, DialogTrigger,
} from "../ui/dialog" } from "../ui/dialog"
import { Plus, Search, Filter, Download, Calendar, AlertTriangle, Store, Eye, Building2, MapPin } from "lucide-react" import { Plus, Search, Filter, Download, Calendar, AlertTriangle, Store, Eye, Building2, MapPin } from "lucide-react"
import { apiPost, apiGet } from "../../lib/services/api" import { apiGet, apiPost, apiPut } from "../../lib/services/api"
// 总商户数据类型 // 总商户数据类型
interface TotalMerchant { interface TotalMerchant {
@@ -61,6 +61,7 @@ export default function MerchantsPage() {
const [searchTerm, setSearchTerm] = useState("") const [searchTerm, setSearchTerm] = useState("")
const [statusFilter, setStatusFilter] = useState("all") const [statusFilter, setStatusFilter] = useState("all")
const [isAddMerchantOpen, setIsAddMerchantOpen] = useState(false) const [isAddMerchantOpen, setIsAddMerchantOpen] = useState(false)
const [isEditMerchantOpen, setIsEditMerchantOpen] = useState(false)
const [isAddEquipmentOpen, setIsAddEquipmentOpen] = useState(false) const [isAddEquipmentOpen, setIsAddEquipmentOpen] = useState(false)
const [selectedMerchant, setSelectedMerchant] = useState<any>(null) const [selectedMerchant, setSelectedMerchant] = useState<any>(null)
const [isEquipmentDialogOpen, setIsEquipmentDialogOpen] = useState(false) const [isEquipmentDialogOpen, setIsEquipmentDialogOpen] = useState(false)
@@ -90,6 +91,20 @@ export default function MerchantsPage() {
detailedAddress: "", detailedAddress: "",
}) })
const [editMerchant, setEditMerchant] = useState({
id: "",
name: "",
contact: "",
phone: "",
mall: "",
mallUserId: "",
totalMerchant: "",
province: "",
businessLicense: "",
businessType: "",
detailedAddress: "",
})
const [isSubmitting, setIsSubmitting] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false)
// 获取用户角色 // 获取用户角色
@@ -362,6 +377,102 @@ export default function MerchantsPage() {
} }
} }
// 打开编辑对话框
const handleEditMerchant = (merchant: any) => {
setEditMerchant({
id: merchant.id,
name: merchant.merchantName || "",
contact: merchant.contactPerson || "",
phone: merchant.contactPhone || "",
mall: merchant.mallId || "",
mallUserId: merchant.mallUserId || "",
totalMerchant: merchant.totalMerchantId || "",
province: merchant.province || "",
businessLicense: merchant.businessLicenseNo || "",
businessType: merchant.businessType || "",
detailedAddress: merchant.detailedAddress || "",
})
setIsEditMerchantOpen(true)
// 加载省份和商场数据
if (provinces.length === 0) {
fetchProvinces()
}
if (merchant.province) {
fetchMalls(merchant.province)
}
}
// 处理编辑省份变化
const handleEditProvinceChange = (province: string) => {
setEditMerchant({
...editMerchant,
province: province,
mall: "",
mallUserId: ""
})
if (province) {
fetchMalls(province)
} else {
setMalls([])
}
}
// 提交编辑
const handleUpdateMerchant = async () => {
if (!editMerchant.name || !editMerchant.contact || !editMerchant.phone) {
alert('请填写必填信息')
return
}
setIsSubmitting(true)
try {
const selectedProvince = provinces.find(p => p.code === editMerchant.province)
const provinceName = selectedProvince ? selectedProvince.name : ""
const fullAddress = `${provinceName}${editMerchant.detailedAddress}`
const merchantData = {
id: editMerchant.id,
merchantName: editMerchant.name,
businessType: editMerchant.businessType,
contactPerson: editMerchant.contact,
contactPhone: editMerchant.phone,
mallId: editMerchant.mall || "",
mallUserId: editMerchant.mallUserId || "",
mallLocation: "",
dealerId: "",
totalMerchantId: editMerchant.totalMerchant || "",
isTotal: editMerchant.totalMerchant ? 1 : 2,
businessLicenseNo: editMerchant.businessLicense,
province: editMerchant.province,
city: "",
district: "",
detailedAddress: editMerchant.detailedAddress,
fullAddress: fullAddress,
status: 1
}
console.log("编辑商户:", merchantData)
const result = await apiPut('/back/merchants/', merchantData)
if (result.code === 200) {
console.log('编辑商户成功:', result)
alert('编辑商户成功!')
setIsEditMerchantOpen(false)
fetchMerchants()
} else {
console.error('编辑商户失败:', result)
alert('编辑商户失败:' + (result.msg || '未知错误'))
}
} catch (error) {
console.error('编辑商户请求失败:', error)
alert('网络错误,请稍后重试')
} finally {
setIsSubmitting(false)
}
}
// 打开设备对话框 // 打开设备对话框
const handleViewEquipment = (merchant: any) => { const handleViewEquipment = (merchant: any) => {
setSelectedMerchant(merchant) setSelectedMerchant(merchant)
@@ -582,7 +693,7 @@ export default function MerchantsPage() {
({merchant.equipmentCount}) ({merchant.equipmentCount})
</Button> </Button>
{userRole !== "merchant" && ( {userRole !== "merchant" && (
<Button variant="outline" size="sm"> <Button variant="outline" size="sm" onClick={() => handleEditMerchant(merchant)}>
</Button> </Button>
)} )}
@@ -675,23 +786,6 @@ export default function MerchantsPage() {
placeholder="请输入商户名称" placeholder="请输入商户名称"
/> />
</div> </div>
<div className="space-y-2">
<Label htmlFor="businessType"></Label>
<Select
value={newMerchant.businessType}
onValueChange={(value) => setNewMerchant({ ...newMerchant, businessType: value })}
>
<SelectTrigger>
<SelectValue placeholder="选择业务类型" />
</SelectTrigger>
<SelectContent>
<SelectItem value="restaurant"></SelectItem>
<SelectItem value="retail"></SelectItem>
<SelectItem value="entertainment"></SelectItem>
<SelectItem value="service"></SelectItem>
</SelectContent>
</Select>
</div>
<div className="space-y-2"> <div className="space-y-2">
<Label htmlFor="contact"></Label> <Label htmlFor="contact"></Label>
<Input <Input
@@ -734,15 +828,6 @@ export default function MerchantsPage() {
</SelectContent> </SelectContent>
</Select> </Select>
</div> </div>
<div className="space-y-2">
<Label htmlFor="businessLicense"></Label>
<Input
id="businessLicense"
value={newMerchant.businessLicense}
onChange={(e) => setNewMerchant({ ...newMerchant, businessLicense: e.target.value })}
placeholder="请输入营业执照号"
/>
</div>
<div className="col-span-2 space-y-2"> <div className="col-span-2 space-y-2">
<Label htmlFor="detailedAddress"></Label> <Label htmlFor="detailedAddress"></Label>
<Textarea <Textarea
@@ -862,6 +947,145 @@ export default function MerchantsPage() {
</DialogContent> </DialogContent>
</Dialog> </Dialog>
{/* 编辑商户对话框 */}
<Dialog open={isEditMerchantOpen} onOpenChange={setIsEditMerchantOpen}>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle></DialogTitle>
<DialogDescription></DialogDescription>
</DialogHeader>
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="edit-province"></Label>
<Select
value={editMerchant.province}
onValueChange={handleEditProvinceChange}
disabled={loadingProvinces}
>
<SelectTrigger>
<SelectValue placeholder={loadingProvinces ? "加载中..." : "选择省份"} />
</SelectTrigger>
<SelectContent>
{provinces.map((province) => (
<SelectItem key={province.code} value={province.code}>
{province.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="edit-mall"></Label>
<Select
value={editMerchant.mallUserId}
onValueChange={(value) => {
const selectedMall = malls.find(mall => mall.mallUser === value)
setEditMerchant({
...editMerchant,
mall: selectedMall?.mallId || value,
mallUserId: value
})
}}
disabled={loadingMalls || !editMerchant.province}
>
<SelectTrigger>
<SelectValue placeholder={
!editMerchant.province
? "请先选择省份"
: loadingMalls
? "加载中..."
: "选择商场(可选)"
} />
</SelectTrigger>
<SelectContent>
{malls.map((mall) => (
<SelectItem key={mall.mallUser || mall.id} value={mall.mallUser}>
{mall.mallName}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="edit-name"></Label>
<Input
id="edit-name"
value={editMerchant.name}
onChange={(e) => setEditMerchant({ ...editMerchant, name: e.target.value })}
placeholder="请输入商户名称"
/>
</div>
<div className="space-y-2">
<Label htmlFor="edit-contact"></Label>
<Input
id="edit-contact"
value={editMerchant.contact}
onChange={(e) => setEditMerchant({ ...editMerchant, contact: e.target.value })}
placeholder="请输入联系人姓名"
/>
</div>
<div className="space-y-2">
<Label htmlFor="edit-phone"></Label>
<Input
id="edit-phone"
value={editMerchant.phone}
onChange={(e) => setEditMerchant({ ...editMerchant, phone: e.target.value })}
placeholder="请输入联系电话"
/>
</div>
<div className="space-y-2">
<Label htmlFor="edit-totalMerchant"></Label>
<Select
value={editMerchant.totalMerchant}
onValueChange={(value) => setEditMerchant({ ...editMerchant, totalMerchant: value })}
disabled={loadingTotalMerchants}
onOpenChange={(open) => {
if (open && totalMerchants.length === 0 && !loadingTotalMerchants) {
fetchTotalMerchants()
}
}}
>
<SelectTrigger>
<SelectValue placeholder={loadingTotalMerchants ? "加载中..." : "选择总商户(可选)"} />
</SelectTrigger>
<SelectContent>
{totalMerchants.map((merchant) => (
<SelectItem key={merchant.userId} value={merchant.userId}>
{merchant.nickName}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="col-span-2 space-y-2">
<Label htmlFor="edit-detailedAddress"></Label>
<Textarea
id="edit-detailedAddress"
value={editMerchant.detailedAddress}
onChange={(e) => setEditMerchant({ ...editMerchant, detailedAddress: e.target.value })}
placeholder="请输入详细地址"
rows={3}
/>
</div>
</div>
<div className="flex justify-end space-x-2 mt-6">
<Button
variant="outline"
onClick={() => setIsEditMerchantOpen(false)}
disabled={isSubmitting}
>
</Button>
<Button
onClick={handleUpdateMerchant}
disabled={!editMerchant.name || !editMerchant.contact || !editMerchant.phone || isSubmitting}
>
{isSubmitting ? '更新中...' : '更新商户'}
</Button>
</div>
</DialogContent>
</Dialog>
{/* 设备详情对话框 */} {/* 设备详情对话框 */}
<Dialog open={isEquipmentDialogOpen} onOpenChange={setIsEquipmentDialogOpen}> <Dialog open={isEquipmentDialogOpen} onOpenChange={setIsEquipmentDialogOpen}>
<DialogContent className="!w-[1400px] !max-w-[1400px] max-h-[800px] overflow-hidden flex flex-col"> <DialogContent className="!w-[1400px] !max-w-[1400px] max-h-[800px] overflow-hidden flex flex-col">