之前的模板消息已经废弃,现在改为订阅消息,订阅消息发布前,需要用户确认后才能接收订阅消息。

小程序端

index.wxml

  1. <button bindtap="send">发送订阅消息</button>

index.js

  1. const app = getApp()
  2. Page({
  3. data: {
  4. },
  5. send:function(){
  6. wx.requestSubscribeMessage({
  7. tmplIds: ['WZiCliW1zVtHXqX7dGnFNmFvxhW-wd9S_W4WfrwNvss'],
  8. success:(res)=> {
  9. wx.request({
  10. url: 'https://www.xxx.com/send.php',
  11. data: {
  12. openid:'要推送的openid',
  13. template_id:'要使用的template_id',
  14. },
  15. header: {
  16. 'content-type': 'application/json'
  17. },
  18. success (res) {
  19. if(res.data.errcode == '43101'){
  20. console.log("拒绝订阅消息")
  21. }else if(res.data.errcode == '0'){
  22. console.log("发送订阅消息")
  23. }else{
  24. console.log("未知错误")
  25. }
  26. }
  27. })
  28. }
  29. })
  30. }
  31. )}

后端

send.php

  1. <?php
  2. //设置 header
  3. header("Content-type:application/json");
  4. //接收参数
  5. $template_id = $_GET["template_id"];
  6. $openid = $_GET["openid"];
  7. //初始化 CURL
  8. $ch = curl_init();
  9. // 获取access_token
  10. // include '';
  11. require_once("access_token.php");
  12. //目标服务器地址
  13. curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token);
  14. //设置要POST的数据
  15. curl_setopt($ch, CURLOPT_POST, true);
  16. $data = '{
  17. "touser": $openid,
  18. "template_id": $template_id,
  19. "page": "pages/index/index",// 要跳转的页面
  20. "miniprogram_state":"developer",
  21. "lang":"zh_CN",
  22. "data": {
  23. "thing4": {
  24. "value": "欢迎使用专插本最前线小程序"
  25. },
  26. "thing5": {
  27. "value": "小程序由公众号:广东专插本最前线开发"
  28. }
  29. }
  30. }';
  31. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  32. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  33. // 对认证证书来源的检查
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  35. // 从证书中检查SSL加密算法是否存在
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  37. //获取的信息以文件流的形式返回,而不是直接输出
  38. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  39. //发起请求
  40. $result = curl_exec($ch);
  41. echo $result;
  42. //关闭请求
  43. curl_close($ch);
  44. ?>

access_token.php

  1. <?php
  2. // 声明页面header
  3. header("Content-type:charset=utf-8");
  4. // APPID、APPSECRET
  5. $appid = "你的小程序APPID";
  6. $appsecret = "你的小程序APPSECRET";
  7. // 获取access_token和jsapi_ticket
  8. function getToken(){
  9. $file = file_get_contents("access_token.json",true);//读取access_token.json里面的数据
  10. $result = json_decode($file,true);
  11. //判断access_token是否在有效期内,如果在有效期则获取缓存的access_token
  12. //如果过期了则请求接口生成新的access_token并且缓存access_token.json
  13. if (time() > $result['expires']){
  14. $data = array();
  15. $data['access_token'] = getNewToken();
  16. $data['expires'] = time()+7000;
  17. $jsonStr = json_encode($data);
  18. $fp = fopen("access_token.json", "w");
  19. fwrite($fp, $jsonStr);
  20. fclose($fp);
  21. return $data['access_token'];
  22. }else{
  23. return $result['access_token'];
  24. }
  25. }
  26. //获取新的access_token
  27. function getNewToken($appid,$appsecret){
  28. global $appid;
  29. global $appsecret;
  30. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";
  31. $access_token_Arr = file_get_contents($url);
  32. $token_jsonarr = json_decode($access_token_Arr, true);
  33. return $token_jsonarr["access_token"];
  34. }
  35. $access_token = getToken();
  36. ?>

逻辑

1、通过button控件出发send函数

2、send函数调用wx.requestSubscribeMessageAPI,微信允许接收订阅消息

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

Author:TANKING

Date:2020-08-24

Web:http://www.likeyun.cn/

WeChat:face6009

微信小程序发送订阅消息(之前是模板消息)的更多相关文章

  1. 微信小程序开发之formId使用(模板消息)

    基于微信小程序的模板消息:基于微信的通知渠道,我们为开发者提供了可以高效触达用户的模板消息能力,以便实现服务的闭环并提供更佳的体验.模板推送位置:服务通知模板下发条件:用户本人在微信体系内与页面有交互 ...

  2. 微信-小程序-开发文档-服务端-模板消息:templateMessage.send

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.send 1.返回顶部 1. templateMessage.send 本接口应在服务器端调用,详细说明参见服 ...

  3. 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateList

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateList 1.返回顶部 1. templateMessage.getTemplateLi ...

  4. 微信-小程序-开发文档-服务端-模板消息:templateMessage.addTemplate

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.addTemplate 1.返回顶部 1. templateMessage.addTemplate 本接口应在 ...

  5. 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryList

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryList 1.返回顶部 1. templateMessage.getTem ...

  6. 微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryById

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.getTemplateLibraryById 1.返回顶部 1. templateMessage.getTem ...

  7. 微信-小程序-开发文档-服务端-模板消息:templateMessage.deleteTemplate

    ylbtech-微信-小程序-开发文档-服务端-模板消息:templateMessage.deleteTemplate 1.返回顶部 1. templateMessage.deleteTemplate ...

  8. .netcore 3.1 C# 微信小程序发送订阅消息

    一.appsettings.json定义小程序配置信息 "WX": { "AppId": "wx88822730803edd44", &qu ...

  9. 微信小程序发送模板消息

    微信小程序发送模板消息 标签(空格分隔): php 看小程序文档 [模板消息文档总览]:https://developers.weixin.qq.com/miniprogram/dev/framewo ...

随机推荐

  1. MacOS下如何设置hosts?

    hosts文件是什么? hosts文件是一个系统文件,其作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”.当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hosts文件中 ...

  2. Python循环控制语句

    Python循环控制语句:主要有三种,break.continue 和 pass 语句. break   语句 :在语句块执行过程中,终止循环.并跳出整个循环. continue 语句  :在语句执行 ...

  3. UDP 绑定信息

    """ 建立->绑定本地ip地址和端口号->接收数据->转码输出->关闭客户端 """ from socket im ...

  4. PHP date_timezone_set() 函数

    ------------恢复内容开始------------ 实例 设置 DateTime 对象的时区: <?php$date=date_create("2013-05-25" ...

  5. PHP import_request_variables() 函数

    import_request_variables() 函数将 GET/POST/Cookie 变量导入到全局作用域中.该函数在最新版本的 PHP 中已经不支持.高佣联盟 www.cgewang.com ...

  6. PHP imageaffinematrixget - 获取矩阵

    imageaffinematrixget — 获取矩阵.高佣联盟 www.cgewang.com 语法 array imageaffinematrixget ( int $type [, mixed ...

  7. PHP xpath() 函数

    定义和用法 xpath() 函数运行对 XML 文档的 XPath 查询.高佣联盟 www.cgewang.com 如果成功,该函数返回 SimpleXMLElements 对象的一个数组.如果失败, ...

  8. Python编程第四版中文 上下册完整版pdf|网盘下载附提取码

    点击此处下载 提取码:drjh 作者简介 Mark Lutz是Python培训的世界的领先者,他是最初和最畅销的Python著作的作者,从1992年起就是Python社区的先锋人物.Mark有25年的 ...

  9. C# Hello Word

    不管学习什么语言,第一个例子绝对是一个经典的HelloWorld程序那么接下来我们使用 vs studio 2019 来创建一个 HelloWorld 程序 启动vs2019选择 文件-新建-项目-新 ...

  10. 034_go语言中的工作池

    代码演示 package main import "fmt" import "time" func worker(id int, jobs <-chan ...