Repository navigation
Expand file tree
/
Copy pathpages.rs
More file actions
345 lines (319 loc) · 17.2 KB
/
Copy pathpages.rs
File metadata and controls
345 lines (319 loc) · 17.2 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
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
use leptos::prelude::*;
use leptos_router::components::A;
pub mod auth;
pub mod component;
pub mod guide; // 新手引导
pub mod help; // 帮助中心
pub mod homepage;
pub mod playground;
pub mod scene_creation;
pub mod share;
pub mod voicefilter;
pub mod voicesetup;
pub mod welcome; // 新增
pub mod workbench;
/// 全局登录状态版本号:登录 / 注册 / 退出 / 修改资料后调用 `changed()`,
/// Header 会据此重新获取用户信息。
#[derive(Clone, Copy)]
pub struct AuthState {
pub version: RwSignal<u32>,
/// 头像更新次数,用于给头像 URL 加版本参数绕过缓存(SSR 与客户端初始均为 0,避免水合不一致)
pub avatar_version: RwSignal<u32>,
}
impl AuthState {
pub fn provide() {
provide_context(Self {
version: RwSignal::new(0),
avatar_version: RwSignal::new(0),
});
}
pub fn use_state() -> Self {
use_context::<Self>().unwrap_or_else(|| Self {
version: RwSignal::new(0),
avatar_version: RwSignal::new(0),
})
}
pub fn changed(&self) {
self.version.update(|v| *v = v.wrapping_add(1));
}
pub fn avatar_changed(&self) {
self.avatar_version.update(|v| *v = v.wrapping_add(1));
self.changed();
}
}
/// 头像地址:版本号为 0 时保持原样,之后追加 `v=` 参数刷新缓存
pub fn avatar_src(url: &str, version: u32) -> String {
if url.trim().is_empty() {
return DEFAULT_AVATAR.to_string();
}
if version == 0 {
url.to_string()
} else if url.contains('?') {
format!("{url}&v={version}")
} else {
format!("{url}?v={version}")
}
}
pub const DEFAULT_AVATAR: &str = "/default-avatar.svg";
/// Keep an unavailable avatar from leaving a broken image; never retry the fallback itself.
pub fn fallback_avatar(ev: leptos::ev::ErrorEvent) {
let image = event_target::<web_sys::HtmlImageElement>(&ev);
if image.get_attribute("src").as_deref() != Some(DEFAULT_AVATAR) {
image.set_src(DEFAULT_AVATAR);
}
}
#[component]
pub fn Header() -> impl IntoView {
// 检查用户登录状态的资源:登录状态变化或路由切换时重新获取
let auth = AuthState::use_state();
let location = leptos_router::hooks::use_location();
let navigate = StoredValue::new(leptos_router::hooks::use_navigate());
let user_resource = Resource::new(
move || (auth.version.get(), location.pathname.get()),
|_| crate::api::user::get_current_user_profile(),
);
let (show_menu, set_show_menu) = signal(false);
view! {
// 顶部导航栏容器
// sticky top-0: 吸顶
// bg-white/70 backdrop-blur-sm: 磨砂玻璃效果
// shadow-soft: 柔和阴影
<header class="py-4 px-6 md:px-12 bg-white/70 backdrop-blur-sm shadow-soft sticky top-0 z-50 transition-all duration-300">
<div class="container mx-auto flex justify-between items-center">
// --- 左侧:Logo ---
// 点击 Logo 返回首页
<A
href="/"
attr:class="flex items-center space-x-2 group text-decoration-none cursor-pointer"
>
// 旋转动画效果
<i class="fa-solid fa-headphones text-primary text-2xl group-hover:rotate-12 transition-transform duration-300"></i>
<h1 class="text-xl md:text-2xl font-bold bg-gradient-to-r from-primary to-secondary bg-clip-text text-transparent">
"耳朵"
</h1>
</A>
// --- 中间:导航菜单 ---
// 使用 flex 和 gap 来排列多个链接
// hidden md:flex: 在移动端隐藏,桌面端显示 (移动端通常需要汉堡菜单,这里暂略)
<nav class="absolute left-1/2 transform -translate-x-1/2 hidden md:flex items-center space-x-1">
// 1. 首页
<A
href="/home"
attr:class="flex items-center space-x-2 px-4 py-2 rounded-full hover:bg-primary/10 transition-colors duration-300 group"
>
<i class="fa-solid fa-house text-gray-400 group-hover:text-primary transition-colors"></i>
<span class="text-gray-600 font-medium group-hover:text-primary transition-colors">
"首页"
</span>
</A>
// 2. 声音生成器
<A
href="/generate"
attr:class="flex items-center space-x-2 px-4 py-2 rounded-full hover:bg-primary/10 transition-colors duration-300 group"
attr:data-guide="nav-generate"
>
<i class="fa-solid fa-wave-square text-gray-400 group-hover:text-primary transition-colors"></i>
<span class="text-gray-600 font-medium group-hover:text-primary transition-colors">
"声音生成"
</span>
</A>
// 3. 声音广场
<A
href="/voice"
attr:class="flex items-center space-x-2 px-4 py-2 rounded-full hover:bg-primary/10 transition-colors duration-300 group"
attr:data-guide="nav-voice"
>
<i class="fa-solid fa-music text-gray-400 group-hover:text-primary transition-colors"></i>
<span class="text-gray-600 font-medium group-hover:text-primary transition-colors">
"声音广场"
</span>
</A>
// 4. 声音滤镜
<A
href="/filters"
attr:class="flex items-center space-x-2 px-4 py-2 rounded-full hover:bg-primary/10 transition-colors duration-300 group"
attr:data-guide="nav-filters"
>
<i class="fa-solid fa-sliders text-gray-400 group-hover:text-primary transition-colors"></i>
<span class="text-gray-600 font-medium group-hover:text-primary transition-colors">
"声音滤镜"
</span>
</A>
// 5. 帮助中心
<A
href="/help"
attr:class="flex items-center space-x-2 px-4 py-2 rounded-full hover:bg-primary/10 transition-colors duration-300 group"
attr:data-guide="nav-help"
>
<i class="fa-solid fa-circle-question text-gray-400 group-hover:text-primary transition-colors"></i>
<span class="text-gray-600 font-medium group-hover:text-primary transition-colors">
"帮助"
</span>
</A>
</nav>
// --- 右侧:头像框 ---
<div class="relative group" data-guide="account">
<Transition fallback=move || {
view! {
<div class="w-10 h-10 rounded-full bg-gray-200 animate-pulse"></div>
}
}>
{move || {
match user_resource.get() {
Some(Ok(Some(user))) => {
view! {
<div class="relative">
<button
class="w-10 h-10 rounded-full p-[2px] bg-gradient-to-tr from-primary to-accent shadow-sm hover:shadow-md transition-all duration-300 overflow-hidden"
on:click=move |_| set_show_menu.update(|v| *v = !*v)
>
<div class="w-full h-full rounded-full bg-white flex items-center justify-center overflow-hidden font-bold text-primary">
// 显示头像
{if user.usermeta.avatar_url.starts_with("/api/")
|| user.usermeta.avatar_url.starts_with("http")
{
view! {
<img
src=avatar_src(&user.usermeta.avatar_url, auth.avatar_version.get_untracked())
alt="avatar"
on:error=fallback_avatar
class="w-full h-full object-cover"
/>
}
.into_any()
} else {
view! { <i class="fa-solid fa-user text-lg"></i> }
.into_any()
}}
</div>
</button>
<Show when=move || show_menu.get()>
// 透明遮罩,点击关闭菜单
<div
class="fixed inset-0 z-40"
on:click=move |_| set_show_menu.set(false)
></div>
// 下拉菜单
<div class="absolute right-0 mt-2 w-32 bg-white rounded-lg shadow-lg py-2 z-50 border border-gray-100 animate-fade-in">
<A
href="/profile"
attr:class="block px-4 py-2 text-sm text-gray-700 hover:bg-primary/10 hover:text-primary transition-colors"
on:click=move |_| set_show_menu.set(false)
>
<i class="fa-solid fa-user mr-2"></i>
"我的主页"
</A>
<A
href="/settings"
attr:class="block px-4 py-2 text-sm text-gray-700 hover:bg-primary/10 hover:text-primary transition-colors"
on:click=move |_| set_show_menu.set(false)
>
<i class="fa-solid fa-gear mr-2"></i>
"设置"
</A>
<div class="border-t border-gray-100 my-1"></div>
<button
class="w-full text-left block px-4 py-2 text-sm text-red-500 hover:bg-red-50 transition-colors"
on:click=move |_| {
set_show_menu.set(false);
let navigate = navigate.get_value();
leptos::task::spawn_local(async move {
let _ = crate::api::user::logout().await;
auth.changed();
navigate("/", Default::default());
});
}
>
<i class="fa-solid fa-right-from-bracket mr-2"></i>
"退出登录"
</button>
</div>
</Show>
</div>
}
.into_any()
}
_ => {
view! {
<A href="/login">
<button class="px-4 py-2 rounded-full bg-dark text-white text-sm hover:bg-gray-700 transition-colors shadow-sm">
"登录"
</button>
</A>
}
.into_any()
}
}
}}
</Transition>
</div>
</div>
</header>
}
}
#[component]
pub fn NotFound() -> impl IntoView {
view! {
// 容器:flex 居中,使用稍微深一点的背景色突出卡片
<div class="min-h-[80vh] flex flex-col items-center justify-center text-center px-4">
// 错误信息卡片 (前景)
// shadow-2xl: 更深的阴影
// max-w-3xl: 稍微加宽一点
<div class="relative z-10 bg-white p-12 md:p-20 rounded-3xl shadow-2xl border border-gray-100 max-w-3xl w-full mx-auto overflow-visible">
// 图标区域
<div class="mb-8 relative inline-block">
// 背景光晕 (调小模糊半径 blur-2xl)
<div class="absolute inset-0 bg-primary/20 rounded-full blur-2xl transform scale-110"></div>
// 图标本身:SVG
// text-primary: 保持主题色
// w-32 h-32 md:w-40 md:h-40: 调小尺寸 (128px - 160px),更精致
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
// 稍微加粗线条,因为图标变小了
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
class="w-32 h-32 md:w-40 md:h-40 text-primary relative z-10 animate-bounce-slow"
>
<path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3Z" />
<path d="M12 9v4" />
<path d="M12 17h.01" />
</svg>
</div>
<h2 class="text-4xl md:text-5xl font-black text-dark mb-6 tracking-tight relative z-20">
"哎呀!页面走丢了"
</h2>
<div class="text-gray-500 text-lg md:text-xl mb-10 leading-relaxed space-y-2 relative z-20">
<p>"你寻找的声音似乎不在这里..."</p>
<p>"但不要灰心,也许它藏在别的地方。"</p>
</div>
// 返回首页按钮
<a
href="/"
class="relative z-20 inline-flex items-center px-8 py-4 bg-primary hover:bg-primary/90 text-white text-lg font-bold rounded-2xl transition-all shadow-lg hover:shadow-xl hover:-translate-y-1 active:translate-y-0 active:shadow-md group"
>
// Home icon SVG
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="mr-2 w-5 h-5 group-hover:scale-110 transition-transform"
>
<path d="m3 9 9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" />
<polyline points="9 22 9 12 15 12 15 22" />
</svg>
"返回首页"
</a>
</div>
</div>
}
}