ProgramTemplate.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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\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 miniprogram.
  78. * @param $data
  79. * @return $this
  80. * @author: 吴汐
  81. * @email: 442384644@qq.com
  82. * @date: 2023/8/22
  83. */
  84. public function setMiniprogram($data)
  85. {
  86. $this->message['miniprogram'] = $data;
  87. return $this;
  88. }
  89. /**
  90. * Set industry.
  91. *
  92. * @param int $industryOne
  93. * @param int $industryTwo
  94. *
  95. * @return \EasyWeChat\Support\Collection
  96. */
  97. public function setIndustry($industryOne, $industryTwo)
  98. {
  99. $params = [
  100. 'industry_id1' => $industryOne,
  101. 'industry_id2' => $industryTwo,
  102. ];
  103. return $this->parseJSON('json', [self::API_SET_INDUSTRY, $params]);
  104. }
  105. /**
  106. * Get industry.
  107. *
  108. * @return \EasyWeChat\Support\Collection
  109. */
  110. public function getIndustry()
  111. {
  112. return $this->parseJSON('json', [self::API_GET_INDUSTRY]);
  113. }
  114. /**
  115. * Add a template and get template ID.
  116. * @param $shortId
  117. * @param $content
  118. * @return \EasyWeChat\Support\Collection|null
  119. * @throws \EasyWeChat\Core\Exceptions\HttpException
  120. * @author: 吴汐
  121. * @email: 442384644@qq.com
  122. * @date: 2023/8/16
  123. */
  124. public function addTemplate($shortId, $content)
  125. {
  126. $params = ['template_id_short' => $shortId, 'keyword_name_list' => $content];
  127. return $this->parseJSON('json', [self::API_ADD_TEMPLATE, $params]);
  128. }
  129. /**
  130. * Get private templates.
  131. *
  132. * @return \EasyWeChat\Support\Collection
  133. */
  134. public function getPrivateTemplates()
  135. {
  136. return $this->parseJSON('json', [self::API_GET_ALL_PRIVATE_TEMPLATE]);
  137. }
  138. /**
  139. * Delete private template.
  140. *
  141. * @param string $templateId
  142. *
  143. * @return \EasyWeChat\Support\Collection
  144. */
  145. public function deletePrivateTemplate($templateId)
  146. {
  147. $params = ['template_id' => $templateId];
  148. return $this->parseJSON('json', [self::API_DEL_PRIVATE_TEMPLATE, $params]);
  149. }
  150. /**
  151. * Send a notice message.
  152. *
  153. * @param $data
  154. *
  155. * @return \EasyWeChat\Support\Collection
  156. *
  157. * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
  158. */
  159. public function send($data = [])
  160. {
  161. $params = array_merge($this->message, $data);
  162. foreach ($params as $key => $value) {
  163. if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) {
  164. throw new InvalidArgumentException("Attribute '$key' can not be empty!");
  165. }
  166. $params[$key] = empty($value) ? $this->message[$key] : $value;
  167. }
  168. $params['data'] = $this->formatData($params['data']);
  169. $this->message = $this->messageBackup;
  170. return $this->parseJSON('json', [static::API_SEND_NOTICE, $params]);
  171. }
  172. /**
  173. * Magic access..
  174. *
  175. * @param string $method
  176. * @param array $args
  177. *
  178. * @return Notice
  179. */
  180. public function __call($method, $args)
  181. {
  182. $map = [
  183. 'template' => 'template_id',
  184. 'templateId' => 'template_id',
  185. 'uses' => 'template_id',
  186. 'to' => 'touser',
  187. 'receiver' => 'touser',
  188. 'url' => 'url',
  189. 'link' => 'url',
  190. 'data' => 'data',
  191. 'with' => 'data',
  192. 'formId' => 'form_id',
  193. 'prepayId' => 'form_id',
  194. ];
  195. if (0 === stripos($method, 'with') && strlen($method) > 4) {
  196. $method = lcfirst(substr($method, 4));
  197. }
  198. if (0 === stripos($method, 'and')) {
  199. $method = lcfirst(substr($method, 3));
  200. }
  201. if (isset($map[$method])) {
  202. $this->message[$map[$method]] = array_shift($args);
  203. }
  204. return $this;
  205. }
  206. /**
  207. * Format template data.
  208. *
  209. * @param array $data
  210. *
  211. * @return array
  212. */
  213. protected function formatData($data)
  214. {
  215. $return = [];
  216. foreach ($data as $key => $item) {
  217. if (is_scalar($item)) {
  218. $value = $item;
  219. $color = $this->defaultColor;
  220. } elseif (is_array($item) && !empty($item)) {
  221. if (isset($item['value'])) {
  222. $value = strval($item['value']);
  223. $color = empty($item['color']) ? $this->defaultColor : strval($item['color']);
  224. } elseif (count($item) < 2) {
  225. $value = array_shift($item);
  226. $color = $this->defaultColor;
  227. } else {
  228. list($value, $color) = $item;
  229. }
  230. } else {
  231. $value = 'error data item.';
  232. $color = $this->defaultColor;
  233. }
  234. $return[$key] = [
  235. 'value' => $value,
  236. 'color' => $color,
  237. ];
  238. }
  239. return $return;
  240. }
  241. }