Express.js 4.85 KB
const LOGISTICS_STATUS = {
  1000: "待揽件",
  1001: "已揽件",
  1002: "运输中",
  1003: "已签收",
  1004: "拒收",
};

// 状态判断规则配置
const STATUS_RULES = [
  {
    keywords: ["已签收", "签收人"],
    status: 1003,
    statusDesc: "已签收",
  },
  {
    keywords: ["拒收", "退回"],
    status: 1004,
    statusDesc: "拒收",
  },
  {
    keywords: ["派件", "驿站", "暂存"],
    status: 1002,
    statusDesc: "运输中",
  },
  {
    keywords: ["转运中心", "发往", "到达"],
    status: 1002,
    statusDesc: "运输中",
  },
  {
    keywords: ["已收件", "揽收"],
    status: 1001,
    statusDesc: "已揽件",
  },
  {
    keywords: ["已发往"],
    status: 1001,
    statusDesc: "已揽件",
  },
];

Component({
  properties: {
    expressInfo: {
      type: Object,
      value: {},
      observer: function (val) {
        if (!val) return;
        this.processExpressInfo(val);
      },
    },
    showAll: {
      type: Boolean,
      value: false,
    },
  },

  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) {
      const statusThresholds = {
        1000: 1000, // 待揽件 - 只显示收件相关记录
        1001: 1001, // 已揽件 - 显示揽件和发往记录
        1002: 1002, // 运输中 - 显示所有运输中的记录
        1003: 1003, // 已签收 - 显示所有记录
        1004: 1004, // 拒收 - 显示所有记录
      };

      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 [];
      }

      // 根据showAll决定显示数量
      const tracksToProcess = this.data.showAll
        ? shipmentTrack
        : shipmentTrack.slice(0, 2);

      return tracksToProcess
        .map((track, index) => {
          const { content = "", operateTime = "" } = track;
          const isLast = index === 0;

          // 根据内容判断状态
          const { status, statusDesc: initialStatusDesc } =
            this._determineStatusByContent(content);

          // 根据整体物流状态进行最终调整
          let finalStatusDesc = initialStatusDesc;
          if (logisticsStatus < status) {
            finalStatusDesc = `${LOGISTICS_STATUS[logisticsStatus]}(进行中)`;
          }

          return {
            ...track,
            content,
            operateTime,
            type: isLast ? "active" : "",
            status,
            statusDesc: finalStatusDesc,
          };
        })
        .filter((track) =>
          this._shouldShowTrack(track.status, logisticsStatus)
        );
    },

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

      const { logisticsStatus, shipmentTrack, ...rest } = expressInfo;

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

      const processedTrack = this.processShipmentTrack(
        shipmentTrack,
        logisticsStatus
      );

      const logisticsData = {
        ...rest,
        logisticsStatus,
        logisticsStatusDesc: LOGISTICS_STATUS[logisticsStatus] || "未知状态",
        shipmentTrack: processedTrack,
      };

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