AI 生成: 一个使用 Flask 构建的 AI 随机生成应用,可随机生成名字、故事、颜色、名言和密码,并支持一键复制。

This commit is contained in:
admin
2026-09-28 22:05:42 +08:00
commit f874c57208
6 changed files with 317 additions and 0 deletions
+92
View File
@@ -0,0 +1,92 @@
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
min-height: 100vh;
background: linear-gradient(135deg, #1e3c72, #2a5298);
color: #fff;
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.container {
width: 100%;
max-width: 720px;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 16px;
padding: 40px 30px;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
text-align: center;
}
h1 {
font-size: 32px;
margin-bottom: 10px;
}
.subtitle {
color: #d0d8ff;
margin-bottom: 30px;
}
.buttons {
display: flex;
flex-wrap: wrap;
gap: 12px;
justify-content: center;
margin-bottom: 30px;
}
button {
padding: 10px 20px;
border: none;
border-radius: 8px;
background: #4ecdc4;
color: #1a1a1a;
font-weight: 600;
cursor: pointer;
transition: transform 0.15s ease, background 0.2s ease;
}
button:hover {
background: #38b2a8;
transform: translateY(-2px);
}
button:active {
transform: translateY(0);
}
.result-box {
background: rgba(0, 0, 0, 0.25);
border-radius: 12px;
padding: 24px;
}
.result {
font-size: 20px;
line-height: 1.6;
word-break: break-word;
min-height: 60px;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 16px;
transition: color 0.3s ease;
}
.copy-btn {
background: #ff6b6b;
color: #fff;
}
.copy-btn:hover {
background: #e85a5a;
}
+55
View File
@@ -0,0 +1,55 @@
// 获取页面元素
const resultEl = document.getElementById("result");
const copyBtn = document.getElementById("copyBtn");
const buttons = document.querySelectorAll(".buttons button");
/**
* 调用后端 API 生成随机内容
* @param {string} kind - 生成类型
*/
async function generate(kind) {
resultEl.textContent = "生成中...";
resultEl.style.color = "#fff";
try {
const res = await fetch(`/api/generate/${kind}`);
const data = await res.json();
if (data.error) {
resultEl.textContent = data.error;
return;
}
// 如果是颜色类型,直接展示色块
if (kind === "color") {
resultEl.textContent = data.result;
resultEl.style.color = data.result;
} else {
resultEl.textContent = data.result;
}
} catch (err) {
resultEl.textContent = "生成失败,请稍后重试";
console.error(err);
}
}
// 为每个按钮绑定点击事件
buttons.forEach((btn) => {
btn.addEventListener("click", () => {
generate(btn.dataset.kind);
});
});
// 复制结果到剪贴板
copyBtn.addEventListener("click", async () => {
const text = resultEl.textContent;
if (!text || text === "点击上方按钮开始生成") return;
try {
await navigator.clipboard.writeText(text);
copyBtn.textContent = "已复制 ✓";
setTimeout(() => (copyBtn.textContent = "复制结果"), 1500);
} catch (err) {
console.error("复制失败", err);
}
});