Express.js 8.52 KB
const LOGISTICS_STATUS = {
  1000: "待揽件",
  1001: "已揽件",
  1002: "运输中",
  1003: "已签收",
  1004: "拒收",
  // 1005: "派件中", // 新增状态
};

// 状态判断规则配置
const STATUS_RULES = [
  {
    keywords: ["已签收", "签收人"],
    status: 1003,
    statusDesc: "已签收",
  },
  {
    keywords: ["拒收", "退回"],
    status: 1004,
    statusDesc: "拒收",
  },
  {
    keywords: [
      "正在派送",
      "正在派件",
      "派送中",
      "正在为您派送",
      "派送",
      "派件",
      "派件中",
    ],
    status: 1002,
    statusDesc: "派件中",
  },
  {
    keywords: ["驿站", "暂存", "已发往", "转运中心", "发往", "到达", "已到达"],
    status: 1002,
    statusDesc: "运输中",
  },
  {
    keywords: ["已收件", "揽收", "揽件"],
    status: 1001,
    statusDesc: "已揽件",
  },
];
Component({
  properties: {
    expressInfo: {
      type: Object,
      value: {},
      observer: function (val) {
        this.processExpressInfo(val);
      },
    },
    showAll: {
      type: Boolean,
      value: false,
    },
    receiver: {
      type: String,
      value: "",
    },
  },

  data: {
    logisticsData: {},
  },

  methods: {
    /**
     * 根据内容判断物流状态
     * @param {string} content - 物流轨迹内容
     * @returns {Object} 包含status和statusDesc的对象
     */
    _determineStatusByContent(content) {
      if (!content || typeof content !== "string") {
        return { status: 1000, statusDesc: "待揽件" };
      }
      for (const rule of STATUS_RULES) {
        if (rule.keywords.some((keyword) => content.includes(keyword))) {
          return {
            status: rule.status,
            statusDesc: rule.statusDesc,
          };
        }
      }

      return { status: 1000, statusDesc: "待揽件" };
    },

    /**
     * 根据物流状态过滤轨迹
     * @param {number} trackStatus - 轨迹状态
     * @param {number} logisticsStatus - 整体物流状态
     * @returns {boolean} 是否显示该轨迹
     */
    _shouldShowTrack(trackStatus, logisticsStatus) {
      return true;
      const statusThresholds = {
        1000: 1000, // 待揽件 - 只显示收件相关记录
        1001: 1001, // 已揽件 - 显示揽件和发往记录
        1002: 1002, // 运输中 - 显示所有运输中的记录
        1003: 1003, // 已签收 - 显示所有记录
        1004: 1004, // 拒收 - 显示所有记录
        1005: 1005, // 正在派件 - 显示所有记录
      };
      const threshold = statusThresholds[logisticsStatus];
      return threshold !== undefined ? trackStatus <= threshold : true;
    },

    /**
     * 处理轨迹数据
     * @param {Array} shipmentTrack - 原始轨迹数据
     * @param {number} logisticsStatus - 物流状态
     * @returns {Array} 处理后的轨迹数据
     */
    processShipmentTrack(shipmentTrack, logisticsStatus) {
      if (!Array.isArray(shipmentTrack) || shipmentTrack.length === 0) {
        return [];
      }
      let lastStatus = null;
      // 第一步:处理所有轨迹数据,获取状态和描述
      const tracks = shipmentTrack
        .reverse()
        .map((track, index) => {
          const { content = "", operateTime = "" } = track;
          // 根据内容判断状态
          let { status, statusDesc } = this._determineStatusByContent(content);
          let type = "normal";
          if (status === 1002 && statusDesc === "运输中") {
            if (status === lastStatus) {
              statusDesc = "";
              type = "small";
            } else {
              lastStatus = status;
            }
          }
          // 提取手机号码
          // const phoneRegex = /(1[3-9]\d{9}|0\d{2,3}[-]?\d{7,8})/g;
          // const phones = [];
          // let match;
          // while ((match = phoneRegex.exec(content)) !== null) {
          //   phones.push(match[1]);
          // }
          // // 高亮手机号码和座机号码
          // const highlightedContent = content.replace(
          //   phoneRegex,
          //   '<span style="color: #fb8a32;" bindtap="makePhoneCall" data-phone="$1">$1</span>'
          // );

          return {
            ...track,
            content,
            // phones: phones, // 添加提取的手机号码数组
            operateTime,
            type,
            status,
            statusDesc,
          };
        })
        .filter((track) =>
          this._shouldShowTrack(track.status, logisticsStatus)
        );

      // 使用数组反转实现同一状态只显示最后一个的逻辑
      // 1. 先反转数组
      // const reversedTracks = [...tracks].reverse();

      // // 2. 应用第一次出现状态显示,后续相同状态不显示的逻辑
      // let lastStatus = null;
      // const processedReversedTracks = reversedTracks.map((track) => {
      //   const result = { ...track };
      //   if (track.status === lastStatus && track.status === 1002) {
      //     result.statusDesc = "";
      //     result.type = "small";
      //   } else {
      //     lastStatus = track.status;
      //   }
      //   return result;
      // });
      // 3. 再次反转数组,恢复原始顺序
      return tracks.reverse();
    },

    /**
     * 处理快递信息
     * @param {Object} expressInfo - 快递信息对象
     */
    processExpressInfo(expressInfo) {
      if (!expressInfo || typeof expressInfo !== "object") {
        wx.p.sentry.reportError(`快递信息异常:expressInfo参数无效`, {
          expressInfo,
        });
        console.warn("Express组件: expressInfo参数无效");
        return;
      }
      const {
        logisticsStatus,
        shipmentTrack,
        expressName,
        shipmentCode,
        ...rest
      } = expressInfo;

      // 验证必要字段
      if (typeof logisticsStatus !== "number") {
        wx.p.sentry.reportError(`快递信息异常:logisticsStatus字段无效`, {
          expressInfo,
        });
        console.warn("Express组件: logisticsStatus字段无效");
        return;
      }

      // 创建默认轨迹对象
      const statusDesc = LOGISTICS_STATUS[logisticsStatus] || "未知状态";
      let defaultContent = "";

      switch (logisticsStatus) {
        case 1000:
          defaultContent = `物流单号已生成,等待快递揽收。${
            expressName ? `物流公司:${expressName}` : ""
          }${shipmentCode ? `,运单号:${shipmentCode}` : ""}`;
          break;
        case 1001:
          defaultContent = `包裹已被${
            expressName || "快递公司"
          }揽收,即将开始运输。`;
          break;
        case 1002:
          defaultContent = `包裹正在运输中,请耐心等待。`;
          break;
        case 1003:
          defaultContent = `包裹已签收,感谢您的购买。`;
          break;
        case 1004:
          defaultContent = `包裹被拒收,请联系客服处理。`;
          break;
        default:
          defaultContent = `包裹状态更新中,请稍后查看。`;
      }

      // 创建默认轨迹节点
      const defaultTrackNode = {
        content: defaultContent,
        operateTime: "",
        type: "normal",
        status: logisticsStatus,
        statusDesc: statusDesc,
      };

      // 处理正常轨迹数据
      let normalTrackNodes = [];
      if (
        shipmentTrack &&
        Array.isArray(shipmentTrack) &&
        shipmentTrack.length > 0
      ) {
        normalTrackNodes = this.processShipmentTrack(
          shipmentTrack,
          logisticsStatus
        );
      } else {
        normalTrackNodes = [defaultTrackNode];
      }
      // 创建收货地址特殊节点
      const receiverNode = {
        content: this.data.receiver,
        operateTime: "",
        type: "special",
        statusDesc: "收货地址",
      };

      // 合并节点:收货地址节点 + 默认轨迹节点 + 正常轨迹节点(如果有)
      // 如果有正常轨迹节点,则将默认轨迹节点放在最后作为补充
      // 如果没有正常轨迹节点,则只使用默认轨迹节点
      let combinedTrack = [...normalTrackNodes, receiverNode];
      if (!this.data.showAll) {
        combinedTrack = combinedTrack.slice(0, 2);
      }
      combinedTrack[0].type = "active";
      const logisticsData = {
        ...rest,
        expressName,
        shipmentCode,
        logisticsStatus,
        logisticsStatusDesc: LOGISTICS_STATUS[logisticsStatus] || "未知状态",
        shipmentTrack: combinedTrack,
      };

      this.setData({
        logisticsData,
      });
    },
  },
});