前言

每次有人申请友链, 我都得手动去改 link.yml 文件, 然后 commit、push、等构建……说实话, 挺烦的

某天摸鱼的时候突然想到 GitHub 不是有 API 吗? 干脆写个自动化的友链系统得了

思路很简单:

  1. 搞个页面让大家填表申请
  2. 后端收到请求后存到数据库, 发邮件通知我
  3. 我点一下"通过",系统自动去 GitHub 开个 PR
  4. PR 一合并,友链就自动上线了

技术栈

  • Express.js 搭建 API 服务
  • MongoDB 存储申请数据
  • Octokit 操作 GitHub API
  • Nodemailer 发送邮件通知
  • Vercel 部署

不过天天白嫖人家也挺过分的 (●’◡’●)

准备工作

在开始之前, 需要准备以下内容:

1. GitHub Personal Access Token

前往 GitHub Settings > Developer settings > Personal access tokens 创建一个新的 Token, 需要勾选 repo 权限

2. MongoDB 数据库

使用 MongoDB Atlas 的免费套餐, 创建一个集群后获取连接字符串

3. SMTP 邮件服务

可以使用 Zoho Mail、Gmail、QQ 邮箱等提供的 SMTP 服务

部署 API 服务

点击下方按钮即可部署到 Vercel:

Deploy with Vercel

环境变量配置

变量名 说明 示例
MONGODB_URI MongoDB 连接字符串 mongodb+srv://user:pass@cluster.mongodb.net/
GITHUB_TOKEN GitHub Personal Access Token(需要 repo 权限) ghp_xxxxxxxxxxxx
GITHUB_REPO GitHub 仓库(格式:owner/repo) username/blog-repo
ADMIN_EMAIL 管理员邮箱(接收友链申请通知) admin@example.com
API_DOMAIN API 域名(用于生成审核链接) https://your-api.vercel.app
PR_PASSWORD 审核密码 your-secure-password
SMTP_HOST SMTP 服务器地址 smtp.example.com
SMTP_PORT SMTP 端口 465
SMTP_USER SMTP 用户名 user@example.com
SMTP_PASS SMTP 密码 your-smtp-password

修改博客源代码

创建友链申请页面

创建 /source/link/apply/index.md

1
2
3
4
5
6
7
8
---
orderby: random
title: 友链申请
top_img: false
comments: false
aside: false
type: friend-apply
---

创建 Pug 模板

在主题目录下创建自定义的友链申请表单模板:

创建 /themes/butterfly/layout/includes/page/friend-apply.pug

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
link(rel="stylesheet" type="text/css" href="/link/apply/link-apply.css")

#article-container.container
.la-form-section
.la-tab-container
.la-tab.active(id="tab-apply") 新增友链申请
.la-tab(id="tab-update") 修改友链信息

form.la-form#la-apply-form(style="display:block;")
.la-form-group
label(for="la-name") 昵称
input(type="text" id="la-name" name="name", required, placeholder="yiiko")
.la-form-group
label(for="la-link") 站点链接
input(type="url" id="la-link" name="link", required, placeholder="https://blog.yik.at")
.la-form-group
label(for="la-avatar") 头像链接
input(type="url" id="la-avatar" name="avatarLink", required, placeholder="https://blog.yik.at/static/img/avatar.png")
.la-form-group
label(for="la-email") 邮箱
input(type="email" id="la-email" name="email", required, placeholder="your@email.com")
.la-form-group
label(for="la-descr") 描述
textarea(id="la-descr" name="descr", required, placeholder="梦想成为庄稼地里的读书人.....")
button.la-btn(type="submit" onclick="applyFriendSumit()") 提交申请
.la-form-msg#la-form-msg 等待提交

form.la-form#la-update-form(style="display:none;")
.la-form-group
label(for="la-original-link")
| 原始链接
a(href="javascript:" style="user-select: none;" onclick="matchFriend()") *点击匹配友链信息
.la-url-match
input(type="url" id="la-original-link" name="originalLink", required, placeholder="https://old.yik.at (填写错误api将无法匹配)")
#la-url-match-result 未匹配
.la-form-group
label(for="la-update-name") 昵称
input(type="text" id="la-update-name" name="name", required, placeholder="yiiko")
.la-form-group
label(for="la-update-link") 站点链接
input(type="url" id="la-update-link" name="link", required, placeholder="https://new.yik.at")
.la-form-group
label(for="la-update-avatar") 头像链接
input(type="url" id="la-update-avatar" name="avatarLink", required, placeholder="https://blog.yik.at/static/img/avatar.png")
.la-form-group
label(for="la-update-email") 邮箱
input(type="email" id="la-update-email" name="email", required, placeholder="i@yik.at")
.la-form-group
label(for="la-update-descr") 描述
textarea(id="la-update-descr" name="descr", required, placeholder="梦想成为庄稼地里的读书人.....")
button.la-btn(type="submit" onclick="updateFriendSumit()") 更新友链
.la-form-msg#la-update-msg 等待提交
.la-table-section
.la-table-title 友链申请列表
#la-loader
#la-spinner
#la-loading-text 正在加载数据...
.la-card-container#la-card-container(style="display:none;")
.la-empty#la-empty(style="display:none;") 列表为空

script(data-pjax, src="/link/apply/link-apply.js")

修改 /themes/butterfly/layout/page.pug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
extends includes/layout.pug

block content
...

#page(class=noCardLayout)
if top_img === false && page.title && page.type != 'music'
.page-title= page.title

case page.type
when 'tags'
include includes/page/tags.pug
+commentLoad
...
+ when 'friend-apply'
include includes/page/friend-apply.pug
default
include includes/page/default-page.pug
+commentLoad

添加样式

创建 /source/link/apply/link-apply.css

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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
:root {
--la-background: #fff;
--la-card-bg: #fff;
--la-ccard-bg: #fff;
--la-font-color: #333;
--la-border-color: #e0e0e0;
--la-theme-color: #3498db;
--la-theme-hover: #217dbb;
--la-second-bg: #f6f8fa;
--la-second-text: #888;
--la-shadow: 0 2px 12px rgba(52, 152, 219, 0.08), 0 1.5px 4px rgba(0, 0, 0, 0.04);
}

[data-theme="dark"] {
--la-background: #1a1a1a;
--la-card-bg: #1d1e22;
--la-ccard-bg: #30343f;
--la-font-color: #eee;
--la-border-color: #444;
--la-theme-color: #3498db;
--la-theme-hover: #5dade2;
--la-second-bg: #333;
--la-second-text: #bbb;
--la-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
}

/* 容器和标题 */
.la-container {
max-width: 700px;
margin: 40px auto;
background: var(--la-card-bg);
border-radius: 18px;
box-shadow: var(--la-shadow);
padding: 32px 24px 32px 24px;
}

.la-title {
display: none;
}

/* 表单样式 */
.la-form {
display: flex;
flex-direction: column;
gap: 14px;
/* margin-bottom: 36px; */
}

.la-form-group {
display: flex;
flex-direction: column;
gap: 4px;
}

.la-form-group label {
font-size: 14px;
color: var(--la-font-color);
}

.la-form-group input,
.la-form-group textarea {
padding: 6px 10px;
border: 1.5px solid var(--la-border-color);
border-radius: 6px;
font-size: 14px;
outline: none;
transition: border-color 0.18s;
background-color: var(--la-card-bg);
color: var(--la-font-color);
}

.la-form-group input:focus,
.la-form-group textarea:focus {
border-color: var(--la-theme-color);
}

.la-form-group textarea {
min-height: 50px;
resize: vertical;
}

.la-btn {
background: var(--la-theme-color);
color: #fff;
border: none;
border-radius: 10px;
max-width: 140px;
padding: 8px 30px;
margin-top: 12px;
margin-bottom: 12px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: background 0.18s;
}

.la-btn:hover {
background: var(--la-theme-hover);
}

.la-btn:disabled {
background: #b5c6d6 !important;
color: #fff !important;
cursor: not-allowed !important;
opacity: 0.7;
box-shadow: none;
}

.la-form-msg {
margin-top: -8px;
font-size: 14px;
color: #e74c3c;
height: 12px;
}

/* 表格区域 */
.la-table-section {
/* max-width: 700px; */
margin: 20px auto 0 auto;
background: var(--la-card-bg);
border-radius: 12px;
box-shadow: var(--la-shadow);
padding: 24px 20px 24px 20px;
/* max-width: 1100px; */
}

.la-table-title {
font-size: 20px;
margin-bottom: 18px;
color: var(--la-font-color);
}

.la-table {
width: 100%;
border-collapse: collapse;
background: var(--la-background);
border-radius: 12px;
overflow: hidden;
box-shadow: 0 1.5px 4px rgba(52, 152, 219, 0.04);
}

.la-table th,
.la-table td {
padding: 10px 8px;
text-align: left;
border-bottom: 1px solid var(--la-border-color);
font-size: 15px;
color: var(--la-font-color);
}

.la-table th {
background: var(--la-second-bg);
font-weight: 700;
}

.la-table td img {
width: 36px;
height: 36px;
border-radius: 50%;
object-fit: cover;
border: 2px solid var(--la-border-color);
background: var(--la-second-bg);
margin: 0 !important;
}

.la-empty {
text-align: center;
color: var(--la-second-text);
font-size: 15px;
margin-top: 18px;
}

/* loading 动画 */
#la-loader {
display: flex;
flex-direction: column;
align-items: center;
margin: 30px 0;
}

#la-spinner {
width: 48px;
height: 48px;
border: 5px solid var(--la-border-color);
border-top: 5px solid var(--la-theme-color);
border-radius: 50%;
animation: la-spin 1s linear infinite;
margin-bottom: 12px;
}

@keyframes la-spin {
0% {
transform: rotate(0deg);
}

100% {
transform: rotate(360deg);
}
}

#la-loading-text {
font-size: 15px;
color: var(--la-second-text);
text-align: center;
letter-spacing: 2px;
}

@media (max-width: 700px) {
.la-container {
padding: 10px 2px 18px 2px;
}

.la-table-title {
font-size: 15px;
}

.la-table th,
.la-table td {
font-size: 12px;
padding: 6px 2px;
}

.la-table td img {
width: 24px;
height: 24px;
}
}

.la-form-section {
margin: 20px auto 0 auto !important;
background: var(--la-card-bg) !important;
border-radius: 12px !important;
box-shadow: var(--la-shadow) !important;
padding: 24px 20px 24px 20px !important;
box-sizing: border-box !important;
}

/* tab 切换样式 */
.la-tab-container {
display: flex;
gap: 12px;
margin-bottom: 20px;
border-bottom: 1px solid var(--la-border-color);
padding-bottom: 10px;
}

.la-tab {
padding: 2px 16px;
font-size: 16px;
font-weight: 500;
color: var(--la-second-text);
cursor: pointer;
border-radius: 10px;
transition: all 0.2s ease;
background: transparent;
border: none;
outline: none;
user-select: none;
}

.la-tab:hover {
color: var(--la-theme-color);
background-color: var(--la-second-bg);
}

.la-tab.active {
color: var(--la-theme-color);
background-color: rgba(52, 152, 219, 0.1);
}

.la-table td:first-child {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 120px;
}

.la-table td:last-child {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 120px;
}

.la-table td:nth-child(2),
.la-table td:nth-child(4) {
max-width: 290px;
word-break: break-all;
white-space: normal;
}

.la-state {
display: inline-block;
padding: 1px 7px;
border-radius: 9px;
font-size: 12px;
font-weight: 500;
text-align: center;
white-space: nowrap;
position: relative;
top: -4px;
right: -3px;
}

.la-state-success {
background-color: #e3f8f0;
color: #27ae60;
}

.la-state-pending {
background-color: #f0f6fc;
color: #3498db;
}

.la-state-rejected {
background-color: #feeaea;
color: #e74c3c;
}

.la-state-other {
background-color: #f5f5f5;
color: #888;
}

/* 卡片布局样式 */
.la-card-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 12px;
margin-top: 12px;
}

.la-card {
background: var(--la-ccard-bg);
border: 1px solid var(--la-border-color);
border-radius: 8px;
padding: 10px 12px;
box-shadow: 0 1px 6px rgba(52, 152, 219, 0.05);
transition: all 0.2s ease;
display: flex;
gap: 8px;
}

/* 卡片类型标识样式 */
.la-card-type {
display: inline-block;
margin-right: 6px;
padding: 2px 6px;
font-size: 11px;
font-weight: 600;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
vertical-align: middle;
line-height: 1;
}

/* 新增类型样式 */
.la-card-type-update {
background-color: #f3e5ff;
color: #612dc1;
}

/* 修改类型样式 */
.la-card-type-add {
background-color: #fff3cd;
color: #d39e00;
}

/* 深色主题适配 */
[data-theme="dark"] .la-card-type-update {
background-color: #340f57;
color: #9065df;
}

[data-theme="dark"] .la-card-type-add {
background-color: #3d3310;
color: #d39e00;
}

.la-card:hover {
box-shadow: 0 2px 8px rgba(52, 152, 219, 0.1);
}

.la-card-avatar {
width: 48px;
height: 48px;
display: none !important;
border-radius: 50%;
object-fit: cover;
border: 1px solid var(--la-border-color);
background: var(--la-second-bg);
flex-shrink: 0;
}

.la-card-info {
flex: 1;
min-width: 0;
}

.la-card-header {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 0px;
line-height: 1.7;
flex-wrap: wrap;
}

.la-card-name {
font-size: 17px;
color: var(--la-font-color);
word-break: break-word;
/* line-height: 1; */
}

.la-card-state {
margin-left: auto;
}

.la-card-meta {
display: none;
font-size: 11px;
color: var(--la-second-text);
margin-bottom: 3px;
line-height: 1;
}

.la-card-link {
font-size: 12px;
display: none !important;
color: var(--la-theme-color);
text-decoration: none;
margin-bottom: 6px;
word-break: break-all;
transition: color 0.2s ease;
}

.la-card-link:hover {
color: var(--la-theme-hover);
text-decoration: underline;
}

.la-card-desc {
font-size: 13px;
color: var(--heo-secondtext);
line-height: 1.4;
word-break: break-word;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
}

/* 响应式设计 */
@media (max-width: 700px) {
.la-card-container {
grid-template-columns: 1fr;
gap: 10px;
}

.la-card {
padding: 10px;
gap: 8px;
}

.la-card-avatar {
width: 44px;
height: 44px;
}
}
.la-card-link {
line-height: 1;
}
[data-theme="dark"] .la-state-success {
background-color: #113728;
}
[data-theme="dark"] .la-state-rejected {
background-color: #4d1616;
}
[data-theme="dark"] .la-state-pending {
background-color: #103051;
}

label {
margin-top: 10px;
}

@media screen and (max-width: 768px) {
.layout > div:first-child:not(.recent-posts) {
padding: 5px !important;
}
}
.la-url-match {
display: flex;
flex-direction: column;
}

.la-url-match-container {
display: flex;
}
#la-url-match-result {
margin-left: 12px;
margin-top: 6px;
display: none;
}

添加脚本

创建 /source/link/apply/link-apply.js

并且修改第一行的API_BASE为你的 API 地址

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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
const API_BASE = "https://yik.at/api"; // 修改为你的API地址

function initFriendApply() {
const form = document.getElementById("la-apply-form");
const msg = document.getElementById("la-form-msg");
const loader = document.getElementById("la-loader");
const cardContainer = document.getElementById("la-card-container");
const empty = document.getElementById("la-empty");
const submitBtn = form ? form.querySelector(".la-btn") : null;

// 初始化 tab 切换
initTabSwitch();

if (!form || !submitBtn) {
console.log("friendApplyReturned", submitBtn, form);
return;
}

loadList();
}

// 初始化 tab 切换功能
function initTabSwitch() {
const tabs = document.querySelectorAll(".la-tab");
const applyForm = document.getElementById("la-apply-form");
const updateForm = document.getElementById("la-update-form");

tabs.forEach((tab) => {
tab.addEventListener("click", () => {
// 移除所有 tab 的 active 类
tabs.forEach((t) => t.classList.remove("active"));
// 添加当前 tab 的 active 类
tab.classList.add("active");

// 切换表单显示
if (tab.id === "tab-apply") {
applyForm.style.display = "block";
updateForm.style.display = "none";
} else if (tab.id === "tab-update") {
applyForm.style.display = "none";
updateForm.style.display = "block";
}
});
});
}

function formatTime(str) {
if (!str) return "";
const d = new Date(str);
return (
d.getFullYear() +
"-" +
(d.getMonth() + 1).toString().padStart(2, "0") +
"-" +
d.getDate().toString().padStart(2, "0") +
" " +
d.getHours().toString().padStart(2, "0") +
":" +
d.getMinutes().toString().padStart(2, "0")
);
}

function getStateClass(state) {
switch (state) {
case "已通过":
case "已批准":
case "通过":
return "la-state-success";
case "待审核":
case "审核中":
return "la-state-pending";
case "已拒绝":
case "拒绝":
return "la-state-rejected";
default:
return "la-state-other";
}
}

initFriendApply();

document.addEventListener("DOMContentLoaded", () => {
setTimeout(() => {
initFriendApply();
}, 0);
});

function loadList() {
const form = document.getElementById("la-apply-form");
const msg = document.getElementById("la-form-msg");
const loader = document.getElementById("la-loader");
const cardContainer = document.getElementById("la-card-container");
const empty = document.getElementById("la-empty");
const submitBtn = form ? form.querySelector(".la-btn") : null;

loader.style.display = "flex";
cardContainer.style.display = "none";
empty.style.display = "none";
fetch(API_BASE + "/friend/list")
.then((res) => res.json())
.then((data) => {
loader.style.display = "none";
if (data.success && Array.isArray(data.list) && data.list.length > 0) {
cardContainer.innerHTML = "";
data.list.forEach((item) => {
// 添加状态列
const state = item.state || "待审核";
// 根据状态设置不同的样式
const stateClass = getStateClass(state);
const formattedTime = formatTime(item.createdAt);

// 创建卡片元素
const card = document.createElement("div");
card.className = "la-card";
// 检查是否包含 originalLink 字段, 判断是新增还是修改
const isUpdate =
item.originalLink !== undefined &&
item.originalLink !== null &&
item.originalLink !== "";
const typeLabel = isUpdate ? "修改" : "新增";
const typeClass = isUpdate
? "la-card-type-update"
: "la-card-type-add";
card.innerHTML = `
<img class="la-card-avatar" src="" alt="${
item.name
}" onerror="this.onerror=null;this.src='/static/img/erravatar.png';">
<div class="la-card-info">
<div class="la-card-header">
<div class="la-card-name">${item.name}</div>
<div class="la-card-state">
<span class="la-state ${stateClass}">${state}</span>
<span class="la-state ${typeClass}">${typeLabel}</span>
</div>
</div>
<a href="${item.link}" class="la-card-link" target="_blank">${
item.link
}</a>
<div class="la-card-meta">申请时间: ${formattedTime}</div>
<div class="la-card-desc">${item.descr || "无描述"}</div>
</div>
`;
cardContainer.appendChild(card);
});
cardContainer.style.display = "";
} else {
empty.style.display = "";
}
})
.catch(() => {
loader.style.display = "none";
empty.style.display = "";
});
}

function applyFriendSumit() {
const form = document.getElementById("la-apply-form");
const msg = document.getElementById("la-form-msg");
const submitBtn = form ? form.querySelector(".la-btn") : null;

form.addEventListener("submit", function (e) {
e.preventDefault();
msg.textContent = "";
const formData = new FormData(form);

const data = {
name: formData.get("name"),
link: formData.get("link"),
avatarLink: formData.get("avatarLink"),
email: formData.get("email"), // 新增邮箱字段
descr: formData.get("descr"),
};

if (
!data.name ||
!data.link ||
!data.avatarLink ||
!data.email ||
!data.descr
) {
msg.textContent = "请填写完整信息";
return;
}

// 验证邮箱格式
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(data.email)) {
msg.textContent = "请输入有效的邮箱地址";
return;
}

msg.textContent = "正在提交...";
submitBtn.disabled = true;
fetch(API_BASE + "/friend/apply", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((res) => {
if (!res.ok) throw new Error("网络错误");
msg.textContent = "提交失败, 请稍后重试. ";
return res.json();
})
.then(() => {
msg.style.color = "#27ae60";
msg.textContent = "申请成功, 等待审核!";
form.reset();
submitBtn.disabled = false;
loadList();
})
.catch((e) => {
msg.style.color = "#e74c3c";
msg.textContent = "提交失败, 请稍后重试. " + e.message;
submitBtn.disabled = false;
});
});
}

// 更新友链提交函数
function updateFriendSumit() {
const form = document.getElementById("la-update-form");
const msg = document.getElementById("la-update-msg");
const submitBtn = form ? form.querySelector(".la-btn") : null;

form.addEventListener("submit", function (e) {
e.preventDefault();
msg.textContent = "";
const formData = new FormData(form);

const data = {
originalLink: formData.get("originalLink"),
name: formData.get("name"),
link: formData.get("link"),
avatarLink: formData.get("avatarLink"),
email: formData.get("email"),
descr: formData.get("descr"),
};

if (
!data.originalLink ||
!data.name ||
!data.link ||
!data.avatarLink ||
!data.email ||
!data.descr
) {
msg.textContent = "请填写完整信息";
return;
}

// 验证邮箱格式
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(data.email)) {
msg.textContent = "请输入有效的邮箱地址";
return;
}

msg.textContent = "正在提交...";
submitBtn.disabled = true;
fetch(API_BASE + "/friend/update-friend", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((res) => {
if (!res.ok) throw new Error("网络错误");
msg.textContent = "提交失败, 请稍后重试. ";
return res.json();
})
.then(() => {
msg.style.color = "#27ae60";
msg.textContent = "友链更新成功!";
form.reset();
submitBtn.disabled = false;
loadList();
})
.catch((e) => {
msg.style.color = "#e74c3c";
msg.textContent = "提交失败, 请稍后重试. " + e.message;
submitBtn.disabled = false;
});
});
}

function matchFriend() {
const ele = document.getElementById("la-url-match-result");
const form = document.getElementById("la-update-form");
const formData = new FormData(form);
const data = formData.get("originalLink");
ele.style.display = "unset";
if (data.trim() === "") {
ele.innerHTML = "输入要匹配的链接";
ele.style.color = "#e74c3c";
return;
}
// 正则匹配url
const regex =
/^(((ht|f)tps?):\/\/)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}(:\d{1,5})?\/?$/;
if (!regex.test(data)) {
ele.innerHTML = "请输入有效的链接";
ele.style.color = "#e74c3c";
return;
}
ele.innerHTML = "匹配中...";

fetch(API_BASE + "/friend/match-friend?url=" + data, {
method: "GET",
})
.then((res) => {
if (!res.ok) {
ele.innerHTML = "匹配失败";
ele.style.color = "#e74c3c";
console.log(res);
}
return res.json();
})
.then((data) => {
console.log(data);
if (data.success) {
ele.innerHTML = "匹配结果: \n</br>" + data.info;
ele.style.color = "#27ae60";
} else {
ele.innerHTML = "匹配失败: \n" + data.info;
ele.style.color = "#e74c3c";
}
})
.catch((error) => {
console.error("匹配过程出错: ", error);
ele.innerHTML = "匹配失败: " + error.message;
ele.style.color = "#e74c3c";
});
}

工作流程

完成以上配置后, 整个友链管理流程如下:

  1. 用户提交申请: 用户在友链页面填写表单提交申请
  2. 邮件通知: 系统自动发送邮件通知 me, 邮件中包含审核链接
  3. 管理员审核: 点击邮件中的链接进入审核页面, 可以修改信息、通过或拒绝
  4. 自动创建 PR: 审核通过后, 系统自动在 GitHub 仓库创建 PR
  5. 合并 PR: 去 GitHub 点一下 Merge,博客自动重新构建
  6. 通知用户: 系统自动发送邮件通知用户审核结果

整个过程我只需要点两下鼠标,舒服了~

核心实现原理

Octokit 创建 PR 的流程

  1. 获取 link.yml 文件的当前内容和 SHA
  2. 在文件末尾追加新的友链信息
  3. 基于默认分支创建新分支
  4. 将修改后的文件推送到新分支
  5. 创建从新分支到默认分支的 PR
  6. 为 PR 添加标签

友链更新的匹配逻辑

更新友链时, 系统会对链接进行标准化处理后再匹配:

  • 去掉 http://https:// 前缀
  • 去掉 www. 前缀
  • 去掉末尾的 /

这样 https://blog.example.com/blog.example.com 都能匹配上

总结

折腾完这套系统之后,友链管理确实轻松多了:

  • 再也不用手动改 YAML 文件了
  • 不用再手动 commit、push
  • 审核流程可追溯(每个友链都有对应的 PR)
  • 用户体验更好(有申请页面和邮件通知)

有兴趣的话可以自己魔改,代码都开源了,欢迎来 Star 和 PR!

项目地址: https://github.com/yiikooo/friend-link-api