ThinkPhP5整合微信小程序訂閱消息實(shí)用代碼
更新時(shí)間:2023年08月26日 10:09:36 投稿:yin
記錄一下開發(fā)小程序消息推送的實(shí)例,配合后端tp推送,要使用微信訂閱通知功能,需要用戶首先在小程序點(diǎn)擊訂閱后,后臺(tái)方可推送相關(guān)訂閱通知模板,否則無法直接推送
前端DEMO
wxml
<view clas="index" style="background: white"> <button bindtap="clickss">觸發(fā)一下</button> </view>
wxjs
clickss:function (){
const templateId = 'RZM7nZoN5P2sA1m6aeiPMmZ-90U1_8PFmjrv_Nf5bhk'; // 訂閱消息模版id
wx.requestSubscribeMessage({
tmplIds: [templateId],
success(res) {
if (res[templateId] == 'accept') {
//用戶同意了訂閱,允許訂閱消息
wx.showToast({
title: '訂閱成功'
})
} else {
//用戶拒絕了訂閱,禁用訂閱消息
wx.showToast({
title: '訂閱失敗'
})
}
},
fail(res) {
console.log('ooooooooooooooo', res)
},
complete(res) {
console.log(res)
}
})
},后端推送代碼
namespace app\api\controller;
use app\common\controller\Api;
use fast\Http;
use think\Cache;
class SubtoMsg extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* @return mixed
* 獲取access_token
* 時(shí)效3600秒
*/
public function getaccess_tonken()
{
if (Cache::has('token')) {
return Cache::get('token');
} else {
$appid = '';
$appsecret = '';
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
//api接口
$content = Http::get($url);
if ($content) {
$content = json_decode($content, true);
}
if (!empty($content['access_token'])) {
session('access_token', $content['access_token']);
Cache::set('token', $content['access_token'], 3600);
return $content['access_token'];
}
}
}
/**
* @param $openid 目標(biāo)用戶openid
* @param $send 發(fā)送數(shù)據(jù)串
*/
public function send($send)
{
/* $send格式demo
$openid = 'o9vhc5BxMc1pTNCzD1IWVuy4JUP0';//目標(biāo)用戶的openid
$templateId = 'laOhzjVJ5J9Tz97JOM4FNlMud2bkwV-g5DPuTYsWUyA';
$data = array(
'phrase1' => array('value' => '劉洋'),
'time2' => array('value' => date('Y-m-d H:i:s', time())),
'thing3' => array('value' => "老王很棒的喲", 'color' => '#333333'),
);
$send = array(
"touser" => $openid,
"template_id" => $templateId,
"data" => $data,
);*/
$access_token = self::getaccess_tonken();
$url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $access_token;
$ret = Http::post($url, urldecode(json_encode($send)));
$this->success('', json_decode($ret));
/*
* 發(fā)送成功
* {
"code": 1,
"msg": "",
"time": "1619316449",
"data": {
"errcode": 0,
"errmsg": "ok",
"msgid": 1840866241775747074
}
}*/
/*
* 失敗的情況
* {
"code": 1,
"msg": "",
"time": "1619316019",
"data": {
"errcode": 43101,
"errmsg": "user refuse to accept the msg rid: 6084cd33-3bd5fcdb-4ed3439f"
}
}*/
}
//用于測試
public function send_text()
{
$openid = 'o9vhc5BxMc1pTNCzD1IWVuy4JUP0';//目標(biāo)用戶的openid
// $templateId="";//訂閱消息的模板id
$templateId = 'laOhzjVJ5J9Tz97JOM4FNlMud2bkwV-g5DPuTYsWUyA';
// $templateId = get_addon_config('dymsg')['noticetemplateid'];
$data = array(
'phrase1' => array('value' => '劉洋'),
'time2' => array('value' => date('Y-m-d H:i:s', time())),
'thing3' => array('value' => "老劉很棒的喲", 'color' => '#333333'),
);
$access_token = self::getaccess_tonken();
$url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $access_token;
$send = array(
"touser" => $openid,
"template_id" => $templateId,
"data" => $data,
);
$ret = Http::post($url, urldecode(json_encode($send)));
$this->success('', json_decode($ret));
/*
* 發(fā)送成功
* {
"code": 1,
"msg": "",
"time": "1619316449",
"data": {
"errcode": 0,
"errmsg": "ok",
"msgid": 1840866241775747074
}
}*/
/*
* 失敗的情況
* {
"code": 1,
"msg": "",
"time": "1619316019",
"data": {
"errcode": 43101,
"errmsg": "user refuse to accept the msg rid: 6084cd33-3bd5fcdb-4ed3439f"
}
}*/
}
}到此這篇關(guān)于ThinkPhP5整合微信小程序訂閱消息實(shí)用代碼的文章就介紹到這了,更多相關(guān)ThinkPhP5整合微信小程序訂閱消息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PHP二維數(shù)組矩形轉(zhuǎn)置實(shí)例
下面小編就為大家?guī)硪黄狿HP二維數(shù)組矩形轉(zhuǎn)置實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
PHP驗(yàn)證碼函數(shù)代碼(簡單實(shí)用)
這篇文章主要分享了php中簡單的驗(yàn)證碼函數(shù)實(shí)現(xiàn)代碼,代碼比較短,但效果卻不錯(cuò),喜歡的朋友可以試試2013-09-09
Laravel最佳分割路由文件(routes.php)的方式
本文是一篇關(guān)于Laravel分割路由文件(routes.php)的最佳方式教程文章,內(nèi)容介紹的很詳細(xì),學(xué)習(xí)Laravel的小伙伴可以參考學(xué)習(xí)。2016-08-08
PHP基于進(jìn)程控制函數(shù)實(shí)現(xiàn)多線程
這篇文章主要介紹了PHP基于進(jìn)程控制函數(shù)實(shí)現(xiàn)多線程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
Laravel實(shí)現(xiàn)自定義錯(cuò)誤輸出內(nèi)容的方法
這篇文章主要介紹了Laravel實(shí)現(xiàn)自定義錯(cuò)誤輸出內(nèi)容的方法,結(jié)合實(shí)例形式分析了Laravel自定義錯(cuò)誤輸出信息的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
命令行執(zhí)行php腳本中的$argv和$argc配置方法
這篇文章主要介紹了命令行執(zhí)行php腳本 中$argv和$argc的方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-01-01

