Material.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\framework\EasyWechat\OfficialAccount;
  3. //use EasyWeChat\Work\ExternalContact\Client as BaseClient;
  4. use EasyWeChat\Kernel\Http\StreamResponse;
  5. use EasyWeChat\Kernel\Messages\Article;
  6. use EasyWeChat\OfficialAccount\Material\Client as BaseClient;
  7. class Material extends BaseClient
  8. {
  9. /**
  10. * @param $article_id
  11. * @param $index
  12. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  13. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  14. * @throws \GuzzleHttp\Exception\GuzzleException
  15. * 删除已发布的文章
  16. */
  17. public function unsetArticle($article_id, $index = null)
  18. {
  19. $params = [
  20. 'article_id' => $article_id,
  21. ];
  22. if (!is_null($index)) {
  23. $params['index'] = intval($index);
  24. }
  25. return $this->httpPostJson('cgi-bin/freepublish/delete', $params);
  26. }
  27. /**
  28. * @param $mediaId
  29. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  30. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  31. * @throws \GuzzleHttp\Exception\GuzzleException
  32. * 发布文章
  33. */
  34. public function pushArticle($mediaId)
  35. {
  36. return $this->httpPostJson('cgi-bin/freepublish/submit', ['media_id' => $mediaId]);
  37. }
  38. /**
  39. * @param $offset
  40. * @param $count
  41. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  42. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  43. * @throws \GuzzleHttp\Exception\GuzzleException
  44. * 获取已发布文章的列表
  45. */
  46. public function getArticleList($offset = 0, $count = 20, $no_content = 0)
  47. {
  48. return $this->httpPostJson('cgi-bin/freepublish/batchget', [
  49. 'offset' => $offset,
  50. 'count' => $count,
  51. 'no_content' => $no_content,
  52. ]);
  53. }
  54. /**
  55. * @param $article_id
  56. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  57. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  58. * @throws \GuzzleHttp\Exception\GuzzleException
  59. * 获取单篇已发布文章
  60. */
  61. public function getArticleOne($article_id)
  62. {
  63. return $this->httpPostJson('cgi-bin/freepublish/getarticle', ['article_id' => $article_id]);
  64. }
  65. /**
  66. * @param $publish_id
  67. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  68. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  69. * @throws \GuzzleHttp\Exception\GuzzleException
  70. * 查询文章发布结果
  71. */
  72. public function checkArticleExamine($publish_id)
  73. {
  74. return $this->httpPostJson('cgi-bin/freepublish/get', ['publish_id' => $publish_id]);
  75. }
  76. /**
  77. * @param string $mediaId
  78. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  79. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  80. * @throws \GuzzleHttp\Exception\GuzzleException
  81. * 微信草稿箱-删除草稿
  82. */
  83. public function draftDelete(string $mediaId)
  84. {
  85. return $this->httpPostJson('cgi-bin/draft/delete', ['media_id' => $mediaId]);
  86. }
  87. /**
  88. * @param int $offset
  89. * @param int $count
  90. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  91. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  92. * @throws \GuzzleHttp\Exception\GuzzleException
  93. * 微信草稿箱-获取草稿列表
  94. */
  95. public function draftList(int $offset = 0, int $count = 20)
  96. {
  97. $params = [
  98. 'no_content' => 0,
  99. 'offset' => $offset,
  100. 'count' => $count,
  101. ];
  102. return $this->httpPostJson('cgi-bin/draft/batchget', $params);
  103. }
  104. /**
  105. * @param string $mediaId
  106. * @return array|\EasyWeChat\Kernel\Http\Response|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  107. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  108. * @throws \GuzzleHttp\Exception\GuzzleException
  109. * 微信草稿箱-获取单个草稿
  110. */
  111. public function draftGet(string $mediaId)
  112. {
  113. $response = $this->requestRaw('cgi-bin/draft/get', 'POST', ['json' => ['media_id' => $mediaId]]);
  114. if (false !== stripos($response->getHeaderLine('Content-disposition'), 'attachment')) {
  115. return StreamResponse::buildFromPsrResponse($response);
  116. }
  117. return $this->castResponseToType($response, $this->app['config']->get('response_type'));
  118. }
  119. /**
  120. * @param string $mediaId
  121. * @param $article
  122. * @param int $index
  123. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  124. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  125. * @throws \GuzzleHttp\Exception\GuzzleException
  126. * 微信草稿箱-编辑草稿
  127. */
  128. public function updateArticle(string $mediaId, $article, int $index = 0)
  129. {
  130. if ($article instanceof Article) {
  131. $article = $article->transformForJsonRequestWithoutType();
  132. }
  133. $params = [
  134. 'media_id' => $mediaId,
  135. 'index' => $index,
  136. 'articles' => isset($article['title']) ? $article : (isset($article[$index]) ? $article[$index] : []),
  137. ];
  138. return $this->httpPostJson('cgi-bin/draft/update', $params);
  139. }
  140. public function getArticle(string $mediaId)
  141. {
  142. $params = [
  143. 'media_id' => $mediaId,
  144. ];
  145. return $this->httpPostJson('cgi-bin/draft/get', $params);
  146. }
  147. /**
  148. * @param $articles
  149. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  150. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  151. * @throws \GuzzleHttp\Exception\GuzzleException
  152. * 微信草稿箱-创建草稿
  153. */
  154. public function uploadArticle($articles)
  155. {
  156. if ($articles instanceof Article || !empty($articles['title'])) {
  157. $articles = [$articles];
  158. }
  159. $params = ['articles' => array_map(function ($article) {
  160. if ($article instanceof Article) {
  161. return $article->transformForJsonRequestWithoutType();
  162. }
  163. return $article;
  164. }, $articles)];
  165. return $this->httpPostJson('cgi-bin/draft/add', $params);
  166. }
  167. }