import hocAuth from "@/hocs/auth"; import { CommonComponent } from "@/types/typings"; import { useEffect, useRef, useState } from "react"; import { View } from "@tarojs/components"; import { Button, Dialog, SafeArea, Step, Steps } from "@nutui/nutui-react-taro"; import orderShip from "@/constant/orderShip"; import Taro from "@tarojs/taro"; import classNames from "classnames"; import { business, poster } from "@/services"; import { buildUrl, convertOrderShipVOToExamplesFormat, PdfTemplate, } from "@/utils"; import { DeliveryStep1Form, DeliveryStep1FormRef, DeliveryStep2Preview, DeliveryStep3Success, } from "@/components"; import dayjs from "dayjs"; // 特殊处理:其他费用要实时获取费用项目 const updateOtherFeesModule = async ( module: any, orderVO: BusinessAPI.OrderVO, ) => { const { data: { data }, } = await business.costItem.listCostItem({ costItemListQry: { status: true, }, }); const costItems = data?.filter((item) => item.type !== "ARTIFICIAL_TYPE") || []; console.log("module", module); return { ...module, config: { ...module.config, feeItems: orderVO.orderCostList?.filter( (item) => item.type !== "ARTIFICIAL_TYPE", ), feeLabels: costItems.reduce((acc: any, item: any) => { acc[item.itemId] = item.name; return acc; }, {}), }, schemas: [ { title: "显示配置", valueType: "group", columns: [ { dataIndex: "feeItems", title: "显示费用项目", valueType: "checkbox", valueEnum: costItems.reduce((acc: any, item: any) => { acc[item.itemId] = item.name; return acc; }, {}), }, ], }, ], }; }; export default hocAuth(function Page(props: CommonComponent) { const { router, setLoading } = props; const orderShipId = router.params .orderShipId as BusinessAPI.OrderShipVO["orderShipId"]; const orderId = router.params.orderId as BusinessAPI.OrderShipVO["orderId"]; const [step, setStep] = useState(1); const [moduleList, setModuleList] = useState([]); const [orderVO, setOrderVO] = useState(); const [orderShipVO, setOrderShipVO] = useState(); const [deliveryTemplate, setDeliveryTemplate] = useState(); const [pdfUrl, setPdfUrl] = useState(); const [picUrl, setPicUrl] = useState(); const step1FormRef = useRef(null); const init = async ( orderId: BusinessAPI.OrderShipVO["orderId"], orderShipId: BusinessAPI.OrderShipVO["orderShipId"], ) => { setLoading(true); const { data } = await business.order.showOrder({ orderShowQry: { orderId, orderShipId, }, }); const orderVO = data.data; if (orderVO) { setOrderVO(orderVO); const orderShip = orderVO.orderShipList[0]; setOrderShipVO(orderShip); if (orderShip.pdfUrl || orderShip.picUrl) { setStep(3); setPdfUrl(orderShip.pdfUrl); setPicUrl(orderShip.picUrl); } const { data } = await business.dealer.showDealer({ dealerShowQry: { dealerId: orderShip?.dealerId, }, }); const deliveryTemplate = data.data?.deliveryTemplate!; setDeliveryTemplate(deliveryTemplate); } setLoading(false); }; useEffect(() => { if (orderId && orderShipId) { init(orderId, orderShipId).then(); } }, [orderId, orderShipId]); const refresh = async ( deliveryTemplate: string, orderVO: BusinessAPI.OrderVO, ) => { const template = JSON.parse(deliveryTemplate); // 将 orderShipVO 转换为 examples 的数据格式,然后再替换 moduleList 里面的 config 数据 const convertedData = convertOrderShipVOToExamplesFormat(orderVO); const updatedTemplate = await updateTemplateConfig( template, convertedData, orderVO, ); setModuleList(updatedTemplate); }; useEffect(() => { if (deliveryTemplate && orderVO) { refresh(deliveryTemplate, orderVO).then(); } }, [orderVO, deliveryTemplate]); // 更新模板配置 const updateTemplateConfig = async ( template: any[], data: any, orderVO: BusinessAPI.OrderVO, ) => { let templateList: any[] = []; template.map(async (module: any) => { let newModule = { ...module }; if (data[module.type]) { newModule.config = { ...module.config, ...data[module.type] }; } // 特殊处理: otherFees 要更新为最新的数据 if (module.type === "otherFees") { newModule = await updateOtherFeesModule(module, orderVO); } templateList.push(newModule); }); return templateList; }; // 预览确认 const previewAndConfirm = () => { if (step === 2) { // 显示确认对话框 Dialog.open("dialog", { title: "生成发货单据", content: "即将生成发货单据,请确认发货单据是否正确。", confirmText: "确认生成", cancelText: "取消", onConfirm: async () => { // 截图预览内容 await capturePreview(); Dialog.close("dialog"); // 进入第三步 setStep(3); }, onCancel: () => { Dialog.close("dialog"); }, }); return; } if (step === 1) { // 调用 Step1Form 的表单校验方法 if (step1FormRef.current && step1FormRef.current.validateForm()) { setStep(2); } return; } if (step < 3) { setStep(step + 1); } }; // 生成发货单据 const generateShippingDocument = async () => { // 显示确认对话框 Dialog.open("dialog", { title: "生成发货单据", content: "即将生成发货单据,请确认发货单据是否正确。", confirmText: "确认生成", cancelText: "取消", onConfirm: async () => { // 截图预览内容 await capturePreview(); Dialog.close("dialog"); // 进入第三步 setStep(3); }, onCancel: () => { Dialog.close("dialog"); }, }); }; // 截图预览内容 const capturePreview = async () => { const template = new PdfTemplate(moduleList); const { data: { data: pdfData }, } = await poster.pdf.postApiV1Pdf({ html: template.generateHtmlString(), }); const { data: { data: picData }, } = await poster.poster.postApiV1Poster({ html: template.generateHtmlString(), //@ts-ignore format: "a4", }); setPdfUrl(pdfData?.path); setPicUrl(picData?.path); const orderShip = orderVO?.orderShipList[0]; // 存储 至 orderShip previewUrl if (orderShip) { let formData: BusinessAPI.OrderShipGenerateDocumentCmd = { orderShipId: orderShip?.orderShipId, pdfUrl: pdfData?.path as string, picUrl: picData?.path as string, }; // 检查各模块中的必填字段是否已填写 for (const module of moduleList) { const contentSchema = module.schemas.find( (schema) => schema.title === "内容配置", ); if ( !contentSchema || !contentSchema.columns || contentSchema.columns.length === 0 ) { continue; } for (const column of contentSchema.columns) { // 检查西瓜品级字段是否开启且已填写 if (column.dataIndex === "requiredWatermelonGrade") { formData.watermelonGrade = orderShip?.watermelonGrade; } // 检查发货地字段是否开启且已填写 else if (column.dataIndex === "requiredShippingFrom") { formData.shippingAddress = orderShip?.shippingAddress; } // 检查预计到仓时间字段是否开启且已填写 else if (column.dataIndex === "requiredEstimatedArrivalTime") { formData.estimatedArrivalDate = dayjs( orderShip?.estimatedArrivalDate, ).format("YYYY-MM-DD"); } // 检查备注字段是否开启且已填写 else if (column.dataIndex === "requiredRemarks") { formData.remark = orderShip?.remark; } // 检查品级字段是否开启且已填写 else if (column.dataIndex === "requiredGrade") { formData.orderShipItemList = orderShip?.orderShipItemList; } } } business.orderShip.generateDocumentOrderShip(formData).then(); } }; // 返回上一步 const goToPrevStep = () => { if (step > 0) { setStep(step - 1); } }; // 重置表单 const resetForm = () => { setStep(1); // 重新加载初始数据 if (orderId && orderShipId) { init(orderId, orderShipId).then(); } }; // 重新生成单据 const regenerateDocument = () => { setStep(1); }; return ( <> {orderShip.steps.map((item, index) => ( ))} {step === 1 && ( )} {step === 2 && } {step === 3 && pdfUrl && ( )} {step == 1 && ( )} {step == 1 && ( )} {step == 2 && ( )} {step == 2 && ( )} {step == 3 && ( )} {step == 3 && ( )} ); });