-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathindex.html
More file actions
184 lines (167 loc) · 7.64 KB
/
index.html
File metadata and controls
184 lines (167 loc) · 7.64 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>在线留言板</title>
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
theme: {
extend: {
colors: {
'fb-blue': '#1877F2',
'fb-blue-dark': '#166FE5',
'fb-bg': '#F0F2F5',
'fb-gray': '#65676B',
'fb-gray-light': '#E4E6EB',
}
}
}
}
</script>
</head>
<body class="min-h-screen bg-fb-bg p-4 md:p-8">
<div class="max-w-4xl mx-auto">
<!-- 标题 -->
<div class="text-center mb-6">
<h1 class="text-3xl md:text-4xl font-bold text-gray-900 mb-2">
在线留言板
</h1>
<p class="text-fb-gray text-sm md:text-base">分享你的想法和反馈</p>
</div>
<!-- 留言表单 -->
<div class="bg-white rounded-lg shadow-md p-6 md:p-8 mb-4">
<div id="notification"></div>
<form id="messageForm" class="space-y-4">
<div>
<label for="content" class="block text-sm font-medium text-gray-700 mb-2">
留言内容
</label>
<textarea
id="content"
name="content"
required
rows="4"
placeholder="分享你的想法..."
class="w-full px-3 py-2 border border-fb-gray-light rounded-lg bg-fb-bg focus:bg-white focus:outline-none focus:ring-2 focus:ring-fb-blue focus:border-transparent transition-all resize-none"
></textarea>
</div>
<div>
<label for="email" class="block text-sm font-medium text-gray-700 mb-2">
邮箱地址
</label>
<input
type="email"
id="email"
name="email"
required
placeholder="your@email.com"
class="w-full px-3 py-2 border border-fb-gray-light rounded-lg bg-fb-bg focus:bg-white focus:outline-none focus:ring-2 focus:ring-fb-blue focus:border-transparent transition-all"
>
</div>
<button
type="submit"
class="w-full bg-fb-blue hover:bg-fb-blue-dark text-white font-semibold py-2.5 px-4 rounded-lg transition-colors disabled:opacity-60 disabled:cursor-not-allowed"
>
发送留言
</button>
</form>
</div>
<!-- 历史留言列表 -->
<div class="bg-white rounded-lg shadow-md p-6 md:p-8">
<h2 class="text-xl md:text-2xl font-bold text-gray-900 mb-4">
所有留言
</h2>
<div id="messagesList">
<div class="text-center text-fb-gray py-8">加载中...</div>
</div>
</div>
</div>
<script>
const API_URL = '/api';
// 加载留言列表
async function loadMessages() {
try {
const response = await fetch(`${API_URL}/messages`);
const data = await response.json();
const messagesList = document.getElementById('messagesList');
if (!data.messages || data.messages.length === 0) {
messagesList.innerHTML = '<div class="text-center text-fb-gray py-12 text-sm">暂无留言,成为第一个留言的人吧!</div>';
return;
}
messagesList.innerHTML = data.messages.map(msg => `
<div class="bg-fb-bg rounded-lg p-4 mb-3 hover:bg-gray-100 transition-colors">
<div class="text-gray-900 text-base mb-2 leading-relaxed">${escapeHtml(msg.content)}</div>
<div class="flex flex-wrap items-center gap-2 text-xs text-fb-gray">
<span class="font-medium">${escapeHtml(msg.email)}</span>
<span>·</span>
<span>${formatDate(msg.created_at)}</span>
</div>
</div>
`).join('');
} catch (error) {
console.error('加载留言失败:', error);
document.getElementById('messagesList').innerHTML =
'<div class="bg-red-50 text-red-600 p-3 rounded-lg text-sm">加载留言失败,请刷新页面重试</div>';
}
}
// 提交留言
document.getElementById('messageForm').addEventListener('submit', async (e) => {
e.preventDefault();
const submitBtn = e.target.querySelector('button[type="submit"]');
const notification = document.getElementById('notification');
const content = document.getElementById('content').value;
const email = document.getElementById('email').value;
submitBtn.disabled = true;
submitBtn.textContent = '发送中...';
notification.innerHTML = '';
try {
const response = await fetch(`${API_URL}/messages`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ content, email })
});
const data = await response.json();
if (response.ok) {
notification.innerHTML = '<div class="bg-green-50 text-green-700 p-3 rounded-lg mb-4 text-sm font-medium">留言发送成功!</div>';
document.getElementById('messageForm').reset();
await loadMessages();
setTimeout(() => {
notification.innerHTML = '';
}, 3000);
} else {
notification.innerHTML = `<div class="bg-red-50 text-red-600 p-3 rounded-lg mb-4 text-sm font-medium">${data.error || '发送失败,请重试'}</div>`;
}
} catch (error) {
console.error('发送留言失败:', error);
notification.innerHTML = '<div class="bg-red-50 text-red-600 p-3 rounded-lg mb-4 text-sm font-medium">发送失败,请检查网络连接</div>';
} finally {
submitBtn.disabled = false;
submitBtn.textContent = '发送留言';
}
});
// HTML转义,防止XSS
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// 格式化日期为 yyyy-mm-dd hh:mm:ss 格式
function formatDate(dateString) {
const date = new Date(dateString);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
// 页面加载时获取留言列表
loadMessages();
</script>
</body>
</html>