eggjs发送企业微信消息通知

问题描述

https://developer.work.weixin.qq.com/document/path/91039 获取access_token

在这里插入图片描述


https://developer.work.weixin.qq.com/document/path/90313 企业微信接口全局错误码查询
https://developer.work.weixin.qq.com/document/path/90236 发送应用消息

在这里插入图片描述


封装到service层 mages.js

'use strict';

const Service = require('egg').Service;

class MagesService extends Service {
    //工号  地点  时间  原因
  async xiaoxi(username,addess,time,content) {
    const {ctx ,app} =this;
    const AgentId= 数字id   //企业应用的id,整型。企业内部开发,可在应用的设置页面查看;
    const Secret= "Secret"  //应用的凭证密钥
    const qiyeid="qiyeid"  //企业ID
    let res = await ctx.curl(`https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${qiyeid}&corpsecret=${Secret}`, {
          method:'GET',
         // rejectUnauthorized: true,//想忽略证书 false
         // cert: fs.readFileSync(cerPaht),//对证书格式有要求
          headers:{//自定义header
              "Accept": "*/*",
              "Content-Type":"application/json"
               },
          data:{ },
          dataType: 'json',
          });
   
    if(res.data.errcode==0){
            let access_token=res.data.access_token
            let msg=await ctx.curl(`https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${access_token}`, {
                method:'POST',
               // rejectUnauthorized: true,//想忽略证书 false
               // cert: fs.readFileSync(cerPaht),//对证书格式有要求
                headers:{//自定义header
                    "Accept": "*/*",
                    "Content-Type":"application/json"
                     },
                data:{
                    "touser" : username,
                    "msgtype" : "markdown",
                    "agentid" : AgentId,
                    "markdown" : {
                        "content" :`系统通知
                                >**通知详情**
                                >事 项:<font color=\"info\">警报</font>
                                >
                                >地  点:<font color=\"info\">${addess}</font>
                                >日 期:<font color=\"warning\">${time}</font>
                                >
                                >原 因:${content}
                             
                    },
                    "enable_duplicate_check": 0,
                    "duplicate_check_interval": 1800
                 },
                dataType: 'json',
                });
            if(msg.data.errcode==0){
                return true;
            }else{
                return false;
            }
          }else{
            return false;
          }    
  }
}

module.exports = MagesService;

调用

 const has_send = await this.service.mages.xiaoxi(eqrr.custodian,didian,dianjiantime, yuyin);
     
      if (has_send) {
          console.log("发送成功")
      }else{
        console.log("发送失败")
      }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)