Skip to content

Commit cadf5f4

Browse files
author
dompling
committed
fix:正则
1 parent 8d7d07a commit cadf5f4

1 file changed

Lines changed: 106 additions & 69 deletions

File tree

message/sms.js

Lines changed: 106 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,132 @@
44
description: 提取快递短信取件码
55
icon: message
66
category: 工具
7-
version: 1.0.2
7+
version: 1.0.3
88
*/
99

1010
function main() {
11-
const messages = sms.read(20);
12-
if (!messages || messages.length === 0) {
13-
console.log("未读取到短信");
14-
return;
15-
}
16-
17-
const patterns = [
18-
{
19-
regex: /(?:|\[\]).*?\s*(\d+).*?\s*(.+?)(?:|$)/,
20-
codeIndex: 1,
21-
locationIndex: 2
22-
},
23-
{
24-
regex: /(?:(.*?)|\[(.*?)\]).*?(?:|)\s*(.+?)(?:|||).*?\s*([A-Za-z0-9-]+?)/,
25-
codeIndex: 4,
26-
locationIndex: 3
11+
try {
12+
const messages = sms.read(20);
13+
if (!messages || messages.length === 0) {
14+
console.log("未读取到短信");
15+
return "未读取到短信";
2716
}
28-
];
2917

30-
const upcomingReminders = reminder.getUpcoming(7) || [];
18+
const patterns = [
19+
{
20+
// 丰巢类
21+
regex: /(?:|\[\]).*?\s*(\d+).*?\s*(.+?)(?:|$)/,
22+
codeIndex: 1,
23+
locationIndex: 2
24+
},
25+
{
26+
// 通用快递类:支持换行、支持"凭xx取件"分两行
27+
regex: /(?:(.*?)|\[(.*?)\]).*?(?:||)\s*(.+?)(?:|||)[\s\S]*?(?:||)\s*([A-Za-z0-9-]+?)\s*(?:|$)/,
28+
codeIndex: 4,
29+
locationIndex: 3
30+
}
31+
];
3132

32-
const STORAGE_KEY = 'sms_processed_codes';
33-
let processedCodes = storage.get(STORAGE_KEY) || [];
33+
const upcomingReminders = reminder.getUpcoming(7) || [];
3434

35-
let processedCount = 0;
36-
let hasNew = false;
35+
// 获取"取件码"列表的 ID(如果存在)
36+
const lists = reminder.getLists() || [];
37+
const pickupList = lists.find(list => list.title === "取件码");
38+
const pickupListId = pickupList ? pickupList.id : null;
3739

38-
for (let i = 0; i < messages.length; i++) {
39-
const msg = messages[i];
40-
let match = null;
41-
let code = null;
42-
let location = null;
40+
const STORAGE_KEY = 'sms_processed_codes';
41+
let processedCodes = storage.get(STORAGE_KEY) || [];
4342

44-
for (const pattern of patterns) {
45-
match = msg.text.match(pattern.regex);
46-
if (match) {
47-
code = match[pattern.codeIndex];
48-
location = match[pattern.locationIndex];
49-
break;
50-
}
51-
}
43+
let processedCount = 0;
44+
let hasNew = false;
5245

53-
if (match && code && location) {
54-
// 唯一标识 (仅使用取件码,防止位置描述不同导致重复)
55-
const uniqueKey = code;
46+
for (let i = 0; i < messages.length; i++) {
47+
const msg = messages[i];
5648

57-
// 查重逻辑 1: 检查持久化缓存 (以及本次运行已处理的)
58-
// 兼容旧的缓存格式 "code|location"
59-
if (processedCodes.some(item => item === uniqueKey || item.startsWith(`${code}|`))) {
60-
console.log(`跳过已缓存的取件码: ${code}`);
61-
continue;
62-
}
49+
// 统一处理文本:把换行、多个空白压成一个空格,增强正则稳定性
50+
const rawText = msg.text || msg.body || "";
51+
const text = rawText.replace(/\s+/g, " ").trim();
52+
53+
let match = null;
54+
let code = null;
55+
let location = null;
6356

64-
const isDuplicateReminder = upcomingReminders.some(r => r.title.includes(code));
65-
if (isDuplicateReminder) {
66-
console.log(`跳过现有提醒事项: ${code}`);
67-
// 即使缓存没有,如果有提醒了,也加入缓存
68-
if (!processedCodes.includes(uniqueKey)) {
69-
processedCodes.push(uniqueKey);
70-
hasNew = true;
57+
for (const pattern of patterns) {
58+
match = text.match(pattern.regex);
59+
if (match) {
60+
code = match[pattern.codeIndex];
61+
location = match[pattern.locationIndex];
62+
break;
7163
}
72-
continue;
7364
}
7465

75-
reminder.createSystemReminder(`取件码: ${code}`, {
76-
notes: `位置: ${location}\n原文: ${msg.text || msg.body}`,
77-
priority: 5,
78-
listTitle: "取件码"
79-
});
66+
if (match && code && location) {
67+
code = String(code).trim();
68+
location = String(location).trim();
8069

81-
notification.send("快递取件提醒", `凭取件码 ${code}${location} 取件`, {});
70+
// 唯一标识:仅使用取件码,避免位置变化导致重复
71+
const uniqueKey = code;
8272

83-
console.log(`已提取取件码: ${code} (${location})`);
73+
// 查重逻辑:检查缓存 / 兼容旧格式 code|location
74+
if (processedCodes.some(item => item === uniqueKey || (typeof item === 'string' && item.startsWith(`${code}|`)))) {
75+
console.log(`跳过已缓存的取件码: ${code}`);
76+
continue;
77+
}
78+
79+
const isDuplicateReminder = upcomingReminders.some(r => {
80+
const title = r && r.title ? String(r.title) : "";
81+
return title.includes(code);
82+
});
83+
84+
if (isDuplicateReminder) {
85+
console.log(`跳过现有提醒事项: ${code}`);
86+
if (!processedCodes.includes(uniqueKey)) {
87+
processedCodes.push(uniqueKey);
88+
storage.set(STORAGE_KEY, processedCodes);
89+
hasNew = true;
90+
}
91+
continue;
92+
}
8493

85-
// 更新缓存
86-
processedCodes.push(uniqueKey);
87-
storage.set(STORAGE_KEY, processedCodes);
88-
hasNew = true;
89-
processedCount++;
94+
// 创建提醒:使用 listId(如果找到"取件码"列表)
95+
const reminderOptions = {
96+
notes: `位置: ${location}\n原文: ${rawText}`,
97+
priority: 5
98+
};
99+
if (pickupListId) {
100+
reminderOptions.listId = pickupListId;
101+
}
102+
103+
const reminderResult = reminder.create(`取件码: ${code}`, reminderOptions);
104+
105+
const notifyResult = notification.send(
106+
"快递取件提醒",
107+
`凭取件码 ${code}${location} 取件`,
108+
{}
109+
);
110+
111+
console.log(`已提取取件码: ${code} (${location})`);
112+
console.log("提醒创建结果:", reminderResult);
113+
console.log("通知发送结果:", notifyResult);
114+
115+
// 更新缓存
116+
processedCodes.push(uniqueKey);
117+
storage.set(STORAGE_KEY, processedCodes);
118+
hasNew = true;
119+
processedCount++;
120+
}
121+
}
122+
123+
if (processedCount === 0) {
124+
console.log("未找到新的取件码短信");
125+
return "未找到新的取件码短信";
90126
}
91-
}
92127

93-
if (processedCount === 0) {
94-
console.log("未找到新的取件码短信");
128+
return `处理完成:${processedCount} 条新取件码`;
129+
} catch (error) {
130+
console.error("脚本执行失败:", error);
131+
return `脚本执行失败:${error}`;
95132
}
96133
}
97134

98-
main();
135+
return main();

0 commit comments

Comments
 (0)