微信小程序发送模板消息

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

官方文档

https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html

使用效果

在这里插入图片描述

具体步骤

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 的合法值

说明最低版本
40037template_id不正确
41028form_id不正确,或者过期
41029form_id已被使用
41030page不正确
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。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值