import { View } from "@tarojs/components"; import { Input, Radio, Toast, Uploader, UploaderFileItem, } from "@nutui/nutui-react-taro"; import { Icon } from "@/components"; import { SupplierVO } from "@/types/typings"; import { forwardRef, useEffect, useImperativeHandle, useState } from "react"; import { uploadFile } from "@/utils/uploader"; import { validatePrice } from "@/utils/format"; // 定义ref暴露的方法接口 export interface WeighRef { validate: () => boolean; } interface IWeightProps { value: SupplierVO; onChange: (supplierVO: SupplierVO) => void; supplierCount: number; } export default forwardRef(function Weigh(props, ref) { const { value, onChange, supplierCount } = props; const [supplierVO, setSupplierVO] = useState(); // 初始化数据 useEffect(() => { setSupplierVO(value); // 初始化空磅照片和总磅照片 if (value.emptyWeightImg) { setEmptyWeightImgList([ { url: value.emptyWeightImg, name: "empty-weight-image", status: "success", }, ]); } if (value.totalWeightImg) { setTotalWeightImgList([ { url: value.totalWeightImg, name: "total-weight-image", status: "success", }, ]); } }, []); // 监听供应商信息变化 useEffect(() => { if (supplierVO) { onChange(supplierVO); } }, [supplierVO]); const [emptyWeightImgList, setEmptyWeightImgList] = useState< UploaderFileItem[] >([]); const [totalWeightImgList, setTotalWeightImgList] = useState< UploaderFileItem[] >([]); const [isPaperError, setIsPaperError] = useState<{ [key: string]: boolean; }>({}); // 添加是否为最后一个瓜农的错误状态 // 添加三个字段的错误状态 const [emptyWeightError, setEmptyWeightError] = useState<{ [key: string]: boolean; }>({}); const [totalWeightError, setTotalWeightError] = useState<{ [key: string]: boolean; }>({}); const [priceError, setPriceError] = useState<{ [key: string]: boolean; }>({}); // 空磅照片变更处理函数 const handleEmptyWeightImgChange = (files: UploaderFileItem[]) => { setEmptyWeightImgList(files); // 如果有文件且上传成功,保存URL到supplierVO if (files.length > 0 && files[0].url) { setSupplierVO((prev) => ({ ...prev!, emptyWeightImg: files[0].url, })); } else { // 如果没有文件,清空URL setSupplierVO((prev) => ({ ...prev!, emptyWeightImg: undefined, })); } }; // 总磅照片变更处理函数 const handleTotalWeightImgChange = (files: UploaderFileItem[]) => { setTotalWeightImgList(files); // 如果有文件且上传成功,保存URL到supplierVO if (files.length > 0 && files[0].url) { setSupplierVO((prev) => ({ ...prev!, totalWeightImg: files[0].url, })); } else { // 如果没有文件,清空URL setSupplierVO((prev) => ({ ...prev!, totalWeightImg: undefined, })); } }; // 将校验方法暴露给父组件 useImperativeHandle(ref, () => ({ validate, })); if (!supplierVO) { return; } // 校验是否为最后一个瓜农函数 const validateIsPaper = (isPaper: any) => { // 必须选择是或否 return isPaper !== undefined; }; // 校验空磅重量 const validateEmptyWeight = (value: any) => { if (value === "" || value === undefined || value === null) { setEmptyWeightError((prev) => ({ ...prev, [supplierVO.orderSupplierId]: true, })); return false; } const numValue = Number(value); if (Number.isNaN(numValue) || numValue < 0) { setEmptyWeightError((prev) => ({ ...prev, [supplierVO.orderSupplierId]: true, })); return false; } setEmptyWeightError((prev) => ({ ...prev, [supplierVO.orderSupplierId]: false, })); return true; }; // 校验总磅重量 const validateTotalWeight = (value: any) => { if (value === "" || value === undefined || value === null) { setTotalWeightError((prev) => ({ ...prev, [supplierVO.orderSupplierId]: true, })); return false; } const numValue = Number(value); if (Number.isNaN(numValue) || numValue < 0) { setTotalWeightError((prev) => ({ ...prev, [supplierVO.orderSupplierId]: true, })); return false; } setTotalWeightError((prev) => ({ ...prev, [supplierVO.orderSupplierId]: false, })); return true; }; // 校验采购单价 const validatePurchasePrice = (value: any) => { if (value === "" || value === undefined || value === null) { setPriceError((prev) => ({ ...prev, [supplierVO.orderSupplierId]: true, })); return false; } const numValue = Number(value); if (Number.isNaN(numValue) || numValue < 0) { setPriceError((prev) => ({ ...prev, [supplierVO.orderSupplierId]: true, })); return false; } setPriceError((prev) => ({ ...prev, [supplierVO.orderSupplierId]: false, })); return true; }; // 对外暴露的校验方法 const validate = () => { const id = supplierVO?.orderSupplierId; if (!id) return false; const isEmptyWeightValid = validateEmptyWeight(supplierVO.emptyWeight); const isTotalWeightValid = validateTotalWeight(supplierVO.totalWeight); const isPurchasePriceValid = validatePurchasePrice( supplierVO.purchasePrice, ); const isPaperValid = validateIsPaper(supplierVO.isPaper); // 更新错误状态 setIsPaperError((prev) => ({ ...prev, [id]: !isPaperValid, })); setPriceError((prev) => ({ ...prev, [id]: !isPurchasePriceValid, })); setTotalWeightError((prev) => ({ ...prev, [id]: !isTotalWeightValid, })); setEmptyWeightError((prev) => ({ ...prev, [id]: !isEmptyWeightValid, })); const isValid = isEmptyWeightValid && isTotalWeightValid && isPurchasePriceValid && isPaperValid; if (!isValid) { Toast.show("toast", { icon: "fail", title: "提示", content: "请完善称重信息后再进行下一步操作", }); } return isValid; }; return ( 空车来的时候带纸箱了吗? {supplierCount == 1 ? ( { // 清除错误状态 setIsPaperError((prev) => ({ ...prev, [supplierVO.orderSupplierId]: false, })); // 根据用户选择设置是否包含空纸箱 const isPaperValue = value === "true" ? true : value === "false" ? false : undefined; setSupplierVO({ ...supplierVO, isPaper: isPaperValue, }); }} > 带了 没带 ) : supplierVO.isPaper === true ? ( "带了" ) : ( "没带" )} {isPaperError[supplierVO.orderSupplierId] && ( 请选择车来的时候有没有带纸箱 )} {supplierVO.name}的称重信息 空磅重量 (kg) { const numValue = validatePrice(value); if (numValue !== undefined) { setSupplierVO({ ...supplierVO, emptyWeight: numValue as any, }); // 校验输入值 validateEmptyWeight(numValue); } }} onBlur={() => { // 失去焦点时进行校验 validateEmptyWeight(supplierVO.emptyWeight); }} /> {emptyWeightError[supplierVO.orderSupplierId] && ( 请输入有效的空磅重量 )} 自动带入上一个瓜农的总磅重量 总磅重量 (kg) { const numValue = validatePrice(value); if (numValue !== undefined) { setSupplierVO({ ...supplierVO, totalWeight: numValue as any, }); // 校验输入值 validateTotalWeight(numValue); } }} onBlur={() => { // 失去焦点时进行校验 validateTotalWeight(supplierVO.totalWeight); }} /> {totalWeightError[supplierVO.orderSupplierId] && ( 请输入有效的总磅重量 )} 采购单价设置 (元/斤) { const numValue = validatePrice(value); if (numValue !== undefined) { setSupplierVO({ ...supplierVO, purchasePrice: numValue as any, }); // 校验输入值 validatePurchasePrice(numValue); } }} onBlur={() => { // 失去焦点时进行校验 validatePurchasePrice(supplierVO.purchasePrice); }} /> {priceError[supplierVO.orderSupplierId] && ( 请输入有效的采购单价 )} } uploadLabel={ 拍照上传空磅照片 } maxCount={1} //@ts-ignore upload={uploadFile} multiple /> } uploadLabel={ 拍照上传总磅照片 } maxCount={1} //@ts-ignore upload={uploadFile} multiple /> ); });