forked from Yuy-0415/verification-pwa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudflare-worker-example.js
More file actions
141 lines (125 loc) · 3.17 KB
/
cloudflare-worker-example.js
File metadata and controls
141 lines (125 loc) · 3.17 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
/**
* Cloudflare Worker 邮箱接码示例
*
* 功能:
* 1. 接收邮件并提取验证码
* 2. 提供 API 获取验证码列表
* 3. 提供 API 删除所有邮件
*
* 部署方式:
* 1. 在 Cloudflare Dashboard 创建 Worker
* 2. 复制此代码到 Worker 编辑器
* 3. 配置邮件路由(Email Routing)
* 4. 部署并获取 Worker URL
*/
// 存储邮件数据(使用 KV 或 D1 数据库会更好)
let emails = [];
/**
* 从邮件内容中提取验证码
* 支持常见的验证码格式
*/
function extractVerificationCode(content) {
// 常见验证码格式
const patterns = [
/验证码[::]\s*(\d{4,8})/,
/verification code[::]\s*(\d{4,8})/i,
/code[::]\s*(\d{4,8})/i,
/(\d{4,8})\s*是您的验证码/,
/您的验证码是\s*(\d{4,8})/,
/(\d{4,8})/ // 最后尝试匹配任意4-8位数字
];
for (const pattern of patterns) {
const match = content.match(pattern);
if (match) {
return match[1];
}
}
return null;
}
/**
* 处理邮件接收
*/
async function handleEmail(message) {
const email = {
id: crypto.randomUUID(),
from: message.from,
to: message.to,
subject: message.headers.get('subject') || '',
content: await message.text(),
receivedAt: Date.now(),
hasVerificationCode: false,
verificationCode: null
};
// 提取验证码
const code = extractVerificationCode(email.content);
if (code) {
email.hasVerificationCode = true;
email.verificationCode = code;
}
// 存储邮件(实际应用中应使用 KV 或 D1)
emails.push(email);
// 只保留最近 100 封邮件
if (emails.length > 100) {
emails = emails.slice(-100);
}
}
/**
* 处理 HTTP 请求
*/
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url);
// CORS 头
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
// 处理 OPTIONS 请求
if (request.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
// API: 获取验证码列表
if (url.pathname === '/api/codes' && request.method === 'GET') {
return new Response(JSON.stringify({
success: true,
emails: emails.filter(e => e.hasVerificationCode)
}), {
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
// API: 删除所有邮件
if (url.pathname === '/api/delete' && request.method === 'POST') {
emails = [];
return new Response(JSON.stringify({
success: true,
message: '已清空所有邮件'
}), {
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
}
// 默认响应
return new Response(JSON.stringify({
success: false,
message: 'API 路径不存在'
}), {
status: 404,
headers: {
'Content-Type': 'application/json',
...corsHeaders
}
});
},
/**
* 邮件处理器
*/
async email(message, env, ctx) {
await handleEmail(message);
}
};