原先的下发统一消息不能用故此有了以下代码
模板消息发送需要公众号的openid
1、微信开放平台需要绑定关系,在小程序获取openid时再存入一个unionid(开放平台不绑定关系获取不到)
2、登录公众号-设置与开发-基本配置 编辑服务器配置 url是你接口接收的地址,令牌是随意输入的(这个地方有个坑,需要把规定的代码都写好才能提交,不然提示token验证失败,代码在下面自己提取)
3、 用户点击公众号后向服务器发送通知 接口的通知里面能拿到用户的openid,再用公众号的opneid再去请求用户的unionid,与小程序做关联。
4、发送模板消息
//获取Openid及其用户的unionid
public function getOpid($code = '')
{
$appid = $this->wxAppId; // 小程序APPID
$secret = $this->wxAppSecret; // 小程序secret
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 5000);
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);
$data = json_decode($res,true);
return $data;
//$this->success('成功',$data);
}
//用户操作公众号时向服务器发送通知(这个是公众号配置服务器时的url地址)
public function getWxInfo()
{
if(isset($_GET['echostr'])){
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = 'xxx';//手动设置的token
$EncodingAESKey = '*****';
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
echo $_GET['echostr'];
}
}
$xml = file_get_contents('php://input', 'r');//这个地方已经拿到公众号的openid
$data = $this->toArray($xml);
if ($data['MsgType'] == "event"){
//关注操作时出发
$appid = 'xxx';//公众号的appid
$secret = 'xxx';
$token=$this->getAccToken($appid,$secret);//获取公众号的token
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" .$token . "&openid=" . $data['FromUserName'] ."&lang=zh_CN";
$UnionID = $this->httpUtil($url);
$arr = json_decode($UnionID,true);//这个地方拿到用户的unionid 和小程序的用户做关联
//后续逻辑自己处理 判断自己加
exit();
}else{
exit();
}
}
//将XML转为array
public function toArray($xml)
{
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $values;
}
public function getAccToken($appid,$secret){
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$token_res = $this->httpUtil($token_url);
$token_res = json_decode($token_res, true);
$token = $token_res['access_token'];
return $token;
}
//发送请求
function httpUtil($url, $data = '', $method = 'GET'){
try {
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
if ($method == 'POST') {
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
if ($data != '') {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
curl_close($curl); // 关闭CURL会话
return $tmpInfo; // 返回数据
} catch (Exception $e) {
return $e->getMessage();
}
}
//发送模板消息
/**
$data 参数样式
[{"openid":"od3kw5gU-","unionid":"oqJ4I6hprjKw6Cs9CUZ4pBdOcE18","first":"房租账单通知","remark":"交租金额以实际为准,仅供参考","keyword1":"920公寓,N101,920到账问题","keyword2":"每月22号","keyword3":"1244.00人民币","house_user_id":181,"house_code_id":1869}]
*/
public function weishoupushs($data)
{
$appid = 'wxfbef***';//公众号的appid
$secret = '56819a68**';
// 获取token
$token=$this->getAccToken($appid,$secret);
//$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=".$token;
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
$data = [
"touser"=>$data['openid'],
"appid"=>"wxf***",//公众号
//"template_id"=>"nVfy1Tz1EXQ3T2YcGkwgicAjrD0M3N1Ca-CHIJCwjQY",//模板id
"template_id"=>"pJKbtGrzmQB_wsLOUc02l8Q0M2mlFDt9k9ukcld62N4",//模板id
"url"=>"",
"miniprogram"=>[
"appid"=>"wx18aa***",//小程序
//"pagepath"=>"/pagesIndex/house/billingDetails?type=3&house_user_id=" . $house_user_id . "&house_code_id=" . $house_code_id//pagesIndex/house/billingDetails 未收账单路径 index?foo=bar
"pagepath"=>"/pagesIndex/house/billingDetails?type=3&id=" . $data['house_user_id'] . "&ids=" . $data['house_code_id']//pagesIndex/house/billingDetails 未收账单路径 index?foo=bar
],
"data"=>[
'first' => array(
'value'=>$data['first'],
'color'=>''
),
'keyword1'=>array(
'value'=>$data['keyword1'],
'color'=>''
),
'keyword2'=>array(
'value'=>$data['keyword2'],
'color'=>''
),
'keyword3'=>array(
'value'=>$data['keyword3'],
'color'=>''
),
'remark'=>array(
'value'=>$data['remark'],
'color'=>''
),
],
];
return $this->http_post_json($url,json_encode($data)); //发送请求
}