<?php
/**
* 发送模板消息
*/
function send_notice(){
//获取access_token
/*if ($_COOKIE['access_token']){
$access_token2=$_COOKIE['access_token'];
echo $access_token2+"11";
}else{*/
$json_token=curl_post("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=");
$access_token1=json_decode($json_token,true);
$access_token2=$access_token1['access_token'];
//setcookie('access_token',$access_token2,7200);
//}
//模板消息
$json_template = json_tempalte();
$url="https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token2;
//$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token2;
$url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token2;
$res=curl_post($url,urldecode($json_template));
print_R($res);
exit;
if ($res['errcode']==0){
return '发送成功';
}else{
return '发送失败';
}
}
/**
* 将模板消息json格式化
*/
function json_tempalte(){
//模板消息
$template=array(
'touser'=>', //用户openid
'template_id'=>"", //在公众号下配置的模板id
'url'=>"http://baidu.com", //点击模板消息会跳转的链接
'topcolor'=>"#7B68EE",
'data'=>array(
'thing1'=>array('value'=>urlencode(""),'color'=>"#FF0000"),
'thing3'=>array('value'=>urlencode(""),'color'=>'#FF0000')
)//keyword需要与配置的模板消息对应
);
$json_template=json_encode($template);
return $json_template;
}
/**
* @param $url
* @param array $data
* @return mixed
* curl请求
*/
function curl_post($url , $data=array()){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
send_notice();
?>
<form?bindsubmit="formSubmit"?report-submit='true'?>
??<view?class="btn-area"><button?formType="submit">Submit</button></view>
</form>
formSubmit:?function?(e)?{
????wx.requestSubscribeMessage({
??????tmplIds:?[''],?//?此处可填写多个模板?ID,但低版本微信不兼容只能授权一个
??????
??????success?(res)?{
????????console.log(res);
??????console.log('已授权接收订阅消息')
??????
??????}
??????
??????})
????//?let?templateId?=?'Ite6-mnfTlONu6rd35AJ-SGQYKQgj1WMvjVj0O5h9kE'
????//?wx.requestSubscribeMessage({
????//?????tmplIds:?[templateId],
????//?????success:?(res)=>?{
????//?????????//?如果用户点击允许
????//?????????if(res[templateId]?==?'accept'){
????//?????????????console.log('点击了允许')
????//?????????????wx.cloud.callFunction({
????//?????????????????name:'sendMessage',
????//?????????????????data:{
????//?????????????????????templateId,
????//?????????????????????content:?this.data.textContent,
????//?????????????????????blogId:?this.properties.blogid,
????//?????????????????}
????//?????????????}).then(res?=>?{??????????????????????
????//?????????????????this.setData({
????//?????????????????????textContent:''
????//?????????????????})
????//?????????????})
????//?????????}?else?{
????//?????????????console.log('点击了取消')
????//?????????}
????//?????},
????//?????fail:(res)?=>?{}
????//?})?
??
??}
需要小程序中用户主动触发订阅次数!
|