feat: 门户网站初始提交
- Go + Gin + html/template 服务端渲染 - 主页:Google 风格搜索框 + 导航卡片 - 后台:卡片 CRUD、搜索引擎配置、主页背景/标题配置 - 图片上传:支持 jpg/jpeg/png/gif,自动压缩,缩略图参数 ?thumb=1 - 安全:登录日志、修改密码、IP 自动封禁、IP 白名单 - 访问统计:主页访问/卡片点击/搜索追踪、实时流量、IP 统计 - SQLite 存储(modernc.org/sqlite,纯 Go) - 内存 Session + bcrypt 密码哈希
This commit is contained in:
@@ -0,0 +1,792 @@
|
||||
/* ===== Reset & Base ===== */
|
||||
*,
|
||||
*::before,
|
||||
*::after {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
|
||||
color: #333;
|
||||
background: #f5f5f5;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #1a73e8;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
/* ===== Home Page ===== */
|
||||
.home-container {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 80px 20px 40px;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
}
|
||||
|
||||
.home-header {
|
||||
text-align: center;
|
||||
margin-bottom: 40px;
|
||||
}
|
||||
|
||||
.home-title {
|
||||
font-size: 48px;
|
||||
font-weight: 700;
|
||||
color: #fff;
|
||||
letter-spacing: 2px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.home-subtitle {
|
||||
font-size: 16px;
|
||||
color: rgba(255, 255, 255, 0.8);
|
||||
}
|
||||
|
||||
/* ===== Search Box ===== */
|
||||
.search-box {
|
||||
width: 100%;
|
||||
max-width: 640px;
|
||||
margin-bottom: 60px;
|
||||
}
|
||||
|
||||
.search-box form {
|
||||
display: flex;
|
||||
background: #fff;
|
||||
border-radius: 24px;
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
|
||||
overflow: hidden;
|
||||
transition: box-shadow 0.2s;
|
||||
}
|
||||
|
||||
.search-box form:focus-within {
|
||||
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
|
||||
}
|
||||
|
||||
.search-input {
|
||||
flex: 1;
|
||||
border: none;
|
||||
outline: none;
|
||||
padding: 14px 24px;
|
||||
font-size: 16px;
|
||||
background: transparent;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.search-input::placeholder {
|
||||
color: #aaa;
|
||||
}
|
||||
|
||||
.search-btn {
|
||||
border: none;
|
||||
background: #667eea;
|
||||
color: #fff;
|
||||
padding: 14px 28px;
|
||||
font-size: 16px;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.search-btn:hover {
|
||||
background: #5a6fd6;
|
||||
}
|
||||
|
||||
/* ===== Card Grid ===== */
|
||||
.card-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
gap: 20px;
|
||||
width: 100%;
|
||||
max-width: 960px;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.card-grid {
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.card-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.card-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 16px 20px;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
|
||||
transition: transform 0.2s, box-shadow 0.2s;
|
||||
text-decoration: none;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.card-item:hover {
|
||||
transform: translateY(-4px);
|
||||
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.card-icon {
|
||||
flex-shrink: 0;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.card-emoji {
|
||||
font-size: 28px;
|
||||
}
|
||||
|
||||
.card-content {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.card-subtitle {
|
||||
font-size: 13px;
|
||||
color: #999;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
/* ===== Home Footer ===== */
|
||||
.home-footer {
|
||||
margin-top: auto;
|
||||
padding-top: 40px;
|
||||
}
|
||||
|
||||
.home-footer a {
|
||||
color: rgba(255, 255, 255, 0.5);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.home-footer a:hover {
|
||||
color: rgba(255, 255, 255, 0.8);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
/* ===== Login Page ===== */
|
||||
.login-container {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: #f5f5f5;
|
||||
}
|
||||
|
||||
.login-card {
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 40px;
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
|
||||
.login-title {
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
text-align: center;
|
||||
margin-bottom: 24px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.login-error {
|
||||
background: #fef2f2;
|
||||
color: #dc2626;
|
||||
padding: 10px 16px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 16px;
|
||||
font-size: 14px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.login-form .form-group {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
/* ===== Admin Layout ===== */
|
||||
.admin-layout {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.admin-nav {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
background: #1f2937;
|
||||
color: #fff;
|
||||
padding: 0 24px;
|
||||
height: 56px;
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.admin-nav-brand {
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
margin-right: 16px;
|
||||
}
|
||||
|
||||
.admin-nav-links {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.admin-nav-link {
|
||||
color: rgba(255, 255, 255, 0.7);
|
||||
padding: 8px 16px;
|
||||
border-radius: 6px;
|
||||
font-size: 14px;
|
||||
transition: background 0.2s, color 0.2s;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.admin-nav-link:hover {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
color: #fff;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.admin-nav-link.active {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.admin-nav-user {
|
||||
margin-left: auto;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
font-size: 14px;
|
||||
color: rgba(255, 255, 255, 0.8);
|
||||
}
|
||||
|
||||
.admin-main {
|
||||
flex: 1;
|
||||
padding: 32px;
|
||||
max-width: 1200px;
|
||||
width: 100%;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.admin-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
.admin-header h1 {
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
color: #111;
|
||||
}
|
||||
|
||||
/* ===== Admin Table ===== */
|
||||
.admin-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
background: #fff;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
|
||||
.admin-table th,
|
||||
.admin-table td {
|
||||
padding: 12px 16px;
|
||||
text-align: left;
|
||||
border-bottom: 1px solid #eee;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.admin-table th {
|
||||
background: #f9fafb;
|
||||
font-weight: 600;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
.admin-table td a {
|
||||
color: #1a73e8;
|
||||
max-width: 200px;
|
||||
display: inline-block;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
vertical-align: bottom;
|
||||
}
|
||||
|
||||
.admin-table td.actions {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
/* ===== Buttons ===== */
|
||||
.btn {
|
||||
display: inline-block;
|
||||
padding: 8px 16px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
font-size: 14px;
|
||||
cursor: pointer;
|
||||
text-align: center;
|
||||
transition: background 0.2s, opacity 0.2s;
|
||||
text-decoration: none;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
opacity: 0.9;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: #667eea;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
background: #5a6fd6;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background: #e5e7eb;
|
||||
color: #374151;
|
||||
}
|
||||
|
||||
.btn-secondary:hover {
|
||||
background: #d1d5db;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background: #ef4444;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.btn-danger:hover {
|
||||
background: #dc2626;
|
||||
}
|
||||
|
||||
.btn-sm {
|
||||
padding: 4px 10px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.btn-block {
|
||||
display: block;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* ===== Forms ===== */
|
||||
.form-group {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
display: block;
|
||||
margin-bottom: 6px;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
color: #374151;
|
||||
}
|
||||
|
||||
.form-group input[type="text"],
|
||||
.form-group input[type="password"],
|
||||
.form-group input[type="url"],
|
||||
.form-group select {
|
||||
width: 100%;
|
||||
padding: 10px 14px;
|
||||
border: 1px solid #d1d5db;
|
||||
border-radius: 8px;
|
||||
font-size: 14px;
|
||||
transition: border-color 0.2s;
|
||||
outline: none;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.form-group input:focus,
|
||||
.form-group select:focus {
|
||||
border-color: #667eea;
|
||||
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
|
||||
}
|
||||
|
||||
.form-group input[type="checkbox"] {
|
||||
margin-right: 6px;
|
||||
width: auto;
|
||||
}
|
||||
|
||||
.input-readonly {
|
||||
background: #f3f4f6 !important;
|
||||
color: #6b7280;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.required {
|
||||
color: #ef4444;
|
||||
}
|
||||
|
||||
.form-actions {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
margin-top: 24px;
|
||||
}
|
||||
|
||||
.form-error {
|
||||
background: #fef2f2;
|
||||
color: #dc2626;
|
||||
padding: 10px 16px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 16px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
/* ===== Badges ===== */
|
||||
.badge {
|
||||
display: inline-block;
|
||||
padding: 2px 10px;
|
||||
border-radius: 12px;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.badge-success {
|
||||
background: #dcfce7;
|
||||
color: #16a34a;
|
||||
}
|
||||
|
||||
.badge-secondary {
|
||||
background: #f3f4f6;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
.form-success {
|
||||
background: #f0fdf4;
|
||||
color: #16a34a;
|
||||
padding: 10px 16px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 16px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
/* ===== Home Page with Background ===== */
|
||||
.home-container.has-background {
|
||||
position: relative;
|
||||
background: none;
|
||||
}
|
||||
|
||||
.home-container.has-background::before {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: rgba(0, 0, 0, 0.35);
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
.home-container.has-background > * {
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
/* ===== Site Subtitle ===== */
|
||||
.site-subtitle {
|
||||
font-size: 16px;
|
||||
color: rgba(255, 255, 255, 0.8);
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
/* ===== Card Icon Image ===== */
|
||||
.card-icon-img {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
object-fit: contain;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
/* ===== Upload Button ===== */
|
||||
.upload-btn-wrapper {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.upload-btn {
|
||||
display: inline-block;
|
||||
padding: 6px 14px;
|
||||
background: #667eea;
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.upload-btn:hover {
|
||||
background: #5a6fd6;
|
||||
}
|
||||
|
||||
.upload-btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.upload-status {
|
||||
font-size: 13px;
|
||||
margin-left: 4px;
|
||||
}
|
||||
|
||||
.upload-success {
|
||||
color: #16a34a;
|
||||
}
|
||||
|
||||
.upload-error {
|
||||
color: #dc2626;
|
||||
}
|
||||
|
||||
/* ===== Background Preview ===== */
|
||||
.background-preview {
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.upload-preview-img {
|
||||
max-width: 320px;
|
||||
max-height: 180px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
display: block;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
|
||||
.preview-link {
|
||||
font-size: 13px;
|
||||
color: #1a73e8;
|
||||
}
|
||||
|
||||
/* ===== Form Section Title ===== */
|
||||
.form-section-title {
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
color: #374151;
|
||||
margin: 32px 0 16px;
|
||||
padding-bottom: 8px;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
}
|
||||
|
||||
.form-section-title:first-of-type {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
/* ===== Badges — danger variant ===== */
|
||||
.badge-danger {
|
||||
background: #fef2f2;
|
||||
color: #dc2626;
|
||||
}
|
||||
|
||||
/* ===== Pagination ===== */
|
||||
.pagination {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
margin-top: 20px;
|
||||
padding: 12px 0;
|
||||
}
|
||||
|
||||
.pagination-info {
|
||||
font-size: 14px;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
/* ===== User-Agent cell truncation ===== */
|
||||
.ua-cell {
|
||||
max-width: 200px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
font-size: 12px;
|
||||
color: #9ca3af;
|
||||
}
|
||||
|
||||
/* ===== Code style ===== */
|
||||
code {
|
||||
background: #f3f4f6;
|
||||
padding: 2px 6px;
|
||||
border-radius: 4px;
|
||||
font-family: "Cascadia Code", "Fira Code", "Consolas", monospace;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
/* ===== Admin nav responsive ===== */
|
||||
@media (max-width: 900px) {
|
||||
.admin-nav {
|
||||
flex-wrap: wrap;
|
||||
height: auto;
|
||||
padding: 8px 16px;
|
||||
gap: 8px;
|
||||
}
|
||||
.admin-nav-links {
|
||||
order: 3;
|
||||
width: 100%;
|
||||
flex-wrap: wrap;
|
||||
gap: 2px;
|
||||
}
|
||||
.admin-nav-link {
|
||||
padding: 4px 10px;
|
||||
font-size: 13px;
|
||||
}
|
||||
}
|
||||
|
||||
/* ===== Whitelist Notice ===== */
|
||||
.whitelist-notice {
|
||||
background: #fef3c7;
|
||||
color: #92400e;
|
||||
padding: 12px 16px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 20px;
|
||||
font-size: 14px;
|
||||
border: 1px solid #fbbf24;
|
||||
}
|
||||
|
||||
.whitelist-notice.notice-info {
|
||||
background: #eff6ff;
|
||||
color: #1e40af;
|
||||
border-color: #93c5fd;
|
||||
}
|
||||
|
||||
/* ===== Stats Grid ===== */
|
||||
.stats-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
gap: 20px;
|
||||
margin-bottom: 32px;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.stats-grid {
|
||||
grid-template-columns: repeat(2, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
.stat-card {
|
||||
background: #fff;
|
||||
border-radius: 12px;
|
||||
padding: 24px;
|
||||
text-align: center;
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
|
||||
transition: transform 0.2s, box-shadow 0.2s;
|
||||
}
|
||||
|
||||
.stat-card:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: 36px;
|
||||
font-weight: 700;
|
||||
color: #667eea;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
font-size: 14px;
|
||||
color: #6b7280;
|
||||
}
|
||||
|
||||
/* ===== Badge Variants ===== */
|
||||
.badge-primary {
|
||||
background: #eff6ff;
|
||||
color: #2563eb;
|
||||
}
|
||||
|
||||
.badge-warning {
|
||||
background: #fffbeb;
|
||||
color: #d97706;
|
||||
}
|
||||
|
||||
/* ===== Detail Cell ===== */
|
||||
.detail-cell {
|
||||
max-width: 200px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
/* ===== Filter Form ===== */
|
||||
.filter-form {
|
||||
background: #fff;
|
||||
border-radius: 8px;
|
||||
padding: 16px 20px;
|
||||
margin-bottom: 20px;
|
||||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
|
||||
.filter-row {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
align-items: flex-end;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.filter-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.filter-group label {
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
color: #374151;
|
||||
}
|
||||
|
||||
.filter-group input[type="text"],
|
||||
.filter-group select {
|
||||
padding: 6px 12px;
|
||||
border: 1px solid #d1d5db;
|
||||
border-radius: 6px;
|
||||
font-size: 14px;
|
||||
outline: none;
|
||||
min-width: 160px;
|
||||
}
|
||||
|
||||
.filter-group input:focus,
|
||||
.filter-group select:focus {
|
||||
border-color: #667eea;
|
||||
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
|
||||
}
|
||||
|
||||
.filter-actions-group {
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* upload.js — Generic file upload handler for Portal admin pages.
|
||||
*
|
||||
* Usage:
|
||||
* setupUpload(inputSelector, uploadType)
|
||||
* - inputSelector: CSS selector for the target input field
|
||||
* - uploadType: "icon" or "background" — determines thumbnail dimensions
|
||||
*
|
||||
* Adds a hidden file input and a visible "上传图片" button next to the target input.
|
||||
* After successful upload, the target input value is set to the returned URL.
|
||||
*/
|
||||
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Sets up an upload button next to the specified input field.
|
||||
* @param {string} inputSelector - CSS selector for the input to fill with the URL
|
||||
* @param {string} uploadType - "icon" or "background"
|
||||
*/
|
||||
function setupUpload(inputSelector, uploadType) {
|
||||
var input = document.querySelector(inputSelector);
|
||||
if (!input) return;
|
||||
|
||||
// Create container for the upload button group
|
||||
var wrapper = document.createElement("div");
|
||||
wrapper.className = "upload-btn-wrapper";
|
||||
|
||||
// Create the visible upload button
|
||||
var btn = document.createElement("button");
|
||||
btn.type = "button";
|
||||
btn.className = "upload-btn";
|
||||
btn.textContent = "上传图片";
|
||||
|
||||
// Create the hidden file input
|
||||
var fileInput = document.createElement("input");
|
||||
fileInput.type = "file";
|
||||
fileInput.accept = "image/jpeg,image/png,image/gif";
|
||||
fileInput.style.display = "none";
|
||||
|
||||
// Create status message element
|
||||
var status = document.createElement("span");
|
||||
status.className = "upload-status";
|
||||
|
||||
// Insert wrapper after the input
|
||||
input.parentNode.insertBefore(wrapper, input.nextSibling);
|
||||
wrapper.appendChild(btn);
|
||||
wrapper.appendChild(fileInput);
|
||||
wrapper.appendChild(status);
|
||||
|
||||
// Click button -> open file dialog
|
||||
btn.addEventListener("click", function () {
|
||||
fileInput.click();
|
||||
});
|
||||
|
||||
// File selected -> upload
|
||||
fileInput.addEventListener("change", function () {
|
||||
if (fileInput.files.length === 0) return;
|
||||
|
||||
var file = fileInput.files[0];
|
||||
|
||||
// Validate file size (5MB)
|
||||
if (file.size > 5 * 1024 * 1024) {
|
||||
status.textContent = "文件太大,最大允许 5MB";
|
||||
status.className = "upload-status upload-error";
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate file type
|
||||
var validTypes = ["image/jpeg", "image/png", "image/gif"];
|
||||
if (validTypes.indexOf(file.type) === -1) {
|
||||
status.textContent = "不支持的文件格式";
|
||||
status.className = "upload-status upload-error";
|
||||
return;
|
||||
}
|
||||
|
||||
// Start upload
|
||||
btn.disabled = true;
|
||||
btn.textContent = "上传中...";
|
||||
status.textContent = "";
|
||||
status.className = "upload-status";
|
||||
|
||||
var formData = new FormData();
|
||||
formData.append("file", file);
|
||||
formData.append("type", uploadType);
|
||||
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/admin/upload", true);
|
||||
|
||||
xhr.onload = function () {
|
||||
btn.disabled = false;
|
||||
btn.textContent = "上传图片";
|
||||
|
||||
if (xhr.status === 200) {
|
||||
try {
|
||||
var resp = JSON.parse(xhr.responseText);
|
||||
if (resp.url) {
|
||||
input.value = resp.url;
|
||||
status.textContent = "上传成功!";
|
||||
status.className = "upload-status upload-success";
|
||||
|
||||
// If there's a preview element, update it
|
||||
var preview = input.parentNode.querySelector(".upload-preview");
|
||||
if (preview) {
|
||||
preview.src = resp.url + "?thumb=1";
|
||||
preview.style.display = "block";
|
||||
}
|
||||
} else {
|
||||
status.textContent = resp.error || "上传失败";
|
||||
status.className = "upload-status upload-error";
|
||||
}
|
||||
} catch (e) {
|
||||
status.textContent = "上传失败";
|
||||
status.className = "upload-status upload-error";
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
var resp = JSON.parse(xhr.responseText);
|
||||
status.textContent = resp.error || "上传失败";
|
||||
} catch (e) {
|
||||
status.textContent = "上传失败 (" + xhr.status + ")";
|
||||
}
|
||||
status.className = "upload-status upload-error";
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = function () {
|
||||
btn.disabled = false;
|
||||
btn.textContent = "上传图片";
|
||||
status.textContent = "网络错误";
|
||||
status.className = "upload-status upload-error";
|
||||
};
|
||||
|
||||
xhr.send(formData);
|
||||
});
|
||||
}
|
||||
|
||||
// Expose globally
|
||||
window.setupUpload = setupUpload;
|
||||
})();
|
||||
Reference in New Issue
Block a user