ProgramSubscribe.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\easywechat\subscribe;
  12. use EasyWeChat\Core\AbstractAPI;
  13. use EasyWeChat\Core\AccessToken;
  14. use EasyWeChat\Core\Exceptions\InvalidArgumentException;
  15. /**
  16. * 小程序订阅消息
  17. * Class ProgramSubscribe
  18. * @package crmeb\utils
  19. * @method $this
  20. * @method $this template(string $template_id) 设置模板id
  21. * @method $this withTemplateId(string $template_id) 设置模板id
  22. * @method $this andTemplateId(string $template_id) 设置模板id
  23. * @method $this andTemplate(string $template_id) 设置模板id
  24. * @method $this andUses(string $template_id) 设置模板id
  25. * @method $this to(string $touser) 设置opendid
  26. * @method $this andReceiver(string $touser) 设置opendid
  27. * @method $this withReceiver(string $touser) 设置opendid
  28. * @method $this with(array $data) 设置发送内容
  29. * @method $this andData(array $data) 设置发送内容
  30. * @method $this withData(array $data) 设置发送内容
  31. * @method $this data(array $data) 设置发送内容
  32. * @method $this withUrl(string $page) 设置跳转路径
  33. */
  34. class ProgramSubscribe extends AbstractAPI
  35. {
  36. /**
  37. * 添加模板接口
  38. */
  39. const API_SET_TEMPLATE_ADD = 'https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate';
  40. /**
  41. * 删除模板消息接口
  42. */
  43. const API_SET_TEMPLATE_DEL = 'https://api.weixin.qq.com/wxaapi/newtmpl/deltemplate';
  44. /**
  45. * 获取模板消息列表
  46. */
  47. const API_GET_TEMPLATE_LIST = 'https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate';
  48. /**
  49. * 获取模板消息分类
  50. */
  51. const API_GET_TEMPLATE_CATE = 'https://api.weixin.qq.com/wxaapi/newtmpl/getcategory';
  52. /**
  53. * 获取模板消息关键字
  54. */
  55. const API_GET_TEMPLATE_KEYWORKS = 'https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatekeywords';
  56. /**
  57. * 获取公共模板
  58. */
  59. const API_GET_PUBLIC_TEMPLATE = 'https://api.weixin.qq.com/wxaapi/newtmpl/getpubtemplatetitles';
  60. /**
  61. * 发送模板消息
  62. */
  63. const API_SUBSCRIBE_SEND = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send';
  64. /**
  65. * Attributes
  66. * @var array
  67. */
  68. protected $message = [
  69. 'touser' => '',
  70. 'template_id' => '',
  71. 'page' => '',
  72. 'data' => [],
  73. ];
  74. /**
  75. * Message backup.
  76. *
  77. * @var array
  78. */
  79. protected $messageBackup;
  80. protected $required = ['template_id', 'touser'];
  81. /**
  82. * ProgramSubscribeService constructor.
  83. * @param AccessToken $accessToken
  84. */
  85. public function __construct(AccessToken $accessToken)
  86. {
  87. parent::__construct($accessToken);
  88. $this->messageBackup = $this->message;
  89. }
  90. /**
  91. * 获取当前拥有的模板列表
  92. * @return \EasyWeChat\Support\Collection|null
  93. * @throws \EasyWeChat\Core\Exceptions\HttpException
  94. */
  95. public function getTemplateList()
  96. {
  97. return $this->parseJSON('get', [self::API_GET_TEMPLATE_LIST]);
  98. }
  99. /**
  100. * 获取公众模板列表
  101. * @param string $ids
  102. * @param int $start
  103. * @param int $limit
  104. * @return \EasyWeChat\Support\Collection|null
  105. * @throws \EasyWeChat\Core\Exceptions\HttpException
  106. */
  107. public function getPublicTemplateList(string $ids, int $start = 0, int $limit = 10)
  108. {
  109. $params = [
  110. 'ids' => $ids,
  111. 'start' => $start,
  112. 'limit' => $limit
  113. ];
  114. return $this->parseJSON('get', [self::API_GET_PUBLIC_TEMPLATE, $params]);
  115. }
  116. /**
  117. * 获取模板分类
  118. * @return \EasyWeChat\Support\Collection|null
  119. * @throws \EasyWeChat\Core\Exceptions\HttpException
  120. */
  121. public function getTemplateCate()
  122. {
  123. return $this->parseJSON('get', [self::API_GET_TEMPLATE_CATE]);
  124. }
  125. /**
  126. * 获取模板标题下的关键词列表
  127. * @param string $tid 模板标题 id,可通过接口获取
  128. * @return \EasyWeChat\Support\Collection|null
  129. * @throws \EasyWeChat\Core\Exceptions\HttpException
  130. */
  131. public function getPublicTemplateKeywords(string $tid)
  132. {
  133. $params = [
  134. 'tid' => $tid
  135. ];
  136. return $this->parseJSON('get', [self::API_GET_TEMPLATE_KEYWORKS, $params]);
  137. }
  138. /**
  139. * 添加订阅模板消息
  140. * @param string $tid 模板标题 id,可通过接口获取,也可登录小程序后台查看获取
  141. * @param array $kidList 模板序列号 关键词顺序可以自由搭配(例如 [3,5,4] 或 [4,5,3]),最多支持5个,最少2个关键词组合
  142. * @param string $sceneDesc 服务场景描述,15个字以内
  143. * @return \EasyWeChat\Support\Collection|null
  144. * @throws \EasyWeChat\Core\Exceptions\HttpException
  145. */
  146. public function addTemplate(string $tid, array $kidList, string $sceneDesc = '')
  147. {
  148. $params = [
  149. 'tid' => $tid,
  150. 'kidList' => $kidList,
  151. 'sceneDesc' => $sceneDesc,
  152. ];
  153. return $this->parseJSON('json', [self::API_SET_TEMPLATE_ADD, $params]);
  154. }
  155. /**
  156. * 删除模板消息
  157. * @param string $priTmplId
  158. * @return \EasyWeChat\Support\Collection|null
  159. * @throws \EasyWeChat\Core\Exceptions\HttpException
  160. */
  161. public function delTemplate(string $priTmplId)
  162. {
  163. $params = [
  164. 'priTmplId' => $priTmplId
  165. ];
  166. return $this->parseJSON('json', [self::API_SET_TEMPLATE_DEL, $params]);
  167. }
  168. /**
  169. * 发送订阅消息
  170. * @param array $data
  171. * @return \EasyWeChat\Support\Collection|null
  172. * @throws InvalidArgumentException
  173. * @throws \EasyWeChat\Core\Exceptions\HttpException
  174. */
  175. public function send(array $data = [])
  176. {
  177. $params = array_merge($this->message, $data);
  178. foreach ($params as $key => $value) {
  179. if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) {
  180. throw new InvalidArgumentException("Attribute '$key' can not be empty!");
  181. }
  182. $params[$key] = empty($value) ? $this->message[$key] : $value;
  183. }
  184. $params['data'] = $this->formatData($params['data']);
  185. $this->message = $this->messageBackup;
  186. return $this->parseJSON('json', [self::API_SUBSCRIBE_SEND, $params]);
  187. }
  188. /**
  189. * 设置订阅消息发送data
  190. * @param array $data
  191. * @return array
  192. */
  193. protected function formatData(array $data)
  194. {
  195. $return = [];
  196. foreach ($data as $key => $item) {
  197. if (is_scalar($item)) {
  198. $value = $item;
  199. } elseif (is_array($item) && !empty($item)) {
  200. if (isset($item['value'])) {
  201. $value = strval($item['value']);
  202. } elseif (count($item) < 2) {
  203. $value = array_shift($item);
  204. } else {
  205. [$value] = $item;
  206. }
  207. } else {
  208. $value = 'error data item.';
  209. }
  210. $return[$key] = ['value' => $value];
  211. }
  212. return $return;
  213. }
  214. /**
  215. * Magic access..
  216. *
  217. * @param $method
  218. * @param $args
  219. * @return $this
  220. */
  221. public function __call($method, $args)
  222. {
  223. $map = [
  224. 'template' => 'template_id',
  225. 'templateId' => 'template_id',
  226. 'uses' => 'template_id',
  227. 'to' => 'touser',
  228. 'receiver' => 'touser',
  229. 'url' => 'page',
  230. 'link' => 'page',
  231. 'data' => 'data',
  232. 'with' => 'data',
  233. ];
  234. if (0 === stripos($method, 'with') && strlen($method) > 4) {
  235. $method = lcfirst(substr($method, 4));
  236. }
  237. if (0 === stripos($method, 'and')) {
  238. $method = lcfirst(substr($method, 3));
  239. }
  240. if (isset($map[$method])) {
  241. $this->message[$map[$method]] = array_shift($args);
  242. }
  243. return $this;
  244. }
  245. }