WxSubMsg.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\common\library\wechat;
  3. /**
  4. * 小程序订阅消息
  5. * Class WxSubMsg
  6. * @package app\common\library\wechat
  7. */
  8. class WxSubMsg extends WxBase
  9. {
  10. /**
  11. * 发送订阅消息
  12. * @param $param
  13. * @return bool
  14. * @throws \app\common\exception\BaseException
  15. */
  16. public function sendTemplateMessage($param)
  17. {
  18. // 微信接口url
  19. $accessToken = $this->getAccessToken();
  20. $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={$accessToken}";
  21. // 构建请求
  22. $params = [
  23. 'touser' => $param['touser'],
  24. 'template_id' => $param['template_id'],
  25. 'page' => $param['page'],
  26. 'data' => $param['data'],
  27. ];
  28. $result = $this->post($url, $this->jsonEncode($params));
  29. // 记录日志
  30. $describe = '发送订阅消息';
  31. $this->doLogs(compact('describe', 'url', 'params', 'result'));
  32. // 返回结果
  33. $response = $this->jsonDecode($result);
  34. if (!isset($response['errcode'])) {
  35. $this->error = 'not found errcode';
  36. return false;
  37. }
  38. if ($response['errcode'] != 0) {
  39. $this->error = $response['errmsg'];
  40. return false;
  41. }
  42. return true;
  43. }
  44. /**
  45. * 获取当前帐号下的模板列表
  46. * @throws \app\common\exception\BaseException
  47. */
  48. public function getTemplateList()
  49. {
  50. // 微信接口url
  51. $accessToken = $this->getAccessToken();
  52. $url = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate?access_token={$accessToken}";
  53. // 执行post请求
  54. $result = $this->get($url);
  55. // 记录日志
  56. $this->doLogs(['describe' => '获取当前帐号下的订阅消息模板列表', 'url' => $url, 'result' => $result]);
  57. // 处理返回结果
  58. $response = $this->jsonDecode($result);
  59. if (!isset($response['errcode'])) {
  60. $this->error = 'not found errcode';
  61. return false;
  62. }
  63. if ($response['errcode'] != 0) {
  64. $this->error = $response['errmsg'];
  65. return false;
  66. }
  67. return $response;
  68. }
  69. /**
  70. * 添加订阅消息模板
  71. * [addTemplates 组合模板并添加至帐号下的个人模板库](订阅消息)
  72. * @param int $tid 模板标题id
  73. * @param array $kidList 模板关键词列表
  74. * @param string $sceneDesc 服务场景描述
  75. * @return bool
  76. * @throws \app\common\exception\BaseException
  77. */
  78. public function addTemplate($tid, $kidList, $sceneDesc)
  79. {
  80. // 微信接口url
  81. $accessToken = $this->getAccessToken();
  82. $url = "https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token={$accessToken}";
  83. // 构建请求
  84. $params = [
  85. 'tid' => $tid,
  86. 'kidList' => $kidList,
  87. 'sceneDesc' => $sceneDesc,
  88. ];
  89. // 执行post请求
  90. $result = $this->post2($url, $params);
  91. // 记录日志
  92. $this->doLogs(['describe' => '添加订阅消息模板', 'url' => $url, 'params' => $params, 'result' => $result]);
  93. // 处理返回结果
  94. $response = $this->jsonDecode($result);
  95. if (!isset($response['errcode'])) {
  96. $this->error = 'not found errcode';
  97. return false;
  98. }
  99. if ($response['errcode'] != 0) {
  100. $this->error = $response['errmsg'];
  101. return false;
  102. }
  103. return $response;
  104. }
  105. }