2024-08-17 16:10:17 +08:00

51 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { readNotifyConfig, writeNotifyConfig, readNotifyList, writeNotifyList } = require('../utils')
const { sendServerChan, sendEmail } = require('../utils/notify')
// const commonTemp = require('../template/commonTemp')
async function getNotifyConfig({ res }) {
const data = await readNotifyConfig()
return res.success({ data })
}
async function updateNotifyConfig({ res, request }) {
let { body: { noticeConfig } } = request
let { type } = noticeConfig
try {
switch(type) {
case 'sct':
await sendServerChan(noticeConfig[type]['sendKey'], 'EasyNode通知测试', '这是一条测试通知')
break
case 'email':
await sendEmail(noticeConfig[type], 'EasyNode通知测试', '这是一条测试通知')
break
}
await writeNotifyConfig(noticeConfig)
return res.success({ msg: '测试通过 | 保存成功' })
} catch (error) {
return res.fail({ msg: error.message })
}
}
async function getNotifyList({ res }) {
const data = await readNotifyList()
res.success({ data })
}
async function updateNotifyList({ res, request }) {
let { body: { type, sw } } = request
if (!([true, false].includes(sw))) return res.fail({ msg: `Error type for sw${ sw }, must be Boolean` })
const notifyList = await readNotifyList()
let target = notifyList.find((item) => item.type === type)
if (!target) return res.fail({ msg: `更新失败, 不存在该通知类型:${ type }` })
target.sw = sw
await writeNotifyList(notifyList)
res.success()
}
module.exports = {
getNotifyConfig,
updateNotifyConfig,
getNotifyList,
updateNotifyList
}