Broadcast.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. /**
  11. * Broadcast.php.
  12. *
  13. * @author overtrue <i@overtrue.me>
  14. * @copyright 2015 overtrue <i@overtrue.me>
  15. *
  16. * @see https://github.com/overtrue
  17. * @see http://overtrue.me
  18. */
  19. namespace EasyWeChat\Broadcast;
  20. use EasyWeChat\Core\AbstractAPI;
  21. use EasyWeChat\Core\Exceptions\HttpException;
  22. /**
  23. * Class Broadcast.
  24. */
  25. class Broadcast extends AbstractAPI
  26. {
  27. const API_SEND_BY_GROUP = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall';
  28. const API_SEND_BY_OPENID = 'https://api.weixin.qq.com/cgi-bin/message/mass/send';
  29. const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete';
  30. const API_PREVIEW = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview';
  31. const API_GET = 'https://api.weixin.qq.com/cgi-bin/message/mass/get';
  32. const PREVIEW_BY_OPENID = 'touser';
  33. const PREVIEW_BY_NAME = 'towxname';
  34. const MSG_TYPE_TEXT = 'text'; // 文本
  35. const MSG_TYPE_NEWS = 'news'; // 图文
  36. const MSG_TYPE_VOICE = 'voice'; // 语音
  37. const MSG_TYPE_IMAGE = 'image'; // 图片
  38. const MSG_TYPE_VIDEO = 'video'; // 视频
  39. const MSG_TYPE_CARD = 'card'; // 卡券
  40. /**
  41. * Send a message.
  42. *
  43. * @param string $msgType message type
  44. * @param mixed $message message
  45. * @param mixed $to
  46. *
  47. * @return mixed
  48. */
  49. public function send($msgType, $message, $to = null)
  50. {
  51. $message = (new MessageBuilder())->msgType($msgType)->message($message)->to($to)->build();
  52. $api = is_array($to) ? self::API_SEND_BY_OPENID : self::API_SEND_BY_GROUP;
  53. return $this->post($api, $message);
  54. }
  55. /**
  56. * Send a text message.
  57. *
  58. * @param mixed $message message
  59. * @param mixed $to
  60. *
  61. * @return mixed
  62. */
  63. public function sendText($message, $to = null)
  64. {
  65. return $this->send(self::MSG_TYPE_TEXT, $message, $to);
  66. }
  67. /**
  68. * Send a news message.
  69. *
  70. * @param mixed $message message
  71. * @param mixed $to
  72. *
  73. * @return mixed
  74. */
  75. public function sendNews($message, $to = null)
  76. {
  77. return $this->send(self::MSG_TYPE_NEWS, $message, $to);
  78. }
  79. /**
  80. * Send a voice message.
  81. *
  82. * @param mixed $message message
  83. * @param mixed $to
  84. *
  85. * @return mixed
  86. */
  87. public function sendVoice($message, $to = null)
  88. {
  89. return $this->send(self::MSG_TYPE_VOICE, $message, $to);
  90. }
  91. /**
  92. * Send a image message.
  93. *
  94. * @param mixed $message message
  95. * @param mixed $to
  96. *
  97. * @return mixed
  98. */
  99. public function sendImage($message, $to = null)
  100. {
  101. return $this->send(self::MSG_TYPE_IMAGE, $message, $to);
  102. }
  103. /**
  104. * Send a video message.
  105. *
  106. * @param mixed $message message
  107. * @param mixed $to
  108. *
  109. * @return mixed
  110. */
  111. public function sendVideo($message, $to = null)
  112. {
  113. return $this->send(self::MSG_TYPE_VIDEO, $message, $to);
  114. }
  115. /**
  116. * Send a card message.
  117. *
  118. * @param mixed $message message
  119. * @param mixed $to
  120. *
  121. * @return mixed
  122. */
  123. public function sendCard($message, $to = null)
  124. {
  125. return $this->send(self::MSG_TYPE_CARD, $message, $to);
  126. }
  127. /**
  128. * Preview a message.
  129. *
  130. * @param string $msgType message type
  131. * @param mixed $message message
  132. * @param string $to
  133. * @param string $by
  134. *
  135. * @return mixed
  136. */
  137. public function preview($msgType, $message, $to, $by = self::PREVIEW_BY_OPENID)
  138. {
  139. $message = (new MessageBuilder())->msgType($msgType)->message($message)->to($to)->buildPreview($by);
  140. return $this->post(self::API_PREVIEW, $message);
  141. }
  142. /**
  143. * Preview a text message.
  144. *
  145. * @param mixed $message message
  146. * @param string $to
  147. * @param string $by
  148. *
  149. * @return mixed
  150. */
  151. public function previewText($message, $to, $by = self::PREVIEW_BY_OPENID)
  152. {
  153. return $this->preview(self::MSG_TYPE_TEXT, $message, $to, $by);
  154. }
  155. /**
  156. * Preview a news message.
  157. *
  158. * @param mixed $message message
  159. * @param string $to
  160. * @param string $by
  161. *
  162. * @return mixed
  163. */
  164. public function previewNews($message, $to, $by = self::PREVIEW_BY_OPENID)
  165. {
  166. return $this->preview(self::MSG_TYPE_NEWS, $message, $to, $by);
  167. }
  168. /**
  169. * Preview a voice message.
  170. *
  171. * @param mixed $message message
  172. * @param string $to
  173. * @param string $by
  174. *
  175. * @return mixed
  176. */
  177. public function previewVoice($message, $to, $by = self::PREVIEW_BY_OPENID)
  178. {
  179. return $this->preview(self::MSG_TYPE_VOICE, $message, $to, $by);
  180. }
  181. /**
  182. * Preview a image message.
  183. *
  184. * @param mixed $message message
  185. * @param string $to
  186. * @param string $by
  187. *
  188. * @return mixed
  189. */
  190. public function previewImage($message, $to, $by = self::PREVIEW_BY_OPENID)
  191. {
  192. return $this->preview(self::MSG_TYPE_IMAGE, $message, $to, $by);
  193. }
  194. /**
  195. * Preview a video message.
  196. *
  197. * @param mixed $message message
  198. * @param string $to
  199. * @param string $by
  200. *
  201. * @return mixed
  202. */
  203. public function previewVideo($message, $to, $by = self::PREVIEW_BY_OPENID)
  204. {
  205. return $this->preview(self::MSG_TYPE_VIDEO, $message, $to, $by);
  206. }
  207. /**
  208. * Preview a card message.
  209. *
  210. * @param mixed $message message
  211. * @param string $to
  212. * @param string $by
  213. *
  214. * @return mixed
  215. */
  216. public function previewCard($message, $to, $by = self::PREVIEW_BY_OPENID)
  217. {
  218. return $this->preview(self::MSG_TYPE_CARD, $message, $to, $by);
  219. }
  220. /**
  221. * Preview a message by name.
  222. *
  223. * @param string $msgType message type
  224. * @param mixed $message message
  225. * @param $to
  226. *
  227. * @return mixed
  228. */
  229. public function previewByName($msgType, $message, $to)
  230. {
  231. return $this->preview($msgType, $message, $to, self::PREVIEW_BY_NAME);
  232. }
  233. /**
  234. * Preview a text message by name.
  235. *
  236. * @param mixed $message message
  237. * @param $to
  238. *
  239. * @return mixed
  240. */
  241. public function previewTextByName($message, $to)
  242. {
  243. return $this->preview(self::MSG_TYPE_TEXT, $message, $to, self::PREVIEW_BY_NAME);
  244. }
  245. /**
  246. * Preview a news message by name.
  247. *
  248. * @param mixed $message message
  249. * @param $to
  250. *
  251. * @return mixed
  252. */
  253. public function previewNewsByName($message, $to)
  254. {
  255. return $this->preview(self::MSG_TYPE_NEWS, $message, $to, self::PREVIEW_BY_NAME);
  256. }
  257. /**
  258. * Preview a voice message by name.
  259. *
  260. * @param mixed $message message
  261. * @param $to
  262. *
  263. * @return mixed
  264. */
  265. public function previewVoiceByName($message, $to)
  266. {
  267. return $this->preview(self::MSG_TYPE_VOICE, $message, $to, self::PREVIEW_BY_NAME);
  268. }
  269. /**
  270. * Preview a image message by name.
  271. *
  272. * @param mixed $message message
  273. * @param $to
  274. *
  275. * @return mixed
  276. */
  277. public function previewImageByName($message, $to)
  278. {
  279. return $this->preview(self::MSG_TYPE_IMAGE, $message, $to, self::PREVIEW_BY_NAME);
  280. }
  281. /**
  282. * Preview a video message by name.
  283. *
  284. * @param mixed $message message
  285. * @param $to
  286. *
  287. * @return mixed
  288. */
  289. public function previewVideoByName($message, $to)
  290. {
  291. return $this->preview(self::MSG_TYPE_VIDEO, $message, $to, self::PREVIEW_BY_NAME);
  292. }
  293. /**
  294. * Preview a card message by name.
  295. *
  296. * @param mixed $message message
  297. * @param $to
  298. *
  299. * @return mixed
  300. */
  301. public function previewCardByName($message, $to)
  302. {
  303. return $this->preview(self::MSG_TYPE_CARD, $message, $to, self::PREVIEW_BY_NAME);
  304. }
  305. /**
  306. * Delete a broadcast.
  307. *
  308. * @param string $msgId
  309. *
  310. * @return \EasyWeChat\Support\Collection
  311. */
  312. public function delete($msgId)
  313. {
  314. $options = [
  315. 'msg_id' => $msgId,
  316. ];
  317. return $this->post(self::API_DELETE, $options);
  318. }
  319. /**
  320. * Get a broadcast status.
  321. *
  322. * @param string $msgId
  323. *
  324. * @return \EasyWeChat\Support\Collection
  325. */
  326. public function status($msgId)
  327. {
  328. $options = [
  329. 'msg_id' => $msgId,
  330. ];
  331. return $this->post(self::API_GET, $options);
  332. }
  333. /**
  334. * post request.
  335. *
  336. * @param string $url
  337. * @param array|string $options
  338. *
  339. * @return \EasyWeChat\Support\Collection
  340. *
  341. * @throws HttpException
  342. */
  343. private function post($url, $options)
  344. {
  345. return $this->parseJSON('json', [$url, $options]);
  346. }
  347. }