File size: 13,583 Bytes
81d8726 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | <template>
<el-form
ref="formRef"
:model="noticeConfig"
:rules="rules"
:inline="false"
:hide-required-asterisk="true"
:show-message="false"
label-width="120px"
label-suffix=":"
>
<el-form-item label="通知方式" prop="type" class="form_item">
<el-select v-model="noticeConfig.type" placeholder="" class="input">
<el-option
v-for="item in noticeTypeList"
:key="item.type"
:label="item.desc"
:value="item.type"
/>
</el-select>
</el-form-item>
<!-- server酱 -->
<template v-if="noticeConfig.type === 'sct'">
<el-form-item label="SendKey" prop="sct.sendKey" class="form_item">
<el-input
v-model.trim="noticeConfig.sct.sendKey"
clearable
placeholder="SCT******"
autocomplete="off"
class="input"
/>
<p class="tips">普通用户每日最多支持5条,有条件建议开通会员服务防止丢失重要通知。<a class="link" href="https://sct.ftqq.com/r/9338" target="_blank">Server酱官网</a> </p>
</el-form-item>
</template>
<!-- 邮箱 -->
<template v-if="noticeConfig.type === 'email'">
<!-- 配置模式切换 -->
<el-form-item label="配置模式" class="form_item">
<el-radio-group v-model="noticeConfig.email.useCustom" @change="handleEmailModeChange">
<el-radio-button :value="false">服务商</el-radio-button>
<el-radio-button :value="true">自定义 SMTP</el-radio-button>
</el-radio-group>
</el-form-item>
<!-- 服务商模式 -->
<template v-if="!noticeConfig.email.useCustom">
<el-form-item label="服务商" prop="email.service" class="form_item">
<el-input
v-model.trim="noticeConfig.email.service"
clearable
placeholder="QQ、126、163、Gmail..."
autocomplete="off"
class="input"
/>
<span class="tips">邮箱服务商, 例如: QQ、126、163、Gmail, 支持列表: <a class="link" href="https://github.com/nodemailer/nodemailer/blob/master/lib/well-known/services.json" target="_blank">点击查询</a> </span>
</el-form-item>
</template>
<!-- 自定义 SMTP 模式 -->
<template v-else>
<el-form-item label="SMTP 服务器" prop="email.host" class="form_item">
<el-input
v-model.trim="noticeConfig.email.host"
clearable
placeholder="smtp.gmail.com"
autocomplete="off"
class="input"
/>
<span class="tips">SMTP 服务器地址,例如:smtp.gmail.com、smtp.163.com</span>
</el-form-item>
<el-form-item label="端口" class="form_item">
<el-select
v-model="noticeConfig.email.port"
placeholder=""
class="input"
@change="handlePortChange"
>
<el-option label="465 (SSL)" :value="465" />
<el-option label="587 (STARTTLS)" :value="587" />
<el-option label="25 (无加密)" :value="25" />
</el-select>
</el-form-item>
<el-form-item label="加密连接" class="form_item">
<el-radio-group v-model="noticeConfig.email.secure">
<el-radio-button :value="true">SSL/TLS</el-radio-button>
<el-radio-button :value="false">STARTTLS/无</el-radio-button>
</el-radio-group>
<span class="tips">端口 465 通常使用 SSL/TLS,端口 587 使用 STARTTLS</span>
</el-form-item>
</template>
<!-- 通用字段 -->
<el-form-item label="邮箱地址" prop="email.user" class="form_item">
<el-input
v-model.trim="noticeConfig.email.user"
clearable
placeholder="your@email.com"
autocomplete="off"
class="input"
/>
</el-form-item>
<el-form-item label="SMTP 密码" prop="email.pass" class="form_item">
<el-input
v-model.trim="noticeConfig.email.pass"
clearable
type="password"
show-password
placeholder="SMTP 授权码/密码"
autocomplete="off"
class="input"
/>
</el-form-item>
<el-form-item label="收件人" class="form_item">
<el-input
v-model.trim="noticeConfig.email.to"
clearable
placeholder="留空则发送给自己"
autocomplete="off"
class="input"
/>
</el-form-item>
</template>
<!-- Telegram -->
<template v-if="noticeConfig.type === 'tg'">
<el-form-item label="Token" prop="tg.token" class="form_item">
<el-input
v-model.trim="noticeConfig.tg.token"
clearable
placeholder="Telegram Token"
autocomplete="off"
class="input"
/>
</el-form-item>
<el-form-item label="ChatId" prop="tg.chatId" class="form_item">
<el-input
v-model="noticeConfig.tg.chatId"
clearable
placeholder="Telegram ChatId"
autocomplete="off"
class="input"
/>
<span class="tips">Telegram Token/ChatId 获取: <a class="link" href="https://easynode.chaoszhu.com/zh/guide/get-tg-token" target="_blank">查看教程</a> </span>
</el-form-item>
</template>
<!-- Webhook -->
<template v-if="noticeConfig.type === 'webhook'">
<el-form-item label="URL" prop="webhook.url" class="form_item">
<el-input
v-model.trim="noticeConfig.webhook.url"
clearable
placeholder="https://example.com/webhook"
autocomplete="off"
class="input"
/>
</el-form-item>
<el-form-item label="请求方法" class="form_item">
<el-select v-model="noticeConfig.webhook.method" placeholder="" class="input">
<el-option label="POST" value="POST" />
<el-option label="GET" value="GET" />
<el-option label="PUT" value="PUT" />
</el-select>
</el-form-item>
<el-form-item label="Content-Type" class="form_item">
<el-select v-model="noticeConfig.webhook.contentType" placeholder="" class="input">
<el-option label="application/json" value="application/json" />
<el-option label="application/x-www-form-urlencoded" value="application/x-www-form-urlencoded" />
<el-option label="multipart/form-data" value="multipart/form-data" />
<el-option label="text/plain" value="text/plain" />
</el-select>
</el-form-item>
<el-form-item label="Header" class="form_item">
<el-input
v-model="noticeConfig.webhook.headers"
type="textarea"
:rows="2"
placeholder="JSON 格式,可选。例如:{"Authorization": "Bearer xxx"}"
autocomplete="off"
class="input"
/>
</el-form-item>
<el-form-item label="自定义模板" class="form_item">
<div class="template-wrapper">
<el-input
v-model="noticeConfig.webhook.template"
type="textarea"
:rows="6"
placeholder="{
"msgtype": "text",
"text": {
"content": "{{title}}\n{{content}}"
}
}"
autocomplete="off"
class="input"
/>
<el-button size="small" class="format-btn" @click="formatTemplate">格式化</el-button>
</div>
<span class="tips">支持变量: <code v-pre>{{title}}</code>, <code v-pre>{{content}}</code>, <code v-pre>{{timestamp}}</code>, <code v-pre>{{datetime}}</code>。留空则使用默认格式</span>
</el-form-item>
</template>
<el-form-item label="" class="form_item">
<el-button
type="primary"
:loading="loading"
@click="handleSave"
>
测试并保存
</el-button>
</el-form-item>
</el-form>
<el-alert type="success" :closable="false">
<template #title>
<span style="letter-spacing: 2px;"> 请确保通知方式能够正常收到测试通知 </span>
</template>
</el-alert>
<el-table v-loading="notifyListLoading" :data="notifyList">
<el-table-column prop="desc" label="通知类型" />
<el-table-column prop="sw" label="开关">
<template #default="{row}">
<el-switch
v-model="row.sw"
:active-value="true"
:inactive-value="false"
:loading="row.loading"
@change="handleChangeSw(row, $event)"
/>
</template>
</el-table-column>
</el-table>
</template>
<script setup>
import { ref, onMounted, computed, reactive } from 'vue'
import { getCurrentInstance } from 'vue'
const { proxy: { $api, $store, $notification, $message } } = getCurrentInstance()
const notifyListLoading = ref(false)
const notifyList = ref([])
const loading = ref(false)
const noticeConfig = ref({})
const noticeTypeList = ref([
{
type: 'email',
desc: '邮箱'
},
{
type: 'sct',
desc: 'Server酱'
},
{
type: 'tg',
desc: 'Telegram'
},
{
type: 'webhook',
desc: 'Webhook'
},
])
const formRef = ref(null)
const rules = reactive({
'sct.sendKey': { required: true, message: '需输入sendKey', trigger: 'change' },
'email.service': { required: true, message: '需输入邮箱提供商', trigger: 'change' },
'email.host': { required: true, message: '需输入SMTP服务器地址', trigger: 'change' },
'email.user': { required: true, type: 'email', message: '需输入邮箱', trigger: 'change' },
'email.pass': { required: true, message: '需输入邮箱SMTP授权码', trigger: 'change' },
'tg.token': { required: true, message: '需输入Telegram Token', trigger: 'change' },
'tg.chatId': [
{ required: true, message: '需输入Telegram ChatId', trigger: 'change' },
{
pattern: /^-?\d+$/,
message: 'ChatId必须为数字',
trigger: ['blur', 'change',]
},
],
'webhook.url': [
{ required: true, message: '需输入Webhook URL', trigger: 'change' },
{ type: 'url', message: '请输入有效的URL', trigger: 'change' },
]
})
// 邮箱模式切换时初始化默认值
const handleEmailModeChange = (useCustom) => {
if (useCustom) {
// 切换到自定义模式,设置默认值
if (!noticeConfig.value.email.port) {
noticeConfig.value.email.port = 465
}
if (noticeConfig.value.email.secure === undefined) {
noticeConfig.value.email.secure = true
}
}
}
// 端口变化时自动调整加密设置
const handlePortChange = (port) => {
if (port === 465) {
noticeConfig.value.email.secure = true
} else {
noticeConfig.value.email.secure = false
}
}
const isPlusActive = computed(() => $store.isPlusActive)
const handleSave = () => {
formRef.value.validate(async (valid) => {
if (!valid) return
// Webhook 类型时校验 JSON 格式
if (noticeConfig.value.type === 'webhook') {
const { template, headers } = noticeConfig.value.webhook || {}
// 校验自定义模板
if (template) {
try {
JSON.parse(template)
} catch (e) {
$message.error('自定义模板不是有效的 JSON 格式,请检查后重试')
return
}
}
// 校验自定义请求头
if (headers) {
try {
JSON.parse(headers)
} catch (e) {
$message.error('自定义请求头不是有效的 JSON 格式,请检查后重试')
return
}
}
}
try {
loading.value = true
await $api.updateNotifyConfig({ noticeConfig: { ...noticeConfig.value } })
// $message.success('保存成功')
$notification.success({
title: '测试通过 | 保存成功',
message: '请确认通知方式是否已收到通知'
})
} catch (error) {
console.error(error)
} finally {
loading.value = false
}
})
}
const getNotifyConfig = async () => {
try {
let { data } = await $api.getNotifyConfig()
noticeConfig.value = data || {}
} catch (error) {
console.error(error)
}
}
const getNotifyList = (flag = true) => {
if (flag) notifyListLoading.value = true
$api.getNotifyList()
.then(({ data }) => {
notifyList.value = data.map((item) => {
item.loading = false
return item
})
})
.finally(() => notifyListLoading.value = false)
}
const handleChangeSw = async (row) => {
row.loading = true
const { type, sw } = row
try {
await $api.updateNotifyList({ type, sw })
// if (this.userEmailList.length === 0) $message.warning('未配置邮箱, 此开关将不会生效')
} finally {
row.loading = false
}
getNotifyList(false)
}
const formatTemplate = () => {
if (!noticeConfig.value.webhook?.template) return
try {
const parsed = JSON.parse(noticeConfig.value.webhook.template)
noticeConfig.value.webhook.template = JSON.stringify(parsed, null, 2)
$message.success('格式化成功')
} catch (e) {
$message.warning('格式化失败:模板不是有效的 JSON 格式')
}
}
onMounted(() => {
getNotifyList()
getNotifyConfig()
})
</script>
<style lang="scss" scoped>
.form_item {
// width: 350px;
.input {
width: 450px;
}
.tips {
width: 100%;
font-size: 14px;
color: #999;
}
.template-wrapper {
display: flex;
flex-direction: column;
gap: 8px;
.format-btn {
align-self: flex-start;
}
}
}
</style>
|