init: initial commit

This commit is contained in:
Blizzard
2026-02-28 17:35:31 +08:00
commit da7ac70eeb
44 changed files with 13146 additions and 0 deletions
+325
View File
@@ -0,0 +1,325 @@
import { useState, useEffect } from 'react';
import {
getChannelListApi,
saveChannelApi,
updateChannelApi,
deleteChannelApi,
getCategoryListApi
} from '../../../api/radio';
import { Button } from '../../../components/ui/button';
import { Input } from '../../../components/ui/input';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '../../../components/ui/table';
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '../../../components/ui/dialog';
import { Label } from '../../../components/ui/label';
import { Plus, Edit, Trash2 } from 'lucide-react';
import { toast } from 'sonner';
export default function Channel() {
const [data, setData] = useState([]);
const [total, setTotal] = useState(0);
const [loading, setLoading] = useState(false);
const [categories, setCategories] = useState([]);
// Pagination & Search
const [page, setPage] = useState(1);
const [pageSize] = useState(10);
const [searchName, setSearchName] = useState('');
// Dialog State
const [open, setOpen] = useState(false);
const [isEdit, setIsEdit] = useState(false);
const [formData, setFormData] = useState({
ID: undefined,
categoryId: '',
name: '',
description: '',
price: 0,
coverId: '',
tags: '',
isVipOnly: 0,
sort: 0,
status: 1
});
const fetchCategories = async () => {
try {
const res: any = await getCategoryListApi();
setCategories(res.list || res || []);
} catch (e) {
console.error(e);
}
}
const fetchData = async () => {
if (loading) return;
setLoading(true);
try {
const res: any = await getChannelListApi({
current: page,
pageSize: pageSize,
keyword: searchName,
name: searchName
});
setData(res.list || []);
setTotal(res.total || 0);
} catch (e) {
console.error(e);
} finally {
setLoading(false);
}
};
// Table data fetch only on pagination change
useEffect(() => {
fetchData();
}, [page, pageSize]);
// Lazy load categories only when dialog opens
useEffect(() => {
if (open) {
fetchCategories();
}
}, [open]);
const handleSearch = () => {
if (page === 1) {
fetchData();
} else {
setPage(1);
}
};
const handleOpenAdd = () => {
setIsEdit(false);
setFormData({
ID: undefined, categoryId: '', name: '', description: '',
price: 0, coverId: '', tags: '', isVipOnly: 0, sort: 0, status: 1
});
setOpen(true);
};
const handleOpenEdit = (record: any) => {
setIsEdit(true);
setFormData({
ID: record.ID,
categoryId: record.categoryId,
name: record.name,
description: record.description,
price: record.price,
coverId: record.coverId,
tags: record.tags,
isVipOnly: record.isVipOnly,
sort: record.sort,
status: record.status
});
setOpen(true);
};
const handleDelete = async (id: number) => {
if (!confirm('确定要删除这个频道吗?')) return;
try {
await deleteChannelApi({ id });
toast.success('删除成功');
fetchData();
} catch (e) {
console.error(e);
}
};
const handleSubmit = async () => {
if (!formData.name) {
toast.error('请填写频道名称');
return;
}
try {
if (isEdit) {
await updateChannelApi(formData);
toast.success('更新成功');
} else {
await saveChannelApi(formData);
toast.success('创建成功');
}
setOpen(false);
fetchData();
} catch (e) {
console.error(e);
}
};
return (
<div className="space-y-4 flex flex-col h-full bg-background rounded-lg border p-6">
<div className="flex justify-between items-center">
<div>
<h1 className="text-2xl font-bold tracking-tight"></h1>
<p className="text-muted-foreground"></p>
</div>
</div>
{/* Toolbar */}
<div className="flex justify-between items-center space-x-2">
<div className="flex space-x-2 w-full max-sm">
<Input
placeholder="根据名称搜索..."
value={searchName}
onChange={(e) => setSearchName(e.target.value)}
onKeyDown={(e) => e.key === 'Enter' && handleSearch()}
/>
<Button variant="secondary" onClick={handleSearch}></Button>
</div>
<Button onClick={handleOpenAdd}>
<Plus className="w-4 h-4 mr-2" />
</Button>
</div>
{/* Data Table */}
<div className="border rounded-md mt-4 flex-1 overflow-auto">
<Table>
<TableHeader>
<TableRow>
<TableHead className="w-[80px]">ID</TableHead>
<TableHead>ID</TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead>()</TableHead>
<TableHead>VIP</TableHead>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{loading && data.length === 0 ? (
<TableRow>
<TableCell colSpan={9} className="text-center h-32">...</TableCell>
</TableRow>
) : data.length === 0 ? (
<TableRow>
<TableCell colSpan={9} className="text-center h-32 text-muted-foreground"></TableCell>
</TableRow>
) : (
data.map((item: any) => (
<TableRow key={item.ID} className={loading ? 'opacity-50' : ''}>
<TableCell>{item.ID}</TableCell>
<TableCell>{item.categoryId}</TableCell>
<TableCell className="font-medium">{item.name}</TableCell>
<TableCell>{item.description}</TableCell>
<TableCell>{item.tags}</TableCell>
<TableCell>{item.price}</TableCell>
<TableCell>{item.isVipOnly === 1 ? '是' : '否'}</TableCell>
<TableCell>{item.status === 1 ? '启用' : '禁用'}</TableCell>
<TableCell className="text-right">
<Button variant="ghost" size="icon" onClick={() => handleOpenEdit(item)}>
<Edit className="w-4 h-4 text-blue-500" />
</Button>
<Button variant="ghost" size="icon" onClick={() => handleDelete(item.ID)}>
<Trash2 className="w-4 h-4 text-red-500" />
</Button>
</TableCell>
</TableRow>
))
)}
</TableBody>
</Table>
</div>
<div className="flex bg-muted p-2 mt-2 rounded items-center justify-between text-sm text-muted-foreground">
<span>: {total} </span>
<span>: {pageSize} </span>
</div>
{/* Form Dialog */}
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="max-w-md">
<DialogHeader>
<DialogTitle>{isEdit ? '编辑频道' : '新增频道'}</DialogTitle>
</DialogHeader>
<div className="space-y-4 py-4 max-h-[60vh] overflow-y-auto px-1">
<div className="space-y-2">
<Label></Label>
<Input
value={formData.name}
onChange={e => setFormData({ ...formData, name: e.target.value })}
placeholder="输入频道名称"
/>
</div>
<div className="space-y-2">
<Label></Label>
<select
className="flex h-10 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm"
value={formData.categoryId}
onChange={e => setFormData({ ...formData, categoryId: e.target.value })}
>
<option value="" disabled></option>
{categories.map((c: any) => (
<option key={c.ID} value={c.ID}>{c.name}</option>
))}
</select>
</div>
<div className="space-y-2">
<Label></Label>
<Input
value={formData.description}
onChange={e => setFormData({ ...formData, description: e.target.value })}
placeholder="频道描述"
/>
</div>
<div className="space-y-2">
<Label> ()</Label>
<Input
value={formData.tags}
onChange={e => setFormData({ ...formData, tags: e.target.value })}
placeholder="例:音乐,新闻"
/>
</div>
<div className="flex space-x-4">
<div className="space-y-2 flex-1">
<Label> ()</Label>
<Input
type="number"
value={formData.price}
onChange={e => setFormData({ ...formData, price: parseInt(e.target.value) || 0 })}
/>
</div>
<div className="space-y-2 flex-1">
<Label></Label>
<Input
type="number"
value={formData.sort}
onChange={e => setFormData({ ...formData, sort: parseInt(e.target.value) || 0 })}
/>
</div>
</div>
<div className="flex space-x-4">
<div className="space-y-2 flex-1">
<Label>VIP</Label>
<select
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
value={formData.isVipOnly}
onChange={e => setFormData({ ...formData, isVipOnly: parseInt(e.target.value) })}
>
<option value={0}></option>
<option value={1}></option>
</select>
</div>
<div className="space-y-2 flex-1">
<Label></Label>
<select
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
value={formData.status}
onChange={e => setFormData({ ...formData, status: parseInt(e.target.value) })}
>
<option value={1}></option>
<option value={0}></option>
</select>
</div>
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => setOpen(false)}></Button>
<Button onClick={handleSubmit}></Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}