MaterialService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace app\common\library\xmwechat\offiaccount;
  3. use app\common\library\xmwechat\SingletonTrait;
  4. use app\common\library\xmwechat\WechatService;
  5. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  6. use EasyWeChat\OfficialAccount\Application;
  7. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  8. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  9. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  10. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  11. use think\exception\HttpResponseException;
  12. use think\Response;
  13. /**
  14. * 素材管理
  15. */
  16. class MaterialService extends WechatService
  17. {
  18. protected ?Application $app;
  19. protected string $accessToken;
  20. use SingletonTrait;
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. if (empty($this->offiAccountConfig['app_id']) || empty($this->offiAccountConfig['secret'])) {
  25. $this->result('请配置公众号appid或secret');
  26. }
  27. $this->app = new Application([
  28. 'app_id' => $this->offiAccountConfig['app_id'],
  29. 'secret' => $this->offiAccountConfig['secret'],
  30. ]);
  31. $accessToken = $this->app->getAccessToken();
  32. $this->accessToken = $accessToken->getToken();
  33. }
  34. /**
  35. * 获取素材列表 音频、视频、图片
  36. * @param $type
  37. * @return array
  38. * @throws TransportExceptionInterface
  39. * @throws ClientExceptionInterface
  40. * @throws RedirectionExceptionInterface
  41. * @throws ServerExceptionInterface
  42. * @throws \Exception
  43. * @author jsjxsz
  44. */
  45. public function getMaterialList($type, $page, $page_size): array
  46. {
  47. try {
  48. $count = $this->getMaterialCount();
  49. if ($type == 'voice') {
  50. $total_count = $count['voice_count'];
  51. } else if ($type == 'video') {
  52. $total_count = $count['video_count'];
  53. } else if ($type == 'image') {
  54. $total_count = $count['image_count'];
  55. } else {
  56. $total_count = 0;
  57. }
  58. if ($total_count == 0) {
  59. return [
  60. 'list' => [],
  61. 'total' => $total_count
  62. ];
  63. }
  64. empty($page) || $page == 0 ? $offset = 0 : $offset = ($page - 1) * $page_size;
  65. // 分页获取素材
  66. $response = $this->app->getClient()->postJson('cgi-bin/material/batchget_material?access_token=' . $this->accessToken, [
  67. 'type' => $type,
  68. 'offset' => $offset,
  69. 'count' => $page_size
  70. ]);
  71. if ($response->isFailed()) {
  72. // 出错了,处理异常
  73. $this->result($response->getContent());
  74. }
  75. $result = $response->getContent();
  76. $data = json_decode($result, true);
  77. if ($type == 'voice') {
  78. $list = $data['item'];
  79. foreach ($list as $k => $v) {
  80. $list[$k]['reply_content'] = json_encode([
  81. 'type' => 'voice',
  82. 'media_id' => $v['media_id'],
  83. 'name' => $v['name'],
  84. ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  85. }
  86. } else if ($type == 'video') {
  87. $list = $data['item'];
  88. foreach ($list as $k => $v) {
  89. $list[$k]['image'] = $v['cover_url'];
  90. $list[$k]['reply_content'] = json_encode([
  91. 'type' => 'video',
  92. 'media_id' => $v['media_id'],
  93. 'name' => $v['name'],
  94. 'cover_url' => str_replace('/', '\/', $v['cover_url']),
  95. 'description' => $v['description'],
  96. 'vid' => $v['vid']
  97. ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  98. }
  99. } else if ($type == 'image') {
  100. $list = $data['item'];
  101. foreach ($list as $k => $v) {
  102. $list[$k]['image'] = $v['url'];
  103. $list[$k]['reply_content'] = json_encode([
  104. 'type' => 'image',
  105. 'media_id' => $v['media_id'],
  106. 'name' => $v['name'],
  107. 'url' => str_replace('/', '\/', $v['url'])
  108. ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  109. }
  110. } else {
  111. $list = [];
  112. }
  113. return [
  114. 'list' => $list,
  115. 'total' => $total_count
  116. ];
  117. } catch (\Exception $e) {
  118. $this->result($e->getMessage());
  119. }
  120. }
  121. /**
  122. * 获取已发布的图文素材
  123. * @return array
  124. * @throws TransportExceptionInterface
  125. * @throws ClientExceptionInterface
  126. * @throws RedirectionExceptionInterface
  127. * @throws ServerExceptionInterface
  128. * @throws \Exception
  129. * @author jsjxsz
  130. */
  131. public function getMaterialFreePublish($page, $page_size): array
  132. {
  133. $list = [];
  134. $total_count = 0;
  135. try {
  136. empty($page) || $page == 0 ? $offset = 0 : $offset = ($page - 1) * $page_size;
  137. $result = $this->app->getClient()->postJson('cgi-bin/freepublish/batchget?access_token=' . $this->accessToken, [
  138. 'offset' => $offset,
  139. 'count' => $page_size,
  140. ]);
  141. if ($result->isFailed()) {
  142. $this->result($result->getContent());
  143. }
  144. $data = json_decode($result, true);
  145. if (!empty($data)) {
  146. $list = $data['item'];
  147. foreach ($list as $k => $v) {
  148. $content = $v['content']['news_item'][0];
  149. $list[$k]['article_id'] = $v['article_id'];
  150. $list[$k]['title'] = $content['title'];
  151. $list[$k]['author'] = $content['author'];
  152. $list[$k]['digest'] = $content['digest'];
  153. $list[$k]['content'] = $content['content'];
  154. $list[$k]['content_source_url'] = $content['content_source_url'];
  155. $list[$k]['thumb_media_id'] = $content['thumb_media_id'];
  156. $list[$k]['show_cover_pic'] = $content['show_cover_pic'];
  157. $list[$k]['url'] = $content['url'];
  158. $list[$k]['image'] = $content['thumb_url'];
  159. $list[$k]['need_open_comment'] = $content['need_open_comment'];
  160. $list[$k]['only_fans_can_comment'] = $content['only_fans_can_comment'];
  161. $list[$k]['is_deleted'] = $content['is_deleted'];
  162. $list[$k]['reply_content'] = json_encode([
  163. 'type' => 'news',
  164. 'article_id' => $v['article_id'],
  165. 'title' => $content['title'],
  166. 'digest' => $content['digest'],
  167. 'show_cover_pic' => str_replace('/', '\/', $content['show_cover_pic']),
  168. 'url' => str_replace('/', '\/', $content['url'])
  169. ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  170. }
  171. $total_count = $data['total_count'];
  172. }
  173. } catch (\Exception $e) {
  174. $this->result($e->getMessage());
  175. }
  176. return [
  177. 'list' => $list,
  178. 'total' => $total_count
  179. ];
  180. }
  181. /**
  182. * 获取素材总数量(音频、视频、图片)
  183. * @return array
  184. * @throws \Exception|TransportExceptionInterface
  185. * @author jsjxsz
  186. */
  187. public function getMaterialCount(): array
  188. {
  189. try {
  190. // 获取素材总数
  191. $materialcount = $this->app->getClient()->get('cgi-bin/material/get_materialcount?access_token=' . $this->accessToken);
  192. if ($materialcount->isFailed()) {
  193. $this->result($materialcount->getContent());
  194. }
  195. return json_decode($materialcount->getContent(), true);
  196. } catch (\Exception $e) {
  197. $this->result($e->getMessage());
  198. }
  199. }
  200. /**
  201. * 根据media_id获取视频素材、只能获取视频素材,音频、图片素材微信已不再返回
  202. * @param $media_id
  203. * @throws ClientExceptionInterface
  204. * @throws RedirectionExceptionInterface
  205. * @throws ServerExceptionInterface
  206. * @throws TransportExceptionInterface
  207. * @throws \Exception
  208. * @author jsjxsz
  209. */
  210. public function getMaterialByMediaId($media_id)
  211. {
  212. try {
  213. //获取素材总数
  214. $result = $this->app->getClient()->postJson('cgi-bin/material/get_material?access_token=' . $this->accessToken, [
  215. 'media_id' => $media_id
  216. ]);
  217. if ($result->isFailed()) {
  218. $this->result($result->getContent());
  219. }
  220. return json_decode($result->getContent(), true);
  221. } catch (\Exception $e) {
  222. $this->result($e->getMessage());
  223. }
  224. }
  225. /**
  226. * 根据article_id获取图文素材
  227. * 接口每日调用上线100次
  228. * @param $article_id
  229. * @throws ClientExceptionInterface
  230. * @throws RedirectionExceptionInterface
  231. * @throws ServerExceptionInterface
  232. * @throws TransportExceptionInterface
  233. * @throws \Exception
  234. * @author jsjxsz
  235. */
  236. public function getMaterialByArticleId($article_id)
  237. {
  238. try {
  239. //获取素材总数
  240. $result = $this->app->getClient()->postJson('cgi-bin/freepublish/getarticle?access_token=' . $this->accessToken, [
  241. 'article_id' => $article_id
  242. ]);
  243. if ($result->isFailed()) {
  244. $this->result($result->getContent());
  245. }
  246. $result = json_decode($result->getContent(), true);
  247. return $result['news_item'][0];
  248. } catch (\Exception $e) {
  249. $this->result($e->getMessage());
  250. }
  251. }
  252. }