小程序 发送模板消息功能封装

// 需添加 微信 域名 https://api.weixin.qq.com 

var requestHandler = {
  openId: null,  //发送用户openid (必填)
  template_id: null, //发送 模板ID (必填)
  page: null,      //(选填) 点击模板卡片后的跳转页面,仅限本小程序内的页面
  form_id: null, // (必填) 表单 form_id 或者  支付的 prepay_id
  data: {}, //模板内容,不填则下发空模板 (选填)
  color: null, //模板内容字体的颜色,不填默认黑色  (选填)
  emphasis_keyword: null, //模板需要放大的关键词,不填则默认无放大 (选填)
  success: function (res, error) {
    // success
  },
  fail: function () {
    // fail
  },
  complete: function () {
    // 执行之后操作
  }

}
function wxNotice(requestHandler){

  wx.request({
    url: "https://api.weixin.qq.com/cgi-bin/token",
    data: {
      "grant_type": "client_credential", // 固定值
      "appid": "wx4f469ee39d85b0ab",  //小程序ID 
      "secret": "4b4172b8888c1c1cc81e9637db90c068", //小程序secret 
    },
    method: "GET", // OPTIONS, , HEAD, POST, PUT, DELETE, TRACE, CONNECT
    success: function (res) {
      var acc_token = res.data.access_token;
      var url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + acc_token;
      if (!requestHandler.openId) {
        console.log("openID为空");
        return ;
      }
      if (!requestHandler.template_id) {
        console.log("模板ID为空");
        return;
      }
      wx.request({
        url: url,
        data: {
          "template_id": requestHandler.template_id,
          "touser": requestHandler.openId,
          "page": requestHandler.page,
          "form_id": requestHandler.form_id,
          "data": requestHandler.data,
          "color": requestHandler.color,
          "emphasis_keyword": requestHandler.emphasis_keyword
        },
        method: "POST", // OPTIONS, , HEAD, POST, PUT, DELETE, TRACE, CONNECT
        success: function (ress) {
          console.log(ress);
          
          if (res.data.errcode == 0 && res.data.errmsg == "ok"){
            
            requestHandler.success(ress, null)
          }else{
            requestHandler.success(null, res.data.errmsg)
          }
        },
        fail: function (){
          if (requestHandler.fail) {
            requestHandler.fail()
          }
        },
        complete : function(){
          if (requestHandler.complete) {
            requestHandler.complete()
          }
        }

      })
    },

  })
}

module.exports = {
  wxNotice: wxNotice,
}

使用样例 eg :

// 
//  var notice = require('../common/notice');
 
// var openId = wx.getStorageSync('openId');
// var formId = e.detail.formId;
// console.log(openId + "++++" + formId);
// notice.wxNotice({
//   openId: openId,
//   template_id: "o8xXtu4DRKtMuplamta0TukE2pGW-KVQxoWH_Allwts",
//   page: "index",
//   form_id: e.detail.formId,
//   data: {
//     "keyword1": {
//       "value": "2018年3月28日下午1点22分",
//       "color": "#173177"
//     },
//     "keyword2": {
//       "value": "10000元",
//       "color": "#173177"
//     },
//     "keyword3": {
//       "value": "4003182001201611291d140743105",
//       "color": "#173177"
//     },
//     "keyword4": {
//       "value": "100.00元",
//       "color": "#173177"
//     },
//     "keyword5": {
//       "value": "10100元",
//       "color": "#173177"
//     },
//     "keyword6": {
//       "value": "充值未到账可联系客服XXXXXX",
//       "color": "#173177"
//     }
//   }, //模板内容,不填则下发空模板 (选填)
//   color: "#27983d",
//   emphasis_keyword: "keyword2.DATA",
//   success: function (res, error) {
//     if (error) {
//       console.log("失败");
//       return;
//     }
//     console.log("成功");
//   }, fail: function () {
//     console.log("失败");
//   }, complete: function () {
//     console.log("完成");
//   }

// }) 

效果图

 
2332815-1a71046772d7a47f.PNG
IMG_2087.PNG
原创文章 18 获赞 102 访问量 7953

猜你喜欢

转载自blog.csdn.net/weixin_46602773/article/details/105006101