Next.js项目MindAI教程 - 第二章:基础架构搭建

ops/2025/3/19 19:20:27/

1. Tailwind CSS 配置

1.1 自定义主题配置

// tailwind.config.ts
import type { Config } from 'tailwindcss'const config: Config = {content: ['./src/pages/**/*.{js,ts,jsx,tsx,mdx}','./src/components/**/*.{js,ts,jsx,tsx,mdx}','./src/app/**/*.{js,ts,jsx,tsx,mdx}',],theme: {extend: {colors: {primary: {50: '#f0f9ff',100: '#e0f2fe',200: '#bae6fd',300: '#7dd3fc',400: '#38bdf8',500: '#0ea5e9',600: '#0284c7',700: '#0369a1',800: '#075985',900: '#0c4a6e',},secondary: {// 添加次要颜色DEFAULT: '#10b981',},},fontFamily: {sans: ['var(--font-inter)'],},},},plugins: [require('@tailwindcss/forms')],
}export default config

1.2 全局样式设置

/* src/app/globals.css */
@tailwind base;
@tailwind components;
@tailwind utilities;@layer components {.btn-primary {@apply px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2;}.btn-secondary {@apply px-4 py-2 bg-gray-200 text-gray-800 rounded-lg hover:bg-gray-300 transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-gray-400 focus:ring-offset-2;}.input-primary {@apply block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring-primary-500;}
}

2. 布局组件创建

2.1 导航栏组件

// src/components/layout/Navbar.tsx
'use client'import { useState } from 'react'
import Link from 'next/link'
import { usePathname } from 'next/navigation'const navigation = [{ name: '首页', href: '/' },{ name: 'AI测评', href: '/assessment' },{ name: '情绪检测', href: '/emotion' },{ name: '在线咨询', href: '/consultation' },{ name: '社区', href: '/community' },
]export default function Navbar() {const pathname = usePathname()const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false)return (<nav className="bg-white shadow"><div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8"><div className="flex justify-between h-16"><div className="flex"><div className="flex-shrink-0 flex items-center"><Link href="/" className="text-2xl font-bold text-primary-600">MindAI</Link></div><div className="hidden sm:ml-6 sm:flex sm:space-x-8">{navigation.map((item) => (<Linkkey={item.name}href={item.href}className={`${pathname === item.href? 'border-primary-500 text-gray-900': 'border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700'} inline-flex items-center px-1 pt-1 border-b-2 text-sm font-medium`}>{item.name}</Link>))}</div></div>{/* 移动端菜单按钮 */}<div className="sm:hidden"><buttontype="button"className="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100"onClick={() => setIsMobileMenuOpen(!isMobileMenuOpen)}><span className="sr-only">打开主菜单</span>{/* 这里可以添加汉堡菜单图标 */}</button></div></div></div>{/* 移动端菜单 */}{isMobileMenuOpen && (<div className="sm:hidden"><div className="pt-2 pb-3 space-y-1">{navigation.map((item) => (<Linkkey={item.name}href={item.href}className={`${pathname === item.href? 'bg-primary-50 border-primary-500 text-primary-700': 'border-transparent text-gray-500 hover:bg-gray-50 hover:border-gray-300 hover:text-gray-700'} block pl-3 pr-4 py-2 border-l-4 text-base font-medium`}>{item.name}</Link>))}</div></div>)}</nav>)
}

2.2 页面布局组件

// src/components/layout/PageLayout.tsx
import Navbar from './Navbar'
import Footer from './Footer'export default function PageLayout({children,
}: {children: React.ReactNode
}) {return (<div className="min-h-screen flex flex-col"><Navbar /><main className="flex-grow"><div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">{children}</div></main><Footer /></div>)
}

2.3 页脚组件

// src/components/layout/Footer.tsx
export default function Footer() {return (<footer className="bg-white border-t"><div className="max-w-7xl mx-auto py-12 px-4 sm:px-6 lg:px-8"><div className="grid grid-cols-1 md:grid-cols-4 gap-8"><div><h3 className="text-sm font-semibold text-gray-400 tracking-wider uppercase">关于我们</h3><p className="mt-4 text-base text-gray-500">MindAI致力于提供智能化的心理健康服务,让每个人都能便捷地获得专业的心理支持。</p></div><div><h3 className="text-sm font-semibold text-gray-400 tracking-wider uppercase">服务</h3><ul className="mt-4 space-y-4"><li><a href="#" className="text-base text-gray-500 hover:text-gray-900">AI心理测评</a></li><li><a href="#" className="text-base text-gray-500 hover:text-gray-900">情绪分析</a></li><li><a href="#" className="text-base text-gray-500 hover:text-gray-900">在线咨询</a></li></ul></div>{/* 可以添加更多列 */}</div><div className="mt-8 border-t border-gray-200 pt-8"><p className="text-base text-gray-400 text-center">&copy; {new Date().getFullYear()} MindAI. All rights reserved.</p></div></div></footer>)
}

3. 路由系统设计

3.1 创建基础页面结构

src/app/
├── page.tsx                # 首页
├── assessment/             # AI测评
│   ├── page.tsx           # 测评首页
│   ├── [type]/           # 具体测评类型
│   │   └── page.tsx
│   └── result/           # 测评结果
│       └── page.tsx
├── emotion/               # 情绪检测
│   ├── page.tsx
│   └── history/
│       └── page.tsx
├── consultation/          # 在线咨询
│   ├── page.tsx
│   └── [id]/
│       └── page.tsx
└── community/            # 社区
    ├── page.tsx
    └── post/
        └── [id]/
            └── page.tsx

3.2 实现动态路由示例

// src/app/assessment/[type]/page.tsx
type Props = {params: {type: string}
}export default function AssessmentPage({ params }: Props) {return (<div><h1 className="text-2xl font-bold mb-4">{decodeURIComponent(params.type)}测评</h1>{/* 测评内容 */}</div>)
}

4. 响应式设计实现

4.1 创建响应式容器组件

// src/components/ui/Container.tsx
type ContainerProps = {children: React.ReactNodeclassName?: string
}export function Container({ children, className = '' }: ContainerProps) {return (<div className={`max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 ${className}`}>{children}</div>)
}

4.2 创建响应式卡片组件

// src/components/ui/Card.tsx
type CardProps = {title?: stringchildren: React.ReactNodeclassName?: string
}export function Card({ title, children, className = '' }: CardProps) {return (<div className={`bg-white rounded-lg shadow-sm overflow-hidden ${className}`}>{title && (<div className="px-4 py-5 sm:px-6"><h3 className="text-lg leading-6 font-medium text-gray-900">{title}</h3></div>)}<div className="px-4 py-5 sm:p-6">{children}</div></div>)
}

5 .启动和测试

5.1 更新根布局

// src/app/layout.tsx
import { Inter } from 'next/font/google'
import PageLayout from '@/components/layout/PageLayout'
import './globals.css'const inter = Inter({ subsets: ['latin'] })export default function RootLayout({children,
}: {children: React.ReactNode
}) {return (<html lang="zh-CN"><body className={inter.className}><PageLayout>{children}</PageLayout></body></html>)
}

5.2 测试响应式布局

在浏览器中调整窗口大小,确保所有组件都能正确响应不同的屏幕尺寸。

6. 下一步计划

  • 创建用户认证系统
  • 开发AI功能模块
  • 实现具体业务功能

http://www.ppmy.cn/ops/167095.html

相关文章

C#本地将labelme数据集转换为机器视觉yolo数据集格式

C#本地&#xff0c;将labelme数据集转换为机器视觉yolo数据集格式 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.Encodings.Web; using System.Text.RegularExpressions; using System.Text.U…

【大语言模型_5】xinference部署embedding模型和rerank模型

一、安装xinference pip install xinference 二、启动xinference ./xinference-local --host0.0.0.0 --port5544 三、注册本地模型 1、注册embedding模型 curl -X POST "http://localhost:5544/v1/models" \ -H "Content-Type: application/json" \…

单片机自学总结

自从工作以来&#xff0c;一直努力耕耘单片机&#xff0c;至今&#xff0c;颇有收获。从51单片机&#xff0c;PIC单片机&#xff0c;直到STM32&#xff0c;以及RTOS和Linux&#xff0c;几乎天天在搞:51单片机&#xff0c;STM8S207单片机&#xff0c;PY32F003单片机&#xff0c;…

微信小程序:修改提示信息placeholder颜色

方法一&#xff1a;使用 placeholder-style 直接在 input 或 textarea 组件中使用 placeholder-style 属性来设置 placeholder 的样式。 <input placeholder"请输入内容" placeholder-style"color: #999; font-size: 14px;" /> 或者&#xff1a; …

计算机网络--访问一个网页的全过程

文章目录 访问一个网页的全过程应用层在浏览器输入URL网址http://www.aspxfans.com:8080/news/index.aspboardID5&ID24618&page1#r_70732423通过DNS获取IP地址生成HTTP请求报文应用层最后 传输层传输层处理应用层报文建立TCP连接传输层最后 网络层网络层对TCP报文进行处…

Ollama 0.4 发布!支持 Llama 3.2 Vision,实现多模态 RAG

“ 阅读本文大概需要5分钟。 前言 最近&#xff0c;Ollama 推出了 0.4 版本&#xff0c;其中最大的亮点就是支持了 Llama 3.2 Vision 模型&#xff0c;该模型具备多模态特性&#xff0c;也就是说能够理解图像并将图像纳入提示词中进行处理&#xff0c;让模型更智能地处理RAG中…

hbuiderx的sass编译器报dart-sass等错误的解决方法

HBuilderX 4.5起&#xff0c;vue2的sass编译器由node-sass改为dart-sass。node-sass是已经被淘汰的不再维护的库&#xff0c;且不支持arm cpu。 node-sass有些过期语法在dart-sass上报错导致无法编译。 虽然默认为dart-sass&#xff0c;但HBuilderX 4.56版也提供了选项&#xf…

K8S学习之基础三十四:K8S之监控Prometheus部署pod版

使用 Kubernetes Pod 的方式部署 Prometheus 是一种常见的方法&#xff0c;尤其是在容器化和微服务架构中。以下是详细的步骤&#xff1a; 1. 创建命名空间&#xff08;可选&#xff09; 为了方便管理&#xff0c;可以为 Prometheus 创建一个单独的命名空间。 yaml 复制 a…