|
| 1 | +/* |
| 2 | + id: ExpressSMS |
| 3 | + name: ExpressSMS |
| 4 | + description: 提取快递短信取件码 |
| 5 | + icon: terminal |
| 6 | + category: 工具 |
| 7 | + version: 1.0.0 |
| 8 | + */ |
| 9 | + |
| 10 | +function main() { |
| 11 | + if (!sms.checkAccess()) { |
| 12 | + console.error("没有短信访问权限"); |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + const messages = sms.read(20); |
| 17 | + if (!messages || messages.length === 0) { |
| 18 | + console.log("未读取到短信"); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const patterns = [ |
| 23 | + { |
| 24 | + regex: /(?:【丰巢】|\[丰巢\]).*?取件码\s*(\d+).*?至\s*(.+?)(?:取件|$)/, |
| 25 | + codeIndex: 1, |
| 26 | + locationIndex: 2 |
| 27 | + }, |
| 28 | + { |
| 29 | + regex: /(?:【(.*?)】|\[(.*?)\]).*?(?:已到|至)\s*(.+?)(?:,|。|、|请).*?凭\s*([A-Za-z0-9-]+?)取件/, |
| 30 | + codeIndex: 4, |
| 31 | + locationIndex: 3 |
| 32 | + } |
| 33 | + ]; |
| 34 | + |
| 35 | + const upcomingReminders = reminder.getUpcoming(7) || []; |
| 36 | + |
| 37 | + const STORAGE_KEY = 'sms_processed_codes'; |
| 38 | + let processedCodes = storage.get(STORAGE_KEY) || []; |
| 39 | + |
| 40 | + let processedCount = 0; |
| 41 | + let hasNew = false; |
| 42 | + |
| 43 | + for (let i = 0; i < messages.length; i++) { |
| 44 | + const msg = messages[i]; |
| 45 | + let match = null; |
| 46 | + let code = null; |
| 47 | + let location = null; |
| 48 | + |
| 49 | + for (const pattern of patterns) { |
| 50 | + match = msg.text.match(pattern.regex); |
| 51 | + if (match) { |
| 52 | + code = match[pattern.codeIndex]; |
| 53 | + location = match[pattern.locationIndex]; |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (match && code && location) { |
| 59 | + // 唯一标识 (仅使用取件码,防止位置描述不同导致重复) |
| 60 | + const uniqueKey = code; |
| 61 | + |
| 62 | + // 查重逻辑 1: 检查持久化缓存 (以及本次运行已处理的) |
| 63 | + // 兼容旧的缓存格式 "code|location" |
| 64 | + if (processedCodes.some(item => item === uniqueKey || item.startsWith(`${code}|`))) { |
| 65 | + console.log(`跳过已缓存的取件码: ${code}`); |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + const isDuplicateReminder = upcomingReminders.some(r => r.title.includes(code)); |
| 70 | + if (isDuplicateReminder) { |
| 71 | + console.log(`跳过现有提醒事项: ${code}`); |
| 72 | + // 即使缓存没有,如果有提醒了,也加入缓存 |
| 73 | + if (!processedCodes.includes(uniqueKey)) { |
| 74 | + processedCodes.push(uniqueKey); |
| 75 | + hasNew = true; |
| 76 | + } |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + reminder.createSystemReminder(`取件码: ${code}`, { |
| 81 | + notes: `位置: ${location}\n原文: ${msg.text || msg.body}`, |
| 82 | + priority: 5, |
| 83 | + listTitle: "取件码" |
| 84 | + }); |
| 85 | + |
| 86 | + notification.send("快递取件提醒", `凭取件码 ${code} 至 ${location} 取件`, {}); |
| 87 | + |
| 88 | + console.log(`已提取取件码: ${code} (${location})`); |
| 89 | + |
| 90 | + // 更新缓存 |
| 91 | + processedCodes.push(uniqueKey); |
| 92 | + hasNew = true; |
| 93 | + processedCount++; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (processedCount === 0) { |
| 98 | + console.log("未找到新的取件码短信"); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +main(); |
0 commit comments