Express.js
8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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,
});
},
},
});