Client.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. namespace EasyWeChat\MiniProgram\UniformMessage;
  11. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  12. use EasyWeChat\OfficialAccount\TemplateMessage\Client as BaseClient;
  13. class Client extends BaseClient
  14. {
  15. const API_SEND = 'cgi-bin/message/wxopen/template/uniform_send';
  16. /**
  17. * {@inheritdoc}.
  18. *
  19. * @var array
  20. */
  21. protected $message = [
  22. 'touser' => '',
  23. ];
  24. /**
  25. * Weapp Attributes.
  26. *
  27. * @var array
  28. */
  29. protected $weappMessage = [
  30. 'template_id' => '',
  31. 'page' => '',
  32. 'form_id' => '',
  33. 'data' => [],
  34. 'emphasis_keyword' => '',
  35. ];
  36. /**
  37. * Official account attributes.
  38. *
  39. * @var array
  40. */
  41. protected $mpMessage = [
  42. 'appid' => '',
  43. 'template_id' => '',
  44. 'url' => '',
  45. 'miniprogram' => [],
  46. 'data' => [],
  47. ];
  48. /**
  49. * Required attributes.
  50. *
  51. * @var array
  52. */
  53. protected $required = ['touser', 'template_id', 'form_id', 'miniprogram', 'appid'];
  54. /**
  55. * @param array $data
  56. *
  57. * @return array
  58. *
  59. * @throws InvalidArgumentException
  60. */
  61. protected function formatMessage(array $data = [])
  62. {
  63. $params = array_merge($this->message, $data);
  64. if (empty($params['touser'])) {
  65. throw new InvalidArgumentException(sprintf('Attribute "touser" can not be empty!'));
  66. }
  67. if (!empty($params['weapp_template_msg'])) {
  68. $params['weapp_template_msg'] = $this->formatWeappMessage($params['weapp_template_msg']);
  69. }
  70. if (!empty($params['mp_template_msg'])) {
  71. $params['mp_template_msg'] = $this->formatMpMessage($params['mp_template_msg']);
  72. }
  73. return $params;
  74. }
  75. /**
  76. * @param array $data
  77. *
  78. * @return array
  79. *
  80. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  81. */
  82. protected function formatWeappMessage(array $data = [])
  83. {
  84. $params = $this->baseFormat($data, $this->weappMessage);
  85. $params['data'] = $this->formatData($params['data'] ?? []);
  86. return $params;
  87. }
  88. /**
  89. * @param array $data
  90. *
  91. * @return array
  92. *
  93. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  94. */
  95. protected function formatMpMessage(array $data = [])
  96. {
  97. $params = $this->baseFormat($data, $this->mpMessage);
  98. if (empty($params['miniprogram']['appid'])) {
  99. $params['miniprogram']['appid'] = $this->app['config']['app_id'];
  100. }
  101. $params['data'] = $this->formatData($params['data'] ?? []);
  102. return $params;
  103. }
  104. /**
  105. * @param array $data
  106. * @param array $default
  107. *
  108. * @return array
  109. *
  110. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  111. */
  112. protected function baseFormat($data = [], $default = [])
  113. {
  114. $params = array_merge($default, $data);
  115. foreach ($params as $key => $value) {
  116. if (in_array($key, $this->required, true) && empty($value) && empty($default[$key])) {
  117. throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key));
  118. }
  119. $params[$key] = empty($value) ? $default[$key] : $value;
  120. }
  121. return $params;
  122. }
  123. }