add. Closes #8 #9

Open
levis wants to merge 11 commits from feature/8-add-payments into production
17 changed files with 421 additions and 30 deletions
Showing only changes of commit 1b211c3148 - Show all commits

2
.env
View File

@ -14,3 +14,5 @@ APP_URL=
SMTP_USER=levishub@yandex.com SMTP_USER=levishub@yandex.com
SMTP_PASS=avhpihoudpyvibtx SMTP_PASS=avhpihoudpyvibtx
DEFAULT_TO_EMAIL=miduway@yandex.ru DEFAULT_TO_EMAIL=miduway@yandex.ru
SMTP_HOST=smtp.yandex.ru
SMTP_PORT=465

View File

@ -16,3 +16,5 @@ APP_URL=
SMTP_USER=levishub@yandex.ru SMTP_USER=levishub@yandex.ru
SMTP_PASS=avhpihoudpyvibtx SMTP_PASS=avhpihoudpyvibtx
DEFAULT_TO_EMAIL=miduway@yandex.ru DEFAULT_TO_EMAIL=miduway@yandex.ru
SMTP_HOST=smtp.yandex.ru
SMTP_PORT=465

View File

@ -30,8 +30,6 @@
<script setup> <script setup>
import { ref, computed } from 'vue' import { ref, computed } from 'vue'
const mail = useMail()
const to = ref('') const to = ref('')
const status = ref('') const status = ref('')
const isLoading = ref(false) const isLoading = ref(false)
@ -40,10 +38,10 @@ const statusClass = computed(() => {
if (status.value.includes('Ошибка')) { if (status.value.includes('Ошибка')) {
return 'bg-red-100 text-red-700 border border-red-200' return 'bg-red-100 text-red-700 border border-red-200'
} }
if (status.value.includes('отправлено') || status.value.includes('подписка')) { if (status.value.includes('Успех')) {
return 'bg-green-100 text-green-700 border border-green-200' return 'bg-green-100 text-green-700 border border-green-200'
} }
return 'bg-blue-100 text-blue-700 border border-blue-200' return 'bg-blue-100 text-blue-700 border-blue-200'
}) })
async function onSubmit() { async function onSubmit() {
@ -56,21 +54,16 @@ async function onSubmit() {
status.value = '' status.value = ''
try { try {
const emailData = { await $fetch('/api/send-order', {
to: to.value, method: 'POST',
subject: 'Новая подписка на рассылку книги!', body: { to: to.value },
text: `Пользователь с e-mail: ${to.value} подписался на рассылку книги.`, })
}
await mail.send(emailData) status.value = 'Успех! Письмо с подтверждением отправлено на вашу почту.'
status.value = 'Успешная подписка! Проверьте вашу почту.'
// Очищаем форму
to.value = '' to.value = ''
} catch (error) { } catch (error) {
console.error('Ошибка отправки письма:', error) console.error('Ошибка отправки письма:', error)
status.value = `Ошибка отправки: ${error.message || 'Неизвестная ошибка'}` status.value = `Ошибка отправки: ${error?.data?.message || error.message || 'Неизвестная ошибка'}`
} finally { } finally {
isLoading.value = false isLoading.value = false
} }

View File

@ -42,24 +42,15 @@ export default defineNuxtConfig({
}, },
}, },
runtimeConfig: {
smtpHost: process.env.SMTP_HOST,
smtpPort: process.env.SMTP_PORT,
smtpUser: process.env.SMTP_USER,
smtpPass: process.env.SMTP_PASS,
},
modules: [ modules: [
...config.modules, ...config.modules,
[ // ['nuxt-mail', ... ] // Модуль больше не нужен
'nuxt-mail',
{
smtp: {
host: 'smtp.yandex.ru',
port: 465,
secure: true,
auth: {
user: process.env.SMTP_USER || 'levishub@yandex.com',
pass: process.env.SMTP_PASS || 'avhpihoudpyvibtx',
},
},
message: {
to: process.env.DEFAULT_TO_EMAIL || 'default@example.com',
},
},
],
], ],
}) })

View File

@ -29,7 +29,6 @@
"husky": "^9.1.7", "husky": "^9.1.7",
"nodemailer": "^7.0.3", "nodemailer": "^7.0.3",
"nuxt": "^3.17.5", "nuxt": "^3.17.5",
"nuxt-mail": "^6.0.2",
"nuxt-schema-org": "^5.0.5", "nuxt-schema-org": "^5.0.5",
"pg": "^8.16.2", "pg": "^8.16.2",
"postgres": "^3.4.7", "postgres": "^3.4.7",
@ -41,6 +40,7 @@
"@nuxt/test-utils": "^3.11.3", "@nuxt/test-utils": "^3.11.3",
"@nuxtjs/tailwindcss": "^6.11.4", "@nuxtjs/tailwindcss": "^6.11.4",
"@types/node": "^22.0.0", "@types/node": "^22.0.0",
"@types/nodemailer": "^6.4.17",
"@types/pg": "^8.15.4", "@types/pg": "^8.15.4",
"@vue/eslint-config-prettier": "^10.2.0", "@vue/eslint-config-prettier": "^10.2.0",
"@vue/eslint-config-typescript": "^14.5.0", "@vue/eslint-config-typescript": "^14.5.0",

View File

@ -0,0 +1,53 @@
import nodemailer from 'nodemailer'
export default defineEventHandler(async (event) => {
const { to } = await readBody(event)
if (!to) {
throw createError({
statusCode: 400,
statusMessage: 'Bad Request: "to" field is required',
})
}
const config = useRuntimeConfig(event)
// Убедимся, что все переменные окружения на месте
if (!config.smtpHost || !config.smtpPort || !config.smtpUser || !config.smtpPass) {
console.error('SMTP configuration is missing in runtime config.')
throw createError({
statusCode: 500,
statusMessage: 'Internal Server Error: SMTP configuration is incomplete.',
})
}
const transporter = nodemailer.createTransport({
host: config.smtpHost,
port: parseInt(config.smtpPort, 10),
secure: parseInt(config.smtpPort, 10) === 465, // true for 465, false for other ports
auth: {
user: config.smtpUser,
pass: config.smtpPass,
},
})
const mailOptions = {
from: `"Vino Galante" <${config.smtpUser}>`,
to: to, // Отправляем на email, полученный от клиента
bcc: config.smtpUser, // Отправляем скрытую копию самому себе для сохранения в "Отправленных"
subject: 'Ваш заказ принят!',
text: 'Спасибо за ваш заказ! Мы скоро свяжемся с вами для уточнения деталей.',
html: '<b>Спасибо за ваш заказ!</b><p>Мы скоро свяжемся с вами для уточнения деталей.</p>',
}
try {
await transporter.sendMail(mailOptions)
return { success: true, message: 'Email sent' }
} catch (error) {
console.error('Error sending email:', error)
throw createError({
statusCode: 500,
statusMessage: `Failed to send email: ${(error as Error).message || 'Unknown error'}`,
})
}
})