import Constants from '@/constants'; import { getPbcBusinessListUsingPost } from '@/services/pop-b2b2c/pbcBusinessController'; import { listAdminTreeUsingGet } from '@/services/pop-b2b2c/pbcCategoryController'; import { getRecordByL3CategoryIdUsingGet } from '@/services/pop-b2b2c/pbcCommonDataController'; import { addOrUpdateProductForAdminUsingPost } from '@/services/pop-b2b2c/pbcProductController'; import { getCities } from '@/utils/cities'; import { ProCard, ProForm, ProFormCascader, ProFormDigit, ProFormInstance, ProFormList, ProFormSelect, ProFormText, ProFormTextArea, ProFormUploadButton, } from '@ant-design/pro-components'; import { PageContainer } from '@ant-design/pro-layout'; import { DndContext, DragEndEvent, PointerSensor, useSensor } from '@dnd-kit/core'; import { arrayMove, SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable'; import { Button, Col, message, Row } from 'antd'; import Upload, { RcFile, UploadFile } from 'antd/es/upload'; import React, { useRef, useState } from 'react'; import { CSS } from '@dnd-kit/utilities'; interface DraggableUploadListItemProps { originNode: React.ReactElement>; file: UploadFile; } const DraggableUploadListItem = ({ originNode, file }: DraggableUploadListItemProps) => { let { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: file.uid, }); const style: React.CSSProperties = { transform: CSS.Translate.toString(transform), transition, cursor: 'move', }; return (
{/* hide error tooltip when dragging */} {file.status === 'error' && isDragging ? originNode.props.children : originNode}
); }; const Detail: React.FC = () => { const [cities] = useState(() => getCities()) const [colorData, setColorData] = useState() const [commonDataList, setCommonDataList] = useState() const formRef = useRef(); const onSave = () => { formRef.current?.submit() const values = formRef.current?.getFieldsValue() console.log(values.colorItems) console.log(values.specItems) } const handleCategoryChange = (value: any[], items: API.PbcCategory[]) => { const [c1, c2, c3] = items formRef.current?.setFieldsValue({ pbcProductTopCategoryName: c1?.pbcCategoryName, pbcProductParentCategoryName: c2?.pbcCategoryName, pbcProductCategoryName: c3?.pbcCategoryName, }) if (value.length === 3) { getRecordByL3CategoryIdUsingGet({ l3CategoryId: value[2] }).then(res => { if (res.retcode && res.data) { setColorData(res.data.colorData) setCommonDataList(res.data.commonDataList) } }) } } const onSubmit = async (values: any) => { let pbcProductOriginalProvince = undefined, pbcProductOriginalCity = undefined; if (values?.pbcZone?.length) { ([pbcProductOriginalProvince, pbcProductOriginalCity] = values.pbcZone); } const [pbcProductTopCategoryId, pbcProductParentCategoryId, pbcProductCategoryId] = values.pbcProductCategoryIdList console.log(values.colorItems) console.log(values.specItems) const specItems: API.PbcProductCommonData[] = [] if (colorData && values.colorItems && values.colorItems.length > 0) { for (let i = 0; i < values.colorItems.length; i++) { const element = values.colorItems[i]; specItems.push({ pbcCommonDataId: colorData.length > 0 ? colorData[0].pbcId : undefined, pbcSystemName: '颜色', pbcSystemInputType: 'text', pbcCommonDataSystem: element.name, pbcColorImageUrl: element.value.length > 0 ? element.value[0].response.data : '' }) } } if (commonDataList) { for (let i = 0; i < commonDataList.length; i++) { const element = commonDataList[i]; console.log(values[`value${i}`]) let name = '' if (element.pbcSystemInputType === 'select' && element.commonDataValueList) { name = element.commonDataValueList.find(e => e.pbcId === values[`value${i}`])?.pbcSystemValue || '' } specItems.push({ pbcCommonDataId: element.pbcId, pbcSystemName: element.pbcSystemName, pbcSystemInputType: element.pbcSystemInputType, pbcCommonDataValueId: element.pbcSystemInputType === 'select' ? values[`value${i}`] : undefined, pbcCommonDataSystemValue: element.pbcSystemInputType === 'select' ? name : undefined, pbcCommonDataSystem: element.pbcSystemInputType === 'select' ? undefined : values[`value${i}`] }) } } const params: API.PbcProductDTO = { ...values, pbcProductOriginalProvince, pbcProductOriginalCity, pbcProductTopCategoryId, pbcProductParentCategoryId, pbcProductCategoryId, productCommonDataList: specItems, pbcProductImages: values.pbcProductImages.filter((e: any) => e.response && e.response.data).map((e: any) => e.response.data).join(','), pbcProductDetailImages: values.pbcProductDetailImages.filter((e: any) => e.response && e.response.data).map((e: any) => e.response.data).join(','), pbcZone: undefined, colorItems: undefined, specItems: undefined, pbcProductCategoryIdList: undefined, } const msg = await addOrUpdateProductForAdminUsingPost(params) if (msg.retcode) { message.success("创建成功!") history.back(); return true } else { message.error(msg.retmsg) return false } } const sensor = useSensor(PointerSensor, { activationConstraint: { distance: 10 }, }); const onDragEnd = ({ active, over }: DragEndEvent, fieldName: string) => { if (active.id !== over?.id) { const arr = formRef.current?.getFieldValue(fieldName) || [] const activeIndex = arr.findIndex((i: any) => i.uid === active.id); const overIndex = arr.findIndex((i: any) => i.uid === over?.id); const newArr = arrayMove(arr, activeIndex, overIndex) formRef.current?.setFieldValue(fieldName, newArr) } }; return ( { history.back(); }} > 返回 , ]} > ); }; export default Detail;