ProgramTemplate.php 7.0 KB

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