import { productcontrollerCreatecategory, productcontrollerCreatecategoryattribute, productcontrollerDeletecategory, productcontrollerDeletecategoryattribute, productcontrollerGetcategoriesall, productcontrollerGetcategoryattributes, productcontrollerUpdatecategory, } from '@/servers/api/product'; import { PlusOutlined } from '@ant-design/icons'; import { PageContainer } from '@ant-design/pro-components'; import { request } from '@umijs/max'; import { Button, Card, Form, Input, Layout, List, Modal, Popconfirm, Select, message, } from 'antd'; import React, { useEffect, useState } from 'react'; import { notAttributes } from '../Product/Attribute/consts'; const { Sider, Content } = Layout; const CategoryPage: React.FC = () => { const [categories, setCategories] = useState([]); const [loadingCategories, setLoadingCategories] = useState(false); const [selectedCategory, setSelectedCategory] = useState(null); const [categoryAttributes, setCategoryAttributes] = useState([]); const [loadingAttributes, setLoadingAttributes] = useState(false); const [isCategoryModalVisible, setIsCategoryModalVisible] = useState(false); const [categoryForm] = Form.useForm(); const [editingCategory, setEditingCategory] = useState(null); const [isAttributeModalVisible, setIsAttributeModalVisible] = useState(false); const [availableDicts, setAvailableDicts] = useState([]); const [selectedDictIds, setSelectedDictIds] = useState([]); const fetchCategories = async () => { setLoadingCategories(true); try { const res = await productcontrollerGetcategoriesall(); setCategories(res || []); } catch (error) { message.error('获取分类列表失败'); } setLoadingCategories(false); }; useEffect(() => { fetchCategories(); }, []); const fetchCategoryAttributes = async (categoryId: number) => { setLoadingAttributes(true); try { const res = await productcontrollerGetcategoryattributes({ categoryItemId: categoryId, }); setCategoryAttributes(res || []); } catch (error) { message.error('获取分类属性失败'); } setLoadingAttributes(false); }; useEffect(() => { if (selectedCategory) { fetchCategoryAttributes(selectedCategory.id); } else { setCategoryAttributes([]); } }, [selectedCategory]); const handleCategorySubmit = async (values: any) => { try { if (editingCategory) { await productcontrollerUpdatecategory( { id: editingCategory.id }, values, ); message.success('更新成功'); } else { await productcontrollerCreatecategory(values); message.success('创建成功'); } setIsCategoryModalVisible(false); fetchCategories(); } catch (error: any) { message.error(error.message || '操作失败'); } }; const handleDeleteCategory = async (id: number) => { try { await productcontrollerDeletecategory({ id }); message.success('删除成功'); if (selectedCategory?.id === id) { setSelectedCategory(null); } fetchCategories(); } catch (error: any) { message.error(error.message || '删除失败'); } }; const handleAddAttribute = async () => { // Fetch all dicts and filter those that are allowed attributes try { const res = await request('/dict/list'); const filtered = (res || []).filter( (d: any) => !notAttributes.has(d.name), ); // Filter out already added attributes const existingDictIds = new Set( categoryAttributes.map((ca: any) => ca.dict.id), ); const available = filtered.filter((d: any) => !existingDictIds.has(d.id)); setAvailableDicts(available); setSelectedDictIds([]); setIsAttributeModalVisible(true); } catch (error) { message.error('获取属性字典失败'); } }; const handleAttributeSubmit = async () => { if (selectedDictIds.length === 0) { message.warning('请选择属性'); return; } try { await productcontrollerCreatecategoryattribute({ categoryItemId: selectedCategory.id, attributeDictIds: selectedDictIds, }); message.success('添加属性成功'); setIsAttributeModalVisible(false); fetchCategoryAttributes(selectedCategory.id); } catch (error: any) { message.error(error.message || '添加失败'); } }; const handleDeleteAttribute = async (id: number) => { try { await productcontrollerDeletecategoryattribute({ id }); message.success('移除属性成功'); fetchCategoryAttributes(selectedCategory.id); } catch (error: any) { message.error(error.message || '移除失败'); } }; return (
分类列表
( setSelectedCategory(item)} actions={[ { e.stopPropagation(); setEditingCategory(item); categoryForm.setFieldsValue(item); setIsCategoryModalVisible(true); }} > 编辑 , { e?.stopPropagation(); handleDeleteCategory(item.id); }} onCancel={(e) => e?.stopPropagation()} > e.stopPropagation()} style={{ color: 'red' }} > 删除 , ]} > )} />
{selectedCategory ? ( 添加关联属性 } > ( handleDeleteAttribute(item.id)} > , ]} > )} /> ) : (
请选择左侧分类
)}
categoryForm.submit()} onCancel={() => setIsCategoryModalVisible(false)} >
setIsAttributeModalVisible(false)} >