tip.js 1.81 KB
const Subscription = require("egg").Subscription;
const { dateFormat } = require("../utils/utils");
module.exports = {
  schedule: {
    cron: "0 2 9,11 * * ?",
    type: "worker", // 指定所有的 worker 都需要执行
  },
  async task(ctx) {
    const reqRes = await ctx.curl(
      "https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=test&env=llx",
      { dataType: "json" }
    );
    let {
      includeList,
      excludeList,
      infoList,
      startDate,
      firstTipText,
      otherTipText,
      secondTipText,
      robotUrl,
    } = reqRes.data.data;
    let idx =
      parseInt(
        (new Date().getTime() - new Date(startDate).getTime()) / (86400000 * 7)
      ) % infoList.length;
    let curDay = dateFormat(new Date()); //今天
    let isWeekend = new Date().getDay() == 0 || new Date().getDay() == 6; //是否是周末
    if (
      includeList.includes(curDay) ||
      (!excludeList.includes(curDay) && isWeekend)
    ) {
      return;
    }
    //如果是节假日或者 不是调休日和周末 则不提醒
    let curPerson = `<@${infoList[idx]["id"]}>`; //当前日报的提醒人
    let content = firstTipText.replace(/@xxx/g, curPerson);
    if (new Date().getDay() == 1 && otherTipText) {
      let otherTip = otherTipText.replace(/@xxx/g, curPerson);
      content = content + otherTip;
    }
    if (new Date().getHours() == 11) {
      content = secondTipText.replace(/@xxx/g, curPerson);
    }
    const ROBOT_DATA = {
      msgtype: "markdown",
      markdown: {
        content,
      },
    };

    const result = await ctx.curl(robotUrl, {
      method: "POST",
      headers: {
        "content-type": "application/json",
      },
      data: JSON.stringify(ROBOT_DATA),
    });

    ctx.app.cache = {
      code: "0",
      data: result,
      msg: "提醒成功",
    };
  },
};