node-schedule
https://www.npmjs.com/package...
使用示例
比如每天下午6点发邮件通知天气
const schedule = require('node-schedule'); //定时服务
//每天18h运行天气预报任务
schedule.scheduleJob('0 0 18 * * *', function () {
console.log('schedule:' + new Date());
email.sendEmail();//执行邮件发送
});
sendEmail 执行邮件发送,此时借助nodemailer邮件服务(https://www.npmjs.com/package...)
const nodemailer = require('nodemailer'); //邮件服务
const axios = require('axios');
module.exports = {
/**
* 借助高德地图 获取天气
*/
getTianqi: async function (citycode){
let { status, data } = await axios.get(`https://restapi.amap.com/v3/weather/weatherInfo?city=${citycode}&extensions=all&key=这里是自己申请的高德key`);
if (status === 200 && data && data.forecasts && data.forecasts.length) {
let { city, casts } = data.forecasts[0];
let { week, dayweather, nightweather, daytemp, nighttemp, daypower, nightpower} = casts[1];
return `周${week} ${city}:${nightweather}${nighttemp}°~${dayweather}${daytemp}°,风${daypower}~${nightpower}`;
}else{
return ''
}
},
/**
* 发送邮件
*/
sendEmail: async function(){
let self = this;
try {
let bj = await self.getTianqi('110105');//获取天气-朝阳区 110105
//发送邮件
let transporter = nodemailer.createTransport({
service: 'qq',
auth: {
user: "xxxxxx@qq.com",
pass: "xxxxx",//授权码 (可在qq邮箱获取)
}
});
await transporter.sendMail({
from: `"系统"<xxxx@qq.com>`,
to: "xxxx@qq.com",
subject: `天气预报`,
text: bj,
});
// console.log('Message sent success: ' + JSON.stringify(info));
} catch (error) {
console.log(error);
}
},
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。