Commit 0b12bed35e6e6b7107ce3c01fd9fc95461b464c0

Authored by 陈鑫涛
1 parent f0ea92b5

解决重复查询

src/components/QuickQuote/index.jsx
... ... @@ -1574,53 +1574,66 @@ const ContentComponent = props => {
1574 1574 } else if (!sAllPartsName) {
1575 1575 sAllPartsName = showName;
1576 1576 }
1577   - // 默认选中盒型 如果配置了默认值
1578   - const sAllPartsNameDefaultRef = useRef(sAllPartsNameDefault);
1579   -
  1577 + const hasAutoSetDefault = useRef(false);
1580 1578 useEffect(() => {
1581   - sAllPartsNameDefaultRef.current = sAllPartsNameDefault;
1582   - }, [sAllPartsNameDefault]);
1583   -
  1579 + hasAutoSetDefault.current = false;
  1580 + }, [selectedNode]);
  1581 + // 2. 监听两个依赖:sAllPartsNameDefault 与 slaveData
1584 1582 useEffect(() => {
1585   - if (sAllPartsNameDefaultRef.current && props.state.slaveData) {
1586   - const boxModelList = sAllPartsName.split(",");
1587   - const slaveData = props.state.slaveData;
1588   - if (slaveData.length === boxModelList.length) {
1589   - const sAllPartsNameDefaultList = JSON.parse(sAllPartsNameDefaultRef.current);
1590   - const getSqlDropDownData = async ({ sId, sSqlCondition = {} }, cb) => {
1591   - const url = `${commonConfig.server_host}business/getSelectLimit/${sId}`;
1592   - const body = {
1593   - sKeyUpFilterName: "",
1594   - pageNum: 1,
1595   - pageSize: 1000,
1596   - sSqlCondition,
1597   - };
1598   - const retrunData = await commonServices.postValueService(props.app.token, body, url);
1599   - const dropDownData = retrunData.data?.dataset?.rows;
1600   - if (dropDownData && sAllPartsNameDefaultList.length) {
1601   - sAllPartsNameDefaultList.forEach((item, index) => {
1602   - const sPartNameList = item.sPartName?.split(",");
1603   - if (sPartNameList && sPartNameList.length) {
1604   - sPartNameList.forEach(sPartName => {
1605   - const box = dropDownData.find(box => box.sId === item.sBoxId); // Define 'box' here
1606   - const boxData = dropDownData.map((itemBox, indexBox) => ({
1607   - ...itemBox,
1608   - iOrder: indexBox + 1,
1609   - }));
1610   - if (box) { // Check if 'box' exists before using it
1611   - props.onDataChange("slaveUp" + sPartName, "sName", { sName: box.sName }, box.sId, boxData);
1612   - }
1613   - });
1614   - }
1615   - });
1616   - }
  1583 + if (
  1584 + !hasAutoSetDefault.current && // 尚未执行
  1585 + selectedNode?.sAllPartsNameDefault?.length && // 有默认值
  1586 + props.state.slaveData?.length // 子表数据已就位
  1587 + ) {
  1588 + const boxModelList = sAllPartsName.split(',');
  1589 + const { slaveData } = props.state;
  1590 +
  1591 + // 数量对不上不处理
  1592 + if (slaveData.length !== boxModelList.length) return;
  1593 +
  1594 + // 标记为已执行,后续不再进来
  1595 + hasAutoSetDefault.current = true;
  1596 +
  1597 + // 取默认值列表(深拷贝,避免后续 JSON.parse 出错)
  1598 + const sAllPartsNameDefaultList = JSON.parse(
  1599 + sAllPartsNameDefault
  1600 + );
  1601 +
  1602 + const getSqlDropDownData = async ({ sId, sSqlCondition = {} }, cb) => {
  1603 + const url = `${commonConfig.server_host}business/getSelectLimit/${sId}`;
  1604 + const body = {
  1605 + sKeyUpFilterName: "",
  1606 + pageNum: 1,
  1607 + pageSize: 1000,
  1608 + sSqlCondition,
1617 1609 };
1618   - getSqlDropDownData(
1619   - { sId: '17428091410008594700322758474000', sSqlCondition: { sProductClassifyId: selectedNode.sId } }
1620   - );
1621   - }
  1610 + const retrunData = await commonServices.postValueService(props.app.token, body, url);
  1611 + const dropDownData = retrunData.data?.dataset?.rows;
  1612 + if (dropDownData && sAllPartsNameDefaultList.length) {
  1613 + sAllPartsNameDefaultList.forEach((item, index) => {
  1614 + const sPartNameList = item.sPartName?.split(",");
  1615 + if (sPartNameList && sPartNameList.length) {
  1616 + sPartNameList.forEach(sPartName => {
  1617 + const box = dropDownData.find(box => box.sId === item.sBoxId); // Define 'box' here
  1618 + const boxData = dropDownData.map((itemBox, indexBox) => ({
  1619 + ...itemBox,
  1620 + iOrder: indexBox + 1,
  1621 + }));
  1622 + if (box) { // Check if 'box' exists before using it
  1623 + props.onDataChange("slaveUp" + sPartName, "sName", { sName: box.sName }, box.sId, boxData);
  1624 + }
  1625 + });
  1626 + }
  1627 + });
  1628 + }
  1629 + };
  1630 +
  1631 + getSqlDropDownData({
  1632 + sId: '17428091410008594700322758474000',
  1633 + sSqlCondition: { sProductClassifyId: selectedNode.sId },
  1634 + });
1622 1635 }
1623   - }, [sAllPartsNameDefaultRef.current, props.state.slaveData]);
  1636 + }, [selectedNode?.sAllPartsNameDefault, props.state.slaveData]);
1624 1637  
1625 1638 const boxModelList = sAllPartsName.split(",");
1626 1639 const [activeKey, setActiveKey] = useState(0);
... ... @@ -2543,10 +2556,10 @@ const BoxComponent = props => {
2543 2556 { src: getImageUrl(removeExtension(slaveRowData.sPackDetailPathUpLoad)), alt: "展开图" },
2544 2557 { src: getImageUrl(removeExtension(slaveRowData.sPackPath)), alt: "立体图" },
2545 2558 ];
2546   - if (!slaveRowData.sPackDetailPathUpLoad) {
2547   - imagesData = [{ src: slaveRowData.sPackPath, alt: "立体图" }];
  2559 + if (!slaveRowData.sPackDetailPathUpLoad) {
  2560 + imagesData = [{ src: getImageUrl(removeExtension(slaveRowData.sPackPath)), alt: "立体图" }];
2548 2561 } else if (!slaveRowData.sPackPath) {
2549   - imagesData = [{ src: slaveRowData.sPackPath, alt: "立体图" }];
  2562 + imagesData = [{ src: getImageUrl(removeExtension(slaveRowData.sPackPath)), alt: "立体图" }];
2550 2563 }
2551 2564 const { activeKey = 0 } = state;
2552 2565 useEffect(() => {
... ...