微信小程序获取formid订阅消息并发送给用户

微信小程序获取formid订阅消息并发送给用户

微信<a href='/tag/miniprogram.html'>小程序</a>获取formid订阅消息并发送给用户

今天带大家了解小程序中如何发送消息给用户

一、小程序代码

先看wxml,在form表单中增加report-submit='true'

<form bindsubmit='registerFormSubmit' report-submit='true'>

      <view class='buttons'>

        <button class='canclebtn' bindtap='verifyCancleAction'>取消</button>

        <button class='confirmbtn' form-type='submit'>确定</button>

      </view>

    </form>

那么js中获取formid

registerFormSubmit: function (e) {
 
     //打印formId
     console.log(e.detail.formId,);
    var formid=e.detail.formId
    //通过request传给后端
    wx.request({
      url:'后端地址',
      data: {
          id: formid
      }
    });

},

二、微信公众号小程序后台添加模板

微信<a href='/tag/miniprogram.html'>小程序</a>获取formid订阅消息并发送给用户

打开订阅消息后,点击右边添加

微信<a href='/tag/miniprogram.html'>小程序</a>获取formid订阅消息并发送给用户

添加完成后会看到模板id

三、小程序提醒用户订阅消息

在提交完表单后提醒用户进行消息订阅,用户同意后才能向用户发送消息,小程序端js代码如下

wx.requestSubscribeMessage({
  tmplIds: ['模板id'],
  success (res) { }
})

此段代码会在小程序弹出一个订阅提醒,如下图

微信<a href='/tag/miniprogram.html'>小程序</a>获取formid订阅消息并发送给用户

四、后端代码

接下来我们后端来发送消息吧

<?php
class TempMessage
{
    private $appId = '';
    private $appSecret = '';


    public function sendmMsg($openid, $formId,$template_id, $content, $username, $url) {
        $tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
        $tokenValue = $this->httpGet($tokenUrl);
        $TmUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$tokenValue->access_token";
        $data = [
            'touser' => $openid,
            'template_id' => $template_id,
            'page' => $url,//小程序消息提醒点击跳转小程序页面路径
            'form_id' => $formId,
            "data" => array(
                'keyword1' => array('value' => $content),
                'keyword2' => array('value' => $username),
                'keyword3' => array('value' => date('Y-m-d H:i:s', time()))
            ),
        ];
        return $this->httpPost($TmUrl, $data);
    }

    private function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_URL, $url);
        $res = curl_exec($curl);
        curl_close($curl);
        return json_decode($res);
    }

    private function httpPost($url, $post_data) {
        $postdata = json_encode($post_data, JSON_UNESCAPED_UNICODE);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/json',
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间(单位:s)
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        return $result;
    }


}

好了,小程序订阅消息推送消息代码就这么多了,有问题请联系我。


{{collectdata}}

网友评论0