feat: 重构首页模板,双栏现代化布局 + 移动端适配

- 全新 Hero Banner:渐变背景,用户欢迎语
- 左侧最新文章卡片列表,含标题/摘要/作者/日期
- 右侧侧边栏:用户卡片(修复头像显示)、社区统计、快捷入口
- 完全重写 home.css,现代化样式,sticky 侧边栏
- 移动端适配:768px 单列布局,比例缩放
- 后端路由注入 PostService,获取最新 6 篇 approved 文章
This commit is contained in:
2026-05-31 19:51:47 +08:00
parent b75e1867f6
commit dae2a38e79
5 changed files with 446 additions and 356 deletions

View File

@ -23,6 +23,7 @@ type dependencies struct {
settingsCtrl *controller.SettingsController
messageCtrl *controller.MessageController
postCtrl *controller.PostController
postService *service.PostService
spaceCtrl *controller.SpaceController
studioCtrl *controller.StudioController
adminPostCtrl *adminCtrl.AdminPostController

View File

@ -49,7 +49,7 @@ func buildDeps(db *gorm.DB, cfg *config.Config, siteSettings *config.SiteSetting
return &dependencies{
authCtrl: authCtrl, authMdw: authMdw, maintenanceMdw: maintenanceMdw,
settingsCtrl: settingsCtrl,
messageCtrl: messageController, postCtrl: postCtrl, spaceCtrl: spaceCtrl,
messageCtrl: messageController, postCtrl: postCtrl, postService: postService, spaceCtrl: spaceCtrl,
studioCtrl: studioCtrl,
adminPostCtrl: adminPostCtrl,
adminCtrl: adminController, auditCtrl: auditController,

View File

@ -22,9 +22,16 @@ func setupFrontendRoutes(r *gin.Engine, cfg *config.Config, d *dependencies) {
})
{
pages.GET("/", func(c *gin.Context) {
posts, total, err := d.postService.List("", 1, 6)
if err != nil {
posts = nil
total = 0
}
c.HTML(http.StatusOK, "home/index.html", common.BuildPageData(c, gin.H{
"Title": "首页",
"ExtraCSS": "/static/css/home.css",
"Posts": posts,
"PostCount": total,
}))
})

View File

@ -3,36 +3,116 @@
{{template "layout/nav.html" .}}
<div class="home-layout">
<main class="hero">
<div class="container">
<h1>Meta<span>Lab</span></h1>
<!-- Hero Banner -->
<section class="hero-banner">
<div class="hero-bg"></div>
<div class="hero-content">
<h1 class="hero-title">Meta<span class="hero-accent">Lab</span></h1>
<p class="hero-tagline">下一代开发者社区</p>
{{if .IsLoggedIn}}
<p class="hero-desc">欢迎回来,{{.Username}}</p>
<p class="hero-welcome">欢迎回来,{{.Username}} 👋</p>
{{else}}
<p class="hero-desc">一个为构建者准备的纯粹空间</p>
<p class="hero-welcome">一个为构建者准备的纯粹空间</p>
{{end}}
</div>
</section>
<!-- Main Content -->
<div class="container home-main">
<div class="home-grid">
<!-- Left: Article Feed -->
<main class="home-feed">
<div class="feed-header">
<h2 class="feed-title">最新文章</h2>
<a href="/posts" class="feed-more">查看全部 →</a>
</div>
{{if .Posts}}
<div class="article-list">
{{range .Posts}}
<article class="article-card">
<div class="article-body">
<h3 class="article-title">
<a href="/posts/{{.ID}}">{{.Title}}</a>
</h3>
<p class="article-excerpt">{{.Excerpt}}</p>
<div class="article-meta">
<span class="article-author">{{.AuthorName}}</span>
<span class="meta-dot">·</span>
<span class="article-time">{{.CreatedAt.Format "2006-01-02"}}</span>
</div>
</div>
</article>
{{end}}
</div>
{{else}}
<div class="empty-feed">
<div class="empty-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" width="48" height="48"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/></svg>
</div>
<p class="empty-text">暂无文章,成为第一个分享的人吧</p>
{{if .IsLoggedIn}}
<a href="/studio/write" class="btn-primary">开始创作</a>
{{end}}
</div>
{{end}}
</main>
<aside class="sidebar">
<!-- Right: Sidebar -->
<aside class="home-sidebar">
<!-- User Card -->
<div class="sidebar-card user-card">
{{if .IsLoggedIn}}
<div class="user-card-avatar">{{slice .Username 0 1}}</div>
{{if .Avatar}}
<img src="{{.Avatar}}" alt="{{.Username}}" class="user-avatar-img" onerror="this.style.display='none';this.nextElementSibling.style.display='flex'">
<div class="user-avatar-fallback" style="display:none">{{substr .Username 0 1}}</div>
{{else}}
<div class="user-avatar-fallback">{{substr .Username 0 1}}</div>
{{end}}
<h3 class="user-card-name">{{.Username}}</h3>
<p class="user-card-desc">欢迎加入 MetaLab</p>
<a href="/settings/profile" class="user-card-link">个人设置</a>
<a href="/space" class="user-card-link">我的空间</a>
{{else}}
<div class="user-card-avatar">
<div class="user-avatar-fallback guest-avatar">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" width="28" height="28"><circle cx="12" cy="8" r="4"/><path d="M4 20c0-4 4-7 8-7s8 3 8 7"/></svg>
</div>
<h3 class="user-card-name">未登录</h3>
<p class="user-card-desc">登录后解锁社区功能</p>
<a href="/auth/login" class="user-card-btn">登录</a>
<a href="/auth/login" class="btn-login">登录</a>
{{end}}
</div>
<!-- Community Stats -->
<div class="sidebar-card stats-card">
<h3 class="stats-title">社区动态</h3>
<div class="stats-grid">
<div class="stat-item">
<span class="stat-value">{{.PostCount}}</span>
<span class="stat-label">篇文章</span>
</div>
</div>
</div>
<!-- Quick Links -->
<div class="sidebar-card links-card">
<h3 class="links-title">快捷入口</h3>
<a href="/posts" class="links-item">
<svg class="links-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>
浏览文章
</a>
{{if .IsLoggedIn}}
<a href="/studio/write" class="links-item">
<svg class="links-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 20h9"/><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"/></svg>
发布文章
</a>
<a href="/space" class="links-item">
<svg class="links-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
个人空间
</a>
{{end}}
</div>
</aside>
</div>
</div>
{{template "layout/footer.html" .}}

View File

@ -1,341 +1,222 @@
/* ============================================================
MetaLab Home Styles — 首页专属样式Hero / Plans / Features / Timeline
仅在首页加载,通过 ExtraCSS 注入
MetaLab Home Styles — 首页专属样式
现代化双栏布局,移动端优先,体验至上
============================================================ */
/* ---------- Two-column layout ---------- */
.home-layout {
display: flex;
min-height: 100vh;
align-items: stretch;
}
/* ---------- Hero ---------- */
.hero {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
padding: 4rem 0;
/* ---------- Hero Banner ---------- */
.hero-banner {
position: relative;
text-align: center;
background: linear-gradient(180deg, #fff 0%, #f0f4f8 100%);
min-width: 0;
padding: 3.5rem 1.5rem 3rem;
overflow: hidden;
background: linear-gradient(135deg, #3498db 0%, #2980b9 30%, #2c3e50 100%);
}
.hero h1 {
font-size: 4rem;
.hero-bg {
position: absolute;
inset: 0;
background: radial-gradient(circle at 20% 50%, rgba(255,255,255,.08) 0%, transparent 60%),
radial-gradient(circle at 80% 30%, rgba(255,255,255,.05) 0%, transparent 50%);
pointer-events: none;
}
.hero-content {
position: relative;
z-index: 1;
max-width: 600px;
margin: 0 auto;
}
.hero-title {
font-size: 3.2rem;
font-weight: 800;
color: var(--color-primary);
margin-bottom: .6rem;
line-height: 1.1;
letter-spacing: -1px;
color: #fff;
margin-bottom: .5rem;
letter-spacing: -.02em;
line-height: 1.15;
}
.hero h1 span {
color: var(--color-accent);
.hero-accent {
color: rgba(255,255,255,.65);
font-weight: 700;
}
.hero-tagline {
font-size: 1.5rem;
color: var(--color-secondary);
margin-bottom: 1.5rem;
font-size: 1.15rem;
color: rgba(255,255,255,.75);
font-weight: 400;
margin-bottom: .75rem;
}
.hero-welcome {
font-size: .95rem;
color: rgba(255,255,255,.55);
font-weight: 400;
}
.hero-desc {
font-size: 1.15rem;
color: var(--color-text-light);
max-width: 500px;
margin: 0 auto 2.2rem;
line-height: 1.8;
/* ---------- Main Layout ---------- */
.home-main {
padding: 2rem 20px 3rem;
}
.hero-actions {
display: flex;
justify-content: center;
gap: 1rem;
flex-wrap: wrap;
}
.cta-button,
.secondary-button {
display: inline-block;
padding: .9rem 2.2rem;
border-radius: var(--radius);
font-weight: 600;
font-size: 1.1rem;
transition: var(--transition);
}
.cta-button {
background: var(--color-accent);
color: #fff;
border: 2px solid var(--color-accent);
}
.cta-button:hover {
background: transparent;
color: var(--color-accent);
}
.secondary-button {
background: transparent;
color: var(--color-accent);
border: 2px solid var(--color-accent);
}
.secondary-button:hover {
background: var(--color-accent);
color: #fff;
}
/* ---------- Shared section headings ---------- */
.section-title {
text-align: center;
font-size: 2.2rem;
color: var(--color-primary);
margin-bottom: .8rem;
}
.section-subtitle {
text-align: center;
color: var(--color-secondary);
font-size: 1.1rem;
max-width: 600px;
margin: 0 auto 3.5rem;
}
/* ---------- Plans ---------- */
.plans {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
padding: 1.5rem 0;
}
.plans .section-title {
font-size: 2rem;
margin-bottom: .4rem;
}
.plans .section-subtitle {
margin-bottom: 1.2rem;
}
.plans-grid {
.home-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 1.25rem;
grid-template-columns: 1fr 340px;
gap: 2rem;
align-items: start;
}
.plan-card {
background: var(--color-surface);
border-radius: var(--radius);
padding: 1.5rem;
box-shadow: var(--shadow);
transition: var(--transition);
border: 1px solid var(--color-border);
/* ---------- Feed (Left) ---------- */
.home-feed {
min-width: 0;
}
.feed-header {
display: flex;
flex-direction: column;
position: relative;
justify-content: space-between;
align-items: baseline;
margin-bottom: 1.25rem;
}
.plan-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,.12);
}
.plan-icon {
width: 50px;
height: 50px;
background: rgba(52,152,219,.1);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 1rem;
color: var(--color-accent);
font-size: 1.6rem;
}
.plan-card h3 {
font-size: 1.25rem;
font-weight: 600;
.feed-title {
font-size: 1.35rem;
font-weight: 700;
color: var(--color-primary);
margin-bottom: .5rem;
}
.plan-card p {
color: var(--color-text-light);
margin-bottom: 1rem;
flex-grow: 1;
font-size: .95rem;
}
.plan-status {
display: inline-block;
background: #e8f4fc;
.feed-more {
font-size: .88rem;
color: var(--color-accent);
padding: .3rem .9rem;
border-radius: 20px;
font-size: .85rem;
font-weight: 500;
text-decoration: none;
transition: opacity .2s ease;
}
/* ---------- Features ---------- */
.features {
min-height: 100vh;
.feed-more:hover {
opacity: .75;
}
/* ---------- Article Cards ---------- */
.article-list {
display: flex;
flex-direction: column;
justify-content: center;
padding: 4rem 0;
gap: 1rem;
}
.article-card {
background: var(--color-surface);
border-radius: 10px;
border: 1px solid var(--color-border);
padding: 1.35rem 1.5rem;
transition: box-shadow .2s ease, transform .2s ease;
cursor: default;
}
.features-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 2.5rem;
margin-top: 3rem;
.article-card:hover {
box-shadow: 0 4px 20px rgba(0,0,0,.08);
transform: translateY(-1px);
}
.feature {
text-align: center;
}
.feature .icon {
font-size: 2.5rem;
color: var(--color-accent);
margin-bottom: 1.5rem;
}
.feature h3 {
font-size: 1.3rem;
margin-bottom: .8rem;
color: var(--color-primary);
}
.feature p {
color: var(--color-text-light);
}
/* ---------- Timeline ---------- */
.section-group {
height: 100vh;
display: flex;
flex-direction: column;
}
.timeline-vision {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
padding: 1.5rem 0 1rem;
.article-body {
overflow: hidden;
}
.timeline-vision .section-title {
margin-bottom: .3rem;
}
.timeline-vision .section-subtitle {
margin-bottom: 1.5rem;
}
.timeline {
max-width: 1100px;
margin: 0 auto;
position: relative;
display: flex;
justify-content: center;
gap: 2rem;
padding: 2rem 0 1rem;
overflow-x: auto;
overflow-y: visible;
-webkit-overflow-scrolling: touch;
overscroll-behavior-y: contain;
}
.timeline::before {
content: '';
position: absolute;
left: 4%;
top: 1.9rem;
width: 92%;
height: 3px;
background: var(--color-accent);
border-radius: 2px;
}
.timeline-item {
display: flex;
flex-direction: column;
align-items: center;
position: relative;
flex-shrink: 0;
min-width: 200px;
max-width: 240px;
padding: 0 .5rem;
}
.timeline-marker {
width: 24px;
height: 24px;
background: var(--color-accent);
border-radius: 50%;
border: 4px solid var(--color-surface);
box-shadow: var(--shadow);
margin-bottom: 1.5rem;
z-index: 1;
flex-shrink: 0;
}
.timeline-content {
background: var(--color-surface);
border-radius: var(--radius);
padding: 1.5rem 1.2rem;
box-shadow: var(--shadow);
border: 1px solid var(--color-border);
text-align: center;
width: 100%;
transition: var(--transition);
}
.timeline-item:hover .timeline-content {
transform: translateY(-4px);
box-shadow: 0 10px 20px rgba(0,0,0,.12);
}
.timeline-title {
.article-title {
font-size: 1.15rem;
font-weight: 600;
font-weight: 650;
margin-bottom: .45rem;
line-height: 1.4;
}
.article-title a {
color: var(--color-primary);
margin-bottom: .5rem;
text-decoration: none;
transition: color .15s ease;
}
.timeline-desc {
.article-title a:hover {
color: var(--color-accent);
}
.article-excerpt {
font-size: .9rem;
color: var(--color-text-light);
font-size: .95rem;
line-height: 1.6;
margin-bottom: .75rem;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
/* ---------- Sidebar ---------- */
.sidebar {
width: 300px;
flex-shrink: 0;
padding: 4rem 20px 4rem 0;
.article-meta {
display: flex;
align-items: center;
gap: .35rem;
font-size: .82rem;
color: var(--color-secondary);
}
.article-author {
font-weight: 500;
color: var(--color-text-light);
}
.meta-dot {
color: var(--color-border);
}
/* ---------- Empty Feed ---------- */
.empty-feed {
text-align: center;
padding: 3.5rem 1.5rem;
background: var(--color-surface);
border-radius: 10px;
border: 1px solid var(--color-border);
}
.empty-icon {
color: var(--color-border);
margin-bottom: 1rem;
}
.empty-text {
font-size: .95rem;
color: var(--color-text-light);
margin-bottom: 1.25rem;
}
.btn-primary {
display: inline-block;
padding: .6rem 1.8rem;
background: var(--color-accent);
color: #fff;
border-radius: 8px;
font-size: .9rem;
font-weight: 600;
text-decoration: none;
transition: background .2s ease, transform .15s ease;
}
.btn-primary:hover {
background: #2980b9;
transform: translateY(-1px);
}
/* ---------- Sidebar (Right) ---------- */
.home-sidebar {
display: flex;
flex-direction: column;
gap: 1rem;
gap: 1.25rem;
position: sticky;
top: 5rem;
}
.sidebar-card {
background: var(--color-surface);
border-radius: var(--radius);
box-shadow: var(--shadow);
border-radius: 10px;
border: 1px solid var(--color-border);
padding: 1.5rem;
padding: 1.35rem 1.5rem;
}
/* --- User Card --- */
@ -343,93 +224,214 @@
text-align: center;
}
.user-card-avatar {
width: 56px;
height: 56px;
.user-avatar-img {
width: 64px;
height: 64px;
border-radius: 50%;
background: var(--color-accent);
object-fit: cover;
border: 3px solid var(--color-accent);
display: block;
margin: 0 auto .75rem;
}
.user-avatar-fallback {
width: 64px;
height: 64px;
border-radius: 50%;
background: linear-gradient(135deg, var(--color-accent), #2980b9);
color: #fff;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto .8rem;
font-size: 1.5rem;
margin: 0 auto .75rem;
font-size: 1.6rem;
font-weight: 700;
text-transform: uppercase;
}
.guest-avatar {
background: var(--color-border);
}
.guest-avatar svg {
color: var(--color-text-light);
}
.user-card-name {
font-size: 1.1rem;
font-weight: 600;
font-weight: 650;
color: var(--color-primary);
margin-bottom: .3rem;
margin-bottom: .25rem;
word-break: break-all;
}
.user-card-desc {
font-size: .85rem;
font-size: .82rem;
color: var(--color-text-light);
margin-bottom: 1rem;
}
.user-card-link {
display: inline-block;
display: block;
font-size: .85rem;
color: var(--color-accent);
font-weight: 500;
transition: var(--transition);
text-decoration: none;
padding: .45rem 0;
border-radius: 6px;
background: rgba(52,152,219,.06);
transition: background .15s ease;
}
.user-card-link:hover {
opacity: .8;
background: rgba(52,152,219,.12);
}
.user-card-btn {
display: inline-block;
padding: .5rem 1.8rem;
.btn-login {
display: block;
padding: .55rem 0;
background: var(--color-accent);
color: #fff;
border-radius: var(--radius);
border-radius: 8px;
font-size: .9rem;
font-weight: 500;
transition: var(--transition);
font-weight: 600;
text-decoration: none;
transition: background .2s ease, transform .15s ease;
}
.user-card-btn:hover {
opacity: .85;
.btn-login:hover {
background: #2980b9;
transform: translateY(-1px);
}
/* --- Stats Card --- */
.stats-title {
font-size: .92rem;
font-weight: 650;
color: var(--color-primary);
margin-bottom: .85rem;
}
.stats-grid {
display: flex;
gap: 1rem;
}
.stat-item {
text-align: center;
flex: 1;
}
.stat-value {
display: block;
font-size: 1.5rem;
font-weight: 700;
color: var(--color-accent);
line-height: 1;
margin-bottom: .25rem;
}
.stat-label {
font-size: .78rem;
color: var(--color-text-light);
}
/* --- Links Card --- */
.links-title {
font-size: .92rem;
font-weight: 650;
color: var(--color-primary);
margin-bottom: .75rem;
}
.links-item {
display: flex;
align-items: center;
gap: .6rem;
padding: .55rem .5rem;
font-size: .88rem;
color: var(--color-text-light);
border-radius: 6px;
text-decoration: none;
transition: background .15s ease, color .15s ease;
}
.links-item:hover {
background: var(--color-bg);
color: var(--color-accent);
}
.links-icon {
width: 18px;
height: 18px;
flex-shrink: 0;
}
/* ---------- Responsive ---------- */
@media (max-width: 960px) {
.home-grid {
grid-template-columns: 1fr 280px;
gap: 1.5rem;
}
.hero-title {
font-size: 2.6rem;
}
}
/* ---------- Responsive (首页专属) ---------- */
@media (max-width: 768px) {
.hero h1 {
font-size: 3rem;
.hero-banner {
padding: 2.5rem 1.25rem 2.25rem;
}
.hero-title {
font-size: 2.2rem;
}
.hero-tagline {
font-size: 1.25rem;
font-size: 1rem;
}
.hero-desc {
.home-main {
padding: 1.5rem 16px 2rem;
}
.home-grid {
grid-template-columns: 1fr;
gap: 1.5rem;
}
.home-sidebar {
position: static;
}
.article-card {
padding: 1.1rem 1.2rem;
}
.article-title {
font-size: 1.05rem;
}
}
.timeline {
justify-content: flex-start;
gap: 1.5rem;
padding: 2rem 20px 1.5rem;
@media (max-width: 480px) {
.hero-banner {
padding: 2rem 1rem 1.75rem;
}
.timeline-item {
min-width: 240px;
max-width: 280px;
.hero-title {
font-size: 1.8rem;
}
.timeline::before {
left: 20px;
width: calc(240px * 4 + 1.5rem * 3 + 20px);
.hero-welcome {
font-size: .85rem;
}
.cta-button,
.secondary-button {
display: block;
width: 100%;
margin: .5rem 0;
.feed-title {
font-size: 1.15rem;
}
.secondary-button {
margin-left: 0;
.home-main {
padding: 1rem 12px 1.5rem;
}
}