微信小程序發送訂閱消息(之(zhī)前是(shì)模闆消息)
發表時(shí)間:2021-1-6
發布人(rén):融晨科技
浏覽次數:86
之(zhī)前的(de)模闆消息已經廢棄,現在(zài)改爲(wéi / wèi)訂閱消息,訂閱消息發布前,需要(yào / yāo)用戶确認後才能接收訂閱消息。
小程序端
index.wxml
<button bindtap="send">發送訂閱消息</button>
index.js
const app = getApp()
Page({
data: {
},
send:function(){
wx.requestSubscribeMessage({
tmplIds: ['WZiCliW1zVtHXqX7dGnFNmFvxhW-wd9S_W4WfrwNvss'],
success:(res)=> {
wx.request({
url: 'https://www.xxx.com/send.php',
data: {
openid:'要(yào / yāo)推送的(de)openid',
template_id:'要(yào / yāo)使用的(de)template_id',
},
header: {
'content-type': 'application/json'
},
success (res) {
if(res.data.errcode == '43101'){
console.log("拒絕訂閱消息")
}else if(res.data.errcode == '0'){
console.log("發送訂閱消息")
}else{
console.log("未知錯誤")
}
}
})
}
})
}
)}
後端
<?php
//設置 header
header("Content-type:application/json");
//接收參數
$template_id = $_GET["template_id"];
$openid = $_GET["openid"];
//初始化 CURL
$ch = curl_init();
// 獲取access_token
// include '';
require_once("access_token.php");
//目标服務器地(dì / de)址
curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token);
//設置要(yào / yāo)POST的(de)數據
curl_setopt($ch, CURLOPT_POST, true);
$data = http://www.wxapp-union.com/'{
"touser": $openid,
"template_id": $template_id,
"page": "pages/index/index",// 要(yào / yāo)跳轉的(de)頁面
"miniprogram_state":"developer",
"lang":"zh_CN",
"data": {
"thing4": {
"value": "歡迎使用專插本最前線小程序"
},
"thing5": {
"value": "小程序由公衆号:廣東專插本最前線開發"
}
}
}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// 對認證證書來(lái)源的(de)檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// 從證書中檢查SSL加密算法是(shì)否存在(zài)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//獲取的(de)信息以(yǐ)文件流的(de)形式返回,而(ér)不(bù)是(shì)直接輸出(chū)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//發起請求
$result = curl_exec($ch);
echo $result;
//關閉請求
curl_close($ch);
?>
access_token.php
<?php
// 聲明頁面header
header("Content-type:charset=utf-8");
// APPID、APPSECRET
$appid = "你的(de)小程序APPID";
$appsecret = "你的(de)小程序APPSECRET";
// 獲取access_token和(hé / huò)jsapi_ticket
function getToken(){
$file = file_get_contents("access_token.json",true);//讀取access_token.json裏面的(de)數據
$result = json_decode($file,true);
//判斷access_token是(shì)否在(zài)有效期内,如果在(zài)有效期則獲取緩存的(de)access_token
//如果過期了(le/liǎo)則請求接口生成新的(de)access_token并且緩存access_token.json
if (time() > $result['expires']){
$data = http://www.wxapp-union.com/array();
$data['access_token'] = getNewToken();
$data['expires'] = time()+7000;
$jsonStr = json_encode($data);
$fp = fopen("access_token.json", "w");
fwrite($fp, $jsonStr);
fclose($fp);
return $data['access_token'];
}else{
return $result['access_token'];
}
}
//獲取新的(de)access_token
function getNewToken($appid,$appsecret){
global $appid;
global $appsecret;
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";
$access_token_Arr = file_get_contents($url);
$token_jsonarr = json_decode($access_token_Arr, true);
return $token_jsonarr["access_token"];
}
$access_token = getToken();
?>
邏輯
1、通過button控件出(chū)發send函數
2、send函數調用wx.requestSubscribeMessage
API,微信允許接收訂閱消息
3、 wx.request
向send.php後端請求
4、後端獲取access_token後,調用訂閱消息接口POST一段json數據即可發送訂閱消息
官方文檔
1、https://developers.weixin.qq.com/miniprogram/dev/api/open-api/subscribe-message/wx.requestSubscribeMessage.html
2、https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.addTemplate.html