Notice.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. /**
  11. * Notice.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\Notice;
  20. use EasyWeChat\Core\AbstractAPI;
  21. use EasyWeChat\Core\AccessToken;
  22. use EasyWeChat\Core\Exceptions\InvalidArgumentException;
  23. /**
  24. * Class Notice.
  25. */
  26. class Notice extends AbstractAPI
  27. {
  28. /**
  29. * Default color.
  30. *
  31. * @var string
  32. */
  33. protected $defaultColor = '#173177';
  34. /**
  35. * Attributes.
  36. *
  37. * @var array
  38. */
  39. protected $message = [
  40. 'touser' => '',
  41. 'template_id' => '',
  42. 'url' => '',
  43. 'data' => [],
  44. 'miniprogram' => '',
  45. ];
  46. /**
  47. * Required attributes.
  48. *
  49. * @var array
  50. */
  51. protected $required = ['touser', 'template_id'];
  52. /**
  53. * Message backup.
  54. *
  55. * @var array
  56. */
  57. protected $messageBackup;
  58. const API_SEND_NOTICE = 'https://api.weixin.qq.com/cgi-bin/message/template/send';
  59. const API_SET_INDUSTRY = 'https://api.weixin.qq.com/cgi-bin/template/api_set_industry';
  60. const API_ADD_TEMPLATE = 'https://api.weixin.qq.com/cgi-bin/template/api_add_template';
  61. const API_GET_INDUSTRY = 'https://api.weixin.qq.com/cgi-bin/template/get_industry';
  62. const API_GET_ALL_PRIVATE_TEMPLATE = 'https://api.weixin.qq.com/cgi-bin/template/get_all_private_template';
  63. const API_DEL_PRIVATE_TEMPLATE = 'https://api.weixin.qq.com/cgi-bin/template/del_private_template';
  64. /**
  65. * Notice constructor.
  66. *
  67. * @param \EasyWeChat\Core\AccessToken $accessToken
  68. */
  69. public function __construct(AccessToken $accessToken)
  70. {
  71. parent::__construct($accessToken);
  72. $this->messageBackup = $this->message;
  73. }
  74. /**
  75. * Set default color.
  76. *
  77. * @param string $color example: #0f0f0f
  78. *
  79. * @return $this
  80. */
  81. public function defaultColor($color)
  82. {
  83. $this->defaultColor = $color;
  84. return $this;
  85. }
  86. /**
  87. * Set industry.
  88. *
  89. * @param int $industryOne
  90. * @param int $industryTwo
  91. *
  92. * @return \EasyWeChat\Support\Collection
  93. */
  94. public function setIndustry($industryOne, $industryTwo)
  95. {
  96. $params = [
  97. 'industry_id1' => $industryOne,
  98. 'industry_id2' => $industryTwo,
  99. ];
  100. return $this->parseJSON('json', [self::API_SET_INDUSTRY, $params]);
  101. }
  102. /**
  103. * Get industry.
  104. *
  105. * @return \EasyWeChat\Support\Collection
  106. */
  107. public function getIndustry()
  108. {
  109. return $this->parseJSON('json', [self::API_GET_INDUSTRY]);
  110. }
  111. /**
  112. * Add a template and get template ID.
  113. *
  114. * @param string $shortId
  115. *
  116. * @return \EasyWeChat\Support\Collection
  117. */
  118. public function addTemplate($shortId)
  119. {
  120. $params = ['template_id_short' => $shortId];
  121. return $this->parseJSON('json', [self::API_ADD_TEMPLATE, $params]);
  122. }
  123. /**
  124. * Get private templates.
  125. *
  126. * @return \EasyWeChat\Support\Collection
  127. */
  128. public function getPrivateTemplates()
  129. {
  130. return $this->parseJSON('json', [self::API_GET_ALL_PRIVATE_TEMPLATE]);
  131. }
  132. /**
  133. * Delete private template.
  134. *
  135. * @param string $templateId
  136. *
  137. * @return \EasyWeChat\Support\Collection
  138. */
  139. public function deletePrivateTemplate($templateId)
  140. {
  141. $params = ['template_id' => $templateId];
  142. return $this->parseJSON('json', [self::API_DEL_PRIVATE_TEMPLATE, $params]);
  143. }
  144. /**
  145. * Send a notice message.
  146. *
  147. * @param $data
  148. *
  149. * @return \EasyWeChat\Support\Collection
  150. *
  151. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  152. */
  153. public function send($data = [])
  154. {
  155. $params = array_merge($this->message, $data);
  156. foreach ($params as $key => $value) {
  157. if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) {
  158. throw new InvalidArgumentException("Attribute '$key' can not be empty!");
  159. }
  160. $params[$key] = empty($value) ? $this->message[$key] : $value;
  161. }
  162. $params['data'] = $this->formatData($params['data']);
  163. $this->message = $this->messageBackup;
  164. return $this->parseJSON('json', [static::API_SEND_NOTICE, $params]);
  165. }
  166. /**
  167. * Magic access..
  168. *
  169. * @param string $method
  170. * @param array $args
  171. *
  172. * @return Notice
  173. */
  174. public function __call($method, $args)
  175. {
  176. $map = [
  177. 'template' => 'template_id',
  178. 'templateId' => 'template_id',
  179. 'uses' => 'template_id',
  180. 'to' => 'touser',
  181. 'receiver' => 'touser',
  182. 'url' => 'url',
  183. 'link' => 'url',
  184. 'data' => 'data',
  185. 'with' => 'data',
  186. 'formId' => 'form_id',
  187. 'prepayId' => 'form_id',
  188. ];
  189. if (0 === stripos($method, 'with') && strlen($method) > 4) {
  190. $method = lcfirst(substr($method, 4));
  191. }
  192. if (0 === stripos($method, 'and')) {
  193. $method = lcfirst(substr($method, 3));
  194. }
  195. if (isset($map[$method])) {
  196. $this->message[$map[$method]] = array_shift($args);
  197. }
  198. return $this;
  199. }
  200. /**
  201. * Format template data.
  202. *
  203. * @param array $data
  204. *
  205. * @return array
  206. */
  207. protected function formatData($data)
  208. {
  209. $return = [];
  210. foreach ($data as $key => $item) {
  211. if (is_scalar($item)) {
  212. $value = $item;
  213. $color = $this->defaultColor;
  214. } elseif (is_array($item) && !empty($item)) {
  215. if (isset($item['value'])) {
  216. $value = strval($item['value']);
  217. $color = empty($item['color']) ? $this->defaultColor : strval($item['color']);
  218. } elseif (count($item) < 2) {
  219. $value = array_shift($item);
  220. $color = $this->defaultColor;
  221. } else {
  222. list($value, $color) = $item;
  223. }
  224. } else {
  225. $value = 'error data item.';
  226. $color = $this->defaultColor;
  227. }
  228. $return[$key] = [
  229. 'value' => $value,
  230. 'color' => $color,
  231. ];
  232. }
  233. return $return;
  234. }
  235. }