tianhunat-query:基于 Next.js 和 Supabase 的核酸检测结果查询系统

用户可通过该系统注册、登录并查询核酸检测结果。支持邮箱验证、密码找回,采用响应式设计适配多端,提供医疗健康主题UI,基于Next.js和Supabase实现安全高效的用户认证与会话管理。【此简介由AI生成】

Branch1Tags0
FilesLast commitLast update
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago

核酸结果查询系统

一个基于 Next.js 和 Supabase 的核酸检测结果在线查询系统,支持用户注册、邮箱验证、登录、找回密码和结果查询功能。

📋 目录

✨ 功能特性

  • ✅ 用户注册(含邮箱验证)
  • ✅ 用户登录(基于 Supabase Auth)
  • ✅ 找回密码(邮箱验证)
  • ✅ 密码重置
  • ✅ 会话管理和中间件保护
  • ✅ 核酸检测结果查询(根据手机号和身份证号匹配)
  • ✅ 响应式设计,支持移动端和桌面端
  • ✅ 美观的医疗健康主题 UI

🛠 技术栈

前端

  • 框架: Next.js 16 (App Router)
  • 语言: TypeScript
  • 样式: Tailwind CSS v4
  • UI 组件: shadcn/ui
  • 状态管理: React Hooks

后端

  • 认证: Supabase Auth
  • 数据库: PostgreSQL (通过 Supabase)
  • API: Next.js API Routes
  • 会话管理: Supabase Session Management

🚀 快速开始

安装依赖

```bash npm install

或

yarn install

或

pnpm install ```

配置环境变量

复制 .env.example 为 .env.local 并填写相应的值:

```bash cp .env.example .env.local ```

运行开发服务器

```bash npm run dev

或

yarn dev

或

pnpm dev ```

访问 http://localhost:3000 查看应用。

⚙️ 环境配置

必需的环境变量

在项目根目录创建 .env.local 文件:

```env

Supabase 配置

NEXT_PUBLIC_SUPABASE_URL=your_supabase_project_url NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key SUPABASE_SERVICE_ROLE_KEY=your_supabase_service_role_key

PostgreSQL 数据库配置

POSTGRES_URL=your_postgres_connection_string POSTGRES_PRISMA_URL=your_postgres_prisma_url POSTGRES_URL_NON_POOLING=your_postgres_non_pooling_url POSTGRES_USER=your_postgres_user POSTGRES_PASSWORD=your_postgres_password POSTGRES_DATABASE=your_postgres_database POSTGRES_HOST=your_postgres_host

Supabase 其他配置

SUPABASE_JWT_SECRET=your_jwt_secret SUPABASE_PUBLISHABLE_KEY=your_publishable_key SUPABASE_SECRET_KEY=your_secret_key

开发环境重定向 URL(可选)

NEXT_PUBLIC_DEV_SUPABASE_REDIRECT_URL=http://localhost:3000/results ```

获取 Supabase 凭证

  1. 访问 Supabase Dashboard
  2. 选择或创建您的项目
  3. 在 Settings > API 中找到:
    • Project URL → NEXT_PUBLIC_SUPABASE_URL
    • anon/public key → NEXT_PUBLIC_SUPABASE_ANON_KEY
    • service_role key → SUPABASE_SERVICE_ROLE_KEY

🗄 数据库设计

核酸检测结果表 (nucleic_acid_results)

该表存储所有核酸检测记录:

```sql CREATE TABLE nucleic_acid_results ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, -- 姓名 id_number VARCHAR(18) NOT NULL, -- 身份证号 phone VARCHAR(11) NOT NULL, -- 手机号 test_date TIMESTAMP NOT NULL, -- 检测日期 test_location VARCHAR(255), -- 检测地点 result VARCHAR(20) NOT NULL, -- 检测结果(阴性/阳性) specimen_type VARCHAR(50), -- 标本类型 test_method VARCHAR(50), -- 检测方法 report_date TIMESTAMP, -- 报告日期 created_at TIMESTAMP DEFAULT NOW(), -- 记录创建时间 updated_at TIMESTAMP DEFAULT NOW() -- 记录更新时间 );

-- 创建索引以提升查询性能 CREATE INDEX idx_phone ON nucleic_acid_results(phone); CREATE INDEX idx_id_number ON nucleic_acid_results(id_number); CREATE INDEX idx_test_date ON nucleic_acid_results(test_date DESC); ```

插入测试数据

```sql INSERT INTO nucleic_acid_results ( name, id_number, phone, test_date, test_location, result, specimen_type, test_method, report_date ) VALUES ( '张三', '110101199001011234', '13800138000', '2024-01-15 10:30:00', '北京市朝阳区检测点', '阴性', '咽拭子', 'RT-PCR', '2024-01-15 18:00:00' ); ```

Supabase Auth 用户元数据

用户注册时,以下信息存储在 Supabase Auth 的 user_metadata 中:

  • name: 用户姓名
  • phone: 手机号
  • id_number: 身份证号

📡 API 接口文档

1. 用户注册

端点: POST /api/auth/sign-up

请求体: ```json { "email": "user@example.com", "password": "password123", "name": "张三", "phone": "13800138000", "idNumber": "110101199001011234" } ```

成功响应 (200): ```json { "success": true, "user": { "id": "uuid", "email": "user@example.com" }, "message": "注册成功!请查看您的邮箱并点击验证链接以完成注册。" } ```

失败响应 (400/500): ```json { "error": "错误信息" } ```

流程说明:

  1. 接收用户注册信息
  2. 调用 Supabase Auth 创建用户
  3. 将姓名、手机号、身份证号存储在用户元数据中
  4. 发送验证邮件到用户邮箱
  5. 用户点击邮件中的链接完成验证

2. 邮箱验证回调

端点: GET /auth/callback?code={verification_code}

功能: 处理 Supabase 邮箱验证回调

流程:

  1. Supabase 发送验证邮件
  2. 用户点击邮件中的链接
  3. 重定向到 /auth/callback 并携带验证码
  4. 后端验证代码并创建会话
  5. 自动跳转到 /results 页面

3. 用户登录

实现位置: app/auth/login/page.tsx (客户端调用)

代码示例: ```typescript const supabase = createBrowserClient() const { data, error } = await supabase.auth.signInWithPassword({ email, password }) ```

流程:

  1. 用户输入邮箱和密码
  2. 调用 Supabase Auth 进行验证
  3. 验证成功后创建会话
  4. 自动跳转到 /results 页面

4. 找回密码

实现位置: app/auth/forgot-password/page.tsx (客户端调用)

代码示例: ```typescript const supabase = createClient() const { error } = await supabase.auth.resetPasswordForEmail(email, { redirectTo: ${window.location.origin}/auth/reset-password }) ```

流程:

  1. 用户输入注册邮箱
  2. 调用 Supabase Auth 发送密码重置邮件
  3. 用户点击邮件中的链接
  4. 重定向到 /auth/reset-password 页面
  5. 用户输入新密码并确认
  6. 密码重置成功后跳转到登录页

5. 重置密码

实现位置: app/auth/reset-password/page.tsx (客户端调用)

代码示例: ```typescript const supabase = createClient() const { error } = await supabase.auth.updateUser({ password: newPassword }) ```

流程:

  1. 用户从密码重置邮件点击链接进入此页面
  2. 输入新密码和确认密码
  3. 调用 Supabase Auth 更新密码
  4. 更新成功后跳转到登录页

6. 查询核酸检测结果

实现位置: app/results/page.tsx (服务端组件)

查询逻辑: ```typescript const supabase = await createClient()

// 获取当前用户 const { data: { user } } = await supabase.auth.getUser()

// 从用户元数据中获取手机号和身份证号 const phone = user.user_metadata.phone const idNumber = user.user_metadata.id_number

// 查询匹配的检测结果 const { data: results } = await supabase .from('nucleic_acid_results') .select('*') .or(phone.eq.${phone},id_number.eq.${idNumber}) .order('test_date', { ascending: false }) ```

返回数据结构: ```typescript interface NucleicAcidResult { id: number name: string id_number: string phone: string test_date: string test_location: string result: string specimen_type: string test_method: string report_date: string created_at: string updated_at: string } ```

7. 用户退出登录

实现位置: app/results/page.tsx (服务端 Action)

代码示例: ```typescript async function signOut() { "use server" const supabase = await createClient() await supabase.auth.signOut() redirect('/auth/login') }

// 在组件中使用

\`\`\`

🗺 页面路由

公开路由

路由 页面 说明
/ 首页 系统介绍和入口
/auth/login 登录页 用户登录
/auth/sign-up 注册页 用户注册
/auth/verify-email 邮箱验证提示 提示用户查看邮箱
/auth/forgot-password 忘记密码 发送密码重置邮件
/auth/reset-password 重置密码 设置新密码
/auth/callback 验证回调 处理邮箱验证回调

受保护路由

路由 页面 说明
/results 结果查询页 显示用户的核酸检测结果

保护机制:

  • 使用 proxy.ts (middleware) 检查用户会话
  • 未登录用户访问受保护路由将被重定向到登录页

📧 邮件模板配置

1. 邮箱验证模板

在 Supabase Dashboard 中配置邮箱验证模板。

配置路径: Authentication > Email Templates > Confirm signup

HTML 模板:

```html

      <!-- Content -->
      <tr>
        <td style="padding: 48px 48px 32px">
          <h2 style="margin: 0 0 16px; color: #0f172a; font-size: 24px; font-weight: 600">验证您的邮箱地址</h2>
          <p style="margin: 0 0 24px; color: #475569; font-size: 16px; line-height: 1.6">
            感谢您注册天互生物核酸结果查询系统。请点击下方按钮验证您的邮箱地址以完成注册流程。
          </p>
          
          <!-- Button -->
          <table role="presentation" style="margin: 32px 0">
            <tr>
              <td align="center">
                <a href="https://gitcode.com/2201_75793443/tianhunat-query/blob/main/%7B%7B%20.ConfirmationURL%20%7D%7D" style="display: inline-block; padding: 16px 48px; background: linear-gradient(135deg, #0ea5e9 0%, #06b6d4 100%); color: #ffffff; text-decoration: none; border-radius: 8px; font-size: 16px; font-weight: 600; box-shadow: 0 4px 12px rgba(6, 182, 212, 0.3)">
                  验证邮箱地址
                </a>
              </td>
            </tr>
          </table>
          
          <p style="margin: 32px 0 0; color: #64748b; font-size: 14px; line-height: 1.6">
            如果按钮无法点击,请复制以下链接到浏览器地址栏访问:
          </p>
          <p style="margin: 12px 0 0; padding: 16px; background-color: #f8fafc; border: 1px solid #e2e8f0; border-radius: 6px; color: #475569; font-size: 13px; word-break: break-all; font-family: 'Courier New', monospace">
            {{ .ConfirmationURL }}
          </p>
        </td>
      </tr>
      
      <!-- Footer -->
      <tr>
        <td style="padding: 32px 48px 48px; border-top: 1px solid #e2e8f0">
          <p style="margin: 0 0 8px; color: #94a3b8; font-size: 13px; line-height: 1.6">
            <strong>重要提示:</strong>该验证链接将在 24 小时后失效。
          </p>
          <p style="margin: 0; color: #94a3b8; font-size: 13px; line-height: 1.6">
            如果您没有注册此账号,请忽略此邮件。
          </p>
          <p style="margin: 24px 0 0; color: #cbd5e1; font-size: 12px; text-align: center">
            © 2025 天互生物 | 核酸结果查询系统
          </p>
        </td>
      </tr>
    </table>
  </td>
</tr>

天互生物

核酸结果查询系统

\`\`\`

2. 密码重置模板

在 Supabase Dashboard 中配置密码重置模板。

配置路径: Authentication > Email Templates > Reset Password

HTML 模板:

```html

      <!-- Content -->
      <tr>
        <td style="padding: 48px 48px 32px">
          <h2 style="margin: 0 0 16px; color: #0f172a; font-size: 24px; font-weight: 600">重置您的密码</h2>
          <p style="margin: 0 0 24px; color: #475569; font-size: 16px; line-height: 1.6">
            我们收到了您的密码重置请求。请点击下方按钮设置新密码。
          </p>
          
          <!-- Button -->
          <table role="presentation" style="margin: 32px 0">
            <tr>
              <td align="center">
                <a href="https://gitcode.com/2201_75793443/tianhunat-query/blob/main/%7B%7B%20.ConfirmationURL%20%7D%7D" style="display: inline-block; padding: 16px 48px; background: linear-gradient(135deg, #0ea5e9 0%, #06b6d4 100%); color: #ffffff; text-decoration: none; border-radius: 8px; font-size: 16px; font-weight: 600; box-shadow: 0 4px 12px rgba(6, 182, 212, 0.3)">
                  重置密码
                </a>
              </td>
            </tr>
          </table>
          
          <p style="margin: 32px 0 0; color: #64748b; font-size: 14px; line-height: 1.6">
            如果按钮无法点击,请复制以下链接到浏览器地址栏访问:
          </p>
          <p style="margin: 12px 0 0; padding: 16px; background-color: #f8fafc; border: 1px solid #e2e8f0; border-radius: 6px; color: #475569; font-size: 13px; word-break: break-all; font-family: 'Courier New', monospace">
            {{ .ConfirmationURL }}
          </p>
        </td>
      </tr>
      
      <!-- Footer -->
      <tr>
        <td style="padding: 32px 48px 48px; border-top: 1px solid #e2e8f0">
          <p style="margin: 0 0 8px; color: #94a3b8; font-size: 13px; line-height: 1.6">
            <strong>安全提示:</strong>该重置链接将在 1 小时后失效。
          </p>
          <p style="margin: 0; color: #94a3b8; font-size: 13px; line-height: 1.6">
            如果您没有请求重置密码,请忽略此邮件,您的密码不会被更改。
          </p>
          <p style="margin: 24px 0 0; color: #cbd5e1; font-size: 12px; text-align: center">
            © 2025 天互生物 | 核酸结果查询系统
          </p>
        </td>
      </tr>
    </table>
  </td>
</tr>

天互生物

核酸结果查询系统

\`\`\`

配置步骤

  1. 登录 Supabase Dashboard
  2. 选择您的项目
  3. 导航到 Authentication > Email Templates
  4. 分别编辑 Confirm signup 和 Reset Password 模板
  5. 将上述 HTML 代码粘贴到对应的模板编辑器中
  6. 点击 Save 保存更改

🔒 中间件保护

proxy.ts 工作流程

系统使用中间件保护需要认证的路由:

```typescript // 检查用户会话 const { data: { user } } = await supabase.auth.getUser()

// 保护 /results 路由 if (pathname.startsWith('/results')) { if (!user) { // 未登录用户重定向到登录页 return NextResponse.redirect(new URL('/auth/login', request.url)) } }

// 刷新会话令牌 return response ```

🚢 部署指南

部署到 Vercel

1. 推送代码到 GitHub

```bash git init git add . git commit -m "Initial commit" git remote add origin your-repo-url git push -u origin main ```

2. 在 Vercel 创建项目

  1. 访问 Vercel Dashboard
  2. 点击 New Project
  3. 导入您的 GitHub 仓库
  4. Vercel 会自动检测 Next.js 项目

3. 配置环境变量

在 Vercel 项目设置中添加所有必需的环境变量:

  1. 进入项目设置
  2. 选择 Environment Variables
  3. 添加所有 .env.local 中的变量
  4. 注意:NEXT_PUBLIC_DEV_SUPABASE_REDIRECT_URL 应设置为生产域名

4. 部署

  • Vercel 会自动构建和部署
  • 每次推送到 main 分支都会触发自动部署
  • 可在 Deployments 标签查看部署状态

Supabase 生产环境配置

1. 设置重定向 URL

  1. 进入 Supabase Dashboard
  2. 选择您的项目
  3. 导航到 Authentication > URL Configuration
  4. 在 Redirect URLs 中添加:
    • https://your-domain.com/auth/callback
    • https://your-domain.com/auth/reset-password
  5. 点击 Save 保存

2. 配置邮件模板

按照上述 邮件模板配置 部分的说明配置邮件模板。

3. SMTP 设置(可选)

  • 默认使用 Supabase 提供的 SMTP 服务
  • 生产环境建议配置自定义 SMTP 以提高邮件送达率
  • 配置路径:Settings > Auth > SMTP Settings

🔐 安全最佳实践

1. 环境变量管理

  • ❌ 永远不要将 .env.local 提交到版本控制
  • ✅ 使用 Vercel 环境变量管理敏感信息
  • ✅ 为不同环境设置不同的变量值(开发/预览/生产)

2. Supabase 行级安全 (RLS)

为 nucleic_acid_results 表启用 RLS 策略:

```sql -- 启用 RLS ALTER TABLE nucleic_acid_results ENABLE ROW LEVEL SECURITY;

-- 允许用户只查看自己的记录 CREATE POLICY "Users can view own results" ON nucleic_acid_results FOR SELECT USING ( phone = (auth.jwt() -> 'user_metadata' ->> 'phone') OR id_number = (auth.jwt() -> 'user_metadata' ->> 'id_number') ); ```

3. API 安全

  • 所有敏感操作在服务端执行
  • SUPABASE_SERVICE_ROLE_KEY 仅在服务端 API 路由中使用
  • 客户端仅使用 NEXT_PUBLIC_SUPABASE_ANON_KEY

4. 密码策略

Supabase 默认密码要求:

  • 最少 6 个字符
  • 可在 Supabase Dashboard 中配置更强的密码策略

❓ 常见问题

Q1: 收不到验证邮件或密码重置邮件?

解决方案:

  1. 检查垃圾邮件文件夹
  2. 确认 Supabase 邮件配置正确
  3. 查看 Supabase Dashboard 的 Auth Logs
  4. 考虑配置自定义 SMTP 服务器
  5. 确认邮件模板已正确配置

Q2: 登录后仍然跳转到登录页?

解决方案:

  1. 检查是否完成了邮箱验证
  2. 清除浏览器 Cookies 和缓存
  3. 检查中间件 proxy.ts 是否正确配置
  4. 查看浏览器控制台的错误信息
  5. 确认环境变量配置正确

Q3: 查询不到检测结果?

解决方案:

  1. 确认数据库中有匹配的记录(手机号或身份证号)
  2. 检查注册时填写的信息是否正确
  3. 在 Supabase Dashboard 中直接查询数据库
  4. 检查 RLS 策略是否配置正确
  5. 查看浏览器控制台和服务端日志

Q4: 密码重置链接无效或过期?

解决方案:

  1. 确认在 1 小时内点击了重置链接
  2. 检查 Supabase 重定向 URL 配置
  3. 尝试重新请求密码重置
  4. 查看浏览器控制台的错误信息

Q5: 如何在本地开发时测试邮件功能?

解决方案:

  • Supabase 会发送真实邮件,即使在本地开发环境
  • 确保 NEXT_PUBLIC_DEV_SUPABASE_REDIRECT_URL 设置为 http://localhost:3000
  • 或者使用 Supabase 的 Inbucket 查看测试邮件

Q6: 如何更改部署域名?

解决方案:

  1. 更新 Supabase 的重定向 URL 配置
  2. 更新 Vercel 环境变量
  3. 重新部署应用

📚 相关资源

📄 许可证

MIT License


开发团队: 天互生物
项目类型: 核酸检测结果查询系统
最后更新: 2025年

Introduction

用户可通过该系统注册、登录并查询核酸检测结果。支持邮箱验证、密码找回,采用响应式设计适配多端,提供医疗健康主题UI,基于Next.js和Supabase实现安全高效的用户认证与会话管理。【此简介由AI生成】

Customize your domain