🆕 重构全局通知方案
This commit is contained in:
parent
525cbef68f
commit
82a89f827a
@ -1,4 +1,5 @@
|
|||||||
const { readNotifyConfig, writeNotifyConfig, readNotifyList, writeNotifyList } = require('../utils')
|
const { readNotifyConfig, writeNotifyConfig, readNotifyList, writeNotifyList } = require('../utils')
|
||||||
|
const { sctTest, emailTest } = require('../utils/test-notify')
|
||||||
// const commonTemp = require('../template/commonTemp')
|
// const commonTemp = require('../template/commonTemp')
|
||||||
|
|
||||||
async function getNotifyConfig({ res }) {
|
async function getNotifyConfig({ res }) {
|
||||||
@ -9,8 +10,21 @@ async function getNotifyConfig({ res }) {
|
|||||||
// 根据type待编写测试方法,测试通过才保存到库
|
// 根据type待编写测试方法,测试通过才保存到库
|
||||||
async function updateNotifyConfig({ res, request }) {
|
async function updateNotifyConfig({ res, request }) {
|
||||||
let { body: { noticeConfig } } = request
|
let { body: { noticeConfig } } = request
|
||||||
await writeNotifyConfig(noticeConfig)
|
let { type } = noticeConfig
|
||||||
return res.success()
|
try {
|
||||||
|
switch(type) {
|
||||||
|
case 'sct':
|
||||||
|
await sctTest(noticeConfig[type])
|
||||||
|
break
|
||||||
|
case 'email':
|
||||||
|
await emailTest(noticeConfig[type])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
await writeNotifyConfig(noticeConfig)
|
||||||
|
return res.success({ msg: '测试通过 | 保存成功' })
|
||||||
|
} catch (error) {
|
||||||
|
return res.fail({ msg: error.message })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getNotifyList({ res }) {
|
async function getNotifyList({ res }) {
|
||||||
|
47
server/app/utils/test-notify.js
Normal file
47
server/app/utils/test-notify.js
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
const nodemailer = require('nodemailer')
|
||||||
|
const axios = require('axios')
|
||||||
|
|
||||||
|
module.exports.sctTest = function ({ sendKey }) {
|
||||||
|
// eslint-disable-next-line no-async-promise-executor
|
||||||
|
return new Promise((async (resolve, reject) => {
|
||||||
|
consola.info('server酱通知测试: ', sendKey)
|
||||||
|
try {
|
||||||
|
let { data } = await axios.get(`https://sctapi.ftqq.com/${ sendKey }.send?title=messagetitle`)
|
||||||
|
resolve(data)
|
||||||
|
consola.success('测试成功')
|
||||||
|
} catch (error) {
|
||||||
|
reject(error)
|
||||||
|
consola.error('测试失败: ', error)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.emailTest = function (conf) {
|
||||||
|
// eslint-disable-next-line no-async-promise-executor
|
||||||
|
return new Promise((async (resolve, reject) => {
|
||||||
|
consola.info('邮箱通知测试: ', conf)
|
||||||
|
try {
|
||||||
|
const { service, user, pass } = conf
|
||||||
|
let transporter = nodemailer.createTransport({
|
||||||
|
service,
|
||||||
|
auth: {
|
||||||
|
user,
|
||||||
|
pass
|
||||||
|
}
|
||||||
|
})
|
||||||
|
let info = await transporter.sendMail({
|
||||||
|
from: user,
|
||||||
|
to: user,
|
||||||
|
subject: 'EasyNode: 测试邮件通知',
|
||||||
|
text: '测试邮件',
|
||||||
|
html: '<b>测试邮件</b>'
|
||||||
|
})
|
||||||
|
consola.info('Message sent: %s', info.messageId)
|
||||||
|
resolve()
|
||||||
|
consola.success('测试成功')
|
||||||
|
} catch (error) {
|
||||||
|
reject(error)
|
||||||
|
consola.error('测试失败: ', error)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}
|
@ -40,9 +40,10 @@
|
|||||||
<el-input
|
<el-input
|
||||||
v-model.trim="noticeConfig.email.service"
|
v-model.trim="noticeConfig.email.service"
|
||||||
clearable
|
clearable
|
||||||
placeholder="邮箱服务商简写, 例如: Gmial、qq、126、163"
|
placeholder=""
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
/>
|
/>
|
||||||
|
<span class="tips">邮箱服务商, 例如: Gmial、qq、126、163, 支持列表: <a class="link" href="https://github.com/nodemailer/nodemailer/blob/master/lib/well-known/services.json" target="_blank">点击查询</a> </span>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item label="邮箱地址" prop="email.user" class="form_item">
|
<el-form-item label="邮箱地址" prop="email.user" class="form_item">
|
||||||
<el-input
|
<el-input
|
||||||
@ -63,7 +64,7 @@
|
|||||||
</template>
|
</template>
|
||||||
<el-form-item label="" class="form_item">
|
<el-form-item label="" class="form_item">
|
||||||
<el-button type="primary" :loading="loading" @click="handleSave">
|
<el-button type="primary" :loading="loading" @click="handleSave">
|
||||||
保存并测试
|
测试并保存
|
||||||
</el-button>
|
</el-button>
|
||||||
<!-- <el-tooltip effect="dark" content="重复添加的邮箱将会被覆盖" placement="right">
|
<!-- <el-tooltip effect="dark" content="重复添加的邮箱将会被覆盖" placement="right">
|
||||||
</el-tooltip> -->
|
</el-tooltip> -->
|
||||||
@ -100,20 +101,28 @@ const rules = reactive({
|
|||||||
const handleSave = () => {
|
const handleSave = () => {
|
||||||
formRef.value.validate(async (valid) => {
|
formRef.value.validate(async (valid) => {
|
||||||
if (!valid) return
|
if (!valid) return
|
||||||
await $api.updateNotifyConfig({ noticeConfig: { ...noticeConfig.value } })
|
try {
|
||||||
$message.success('保存成功')
|
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 () => {
|
const getNotifyConfig = async () => {
|
||||||
try {
|
try {
|
||||||
loading.value = true
|
|
||||||
let { data } = await $api.getNotifyConfig()
|
let { data } = await $api.getNotifyConfig()
|
||||||
noticeConfig.value = data || {}
|
noticeConfig.value = data || {}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
} finally {
|
|
||||||
loading.value = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,5 +135,10 @@ onMounted(() => {
|
|||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
.form_item {
|
.form_item {
|
||||||
width: 350px;
|
width: 350px;
|
||||||
|
|
||||||
|
.tips {
|
||||||
|
font-size: 12px;
|
||||||
|
color: #999;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user