微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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("发送失败")
      }

原文地址:https://www.jb51.cc/wenti/3288526.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐