找回密码
 立即注册
搜索
查看: 582|回复: 0

给小程序所绑定的公众号发送模板消息

[复制链接]

581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

发表于 2022-5-20 17:29:10 | 显示全部楼层 |阅读模式
问题需求背景1,小程序模板消息在官网文档中表示已经废弃了,让使用小程序订阅消息
2,小程序订阅消息又分为:一次性订阅消息和长期订阅消息
3,一次性订阅消息发一次模板消息就不能用了,而长期订阅消息需要申请,难度大;


最完美的办法就是:
1、如果小程序订阅消息能发送成功,就优先用小程序消息(优点:不关注公众号也能收到,缺点,一次性的只能接收一次,长期的申请难度大)
2、如果小程序订阅消息发送不成功,就判断用户有无关注公众号,如果关注公众号就发送模板消息,如果没有关注公众号,就走发短信流程


参考连接:https://blog.csdn.net/php_lzr/article/details/84991093
官方文档:https://developers.weixin.qq.com ... rmMessage.send.html

结合微擎实现流程:


1、获取token
  1. $account_api = WeAccount::create();
  2.     $token = $account_api->getAccessToken();
  3.     if (is_error($token)) {
  4.         return $token;
  5.     }
复制代码


2,组合要发送的数据,根据数据结构,方法在后面实现
  1. $dataa = json_encode($this->getTemp());
复制代码


3,发送消息
  1. //微信小程序 公众号 绑定 统一发送消息
  2.     $post_url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token='.$token;
  3.     $response = ihttp_request($post_url, $dataa);

  4.                   
  5.        if (is_error($response)) {
  6.             return error(-1, "访问小程序接口失败, 错误: {$response['message']}");
  7.         }
  8.         $result = @json_decode($response['content'], true);
  9.         if (empty($result)) {
  10.             return error(-1, "接口调用失败, 元数据: {$response['meta']}");
  11.         } elseif (!empty($result['errcode'])) {
  12.             return error(-1, "访问小程序接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},信息详情:{$this->errorCode($result['errcode'])}");
  13.         }
  14.         return true;   
复制代码


4,发送参数格式组合
  1. function getTemp() {
  2.         $data = ['张三', '13888888888', '2016-09-01', "个人详细资料"];
  3.         $sendArr = array();
  4.         $sendArr['touser'] =  'TODO' ;    //用户的openid(小程序的OPENID和公众号的openid都行)
  5.         $sendArr['mp_template_msg']['appid'] = 'TODO';     //绑定的公众号appid
  6.         $sendArr['mp_template_msg']['template_id'] = 'TODO'; //绑定的公众号公众号的模板id
  7.         $sendArr['mp_template_msg']['url'] = 'TODO';//公众号 目前没有跳转链接  写死 跳转到网站的m站
  8.         $sendArr['mp_template_msg']['miniprogram']['appid'] = 'TODO';     //绑定的小程序appid
  9.         $sendArr['mp_template_msg']['miniprogram']['pagepath'] = '';//小程序跳转链接,必须正式发布版后才能填写,开发板填了会报错
  10.         $sendArr['mp_template_msg']['data'] = $this->addkey($data);
  11.         return $sendArr;
  12.     }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|十三博客 ( 鲁ICP备2023000528号 )

GMT+8, 2026-6-1 17:23 , Processed in 0.049054 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表