From f3f629cf10fcf0eca6009720eac41bc91694e723 Mon Sep 17 00:00:00 2001 From: "gao.zhiyuan" Date: Thu, 20 Oct 2022 10:27:12 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AD=9B=E9=80=89=E5=AF=BC=E5=87=BA=E7=9A=84?= =?UTF-8?q?=EF=BC=88=E9=A2=9D=E5=A4=96=EF=BC=89=E5=AD=97=E6=AE=B5key?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/lib/schedule/timor_holiday.js | 137 ++++++++++++++------------ api/app/lib/utils/push.js | 62 ++++++++++++ 2 files changed, 137 insertions(+), 62 deletions(-) create mode 100644 api/app/lib/utils/push.js diff --git a/api/app/lib/schedule/timor_holiday.js b/api/app/lib/schedule/timor_holiday.js index e530a0a..28afef3 100644 --- a/api/app/lib/schedule/timor_holiday.js +++ b/api/app/lib/schedule/timor_holiday.js @@ -6,79 +6,92 @@ module.exports = function (app, opts) { const timorHoliday = app.fs.scheduleInit( { interval: '54 42 4 */7 * * *', - // immediate: true, - proRun: true, + immediate: true, + // proRun: true, }, async () => { const { timorApiUrl } = opts const { models } = app.fs.dc - + const { pushByEmail } = app.fs.utils const existCount = await models.Holiday.count() let begainDay = existCount ? moment() : moment('2022-01-01') let endDay = moment().add(3, 'months').endOf('month') let checkDay = begainDay.clone() - while (checkDay.isSameOrBefore(endDay)) { - try { - const checkDayStr = checkDay.format('YYYY-MM-DD') - let holidayRes = await request.get( - timorApiUrl + `holiday/info/${checkDayStr}`, - ) - let holidayData = holidayRes.body || {} - if (holidayData.code == 0) { + // while (checkDay.isSameOrBefore(endDay)) { + // try { + // let start = (new Date()).getTime(); + // const checkDayStr = checkDay.format('YYYY-MM-DD') + // let holidayRes = await request.get( + // timorApiUrl + `holiday/info/${checkDayStr}`, + // ) + // let holidayData = holidayRes.body || {} + // if (holidayData.code == 0) { + + // let holidayType = holidayData.type.type + // let dayType = '' + // if ( + // holidayType == 2 + // && holidayData.holiday + // && holidayData.holiday.wage == 3 + // ) { + // // 正经节假日 3倍工资那种 + // // festivals + // dayType = 'festivals' + // } else if ( + // holidayType == 1 + // || ( + // holidayType == 2 + // && holidayData.holiday + // && holidayData.holiday.wage < 3 + // ) + // ) { + // // 普假 休息日非节假日 + // dayType = 'dayoff' + // } else if (holidayType == 0 || holidayType == 3) { + // // 工作日或补班 + // dayType = 'workday' + // } + + // const dbRes = await models.Holiday.findOne({ + // where: { + // day: checkDayStr + // } + // }) + // if (dbRes) { + // await models.Holiday.update({ + // holiday: holidayData, + // type: dayType + // }, { + // where: { + // day: checkDayStr + // } + // }) + // } else { + // await models.Holiday.create({ + // day: checkDayStr, + // holiday: holidayData, + // type: dayType + // }) + // } + // } + // checkDay = checkDay.add(1, 'day') - let holidayType = holidayData.type.type - let dayType = '' - if ( - holidayType == 2 - && holidayData.holiday - && holidayData.holiday.wage == 3 - ) { - // 正经节假日 3倍工资那种 - // festivals - dayType = 'festivals' - } else if ( - holidayType == 1 - || ( - holidayType == 2 - && holidayData.holiday - && holidayData.holiday.wage < 3 - ) - ) { - // 普假 休息日非节假日 - dayType = 'dayoff' - } else if (holidayType == 0 || holidayType == 3) { - // 工作日或补班 - dayType = 'workday' - } + // await new Promise(resolve => setTimeout(() => resolve(), 3000)); + // } catch (error) { + // console.error(error); + // } + // } - const dbRes = await models.Holiday.findOne({ - where: { - day: checkDayStr - } - }) - if (dbRes) { - await models.Holiday.update({ - holiday: holidayData, - type: dayType - }, { - where: { - day: checkDayStr - } - }) - } else { - await models.Holiday.create({ - day: checkDayStr, - holiday: holidayData, - type: dayType - }) - } - } - checkDay = checkDay.add(1, 'day') - } catch (error) { - console.error(error); - // TODO 发邮件 - } + let existLastDayRes = await models.Holiday.findOne({ + order: [['day', 'DESC']] + }) + console.log(moment(existLastDayRes.day).diff(moment(), 'day')); + if (moment(existLastDayRes.day).diff(moment(), 'day') < 32 || 1) { + console.log(1); + await pushByEmail({ + email:[] + }) } } ) diff --git a/api/app/lib/utils/push.js b/api/app/lib/utils/push.js new file mode 100644 index 0000000..a0cf5ed --- /dev/null +++ b/api/app/lib/utils/push.js @@ -0,0 +1,62 @@ +'use strict'; + +const moment = require('moment') +const Core = require('@alicloud/pop-core'); +const nodemailer = require('nodemailer') + +module.exports = function (app, opts) { + const pushBySms = async ({ phone = [], templateCode, templateParam } = {}) => { + try { + if (phone.length) { + const client = new Core({ + accessKeyId: opts.sms.accessKey, + accessKeySecret: opts.sms.accessSecret, + endpoint: 'http://dysmsapi.aliyuncs.com',//固定 + apiVersion: '2017-05-25'//固定 + }); + const SendSmsRes = await client.request('SendSms', { + "PhoneNumbers": phone.join(','),//接收短信的手机号码。 + "SignName": "飞尚尚视",//短信签名名称。必须是已添加、并通过审核的短信签名。 + "TemplateCode": templateCode,//短信模板ID。必须是已添加、并通过审核的短信签名;且发送国际/港澳台消息时,请使用国际/港澳台短信模版。 + "TemplateParam": JSON.stringify(templateParam)//短信模板变量对应的实际值,JSON格式。 + }, { + method: 'POST' + }); + return SendSmsRes + } + } catch (error) { + throw error + } + } + + const pushByEmail = async ({ email = [], title, text = '', html = '', attachments = undefined, } = {}) => { + try { + let transporter = nodemailer.createTransport({ + host: opts.email.host, + port: opts.email.port, + secure: true, + auth: { + user: opts.email.sender.address, + pass: opts.email.sender.password, + } + }); + + // send mail with defined transport object + await transporter.sendMail({ + from: `${opts.email.sender.name}<${opts.email.sender.address}>`, // sender address + to: email.join(','), // list of receivers 逗号分隔字符串 + subject: title, // Subject line + text: text, // plain text body + html: html, // html body + attachments: attachments + }); + } catch (error) { + throw error + } + } + + return { + pushByEmail, + pushBySms, + } +} \ No newline at end of file