Material.php 6.8 KB

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