ERPTurbo_Client/packages/app-client/src/components/purchase/module/MelonFarmer.tsx
shenyifei 837c27e9bd feat(purchase): 重构纸箱使用逻辑并优化采购计算
- 移除 OWN 类型纸箱,统一使用 USED 类型处理
- 简化纸箱使用流程,去除瓜农是否包纸箱的选择
- 更新采购成本计算逻辑,支持按毛重或净重报价
- 优化界面布局,提升用户体验
- 调整数据结构定义,确保类型一致性
- 增加版本号至 v0.0.31
- 添加被驳回订单的编辑功能
- 根据经销商控制表单展示内容
- 修复部分计算逻辑中的过滤条件
- 清理冗余代码和无用字段
2025-12-10 15:47:32 +08:00

941 lines
28 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Text, View } from "@tarojs/components";
import {
Button,
Dialog,
Input,
Radio,
Toast,
Uploader,
UploaderFileItem,
} from "@nutui/nutui-react-taro";
import { Icon, SupplierPicker } from "@/components";
import { forwardRef, useEffect, useImperativeHandle, useState } from "react";
import { buildUrl, generateShortId, uploadFile } from "@/utils";
import Taro from "@tarojs/taro";
import { business } from "@/services";
// 定义ref暴露的方法接口
export interface MelonFarmerRef {
validate: () => boolean;
}
interface IMelonFarmerProps {
index: number;
value: BusinessAPI.PurchaseOrderVO;
onChange: (purchaseOrderVO: BusinessAPI.PurchaseOrderVO) => void;
}
export default forwardRef<MelonFarmerRef, IMelonFarmerProps>(
function MelonFarmer(props, ref) {
const onRemove = (supplierVO: BusinessAPI.OrderSupplier) => {
if (orderSupplierList.length <= 1) {
setOrderSupplierList([
{
orderSupplierId: generateShortId(),
supplierId: "",
name: "瓜农1",
idCard: "",
bankCard: "",
phone: "",
selected: true,
orderPackageList: [],
packageUsage: [
{
boxType: "USED",
isUsed: 0,
},
{
boxType: "EXTRA_USED",
isUsed: 0,
},
{
boxType: "EXTRA",
isUsed: 0,
},
{
boxType: "REMAIN",
isUsed: 0,
},
],
} as any,
]);
return;
} else {
let temp = orderSupplierList.filter(
(item: BusinessAPI.OrderSupplier) =>
item.orderSupplierId !== supplierVO.orderSupplierId,
) as BusinessAPI.OrderSupplier[];
temp[0].selected = true;
setOrderSupplierList(temp);
}
};
const { value, onChange, index } = props;
const { orderSupplierList } = value;
const supplierVO = orderSupplierList[index] as BusinessAPI.OrderSupplier;
const supplierCount = orderSupplierList.length;
const isLast = index === supplierCount - 1;
// 获取已选择的供应商ID列表排除当前项
const selectedSupplierIds = orderSupplierList
.filter((supplier, idx) => idx !== index && supplier.supplierId)
.map((supplier) => supplier.supplierId!);
// 获取已选择的供应商ID列表排除当前项
const selectedSupplierNames = orderSupplierList
.filter((supplier, idx) => idx !== index && supplier.supplierId)
.map((supplier) => supplier.name!);
const setOrderSupplierList = (
newOrderSupplierList: BusinessAPI.OrderSupplier[],
) => {
onChange({
...value,
orderSupplierList: newOrderSupplierList,
});
};
const setSupplierVO = (newSupplierVO: BusinessAPI.OrderSupplier) => {
console.log("setSupplierVO", newSupplierVO);
setOrderSupplierList([
...orderSupplierList.map((item: BusinessAPI.OrderSupplier) => {
if (item.orderSupplierId == newSupplierVO.orderSupplierId) {
return {
...item,
...newSupplierVO,
};
}
return item;
}),
]);
};
// 初始化数据
useEffect(() => {
// 初始化微信二维码图片列表
if (supplierVO.wechatQr) {
setPicList([
{
url: supplierVO.wechatQr,
name: "wechat-qrcode",
status: "success",
},
]);
}
}, [supplierVO]);
const [picList, setPicList] = useState<UploaderFileItem[]>([]);
// 微信二维码变更处理函数
const handleWechatQrChange = (files: UploaderFileItem[]) => {
setPicList(files);
// 如果有文件且上传成功保存URL到supplierVO
if (files.length > 0 && files[0].url) {
setSupplierVO({
...supplierVO,
wechatQr: files[0].url,
});
} else {
// 如果没有文件清空URL
setSupplierVO({
...supplierVO,
wechatQr: undefined,
});
}
};
// 校验状态
const [nameError, setNameError] = useState<{ [key: string]: boolean }>({});
const [idCardError, setIdCardError] = useState<{ [key: string]: boolean }>(
{},
);
const [bankCardError, setBankCardError] = useState<{
[key: string]: boolean;
}>({});
const [phoneError, setPhoneError] = useState<{ [key: string]: boolean }>(
{},
);
const [isLastFarmerError, setIsLastFarmerError] = useState<{
[key: string]: boolean;
}>({}); // 添加是否要拼车的错误状态
// 校验姓名函数
const validateName = (name: string) => {
if (!name) {
return false;
}
// 姓名至少2个字符
return name.length >= 2;
};
// 校验身份证号函数
const validateIdCard = (idCard: string) => {
if (!idCard) {
return false;
}
// 18位身份证号正则表达式
const idCardRegex =
/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
return idCardRegex.test(idCard);
};
// 校验银行卡号函数
const validateBankCard = (bankCard: string) => {
if (!bankCard) {
return false;
}
// 银行卡号一般为16-19位数字
const bankCardRegex = /^\d{16,19}$/;
return bankCardRegex.test(bankCard);
};
// 校验手机号函数 (使用项目中已有的规则)
const validatePhone = (phone: string) => {
if (!phone) {
return false;
}
// 使用项目规范的手机号正则表达式
const phoneRegex = /^1[3456789]\d{9}$/;
return phoneRegex.test(phone);
};
// 校验是否要拼车函数
// 校验是否要拼车函数
const validateIsLastFarmer = (isLast?: boolean) => {
// 必须选择要不要
return isLast !== undefined;
};
// 对外暴露的校验方法
const validate = () => {
const id = supplierVO?.orderSupplierId;
if (!id) return false;
const isNameValid = validateName(supplierVO.name || "");
if (
selectedSupplierNames &&
selectedSupplierNames.includes(supplierVO.name || "")
) {
Toast.show("toast", {
icon: "fail",
title: "提示",
content: "该瓜农已被选择,请输入其他名称",
});
return false;
}
const isIdCardValid = validateIdCard(supplierVO.idCard || "");
const isBankCardValid = validateBankCard(supplierVO.bankCard || "");
const isPhoneValid = validatePhone(supplierVO.phone || "");
const isLastFarmerValid = validateIsLastFarmer(supplierVO?.isLast); // 校验是否需要拼车
// 更新错误状态
setNameError((prev) => ({
...prev,
[id]: !isNameValid,
}));
setIdCardError((prev) => ({
...prev,
[id]: !isIdCardValid,
}));
setBankCardError((prev) => ({
...prev,
[id]: !isBankCardValid,
}));
setPhoneError((prev) => ({
...prev,
[id]: !isPhoneValid,
}));
// 更新是否为最后一个瓜农的错误状态
setIsLastFarmerError((prev) => ({
...prev,
[id]: !isLastFarmerValid,
}));
const isValid =
isNameValid &&
isIdCardValid &&
isBankCardValid &&
isPhoneValid &&
isLastFarmerValid;
if (!isValid) {
Toast.show("toast", {
icon: "fail",
title: "提示",
content: "请完善瓜农信息后再进行下一步操作",
});
}
return isValid;
};
// 将校验方法暴露给父组件
useImperativeHandle(ref, () => ({
validate,
}));
// 处理姓名变化
const handleNameChange = (
value: string,
supplierVO: BusinessAPI.OrderSupplier,
) => {
setSupplierVO({
...supplierVO!,
name: value,
});
const id = supplierVO.orderSupplierId;
// 校验并更新错误状态
const isValid = validateName(value);
setNameError((prev) => ({
...prev,
[id]: !isValid && value !== "",
}));
if (isValid) {
setNameError((prev) => ({
...prev,
[id]: false,
}));
}
if (selectedSupplierNames && selectedSupplierNames.includes(value)) {
Toast.show("toast", {
icon: "fail",
title: "提示",
content: "该瓜农已被选择,请输入其他名称",
});
}
};
// 处理姓名失焦
const handleNameBlur = async (value: string, id: any) => {
if (value && !validateName(value)) {
setNameError((prev) => ({
...prev,
[id]: true,
}));
return;
} else {
setNameError((prev) => ({
...prev,
[id]: false,
}));
}
// 检查瓜农姓名是否在系统中存在
console.log("checkSupplier", value);
if (value) {
if (selectedSupplierNames && selectedSupplierNames.includes(value)) {
Toast.show("toast", {
icon: "fail",
title: "提示",
content: "该瓜农已被选择,请输入其他名称",
});
return;
}
try {
const {
data: { data: newSupplierVO, success },
} = await business.supplier.checkSupplier({
supplierCheckQry: {
name: value,
},
});
if (success && newSupplierVO) {
// 系统中已存在该瓜农信息,检查是否需要提示用户快速填入
// 条件1: 当前没有选择瓜农 (没有supplierId)
// 条件2: 当前选择的瓜农与系统中找到的瓜农不一致
const shouldPrompt =
!supplierVO?.supplierId ||
(supplierVO?.supplierId &&
supplierVO?.supplierId !== newSupplierVO.supplierId);
if (shouldPrompt) {
// 判断是首次填入还是冲突替换
const isConflict =
supplierVO?.supplierId &&
supplierVO?.supplierId !== newSupplierVO.supplierId;
Dialog.open("dialog", {
title: "提示",
content: isConflict
? `系统中存在瓜农"${value}"的信息与当前瓜农不同,是否替换为系统中的信息?`
: `系统中已存在瓜农"${value}"的信息,是否快速填入?`,
onConfirm: () => {
// 用户确认,填入信息
setSupplierVO({
...supplierVO!,
...newSupplierVO,
});
// 清除所有错误状态
setNameError((prev) => ({
...prev,
[id]: false,
}));
setPhoneError((prev) => ({
...prev,
[id]: false,
}));
setBankCardError((prev) => ({
...prev,
[id]: false,
}));
setIdCardError((prev) => ({
...prev,
[id]: false,
}));
Dialog.close("dialog");
},
onCancel: () => {
Dialog.close("dialog");
},
});
}
}
} catch (error) {
console.error("检查瓜农信息失败", error);
}
}
};
// 处理身份证号变化
const handleIdCardChange = (
value: string,
supplierVO: BusinessAPI.OrderSupplier,
) => {
setSupplierVO({
...supplierVO,
idCard: value,
});
const id = supplierVO.orderSupplierId;
// 校验并更新错误状态
const isValid = validateIdCard(value);
setIdCardError((prev) => ({
...prev,
[id]: !isValid && value !== "",
}));
if (isValid) {
setIdCardError((prev) => ({
...prev,
[id]: false,
}));
}
};
// 处理身份证号失焦
const handleIdCardBlur = (value: string, id: any) => {
if (value && !validateIdCard(value)) {
setIdCardError((prev) => ({
...prev,
[id]: true,
}));
Toast.show("toast", {
icon: "fail",
title: "提示",
content: "请输入正确的身份证号",
});
} else {
setIdCardError((prev) => ({
...prev,
[id]: false,
}));
}
};
// 处理银行卡号变化
const handleBankCardChange = (
value: string,
supplierVO: BusinessAPI.OrderSupplier,
) => {
setSupplierVO({
...supplierVO,
bankCard: value,
});
const id = supplierVO.orderSupplierId;
// 校验并更新错误状态
const isValid = validateBankCard(value);
setBankCardError((prev) => ({
...prev,
[id]: !isValid && value !== "",
}));
if (isValid) {
setBankCardError((prev) => ({
...prev,
[id]: false,
}));
}
};
// 处理银行卡号失焦
const handleBankCardBlur = (value: string, id: any) => {
if (value && !validateBankCard(value)) {
setBankCardError((prev) => ({
...prev,
[id]: true,
}));
Toast.show("toast", {
icon: "fail",
title: "提示",
content: "请输入正确的银行卡号",
});
} else {
setBankCardError((prev) => ({
...prev,
[id]: false,
}));
}
};
// 处理手机号变化
const handlePhoneChange = (
value: string,
supplierVO: BusinessAPI.OrderSupplier,
) => {
setSupplierVO({
...supplierVO,
phone: value,
});
const id = supplierVO.orderSupplierId;
// 校验并更新错误状态
const isValid = validatePhone(value);
setPhoneError((prev) => ({
...prev,
[id]: !isValid && value !== "",
}));
if (isValid) {
setPhoneError((prev) => ({
...prev,
[id]: false,
}));
}
};
// 处理手机号失焦
const handlePhoneBlur = (value: string, id: any) => {
if (value && !validatePhone(value)) {
setPhoneError((prev) => ({
...prev,
[id]: true,
}));
Toast.show("toast", {
icon: "fail",
title: "提示",
content: "请输入正确的手机号",
});
} else {
setPhoneError((prev) => ({
...prev,
[id]: false,
}));
}
};
// 处理删除瓜农确认
const handleRemoveConfirm = (supplierVO: BusinessAPI.OrderSupplier) => {
Dialog.open("dialog", {
title: "移除瓜农",
content: `确定要移除"${supplierVO.name || "这个瓜农"}"吗?移除后如果需要可以重新添加。`,
onConfirm: () => {
onRemove(supplierVO);
Dialog.close("dialog");
},
onCancel: () => {
Dialog.close("dialog");
},
});
};
console.log("supplierVO", supplierVO);
if (!supplierVO) {
return;
}
return (
<View
className="flex flex-1 flex-col gap-2.5 p-2.5"
style={{
//@ts-ignore
"--nutui-input-padding": 0,
}}
>
{/* 功能提醒 */}
<View className="flex items-center rounded-lg border border-blue-200 bg-blue-50 p-2.5">
<Icon
className={"mr-1"}
name="circle-info"
color={"var(--color-blue-700)"}
size={18}
/>
<View className={"text-sm text-blue-700"}>
</View>
</View>
{/* 只有最后一个瓜农才显示是否为最后一个瓜农的选项和添加按钮 */}
{isLast && (
<View className="rounded-lg bg-white p-2.5 shadow-sm">
<View className="flex items-center justify-between">
{supplierCount > 1 ? (
<View className="text-sm"></View>
) : (
<View className="text-sm"></View>
)}
<View className="text-neutral-darkest text-sm font-medium">
<Radio.Group
direction="horizontal"
value={
supplierVO.isLast === true
? "true"
: supplierVO.isLast === false
? "false"
: undefined
}
onChange={(value) => {
// 清除错误状态
setIsLastFarmerError((prev) => ({
...prev,
[supplierVO.orderSupplierId]: false,
}));
// 根据用户选择设置是否为最后一个瓜农
const isLastValue =
value === "true"
? true
: value === "false"
? false
: undefined;
setSupplierVO({
...supplierVO,
// @ts-ignore
isLast: isLastValue,
});
}}
>
<Radio value="false"></Radio>
<Radio value="true"></Radio>
</Radio.Group>
</View>
</View>
{isLastFarmerError[supplierVO.orderSupplierId] && (
<View className="mt-1 text-xs text-red-500">
</View>
)}
</View>
)}
{/* 快捷工具 */}
<View className="flex gap-2">
<View className={"flex-1"}>
<Button
block
icon={<Icon name={"id-card"} size={28} color={"white"} />}
type={"primary"}
size={"xlarge"}
className="bg-primary flex flex-1 items-center justify-center text-white"
onClick={() => {
Taro.navigateTo({
url: buildUrl("/pages/public/camera/ocr", {
type: "idcard",
}),
complete: () => {
Taro.eventCenter.on("ocr", (res) => {
console.log("识别结果为:", res.result);
setSupplierVO({
...supplierVO,
name: res.result.name,
idCard: res.result.idCard,
});
Taro.eventCenter.off("ocr");
});
},
});
}}
>
<View></View>
</Button>
</View>
<View className={"flex-1"}>
<Button
block
icon={<Icon name={"credit-card"} size={28} color={"white"} />}
type={"primary"}
size={"xlarge"}
className="bg-primary flex flex-1 items-center justify-center text-white"
onClick={() => {
Taro.navigateTo({
url: buildUrl("/pages/public/camera/ocr", {
type: "bankcard",
}),
complete: () => {
Taro.eventCenter.on("ocr", (res) => {
console.log("识别结果为:", res.result);
setSupplierVO({
...supplierVO,
bankCard: res.result.number,
});
Taro.eventCenter.off("ocr");
});
},
});
}}
>
<View></View>
</Button>
</View>
</View>
{/* 瓜农信息 */}
<View className="rounded-lg bg-white p-2.5 shadow-sm">
<View className="mb-2.5">
<View className="flex items-center justify-between">
<View className={"text-primary text-sm font-bold"}>
{supplierVO.name || "瓜农"}
</View>
{supplierCount > 1 && isLast && (
<View
className="cursor-pointer text-sm text-red-500"
onClick={() => handleRemoveConfirm(supplierVO)}
>
</View>
)}
</View>
</View>
<View className="mb-2.5">
<View className="mb-1 flex flex-row justify-between">
<View className={"block text-sm font-normal text-[#000000]"}>
</View>
<SupplierPicker
trigger={
<View className="flex items-center">
<Icon
name="address-book"
size={16}
color="var(--color-primary)"
/>
<Text className={"text-primary ml-1 text-sm font-bold"}>
</Text>
</View>
}
onFinish={(supplierVO1) => {
// 检查是否已经选择了该瓜农
if (
selectedSupplierIds &&
selectedSupplierIds.includes(supplierVO1.supplierId!)
) {
Toast.show("toast", {
icon: "fail",
title: "提示",
content: "该瓜农已被选择,请选择其他瓜农",
});
return;
}
setSupplierVO({
...supplierVO,
...supplierVO1,
});
if (supplierVO1.wechatQr) {
setPicList([
{
url: supplierVO1.wechatQr,
name: "wechat-qrcode",
status: "success",
},
]);
}
// 清除所有错误状态
setNameError((prev) => ({
...prev,
[supplierVO.orderSupplierId]: false,
}));
setPhoneError((prev) => ({
...prev,
[supplierVO.orderSupplierId]: false,
}));
setBankCardError((prev) => ({
...prev,
[supplierVO.orderSupplierId]: false,
}));
setIdCardError((prev) => ({
...prev,
[supplierVO.orderSupplierId]: false,
}));
}}
/>
</View>
<View
className={`flex h-10 w-full items-center rounded-md ${nameError[supplierVO.orderSupplierId] ? "border-4 border-red-500" : "border-4 border-gray-300"}`}
>
<Icon name="user" size={16} color="#999" className="mx-2" />
<Input
type="text"
placeholder="请输入姓名"
value={supplierVO.name}
onChange={(value) => handleNameChange(value, supplierVO)}
onBlur={() =>
handleNameBlur(
supplierVO?.name || "",
supplierVO.orderSupplierId,
)
}
className="flex-1"
/>
</View>
{nameError[supplierVO.orderSupplierId] && (
<View className="mt-1 text-xs text-red-500">
{`姓名"${supplierVO.name}"至少2个字符`}
</View>
)}
</View>
<View className="mb-2.5">
<View className="mb-1 block text-sm font-normal text-[#000000]">
</View>
<View
className={`flex h-10 w-full items-center rounded-md ${idCardError[supplierVO.orderSupplierId] ? "border-4 border-red-500" : "border-4 border-gray-300"}`}
>
<Icon name="id-card" size={16} color="#999" className="mx-2" />
<Input
type="idcard"
placeholder="请输入身份证号"
value={supplierVO.idCard}
onChange={(value) => handleIdCardChange(value, supplierVO)}
onBlur={() =>
handleIdCardBlur(
supplierVO?.idCard || "",
supplierVO.orderSupplierId,
)
}
className="flex-1"
/>
</View>
{idCardError[supplierVO.orderSupplierId] && (
<View className="mt-1 text-xs text-red-500">
</View>
)}
</View>
<View className="mb-2.5">
<View className="mb-1 block text-sm font-normal text-[#000000]">
</View>
<View
className={`flex h-10 w-full items-center rounded-md ${bankCardError[supplierVO.orderSupplierId] ? "border-4 border-red-500" : "border-4 border-gray-300"}`}
>
<Icon
name="credit-card"
size={16}
color="#999"
className="mx-2"
/>
<Input
type="digit"
placeholder="请输入银行卡号"
value={supplierVO.bankCard}
onChange={(value) => handleBankCardChange(value, supplierVO)}
onBlur={() =>
handleBankCardBlur(
supplierVO?.bankCard || "",
supplierVO.orderSupplierId,
)
}
className="flex-1"
/>
</View>
{bankCardError[supplierVO.orderSupplierId] && (
<View className="mt-1 text-xs text-red-500">
</View>
)}
</View>
<View className="mb-2.5">
<View className="mb-1 block text-sm font-normal text-[#000000]">
</View>
<View
className={`flex h-10 w-full items-center rounded-md ${phoneError[supplierVO.orderSupplierId] ? "border-4 border-red-500" : "border-4 border-gray-300"}`}
>
<Icon name="phone" size={16} color="#999" className="mx-2" />
<Input
type="tel"
placeholder="请输入手机号码"
value={supplierVO.phone}
onChange={(value) => handlePhoneChange(value, supplierVO)}
onBlur={() =>
handlePhoneBlur(
supplierVO?.phone || "",
supplierVO.orderSupplierId,
)
}
className="flex-1"
/>
</View>
{phoneError[supplierVO.orderSupplierId] && (
<View className="mt-1 text-xs text-red-500">
</View>
)}
</View>
</View>
{/* 若瓜农无法开发票,则可打款到微信 */}
<View className="rounded-lg bg-white p-2.5 shadow-sm">
<View className={`flex w-full border-gray-300`}>
<Uploader
className={"w-full"}
value={picList}
onChange={handleWechatQrChange}
sourceType={["album", "camera"]}
uploadIcon={<Icon name={"camera"} size={36} />}
uploadLabel={
<View className={"flex flex-col items-center"}>
<View className="text-sm"></View>
<View className="mt-1 text-xs text-gray-400">
</View>
<View className="mt-1 text-xs text-gray-400"></View>
</View>
}
maxCount={1}
//@ts-ignore
upload={uploadFile}
multiple
/>
</View>
</View>
</View>
);
},
);