Bläddra i källkod

refactor(联系表单): 优化邮件发送逻辑和表单提交处理

- 合并表单提交和邮件发送逻辑,减少嵌套,提高代码可读性。
- 添加邮件内容模板,包含用户输入的信息和提交时间,提升邮件的可读性。
master
lizhuang 4 veckor sedan
förälder
incheckning
83326a0d00
1 ändrade filer med 31 tillägg och 13 borttagningar
  1. 31
    13
      pages/contact.vue

+ 31
- 13
pages/contact.vue Visa fil

@@ -36,10 +36,7 @@
<div class="text-white text-3xl font-medium mb-6">
{{ t("contact.title") }}
</div>
<form
@submit="handleSubmit"
class="flex flex-col gap-6"
>
<form @submit="handleSubmit" class="flex flex-col gap-6">
<div class="relative">
<input
v-model="form.name"
@@ -314,26 +311,25 @@ const form = ref({
async function handleSubmit(event: Event) {
// 阻止表单默认提交行为,防止重复提交
event.preventDefault();
if (isSubmitting.value) {
return; // 如果已经在提交中,不重复处理
}
isSubmitting.value = true;
submitSuccess.value = false;
try {
// 验证验证码
const isCaptchaValid = captcha.validateCaptcha();
if (!isCaptchaValid) {
isSubmitting.value = false;
return;
}
// 使用单独的异步函数发送邮件,减少嵌套
await sendEmail();
} catch (error) {
console.error("Error during form submission:", error);
showFormStatus("error", t("contact.submitError"));
@@ -348,9 +344,31 @@ async function handleSubmit(event: Event) {
async function sendEmail() {
try {
const result = await wrapAsync(async () => {
const message_template = `
📨 来自Hanye官网的联系表单

姓名:${form.value.name}
邮箱:${form.value.email}

消息内容:
${form.value.message}

提交时间:${new Date().toLocaleString()}
来源页面:${window.location.href}

---
此邮件由Hanye官网的联系表单自动发送,请勿回复。
`;
const title = `您有一封来自Hanye官网的联系表单`;
const receiveEmail = "hanye@hanye.cn";

// 对包含HTML标签的参数进行URL编码
const encodedMessage = encodeURIComponent(message_template);
const encodedTitle = encodeURIComponent(title);

// 发送邮件
const response = await fetch(
`https://digital.sohomall.jp/prod-api/system/zsEmail/sendEmailWeb?receiveEmail=${form.value.email}&context=${form.value.message}&title=[Hanye Website]${form.value.name}`,
`https://digital.sohomall.jp/prod-api/system/zsEmail/sendEmailWeb?receiveEmail=${receiveEmail}&context=${encodedMessage}&title=${encodedTitle}`,
{
method: "GET",
headers: {
@@ -366,7 +384,7 @@ async function sendEmail() {
email: "",
message: "",
};
captcha.state.userInput = ''; // 清空验证码输入
captcha.state.userInput = ""; // 清空验证码输入
captcha.generateCaptcha(); // 成功后刷新验证码
submitSuccess.value = true;
showFormStatus("success", t("contact.submitSuccess"));
@@ -376,7 +394,7 @@ async function sendEmail() {
throw new Error("Failed to send email");
}
});
return result;
} catch (error) {
console.error("Error sending email:", error);

Laddar…
Avbryt
Spara