Messages.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\wechat;
  12. use EasyWeChat\Kernel\Messages\Text;
  13. use EasyWeChat\Kernel\Messages\Image;
  14. use EasyWeChat\Kernel\Messages\TextCard;
  15. use EasyWeChat\Kernel\Messages\Transfer;
  16. use EasyWeChat\Kernel\Messages\Video;
  17. use EasyWeChat\Kernel\Messages\Voice;
  18. use EasyWeChat\Kernel\Messages\News;
  19. use EasyWeChat\Kernel\Messages\Article;
  20. use EasyWeChat\Kernel\Messages\Media;
  21. use EasyWeChat\Kernel\Messages\NewsItem;
  22. /**
  23. * 消息回复模板
  24. * Class Messages
  25. * @package crmeb\services\wechat
  26. */
  27. class Messages
  28. {
  29. /**
  30. * 回复文本消息
  31. * @param string $content 文本内容
  32. * @return Text
  33. */
  34. public static function textMessage(string $content = '')
  35. {
  36. $content = str_replace("\\n", "\n", $content); //返回数据处理换行符
  37. $content = str_replace("\\r", "\r", $content); //返回数据处理换行符
  38. return new Text($content);
  39. }
  40. /**
  41. * 回复图片消息
  42. * @param string $mediaId 媒体资源 ID
  43. * @return Image
  44. */
  45. public static function imageMessage(string $mediaId)
  46. {
  47. return new Image($mediaId);
  48. }
  49. /**
  50. * 回复视频消息
  51. * @param string $mediaId 媒体资源 ID
  52. * @param string $title 标题
  53. * @param string $description 描述
  54. * @param null $thumb_media_id 封面资源 ID
  55. * @return Video
  56. */
  57. public static function videoMessage(string $mediaId, $title = '', $description = '...', $thumb_media_id = null)
  58. {
  59. return new Video($mediaId, compact('title', 'description', 'thumb_media_id'));
  60. }
  61. /**
  62. * 回复声音消息
  63. * @param string $mediaId 媒体资源 ID
  64. * @return Voice
  65. */
  66. public static function voiceMessage(string $mediaId)
  67. {
  68. return new Voice($mediaId);
  69. }
  70. /**
  71. * 回复图文消息
  72. * @param $title
  73. * @param string $description
  74. * @param string $url
  75. * @param string $image
  76. * @return array|News
  77. */
  78. public static function newsMessage($title, $description = '...', $url = '', $image = '')
  79. {
  80. if (is_array($title)) {
  81. $items = [
  82. new NewsItem([
  83. 'title' => $title['title'],
  84. 'description' => $title['description'],
  85. 'url' => $title['url'],
  86. 'image' => $title['image']
  87. ])
  88. ];
  89. } else {
  90. $items = [
  91. new NewsItem([
  92. 'title' => $title,
  93. 'description' => $description,
  94. 'url' => $url,
  95. 'image' => $image
  96. ])
  97. ];
  98. }
  99. return new News($items);
  100. }
  101. /**
  102. * 创建新闻消息类型
  103. * @param $title
  104. * @param string $description
  105. * @param string $url
  106. * @param string $image
  107. * @return News
  108. */
  109. public static function newMessage($title, string $description = '...', string $url = '', string $image = '')
  110. {
  111. if (is_array($title)) {
  112. $items = [
  113. new NewsItem([
  114. 'title' => $title['title'],
  115. 'description' => $title['description'],
  116. 'url' => $title['url'],
  117. 'image' => $title['image']
  118. ])
  119. ];
  120. } else {
  121. $items = [
  122. new NewsItem([
  123. 'title' => $title,
  124. 'description' => $description,
  125. 'url' => $url,
  126. 'image' => $image
  127. ])
  128. ];
  129. }
  130. return new News($items);
  131. }
  132. /**
  133. * 回复文章消息
  134. * @param string|array $title 标题
  135. * @param string $thumb_media_id 图文消息的封面图片素材id(必须是永久 media_ID)
  136. * @param string $source_url 图文消息的原文地址,即点击“阅读原文”后的URL
  137. * @param string $content 图文消息的具体内容,支持HTML标签,必须少于2万字符,小于1M,且此处会去除JS
  138. * @param string $author 作者
  139. * @param string $digest 图文消息的摘要,仅有单图文消息才有摘要,多图文此处为空
  140. * @param int $show_cover_pic 是否显示封面,0为false,即不显示,1为true,即显示
  141. * @param int $need_open_comment 是否打开评论,0不打开,1打开
  142. * @param int $only_fans_can_comment 是否粉丝才可评论,0所有人可评论,1粉丝才可评论
  143. * @return Article
  144. */
  145. public static function articleMessage($title, $thumb_media_id, $source_url, $content = '', $author = '', $digest = '', $show_cover_pic = 0, $need_open_comment = 0, $only_fans_can_comment = 1)
  146. {
  147. $data = is_array($title) ? $title : compact('title', 'thumb_media_id', 'source_url', 'content', 'author', 'digest', 'show_cover_pic', 'need_open_comment', 'only_fans_can_comment');
  148. return new Article($data);
  149. }
  150. /**
  151. * 回复素材消息
  152. * @param string $mediaId
  153. * @param null $type mpnews、 mpvideo、voice、image
  154. * @return Media
  155. */
  156. public static function materialMessage(string $mediaId, $type = null)
  157. {
  158. return new Media($mediaId, $type);
  159. }
  160. /**
  161. * 多客服消息转发
  162. * @param string|null $account
  163. * @return Transfer
  164. */
  165. public static function transfer(string $account = null)
  166. {
  167. return new Transfer($account);
  168. }
  169. /**
  170. * 消息模板
  171. * @param string $title
  172. * @param string $description
  173. * @param string $url
  174. * @return TextCard
  175. */
  176. public static function TextCardMessage(string $title, string $description, string $url)
  177. {
  178. return new TextCard([
  179. 'title' => $title,
  180. 'description' => $description,
  181. 'url' => $url
  182. ]);
  183. }
  184. }