用户中心案例

wen java案例 3

本文目录导读:

用户中心案例

  1. 核心功能模块
  2. 完整代码案例(可直接运行)
  3. 代码设计与功能拆解
  4. 拓展建议(如何用于实际项目)

我来为你提供一个完整的 用户中心(User Center) 前端案例,这通常是管理后台或APP中最常见的模块,包含个人信息展示、资料编辑、账号安全等核心功能。

为了让你看得更直观,我将用 HTML + CSS (Tailwind) + Vue 3 编写一个高可用的响应式案例,并在下方附上功能拆解拓展建议

核心功能模块

  1. 用户信息卡片: 头像、昵称、等级、简介。
  2. 数据统计: 作品、关注、粉丝、获赞的数量。
  3. 菜单列表: 我的订单、收藏夹、足迹、优惠券。
  4. 安全设置: 绑定手机、修改密码、第三方账号绑定。
  5. 退出登录

完整代码案例(可直接运行)

这是一个基于 Vue 3 和 Tailwind CSS 的响应式组件,为了便于你直接复制预览,我使用了 CDN 方式引入。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">用户中心 - 现代UI案例</title>
    <!-- Tailwind CSS (通过 CDN) -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Vue 3 (通过 CDN) -->
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <!-- 引入图标库 (Font Awesome) -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <style>
        /* 自定义滚动条样式,美化细节 */
        .custom-scrollbar::-webkit-scrollbar {
            width: 4px;
        }
        .custom-scrollbar::-webkit-scrollbar-thumb {
            background-color: #cbd5e1;
            border-radius: 10px;
        }
        /* 渐变背景 */
        .bg-gradiant-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .hover-scale {
            transition: transform 0.2s ease-in-out;
        }
        .hover-scale:hover {
            transform: translateY(-4px);
        }
    </style>
</head>
<body class="bg-gray-100 flex items-start justify-center min-h-screen p-4">
    <!-- Vue 应用挂载点 -->
    <div id="app" class="w-full max-w-md">
        <!-- 用户中心卡片容器 -->
        <div class="bg-white rounded-2xl shadow-xl overflow-hidden">
            <!-- 1. 头部渐变区域(包含封面和头像) -->
            <div class="bg-gradiant-primary h-28 relative">
                <!-- 右上角设置图标 -->
                <button @click="showToast('打开设置')" class="absolute top-4 right-4 text-white hover:rotate-90 transition duration-300">
                    <i class="fas fa-cog text-xl"></i>
                </button>
                <!-- 左下角消息图标 -->
                <button @click="showToast('打开消息')" class="absolute top-4 left-4 text-white hover:scale-110 transition">
                    <i class="fas fa-bell text-xl"></i>
                    <!-- 小红点 -->
                    <span class="absolute -top-1 -right-1 flex h-3 w-3">
                        <span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-red-400 opacity-75"></span>
                        <span class="relative inline-flex rounded-full h-3 w-3 bg-red-500"></span>
                    </span>
                </button>
            </div>
            <!-- 头像区域 (负边距实现重叠效果) -->
            <div class="flex justify-center -mt-12 mb-4 relative z-10">
                <div class="relative">
                    <img class="w-24 h-24 rounded-full border-4 border-white shadow-lg object-cover" 
                         :src="user.avatar" 
                         alt="用户头像">
                    <!-- VIP 标识 -->
                    <span v-if="user.isVip" class="absolute bottom-1 right-1 bg-yellow-400 text-yellow-900 text-[10px] font-bold px-1.5 py-0.5 rounded-full shadow">
                        <i class="fas fa-crown mr-0.5"></i>VIP
                    </span>
                </div>
            </div>
            <!-- 2. 用户基本信息 -->
            <div class="text-center px-4">
                <h2 class="text-xl font-bold text-gray-800 flex items-center justify-center gap-1">
                    {{ user.name }}
                    <!-- 官方认证标识 -->
                    <span v-if="user.verified" class="text-blue-400 text-sm">
                        <i class="fas fa-check-circle"></i>
                    </span>
                </h2>
                <p class="text-sm text-gray-500 mt-1">ID: {{ user.id }}</p>
                <p class="text-sm text-gray-500 mt-2">{{ user.bio }}</p>
                <!-- 操作按钮组 -->
                <div class="flex justify-center gap-3 mt-4">
                    <button @click="showToast('编辑资料')" class="bg-gray-900 text-white text-sm font-medium px-4 py-2 rounded-lg hover:bg-gray-700 transition">
                        <i class="fas fa-edit mr-1"></i> 编辑资料
                    </button>
                    <button @click="showToast('分享主页')" class="border border-gray-300 text-gray-600 text-sm font-medium px-4 py-2 rounded-lg hover:bg-gray-50 transition">
                        <i class="fas fa-share-alt mr-1"></i> 分享
                    </button>
                </div>
            </div>
            <!-- 3. 用户数据统计卡片 -->
            <div class="flex justify-around my-6 px-6">
                <div v-for="stat in stats" :key="stat.label" class="text-center hover-scale cursor-pointer" @click="showToast(stat.label)">
                    <p class="text-xl font-extrabold text-gray-800">{{ stat.value }}</p>
                    <p class="text-xs text-gray-400 mt-1">{{ stat.label }}</p>
                </div>
            </div>
            <!-- 分割线 -->
            <div class="border-t border-dashed border-gray-200 my-2"></div>
            <!-- 4. 功能菜单列表 -->
            <div class="p-4 space-y-1">
                <!-- 灵活使用 v-for 渲染菜单 -->
                <div v-for="item in menuItems" :key="item.title">
                    <button @click="showToast(item.title)" class="w-full flex items-center justify-between p-3 rounded-xl hover:bg-gray-50 transition group">
                        <div class="flex items-center gap-3">
                            <!-- 图标区域(带彩色背景) -->
                            <span class="w-9 h-9 rounded-lg flex items-center justify-center" :class="item.bgColor">
                                <i :class="[item.icon, item.color]" class="text-sm"></i>
                            </span>
                            <span class="font-medium text-gray-700">{{ item.title }}</span>
                        </div>
                        <div class="flex items-center gap-2">
                            <!-- 如果有角标数字 -->
                            <span v-if="item.badge" class="bg-red-500 text-white text-[10px] px-2 py-0.5 rounded-full">{{ item.badge }}</span>
                            <i class="fas fa-chevron-right text-xs text-gray-300 group-hover:text-gray-400 transition"></i>
                        </div>
                    </button>
                </div>
            </div>
            <!-- 5. 退出登录按钮 -->
            <div class="p-4 pt-0">
                <button @click="handleLogout" class="w-full py-2.5 text-red-500 font-medium text-sm border border-red-200 rounded-xl hover:bg-red-50 transition">
                    <i class="fas fa-sign-out-alt mr-2"></i> 退出登录
                </button>
            </div>
        </div>
    </div>
    <script>
        const { createApp, ref, reactive } = Vue;
        createApp({
            setup() {
                // 用户数据
                const user = reactive({
                    name: '张三',
                    id: 'JD123456789',
                    avatar: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=150&h=150&fit=crop&crop=faces&auto=format&q=60',
                    bio: '坚持努力,终将美好。',
                    isVip: true,
                    verified: true
                });
                // 统计数据
                const stats = reactive([
                    { label: '作品', value: '128' },
                    { label: '关注', value: '1.2k' },
                    { label: '粉丝', value: '8.6k' },
                    { label: '获赞', value: '45.3k' }
                ]);
                // 菜单项
                const menuItems = reactive([
                    { title: '我的订单', icon: 'fas fa-shopping-bag', color: 'text-blue-500', bgColor: 'bg-blue-50', badge: '' },
                    { title: '会员中心', icon: 'fas fa-crown', color: 'text-yellow-500', bgColor: 'bg-yellow-50', badge: 'VIP' },
                    { title: '我的收藏', icon: 'fas fa-heart', color: 'text-pink-500', bgColor: 'bg-pink-50', badge: '' },
                    { title: '浏览足迹', icon: 'fas fa-history', color: 'text-purple-500', bgColor: 'bg-purple-50', badge: '' },
                    { title: '优惠券', icon: 'fas fa-ticket-alt', color: 'text-green-500', bgColor: 'bg-green-50', badge: '3' },
                    { title: '联系客服', icon: 'fas fa-headset', color: 'text-orange-500', bgColor: 'bg-orange-50', badge: '' },
                    { title: '设置与隐私', icon: 'fas fa-shield-alt', color: 'text-gray-500', bgColor: 'bg-gray-100', badge: '' }
                ]);
                // Toast 提示 (简单模拟)
                const showToast = (msg) => {
                    alert(`点击了: ${msg}`); // 这里可替换为优雅的 toast 组件
                };
                // 退出
                const handleLogout = () => {
                    if(confirm('确定要退出登录吗?')) {
                        alert('已退出登录(演示)');
                    }
                };
                return { user, stats, menuItems, showToast, handleLogout };
            }
        }).mount('#app');
    </script>
</body>
</html>

代码设计与功能拆解

你可以看到这个案例不仅静态好看,且交互逻辑清晰:

  1. 数据驱动 (Vue Reactive)

    • 所有的用户信息、统计数据、菜单列表都存储在 user, stats, menuItems 中,后续接入后端 API,只需替换这些值即可。
  2. 视觉层次

    • 使用同色系渐变营造高级感。
    • 利用负边距 (-mt-12) 让头像“破框而出”,这是目前最流行的设计趋势。
    • 卡片式的菜单项 (rounded-xl) 配上柔和的颜色,让点击区域更大。
  3. 交互细节

    • 红色通知小红点的呼吸灯效果 (animate-ping)。
    • 菜单项右侧的角标 (Badge) 用于通知未读数。
    • 所有可点击元素都带有悬停状态 (hover:scalehover:bg),提升操作反馈。

拓展建议(如何用于实际项目)

如果你要把它做成一个真正的后台或 App 页面,可以这样拓展:

  1. 替换静态 URL:将 avatar 链接替换为真实的图片上传/裁剪组件。
  2. 路由跳转:将 @click="showToast(...)" 替换为 vue-routerrouter.push('/order')uni.navigateTo
  3. 组件化
    • 菜单列表 拆分为 MenuItem.vue 组件。
    • 用户信息 拆分为 UserProfile.vue 组件。
  4. 状态管理:如果多个页面需要用户信息,可以把 user 数据放入 PiniaVuex 中进行全局共享。
  5. 深色模式:Tailwind 支持 dark: 前缀,可以一键添加深色模式样式。

你可以把这段代码保存为 index.html 并在浏览器中打开,预览这个精美的用户中心,如果需要整合到 Vue 单文件组件 (SFC) 中,也只需要将 templatescriptstyle 部分抽离即可。

抱歉,评论功能暂时关闭!