feat(notification): 增加邮件通知设置在线测试与保存门禁功能
This commit is contained in:
1 parent
72af7caee8
commit
b7a99cc996
10 files changed
+470
-16
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
||||
import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
||||
import { settingsPayload } from "./SettingsPayload.js";
|
||||
import { deploymentSummary, serverAuthLabel } from "./DeploymentFeedback.js";
|
||||
import { useDomainForm } from "./DomainForm.js";
|
||||
@@ -53,6 +53,10 @@ export function useDashboard() {
|
||||
const settingsError = ref("");
|
||||
const dnsTesting = ref(false);
|
||||
const dnsTestError = ref("");
|
||||
const mailTesting = ref(false);
|
||||
const mailTestError = ref("");
|
||||
const mailTestSuccess = ref("");
|
||||
const mailVerified = ref(false);
|
||||
const dnsVerificationElapsedSeconds = ref(0);
|
||||
const dnsVerificationTimeoutSeconds = ref(defaultDNSVerificationTimeoutSeconds);
|
||||
const verificationClock = ref(Date.now());
|
||||
@@ -128,6 +132,29 @@ export function useDashboard() {
|
||||
}
|
||||
});
|
||||
|
||||
watch(
|
||||
() => [
|
||||
settingsForm.value.notification.smtp_enabled,
|
||||
settingsForm.value.notification.smtp_host,
|
||||
settingsForm.value.notification.smtp_port,
|
||||
settingsForm.value.notification.smtp_username,
|
||||
settingsForm.value.notification.smtp_password,
|
||||
settingsForm.value.notification.smtp_mode,
|
||||
settingsForm.value.notification.smtp_allow_plaintext_auth,
|
||||
(settingsForm.value.notification.recipients || []).join(","),
|
||||
],
|
||||
() => {
|
||||
mailVerified.value = false;
|
||||
mailTestSuccess.value = "";
|
||||
},
|
||||
);
|
||||
|
||||
const mailReadyToSave = computed(() => {
|
||||
if (mailTesting.value) return false;
|
||||
if (!settingsForm.value.notification.smtp_enabled) return true;
|
||||
return mailVerified.value;
|
||||
});
|
||||
|
||||
watch(hasOpenModal, (isOpen) => {
|
||||
if (isOpen) {
|
||||
previousBodyOverflow = document.body.style.overflow;
|
||||
@@ -800,9 +827,72 @@ export function useDashboard() {
|
||||
}
|
||||
}
|
||||
|
||||
async function testMail() {
|
||||
if (!canManageSystemSettings.value) {
|
||||
mailTestError.value = "只有超级管理员可以进行邮件发信测试";
|
||||
return;
|
||||
}
|
||||
const notif = settingsForm.value.notification;
|
||||
mailTestError.value = "";
|
||||
mailTestSuccess.value = "";
|
||||
if (notif.smtp_enabled) {
|
||||
if (!notif.smtp_host || !notif.smtp_host.trim()) {
|
||||
mailTestError.value = "请填写 SMTP 主机";
|
||||
return;
|
||||
}
|
||||
if (!notif.smtp_username || !notif.smtp_username.trim()) {
|
||||
mailTestError.value = "请填写 SMTP 用户名";
|
||||
return;
|
||||
}
|
||||
if (!notif.smtp_credential_configured && (!notif.smtp_password || !notif.smtp_password.trim())) {
|
||||
mailTestError.value = "请填写 SMTP 密码";
|
||||
return;
|
||||
}
|
||||
}
|
||||
const recipients = (notif.recipients || []).filter(Boolean);
|
||||
const targetRecipient = recipients[0] || (currentUser.value?.email || "").trim();
|
||||
if (!targetRecipient) {
|
||||
mailTestError.value = "请在收件人中至少填写一个有效的测试接收邮箱";
|
||||
return;
|
||||
}
|
||||
|
||||
mailTesting.value = true;
|
||||
try {
|
||||
const response = await fetchAdmin("/api/v1/notification/test", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
smtp_enabled: Boolean(notif.smtp_enabled),
|
||||
smtp_host: (notif.smtp_host || "").trim(),
|
||||
smtp_port: Number(notif.smtp_port) || 0,
|
||||
smtp_username: (notif.smtp_username || "").trim(),
|
||||
smtp_password: notif.smtp_password || "",
|
||||
smtp_mode: notif.smtp_mode || "starttls",
|
||||
smtp_allow_plaintext_auth: Boolean(notif.smtp_allow_plaintext_auth),
|
||||
recipient: targetRecipient,
|
||||
http_enabled: Boolean(notif.http_enabled),
|
||||
}),
|
||||
});
|
||||
const result = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
throw new Error(result.error || "邮件发信测试失败");
|
||||
}
|
||||
mailVerified.value = true;
|
||||
mailTestSuccess.value = result.message || "测试邮件已发送成功,请查收!";
|
||||
} catch (err) {
|
||||
mailVerified.value = false;
|
||||
mailTestError.value = err.message || "邮件发信测试失败";
|
||||
} finally {
|
||||
mailTesting.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function openSettings() {
|
||||
settingsError.value = "";
|
||||
dnsTestError.value = "";
|
||||
mailTestError.value = "";
|
||||
mailTestSuccess.value = "";
|
||||
mailVerified.value = false;
|
||||
if (!canManageSystemSettings.value) {
|
||||
settingsForm.value = normalizeSettings(null);
|
||||
showSettings.value = false;
|
||||
@@ -832,6 +922,11 @@ export function useDashboard() {
|
||||
settingsError.value = "DNS 服务商配置尚未通过在线测试,请先完成测试";
|
||||
return;
|
||||
}
|
||||
// 邮件通知在启用时必须先测试通过
|
||||
if ((!section || section === "notification") && !mailReadyToSave.value) {
|
||||
settingsError.value = "邮件通知配置尚未通过在线测试,请先完成测试";
|
||||
return;
|
||||
}
|
||||
settingsSaving.value = true;
|
||||
try {
|
||||
const payload = settingsPayload(settingsForm.value, section);
|
||||
@@ -1175,6 +1270,12 @@ export function useDashboard() {
|
||||
dnsVerificationTimeoutSeconds,
|
||||
dnsReadyToSave,
|
||||
dnsVerificationRequired,
|
||||
mailTesting,
|
||||
mailTestError,
|
||||
mailTestSuccess,
|
||||
mailVerified,
|
||||
mailReadyToSave,
|
||||
testMail,
|
||||
handleDNSProviderChange,
|
||||
clearDNSCredentials,
|
||||
testDNS,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
.mail-smtp-fields {
|
||||
.mail-smtp-fields {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 12px 16px;
|
||||
@@ -76,6 +76,48 @@
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.mail-test-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
margin-top: 16px;
|
||||
padding-top: 12px;
|
||||
border-top: 1px dashed #dce6f1;
|
||||
}
|
||||
|
||||
.mail-verification-status {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
padding: 3px 10px;
|
||||
border-radius: 999px;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.mail-verification-status.pending {
|
||||
background: #fff1f2;
|
||||
color: #e11d48;
|
||||
border: 1px solid #fecdd3;
|
||||
}
|
||||
|
||||
.mail-verification-status.verified {
|
||||
background: #f0fdf4;
|
||||
color: #16a34a;
|
||||
border: 1px solid #bbf7d0;
|
||||
}
|
||||
|
||||
.mail-form-error {
|
||||
margin-top: 8px;
|
||||
color: #dc2626;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.mail-form-success {
|
||||
margin-top: 8px;
|
||||
color: #16a34a;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
@media (max-width: 700px) {
|
||||
.mail-smtp-fields {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
<script setup>
|
||||
<script setup>
|
||||
import HelpTip from "../components/HelpTip/HelpTip.vue";
|
||||
|
||||
defineProps({ notification: { type: Object, required: true } });
|
||||
const props = defineProps({
|
||||
notification: { type: Object, required: true },
|
||||
mailTesting: { type: Boolean, default: false },
|
||||
mailTestError: { type: String, default: "" },
|
||||
mailTestSuccess: { type: String, default: "" },
|
||||
mailVerified: { type: Boolean, default: false },
|
||||
});
|
||||
|
||||
const emit = defineEmits(["test-mail"]);
|
||||
|
||||
function handleTest() {
|
||||
emit("test-mail");
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -54,6 +66,29 @@ defineProps({ notification: { type: Object, required: true } });
|
||||
placeholder="多个地址用逗号分隔"
|
||||
/>
|
||||
</label>
|
||||
|
||||
<div v-if="notification.smtp_enabled" class="mail-test-actions">
|
||||
<button
|
||||
class="secondary-button"
|
||||
type="button"
|
||||
:disabled="mailTesting"
|
||||
@click="handleTest"
|
||||
>
|
||||
{{ mailTesting ? "正在测试发信……" : "测试邮件发送" }}
|
||||
</button>
|
||||
<span
|
||||
class="mail-verification-status"
|
||||
:class="mailVerified ? 'verified' : 'pending'"
|
||||
>
|
||||
{{ mailVerified ? "已通过测试" : "待在线测试" }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<p v-if="mailTestError" class="form-error mail-form-error">{{ mailTestError }}</p>
|
||||
<p v-if="mailTestSuccess" class="form-success mail-form-success">{{ mailTestSuccess }}</p>
|
||||
<HelpTip v-if="notification.smtp_enabled && !mailVerified">
|
||||
保存邮件通知设置前,必须先点击“测试邮件发送”并通过在线测试验证。
|
||||
</HelpTip>
|
||||
</fieldset>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import assert from "node:assert/strict";
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { parse, compileScript, compileTemplate } from "@vue/compiler-sfc";
|
||||
@@ -24,7 +24,7 @@ test("邮件通知组件只保留 SMTP 通道和收件人配置", () => {
|
||||
}
|
||||
assert.doesNotMatch(mailSource, /HTTP 接口|sender_email|notification\.http_enabled/);
|
||||
assert.equal(mailSource.split(':value="notification.recipients.join').length - 1, 1);
|
||||
assert.match(source, /<MailNotificationSettings :notification="settingsForm.notification"/);
|
||||
assert.match(source, /<MailNotificationSettings[\s\S]*:notification="settingsForm.notification"/);
|
||||
assert.doesNotMatch(mailSource, /role="tab"/);
|
||||
});
|
||||
|
||||
@@ -128,3 +128,4 @@ test("settings.keyboardNavigationWrapsAndFocusesTabs", async () => {
|
||||
await state.navigateTabs({ ...event, key: "Home" });
|
||||
assert.equal(state.activeSection.value, "service");
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<script setup>
|
||||
<script setup>
|
||||
import HelpTip from "../components/HelpTip/HelpTip.vue";
|
||||
import { useSettingsModal } from "./SettingsModal.js";
|
||||
import MailNotificationSettings from "./MailNotificationSettings.vue";
|
||||
@@ -23,6 +23,12 @@ const {
|
||||
closeSettings,
|
||||
saveSettings,
|
||||
dnsVerificationRequired,
|
||||
mailTesting,
|
||||
mailTestError,
|
||||
mailTestSuccess,
|
||||
mailVerified,
|
||||
mailReadyToSave,
|
||||
testMail,
|
||||
} = props.state;
|
||||
const { activeSection, submitSettings, navigateTabs } = useSettingsModal(saveSettings);
|
||||
</script>
|
||||
@@ -385,10 +391,17 @@ const { activeSection, submitSettings, navigateTabs } = useSettingsModal(saveSet
|
||||
class="settings-panel"
|
||||
>
|
||||
<h3>邮件通知</h3>
|
||||
<MailNotificationSettings :notification="settingsForm.notification" />
|
||||
<MailNotificationSettings
|
||||
:notification="settingsForm.notification"
|
||||
:mail-testing="mailTesting"
|
||||
:mail-test-error="mailTestError"
|
||||
:mail-test-success="mailTestSuccess"
|
||||
:mail-verified="mailVerified"
|
||||
@test-mail="testMail"
|
||||
/>
|
||||
<div class="modal-actions">
|
||||
<button class="secondary-button" type="button" :disabled="settingsSaving" @click="closeSettings">取消</button>
|
||||
<button class="primary-button" type="button" :disabled="settingsSaving" @click="submitSettings">{{ settingsSaving ? "保存中……" : "保存邮件通知" }}</button>
|
||||
<button class="primary-button" type="button" :disabled="settingsSaving || !mailReadyToSave" @click="submitSettings">{{ settingsSaving ? "保存中……" : "保存邮件通知" }}</button>
|
||||
</div>
|
||||
</section>
|
||||
<p v-if="settingsError" class="form-error">{{ settingsError }}</p>
|
||||
@@ -400,3 +413,5 @@ const { activeSection, submitSettings, navigateTabs } = useSettingsModal(saveSet
|
||||
|
||||
<style scoped src="./Dashboard.css"></style>
|
||||
<style scoped src="./SettingsModal.css"></style>
|
||||
|
||||
|
||||
Reference in new issue
Block a user