64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
import { nowStr } from '~/server/utils/auth'
|
|
import { execute } from '~/server/utils/mysql'
|
|
import { notifyPartnerSubmission } 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
|
|
companyName?: string
|
|
country?: string
|
|
email?: string
|
|
category?: string
|
|
collaborate?: string
|
|
}>(event)
|
|
|
|
const lang = isSiteLocale(body.lang || '') ? body.lang : 'en'
|
|
const firstName = body.firstName?.trim()
|
|
const lastName = body.lastName?.trim()
|
|
const companyName = body.companyName?.trim()
|
|
const country = body.country?.trim()
|
|
const email = body.email?.trim()
|
|
const category = body.category?.trim()
|
|
const collaborate = body.collaborate?.trim()
|
|
|
|
if (!firstName) return fail('请填写名')
|
|
if (!lastName) return fail('请填写姓')
|
|
if (!companyName) return fail('请填写公司名称')
|
|
if (!country) return fail('请填写国家/地区')
|
|
if (!email) return fail('请填写电子邮箱')
|
|
if (!category) return fail('请选择合作类别')
|
|
if (!collaborate) return fail('请填写合作说明')
|
|
|
|
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
|
|
if (!emailPattern.test(email)) return fail('邮箱格式不正确')
|
|
|
|
const createdAt = nowStr()
|
|
|
|
await execute(
|
|
`INSERT INTO partner_submissions
|
|
(lang, first_name, last_name, company_name, country, email, category, collaborate, status, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'unread', ?)`,
|
|
[lang, firstName, lastName, companyName, country, email, category, collaborate, createdAt]
|
|
)
|
|
|
|
void notifyPartnerSubmission({
|
|
lang,
|
|
firstName,
|
|
lastName,
|
|
companyName,
|
|
country,
|
|
email,
|
|
category,
|
|
collaborate,
|
|
createdAt,
|
|
}).catch((error) => {
|
|
console.error('[mail] 合作申请通知发送失败:', error)
|
|
})
|
|
|
|
return ok(null, '提交成功')
|
|
})
|