首先讲两个注意事项
1、小程序和微信公众号的用户openid是不同的。
2、小程序需要用户手动授权订阅消息通知(一次性订阅是订阅一次发一次,长期订阅可以多发)。
关于小程序和公众号AppID和AppSecret的获取可以自行百度一下,挺简单的。
小程序订阅消息
1.1 在后台选中自己需要的模板和相应的字段,注意不同的字段字符长度和格式的问题。
参考文档:subscribeMessage.send | 微信开放文档
【订阅消息参数值内容限制说明】
1.2 小程序授权按钮
<button class="btn" bindtap="onSubscribe" hover-class="btn-hover">
订阅挂号成功提醒
</button>
//开启消息通知
onSubscribe: function(e) {
// 消息推送
wx.requestSubscribeMessage({
tmplIds: ['daPvT7nLnOY***************F_A9VCBI'], //你的模板id
success(res) {
console.log('授权成功', res)
},
fail(res) {
console.log('授权失败', res)
}
})
},
1.3 我这里用的HTTPS发送,代码是php
<?php
/*
* 微信小程序消息订阅
*/
public function actionSubmessage($openid = null){
//配置appid
$appid = "";
//配置appscret
$secret = "";
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
//获取access_token
$access_token = $this->geturl($url)["access_token"];
$urlz="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token;
//模板id
$template_id = "daPvT7nLnO*********************A9VCBI";
$data = [] ;
//接收者(用户)的 openid
$data['touser'] = $openid;
//所需下发的订阅模板id
$data['template_id'] = $template_id ;
//点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
$data['page'] = "pages/index/index" ;
//模板内容 phrase date thing 这些类型有字符长短要求,注意看文档
$data['data'] = [
"phrase2" => [
'value' => '测试'
],
"phrase6" => [
'value' => '就诊李主任'
],
"date14" => [
'value' => '2022年06月11日'
],
"date8" => [
'value'=>'8:30 ~ 17:00'
],
'thing18'=>[
'value'=> '呼吸内科'
]
];
//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
$data['miniprogram_state'] = 'developer';
var_dump($this->posturl($urlz,$data));
}
/*
* curl get请求封装,返回json数据格式数据
*/
public function geturl($url){
$headerArray =array("Content-type:application/json;","Accept:application/json");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
return $output;
}
/*
* curl post请求封装,返回json数据格式数据
*/
public function posturl($url,$data){
$data = json_encode($data);
$headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output,true);
}
?>
公众号模板消息推送
2.1 微信公众号开启模板消息推送
2.2 这个前端没啥特别的,直接上后端代码,用的tp6框架,可以根据实际情况自己改
类文件 WxMessage.php
<?php
//WxMessage.php
namespace Think;
class WxMessage
{
//服务号
var $appid = '';
var $appsecret = '';
//构造函数,获取Access_Token
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid && $appsecret){
$this->appid = $appid;
$this->appsecret = $appsecret;
}
//$this->lasttime = 1654756504;
//$this->access_token = "57_cu07w68F9BPur3VvmkFPS6i_OLkN5dGJeCeJn8u8PKNRgxOmQ4PtCVesDNFwIj-6ExGmHhM2vzoparead5OnW7FZmuYWEyr2JwszreshbNdDQ8mtDaGncwnbgVVXjf8UKvaagCfqOct5hS97ZQHhAFALCT";
//if(time() > ($this->lasttime + 7200)){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
$res = $this->http_request($url);
$result = json_decode($res, true);
//echo $result["access_token"];
$this->access_token = $result["access_token"];
$this->lasttime = time();
//}
}
public function send_template_message($data)
{
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$this->access_token;
$res = $this->http_request($url, $data);
return json_decode($res, true);
}
//https请求(支持GET和POST)
protected function http_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}
?>
<?php
namespace app\admin\controller;
use think\Controller;
use think\Validate;
use think\facade\Request;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use think\WxMessage; //引入类
class Weixinqf extends Base
{
//发送提醒
public function sendMessage(){
//构造消息模板
$openid = ""; //自己去获取
$zname = "程序测试";
$zsource = "公众号—就诊(李主任)";
$zdate = "2022-06-08(上午)";
$template = $this->orderSuccess($openid,'',$zname,$zsource,$zdate);
//实例化消息类
$message = new WxMessage();
//发送消息
$message->send_template_message(urldecode(json_encode($template)));
}
//成功通知模板
/*
* openid:微信唯一标识
* zname:姓名
* zsource:来源
* zdate:时间
*/
public function orderSuccess($openid=null,$url=null,$zname=null,$zsource=null,$zdate=null){
$template = array(
"touser" => "{$openid}",
"template_id" => "7zQSy****************KOlsc",
//'url' => "{$url}", //也可以配置这个,就可以进入自己配置的网址
"miniprogram" => array( //我这里引入的小程序
"appid" => "*******", "pagepath"=>"pages/index/index"
),
'data' => array(
'first' => array('value' =>urlencode('挂号成功'),'color' => "#FF0000"),
'keyword1' => array('value'=>urlencode($zname)),
'keyword2' => array('value'=>urlencode($zsource)),
'keyword3' => array('value'=>urlencode($zdate)),
'keyword4' => array('value'=>urlencode('8:30 - 17:00')),
'keyword5' => array('value'=>urlencode('呼吸内科')),
'remark' => array('value'=>urlencode('凭预约短信和就诊人身份证在门诊一楼导医台取号就诊,一对一问诊,医务人员全程陪同。详情请咨询:028-******!'),'color' => "#FF0000"),
),
);
return $template;
}
}
?>