Template.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeMini;
  16. use WeChat\Contracts\BasicWeChat;
  17. /**
  18. * 公众号小程序模板消息支持
  19. * Class Mini
  20. * @package WeChat
  21. */
  22. class Template extends BasicWeChat
  23. {
  24. /**
  25. * 获取小程序模板库标题列表
  26. * @return array
  27. * @throws \WeChat\Exceptions\InvalidResponseException
  28. * @throws \WeChat\Exceptions\LocalCacheException
  29. */
  30. public function getTemplateLibraryList()
  31. {
  32. $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/library/list?access_token=ACCESS_TOKEN';
  33. return $this->callPostApi($url, ['offset' => '0', 'count' => '20'], true);
  34. }
  35. /**
  36. * 获取模板库某个模板标题下关键词库
  37. * @param string $template_id 模板标题id,可通过接口获取,也可登录小程序后台查看获取
  38. * @return array
  39. * @throws \WeChat\Exceptions\InvalidResponseException
  40. * @throws \WeChat\Exceptions\LocalCacheException
  41. */
  42. public function getTemplateLibrary($template_id)
  43. {
  44. $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/library/get?access_token=ACCESS_TOKEN';
  45. return $this->callPostApi($url, ['id' => $template_id], true);
  46. }
  47. /**
  48. * 组合模板并添加至帐号下的个人模板库
  49. * @param string $template_id 模板标题id,可通过接口获取,也可登录小程序后台查看获取
  50. * @param array $keyword_id_list 开发者自行组合好的模板关键词列表,关键词顺序可以自由搭配(例如[3,5,4]或[4,5,3]),最多支持10个关键词组合
  51. * @return array
  52. * @throws \WeChat\Exceptions\InvalidResponseException
  53. * @throws \WeChat\Exceptions\LocalCacheException
  54. */
  55. public function addTemplate($template_id, array $keyword_id_list)
  56. {
  57. $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/add?access_token=ACCESS_TOKEN';
  58. return $this->callPostApi($url, ['id' => $template_id, 'keyword_id_list' => $keyword_id_list], true);
  59. }
  60. /**
  61. * 获取帐号下已存在的模板列表
  62. * @return array
  63. * @throws \WeChat\Exceptions\InvalidResponseException
  64. * @throws \WeChat\Exceptions\LocalCacheException
  65. */
  66. public function getTemplateList()
  67. {
  68. $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=ACCESS_TOKEN';
  69. return $this->callPostApi($url, ['offset' => '0', 'count' => '20'], true);
  70. }
  71. /**
  72. * 删除模板消息
  73. * @param string $template_id 要删除的模板id
  74. * @return array
  75. * @throws \WeChat\Exceptions\InvalidResponseException
  76. * @throws \WeChat\Exceptions\LocalCacheException
  77. */
  78. public function delTemplate($template_id)
  79. {
  80. $url = 'https://api.weixin.qq.com/cgi-bin/wxopen/template/del?access_token=ACCESS_TOKEN';
  81. return $this->callPostApi($url, ['template_id' => $template_id], true);
  82. }
  83. /**
  84. * 发送模板消息
  85. * @param array $data 发送的消息对象数组
  86. * @return array
  87. * @throws \WeChat\Exceptions\InvalidResponseException
  88. * @throws \WeChat\Exceptions\LocalCacheException
  89. */
  90. public function send(array $data)
  91. {
  92. $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN';
  93. return $this->callPostApi($url, $data, true);
  94. }
  95. }