|
|
问题需求背景1,小程序模板消息在官网文档中表示已经废弃了,让使用小程序订阅消息
2,小程序订阅消息又分为:一次性订阅消息和长期订阅消息
3,一次性订阅消息发一次模板消息就不能用了,而长期订阅消息需要申请,难度大;
最完美的办法就是:
1、如果小程序订阅消息能发送成功,就优先用小程序消息(优点:不关注公众号也能收到,缺点,一次性的只能接收一次,长期的申请难度大)
2、如果小程序订阅消息发送不成功,就判断用户有无关注公众号,如果关注公众号就发送模板消息,如果没有关注公众号,就走发短信流程
参考连接:https://blog.csdn.net/php_lzr/article/details/84991093
官方文档:https://developers.weixin.qq.com ... rmMessage.send.html
结合微擎实现流程:
1、获取token
- $account_api = WeAccount::create();
- $token = $account_api->getAccessToken();
- if (is_error($token)) {
- return $token;
- }
复制代码
2,组合要发送的数据,根据数据结构,方法在后面实现
- $dataa = json_encode($this->getTemp());
复制代码
3,发送消息
- //微信小程序 公众号 绑定 统一发送消息
- $post_url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token='.$token;
- $response = ihttp_request($post_url, $dataa);
-
- if (is_error($response)) {
- return error(-1, "访问小程序接口失败, 错误: {$response['message']}");
- }
- $result = @json_decode($response['content'], true);
- if (empty($result)) {
- return error(-1, "接口调用失败, 元数据: {$response['meta']}");
- } elseif (!empty($result['errcode'])) {
- return error(-1, "访问小程序接口错误, 错误代码: {$result['errcode']}, 错误信息: {$result['errmsg']},信息详情:{$this->errorCode($result['errcode'])}");
- }
- return true;
复制代码
4,发送参数格式组合
- function getTemp() {
- $data = ['张三', '13888888888', '2016-09-01', "个人详细资料"];
- $sendArr = array();
- $sendArr['touser'] = 'TODO' ; //用户的openid(小程序的OPENID和公众号的openid都行)
- $sendArr['mp_template_msg']['appid'] = 'TODO'; //绑定的公众号appid
- $sendArr['mp_template_msg']['template_id'] = 'TODO'; //绑定的公众号公众号的模板id
- $sendArr['mp_template_msg']['url'] = 'TODO';//公众号 目前没有跳转链接 写死 跳转到网站的m站
- $sendArr['mp_template_msg']['miniprogram']['appid'] = 'TODO'; //绑定的小程序appid
- $sendArr['mp_template_msg']['miniprogram']['pagepath'] = '';//小程序跳转链接,必须正式发布版后才能填写,开发板填了会报错
- $sendArr['mp_template_msg']['data'] = $this->addkey($data);
- return $sendArr;
- }
复制代码
|
|