diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..e6bd401 --- /dev/null +++ b/.env.example @@ -0,0 +1,16 @@ +# 本地开发环境变量示例 +# 复制为 .env 后按需修改(.env 已被 .gitignore 忽略,不会提交) + +# PostgreSQL 连接串(docker compose 起库后默认可用,宿主机端口 5433) +NUXT_DATABASE_URL=postgres://arcticnewone:arcticnewone_dev_password@localhost:5433/arcticnewone + +# JWT 签名密钥(生产环境务必改为随机长字符串) +NUXT_JWT_SECRET=dev-only-secret-change-in-production + +# SMTP 邮件通知(可选;不配置时联系表单/订阅仅入库) +# NUXT_SMTP_HOST=smtp.example.com +# NUXT_SMTP_PORT=465 +# NUXT_SMTP_USER=your-smtp-user +# NUXT_SMTP_PASS=your-smtp-password +# NUXT_SMTP_FROM=北极新一 +# NUXT_SMTP_CONTACT_TO=chenchun@virtheart.com diff --git a/README.md b/README.md index 5104a48..c25ac40 100644 --- a/README.md +++ b/README.md @@ -4,21 +4,35 @@ ## 技术栈 -- [Nuxt 4](https://nuxt.com/) — 应用框架(服务端渲染 + 静态生成) +- [Nuxt 4](https://nuxt.com/) — 应用框架(服务端渲染 + 内置 Nitro Server API) - [Vue 3](https://vuejs.org/) — 视图层 - [Tailwind CSS](https://tailwindcss.com/) — 样式方案(通过 `@nuxtjs/tailwindcss` 集成) - [GSAP](https://gsap.com/) — 页面动画(配合 Lenis 平滑滚动) - [Pinia](https://pinia.vuejs.org/) — 状态管理 - [@nuxtjs/i18n](https://i18n.nuxtjs.org/) — 国际化(中/英双语) - [@nuxt/icon](https://nuxt.com/modules/icon) — 图标 +- [PostgreSQL](https://www.postgresql.org/) — 数据库(用户 / 联系留言 / 资讯订阅) +- [node-postgres (pg)](https://node-postgres.com/) — 数据库驱动 +- [jose](https://github.com/panva/jose) — JWT 会话(httpOnly Cookie) +- [bcryptjs](https://github.com/dcodeIO/bcryptjs) — 密码哈希 +- [nodemailer](https://nodemailer.com/) — SMTP 邮件通知(可选) - [pnpm](https://pnpm.io/) — 包管理器 +## 功能 + +- 展示型页面:首页、工作室、作品集、动态资讯、工具箱、联系 +- 用户系统:注册 / 登录 / 登出(JWT + httpOnly Cookie,密码 bcrypt 哈希) +- 联系表单:留言入库 PostgreSQL,可选 SMTP 邮件通知 +- 资讯订阅:邮箱订阅入库(唯一约束防重复),可选 SMTP 通知 +- 国际化:中/英双语,默认中文 + ## 本地开发 ### 环境要求 - Node.js ≥ 20(推荐 22,与 Docker 构建镜像一致) - pnpm ≥ 9 +- Docker(用于本地 PostgreSQL,或自行提供 PostgreSQL 16+) ### 安装与启动 @@ -26,7 +40,13 @@ # 1. 安装依赖 pnpm install -# 2. 启动开发服务器(默认 http://localhost:3000) +# 2. 启动本地数据库(PostgreSQL 16,映射宿主机 5433 端口) +docker compose up -d postgres + +# 3. 配置环境变量(可写入 .env 文件,已被 .gitignore 忽略) +cp .env.example .env + +# 4. 启动开发服务器(默认 http://localhost:3000) pnpm dev ``` @@ -38,10 +58,43 @@ pnpm dev | --- | --- | | `pnpm dev` | 启动开发服务器 | | `pnpm build` | 构建生产版本(生成 `.output/`) | -| `pnpm generate` | 静态站点生成(输出到 `dist/`) | +| `pnpm generate` | 静态站点生成(输出到 `dist/`,注意:Server API 需要 Node 运行时不支持静态生成) | | `pnpm preview` | 本地预览生产构建 | | `pnpm start` | 以 Node 方式启动 `.output/server/index.mjs` | +## 环境变量 + +所有变量均可通过 `.env` 文件或系统环境变量注入,Nuxt 自动读取 `NUXT_*` 前缀: + +| 变量 | 必填 | 默认值 | 说明 | +| --- | --- | --- | --- | +| `NUXT_DATABASE_URL` | ✅ | — | PostgreSQL 连接串,如 `postgres://user:pass@localhost:5433/arcticnewone` | +| `NUXT_JWT_SECRET` | ✅(生产) | 开发默认值 | JWT 签名密钥,生产必须设置为随机长字符串(`openssl rand -hex 32`) | +| `NUXT_SMTP_HOST` | 否 | — | SMTP 服务器地址(配置后联系表单/订阅会发邮件通知) | +| `NUXT_SMTP_PORT` | 否 | 465 | SMTP 端口 | +| `NUXT_SMTP_USER` | 否 | — | SMTP 用户名 | +| `NUXT_SMTP_PASS` | 否 | — | SMTP 密码 | +| `NUXT_SMTP_FROM` | 否 | — | 发件人,如 `北极新一 ` | +| `NUXT_SMTP_CONTACT_TO` | 否 | SMTP_USER | 收件人(联系表单与订阅通知发往此邮箱) | +| `POSTGRES_PASSWORD` | 仅 compose | `arcticnewone_dev_password` | docker-compose 中数据库密码(本地开发) | + +SMTP 未配置时,联系表单与订阅**仍然正常入库**,只是不发送邮件通知。 + +## Server API + +项目内置 Nitro Server API(`server/` 目录),应用启动时自动建表(幂等): + +| 接口 | 方法 | 说明 | +| --- | --- | --- | +| `/api/auth/register` | POST | 注册(用户名/邮箱唯一校验,成功即自动登录) | +| `/api/auth/login` | POST | 登录(支持用户名/邮箱/手机号 + 密码) | +| `/api/auth/logout` | POST | 登出(清除 Cookie) | +| `/api/auth/me` | GET | 从 Cookie 恢复当前用户 | +| `/api/contact` | POST | 提交联系表单(入库 + 可选邮件通知) | +| `/api/newsletter/subscribe` | POST | 邮箱订阅资讯(入库,重复订阅返回 409) | + +错误响应统一返回 `{ statusMessage: '' }`,前端按错误码显示对应语言的文案。 + ## 项目结构 ```text @@ -49,20 +102,25 @@ pnpm dev ├── app/ # 应用源码(Nuxt 4 约定目录) │ ├── app.vue # 应用入口(全局 SEO / Meta) │ ├── assets/ # 静态资源(样式、图片等) -│ ├── components/ # 组件(common / home) +│ ├── components/ # 组件(common / home / layouts) │ ├── composables/ # 组合式函数 │ ├── layouts/ # 布局 │ ├── pages/ # 页面(首页、工作室、作品、动态、工具、联系) │ ├── plugins/ # 插件(如 Lenis 平滑滚动) -│ ├── stores/ # Pinia 状态 +│ ├── stores/ # Pinia 状态(user / app) │ └── types/ # 类型定义 +├── server/ # Nitro Server API(后端) +│ ├── api/ # 接口(auth / contact / newsletter) +│ ├── db/ # PostgreSQL 连接池 + Schema +│ ├── plugins/ # 启动插件(自动建表) +│ └── utils/ # JWT 认证、校验、邮件工具 ├── i18n/ │ └── locales/ # 国际化文案(zh.json / en.json) ├── public/ # 公开静态文件(favicon、robots.txt 等) ├── Dockerfile # 容器镜像构建 -├── deploy/ # 生产部署配置(Kubernetes Deployment/Service/Ingress) -├── docker-compose.yml # 本地容器编排(使用 SWR 镜像) -├── nuxt.config.ts # Nuxt 配置(模块、i18n、Tailwind 等) +├── deploy/ # 生产部署配置(Kubernetes Deployment/Service/Ingress/PostgreSQL) +├── docker-compose.yml # 本地容器编排(应用 + PostgreSQL) +├── nuxt.config.ts # Nuxt 配置(模块、i18n、runtimeConfig 等) ├── tailwind.config.js # Tailwind 配置 └── package.json # 项目依赖与脚本 ``` @@ -80,15 +138,15 @@ pnpm dev Dockerfile 基础镜像使用国内可访问的 DaoCloud 镜像源(`docker.m.daocloud.io/library/node:22-alpine`,Docker Hub 在国内不可达)。 -本地启动(使用 SWR 镜像,与生产一致): +本地一键启动(应用 + PostgreSQL,与生产一致): ```bash docker compose up -d ``` -生产部署使用 `deploy/` 目录下的 Kubernetes 配置(Deployment / Service / Ingress), +生产部署使用 `deploy/` 目录下的 Kubernetes 配置(Deployment / Service / Ingress / 可选 PostgreSQL), 镜像统一推送到华为云 SWR(`swr.cn-north-4.myhuaweicloud.com/virtheart/arcticnewone:latest`), -部署与回滚步骤见 [deploy/README.md](deploy/README.md)。 +部署、Secret 创建与回滚步骤见 [deploy/README.md](deploy/README.md)。 ## 国际化(i18n) @@ -97,6 +155,7 @@ docker compose up -d - 语言文件位于 `i18n/locales/`:`zh.json`(中文)、`en.json`(英文) - 默认语言为中文(`defaultLocale: 'zh'`),URL 不包含语言前缀(`strategy: 'no_prefix'`),不自动探测浏览器语言 - 组件中通过 `useI18n()` 的 `t()` 方法读取文案,例如 `t('nav.home')` +- 服务端错误码与文案键一一对应(如 `auth.emailTaken`),新增接口错误时需同步添加中英文键 新增文案时,在 `zh.json` 和 `en.json` 中同步添加对应键即可。 diff --git a/app/components/common/LoginModal.vue b/app/components/common/LoginModal.vue index 8d87411..e13f6ed 100644 --- a/app/components/common/LoginModal.vue +++ b/app/components/common/LoginModal.vue @@ -75,11 +75,6 @@ - -

- {{ t('auth.demoHint') }} -

-

{{ t('auth.noAccount') }} @@ -146,7 +141,7 @@ const handleLogin = async () => { password.value = '' closeModal() } else { - errorMessage.value = t('auth.loginError') + errorMessage.value = t(result.error) } } catch (error) { console.error('Login error:', error) diff --git a/app/components/common/RegisterModal.vue b/app/components/common/RegisterModal.vue index cb600c5..85e17ff 100644 --- a/app/components/common/RegisterModal.vue +++ b/app/components/common/RegisterModal.vue @@ -222,7 +222,7 @@ const handleRegister = async () => { closeModal() }, 2000) } else { - errorMessage.value = result.error || t('auth.registerError') + errorMessage.value = t(result.error) } } catch (error) { console.error('Register error:', error) diff --git a/app/components/layouts/ArcticNewOneFooter.vue b/app/components/layouts/ArcticNewOneFooter.vue index 8a096b5..9a2dd72 100755 --- a/app/components/layouts/ArcticNewOneFooter.vue +++ b/app/components/layouts/ArcticNewOneFooter.vue @@ -13,6 +13,7 @@ const footerRef = ref(null) const newsletterEmail = ref('') const isSubscribing = ref(false) const subscribeStatus = ref<'idle' | 'success' | 'error'>('idle') +const newsletterMessage = ref('') let ctx: gsap.Context const pagesLinks = [ @@ -33,39 +34,45 @@ const handleNewsletterSubmit = async () => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ if (!emailRegex.test(newsletterEmail.value)) { subscribeStatus.value = 'error' + newsletterMessage.value = t('footer.newsletterInvalidEmail') setTimeout(() => { subscribeStatus.value = 'idle' + newsletterMessage.value = '' }, 3000) return } isSubscribing.value = true subscribeStatus.value = 'idle' + newsletterMessage.value = '' try { - // 模拟 API 调用 - 你需要替换为真实的 Newsletter 服务 - // 可选方案: - // 1. Mailchimp: await $fetch('/api/newsletter/subscribe', { method: 'POST', body: { email: newsletterEmail.value } }) - // 2. ConvertKit, SendGrid, 或其他邮件服务 - - await new Promise(resolve => setTimeout(resolve, 1000)) // 模拟网络延迟 - - console.log('Newsletter subscription:', newsletterEmail.value) + // 真实订阅:入库(PostgreSQL,邮箱唯一)+ 可选 SMTP 通知 + await $fetch('/api/newsletter/subscribe', { + method: 'POST', + body: { email: newsletterEmail.value }, + }) subscribeStatus.value = 'success' + newsletterMessage.value = t('footer.newsletterSuccess') newsletterEmail.value = '' // 3秒后重置状态 setTimeout(() => { subscribeStatus.value = 'idle' + newsletterMessage.value = '' }, 3000) - } catch (error) { + } catch (error: any) { console.error('Newsletter subscription error:', error) subscribeStatus.value = 'error' + // 服务端错误码(如已订阅 409)优先展示 + const code = error?.data?.statusMessage || error?.statusMessage || '' + newsletterMessage.value = code && code !== 'Bad Request' ? t(code) : t('footer.newsletterError') setTimeout(() => { subscribeStatus.value = 'idle' + newsletterMessage.value = '' }, 3000) } finally { isSubscribing.value = false @@ -183,6 +190,15 @@ onUnmounted(() => { {{ t('footer.newsletterNote') }} + + +

+ {{ newsletterMessage }} +

diff --git a/app/components/layouts/ArcticNewOneHeader.vue b/app/components/layouts/ArcticNewOneHeader.vue index a6484cf..0424490 100755 --- a/app/components/layouts/ArcticNewOneHeader.vue +++ b/app/components/layouts/ArcticNewOneHeader.vue @@ -386,11 +386,11 @@ if (typeof window !== 'undefined') { class="flex items-center gap-3 px-4 py-2 rounded-full hover:bg-gray-100 transition-all duration-300" > - {{ userStore.user?.name }} + {{ userStore.user?.username }}
-

{{ userStore.user?.name }}

+

{{ userStore.user?.username }}

{{ userStore.user?.email }}