63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import { nowStr } from '~/server/utils/auth'
|
|
import { execute } from '~/server/utils/mysql'
|
|
import { notifyAssessmentSubmission } from '~/server/utils/form-notification'
|
|
import { fail, ok } from '~/server/utils/response'
|
|
import { isSiteLocale } from '~/types/locale'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody<{
|
|
lang?: string
|
|
firstName?: string
|
|
lastName?: string
|
|
email?: string
|
|
condition?: string
|
|
reports?: string
|
|
message?: string
|
|
}>(event)
|
|
|
|
const lang = isSiteLocale(body.lang || '') ? body.lang : 'en'
|
|
const firstName = body.firstName?.trim()
|
|
const email = body.email?.trim()
|
|
const condition = body.condition?.trim()
|
|
|
|
if (!firstName) return fail('请填写患者名')
|
|
if (!email) return fail('请填写联系邮箱')
|
|
if (!condition) return fail('请填写主要病情/诊断')
|
|
|
|
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
|
if (!emailPattern.test(email)) return fail('邮箱格式不正确')
|
|
|
|
const createdAt = nowStr()
|
|
|
|
await execute(
|
|
`INSERT INTO assessment_submissions
|
|
(lang, first_name, last_name, email, condition_text, reports, message, status, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, 'unread', ?)`,
|
|
[
|
|
lang,
|
|
firstName,
|
|
body.lastName?.trim() || null,
|
|
email,
|
|
condition,
|
|
body.reports || null,
|
|
body.message?.trim() || null,
|
|
createdAt,
|
|
]
|
|
)
|
|
|
|
void notifyAssessmentSubmission({
|
|
lang,
|
|
firstName,
|
|
lastName: body.lastName?.trim() || null,
|
|
email,
|
|
condition,
|
|
reports: body.reports || null,
|
|
message: body.message?.trim() || null,
|
|
createdAt,
|
|
}).catch((error) => {
|
|
console.error('[mail] 免费评估表通知发送失败:', error)
|
|
})
|
|
|
|
return ok(null, '提交成功')
|
|
})
|