Receive.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WeChat;
  16. use WeChat\Contracts\BasicPushEvent;
  17. /**
  18. * 公众号推送管理
  19. * Class Receive
  20. * @package WeChat
  21. */
  22. class Receive extends BasicPushEvent
  23. {
  24. /**
  25. * 转发多客服消息
  26. * @param string $account
  27. * @return $this
  28. */
  29. public function transferCustomerService($account = '')
  30. {
  31. $this->message = [
  32. 'CreateTime' => time(),
  33. 'ToUserName' => $this->getOpenid(),
  34. 'FromUserName' => $this->getToOpenid(),
  35. 'MsgType' => 'transfer_customer_service',
  36. ];
  37. empty($account) || $this->message['TransInfo'] = ['KfAccount' => $account];
  38. return $this;
  39. }
  40. /**
  41. * 设置文本消息
  42. * @param string $content 文本内容
  43. * @return $this
  44. */
  45. public function text($content = '')
  46. {
  47. $this->message = [
  48. 'MsgType' => 'text',
  49. 'CreateTime' => time(),
  50. 'Content' => $content,
  51. 'ToUserName' => $this->getOpenid(),
  52. 'FromUserName' => $this->getToOpenid(),
  53. ];
  54. return $this;
  55. }
  56. /**
  57. * 设置回复图文
  58. * @param array $newsData
  59. * @return $this
  60. */
  61. public function news($newsData = [])
  62. {
  63. $this->message = [
  64. 'CreateTime' => time(),
  65. 'MsgType' => 'news',
  66. 'Articles' => $newsData,
  67. 'ToUserName' => $this->getOpenid(),
  68. 'FromUserName' => $this->getToOpenid(),
  69. 'ArticleCount' => count($newsData),
  70. ];
  71. return $this;
  72. }
  73. /**
  74. * 设置图片消息
  75. * @param string $mediaId 图片媒体ID
  76. * @return $this
  77. */
  78. public function image($mediaId = '')
  79. {
  80. $this->message = [
  81. 'MsgType' => 'image',
  82. 'CreateTime' => time(),
  83. 'ToUserName' => $this->getOpenid(),
  84. 'FromUserName' => $this->getToOpenid(),
  85. 'Image' => ['MediaId' => $mediaId],
  86. ];
  87. return $this;
  88. }
  89. /**
  90. * 设置语音回复消息
  91. * @param string $mediaid 语音媒体ID
  92. * @return $this
  93. */
  94. public function voice($mediaid = '')
  95. {
  96. $this->message = [
  97. 'CreateTime' => time(),
  98. 'MsgType' => 'voice',
  99. 'ToUserName' => $this->getOpenid(),
  100. 'FromUserName' => $this->getToOpenid(),
  101. 'Voice' => ['MediaId' => $mediaid],
  102. ];
  103. return $this;
  104. }
  105. /**
  106. * 设置视频回复消息
  107. * @param string $mediaid 视频媒体ID
  108. * @param string $title 视频标题
  109. * @param string $description 视频描述
  110. * @return $this
  111. */
  112. public function video($mediaid = '', $title = '', $description = '')
  113. {
  114. $this->message = [
  115. 'CreateTime' => time(),
  116. 'MsgType' => 'video',
  117. 'ToUserName' => $this->getOpenid(),
  118. 'FromUserName' => $this->getToOpenid(),
  119. 'Video' => [
  120. 'Title' => $title,
  121. 'MediaId' => $mediaid,
  122. 'Description' => $description,
  123. ],
  124. ];
  125. return $this;
  126. }
  127. /**
  128. * 设置音乐回复消息
  129. * @param string $title 音乐标题
  130. * @param string $desc 音乐描述
  131. * @param string $musicurl 音乐地址
  132. * @param string $hgmusicurl 高清音乐地址
  133. * @param string $thumbmediaid 音乐图片缩略图的媒体id(可选)
  134. * @return $this
  135. */
  136. public function music($title, $desc, $musicurl, $hgmusicurl = '', $thumbmediaid = '')
  137. {
  138. $this->message = [
  139. 'CreateTime' => time(),
  140. 'MsgType' => 'music',
  141. 'ToUserName' => $this->getOpenid(),
  142. 'FromUserName' => $this->getToOpenid(),
  143. 'Music' => [
  144. 'Title' => $title,
  145. 'Description' => $desc,
  146. 'MusicUrl' => $musicurl,
  147. 'HQMusicUrl' => $hgmusicurl,
  148. ],
  149. ];
  150. if ($thumbmediaid) {
  151. $this->message['Music']['ThumbMediaId'] = $thumbmediaid;
  152. }
  153. return $this;
  154. }
  155. }