win10消息提醒

/**
 * 提醒配置放在configs里
 * {
 *  status: 0, // 不用管
    step: 40,  // 间隔多少分钟
    startTime: moment(), // 不用管
    msg: {
      title: "警告", // 标题
      message: "大哥,工作辛苦了,喝口水吧!", // 内容
      icon: path.join(__dirname, "imgs", "kek.jpg"), // 图标
      sound: true, // 声音
      wait: true, // 等待点击
    },
 * }
 *
 * 依赖包 npm i node-notifier moment axios
 */

const notifier = require("node-notifier");
const moment = require("moment");
const path = require("path");
const axios = require("axios");

const configs = [
  {
    status: 0,
    step: 40,
    startTime: moment(),
    msg: {
      title: "警告",
      message: "大哥,工作辛苦了,喝口水吧!",
      icon: path.join(__dirname, "imgs", "kek.jpg"),
      sound: true,
      wait: true,
    },
  },
  {
    status: 0,
    step: () => {
      return Math.round(Math.random() * 10 + 5);
    },
    startTime: moment(),
    msg: async () => {
      const { data, status } = await axios.get("https://v1.hitokoto.cn/");
      let message = `${data.hitokoto}   --- ${data.from}`;
      if (status !== 200) {
        message = "API IS ERROR";
      }
      return {
        title: "一言",
        message,
        icon: path.join(__dirname, "imgs", "2.png"),
        sound: true,
        wait: true,
      };
    },
  },
  {
    status: 0,
    step: 5,
    startTime: moment(),
    where: () => {
      return Number(moment().format("HH")) >= 18;
    },
    msg: {
      title: "警告",
      message: "大哥,下班了,记得打卡哦!",
      icon: path.join(__dirname, "imgs", "1.png"),
      sound: true,
      wait: true,
    },
  },
];

(async () => {
  console.log("预览一下");
  for (const config of configs) {
    let msg = config.msg;
    if (typeof config.msg === "function") {
      msg = await config.msg();
    }
    notifier.notify({ ...msg });
  }

  while (true) {
    for (const config of configs) {
      setImmediate(async () => {
        const step =
          typeof config.step === "function" ? config.step() : config.step;

        const isTip =
          config.startTime.clone().add(step, "minutes").format("HH:mm") ===
          moment().format("HH:mm");

        const where = config.where ? config.where() : true;
        if (config.status === 0 && isTip && where) {
          config.status = 1;
          config.startTime = moment();
          let msg = config.msg;
          if (typeof config.msg === "function") {
            msg = await config.msg();
          }
          notifier.notify({ ...msg });
          console.log(msg.message);
        }

        const isTiped =
          config.startTime.clone().add(1, "minutes").format("HH:mm") ===
          moment().format("HH:mm");
        if (config.status === 1 && isTiped) {
          config.status = 0;
        }
      });
    }
    // console.log(moment().minute(), configs[0].startTime.format("HH:mm:ss"));
    await new Promise((r) => setTimeout(r, 5000));
  }
})();
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注