Template.php 4.0 KB

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