微信小程序发送模板消息

时间:2022-07-25
本文章向大家介绍微信小程序发送模板消息,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

微信小程序还提供了给客服发送模板消息的功能,以便商家给客户发送通知。

官方文档

https://developers.weixin.qq....

使用效果

具体步骤

1、设置模板

登录 https://mp.weixin.qq.com/ “功能”-“模板消息”-“模板库” 这里我们选择合适的模板,点"选用”。 回到"我的模板",这里会看到选用的模板,有相应的模板id。

2、发送模板消息

//获取access_token
 $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $wxConfig['AppID'] . '&secret=' . $wxConfig['AppSecret']; //替换成自己的小程序appid和appsecret
        $weixin = file_get_contents($url);
        $jsondecode = json_decode($weixin);
        $array = get_object_vars($jsondecode);
        $token = $array['access_token'];

        $data = [];
        $data['touser'] =$openid;//这里填用户的opentid
        $data['template_id'] = 'xxxxxxxxxxxxxxxxxxx';//这里填第一步选用的模板id
        //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
        $data['page'] = 'pages/index/main';
        
        $data['form_id'] = $row['prepay_id'];//表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id,本例为prepay_id,即支付时返回的prepay_id
        //
        $data['emphasis_keyword'] = "keyword1.DATA";//模板需要放大的关键词,不填则默认无放大
        //模板内容,不填则下发空模板。具体格式请参考示例。
        $data['data'] = [
            'keyword1' => ["value" => $row['money'],
                // "color"=>"#173177" //这里还可以设置字体颜色
            ],
            'keyword2' => ["value" => $row['trade_sn'],
                // "color"=>"#173177" //这里还可以设置字体颜色
            ],
            'keyword3' => ["value" => $row['ptime'],
              // "color"=>"#173177" //这里还可以设置字体颜色
            ],
            'keyword4' => ["value" => $buyRow['nickname'],
              // "color"=>"#173177" //这里还可以设置字体颜色
            ],
        ];
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $token;
        $data = json_encode($data, true);
        $return = $this->Post($url, $data);
 public function Post($url, $data)
    {
        $cl = curl_init();
        if (stripos($url, 'https://') !== FALSE) {
            curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($cl, CURLOPT_SSLVERSION, 1);
        }
        curl_setopt($cl, CURLOPT_URL, $url);
        curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($cl, CURLOPT_POST, true);
        curl_setopt($cl, CURLOPT_POSTFIELDS, $data);
        $content = curl_exec($cl);
        $status = curl_getinfo($cl);
        curl_close($cl);
        if (isset($status['http_code']) && $status['http_code'] == 200) {
            return $content;
        } else {
            return FALSE;
        }
    }

3、返回值

返回示例

{
 "errcode": 0,
 "errmsg": "ok"
}

errcode: 0:为成功

errCode 的合法值

说明

最低版本

40037

template_id不正确

41028

form_id不正确,或者过期

41029

form_id已被使用

41030

page不正确

45009

接口调用超过限额(目前默认每个帐号日调用限额为100万)

小结

模板推送位置:服务通知 模板下发条件:用户本人在微信体系内与页面有交互行为后触发,详见 下发条件说明 模板跳转能力:点击查看详情仅能跳转下发模板的该帐号的各个页面 需要注意的是formID有两种方式: 一种是小程序前端,页面的 form 组件,属性 report-submit 为 true 时,可以声明为需要发送模板消息,此时点击按钮提交表单可以获取 formId,用于发送模板消息。 wxml文件代码:

<form bindsubmit="submit" report-submit="true">
  <!--这里是表单的各种 <input>-->
  <button formType="submit">提交</button>
</form>

js 点击将formid 传给服务端。

submitForm:function(e){
    var formid = e.detail.formid;
    wx.request({
      url: 'http://xxxxxxxxx.php',//服务器地址
      data:{
        formid:formid
      },
      header:{
        "Content-type":"application/json",
      },
      success:function(res){
        console.log(res.data);
      },
      fail:function(err){
        console.log(err);
      }
    })
  }

还有一种就服务端完成支付行为后返回的prepay_id。