WechatMediaService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\wechat;
  12. use app\dict\sys\StorageDict;
  13. use app\dict\sys\WechatMediaDict;
  14. use app\model\wechat\WechatMedia;
  15. use app\service\core\upload\CoreUploadService;
  16. use app\service\core\wechat\CoreWechatMediaService;
  17. use core\base\BaseAdminService;
  18. /**
  19. * 微信素材管理
  20. * @package app\service\core\wechat
  21. */
  22. class WechatMediaService extends BaseAdminService
  23. {
  24. private $root_path = 'attachment';
  25. protected CoreWechatMediaService $core_wechat_media_service;
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. $this->core_wechat_media_service = new CoreWechatMediaService();
  30. $this->model = new WechatMedia();
  31. }
  32. /**
  33. * 素材列表
  34. * @return array
  35. */
  36. public function getMediaPage(array $where = []){
  37. $search_model = $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withSearch([ 'create_time', 'type' ], $where)->field('*')->append([ 'type_name' ])->order('create_time desc');
  38. return $this->pageQuery($search_model);
  39. }
  40. /**
  41. * 上传图片素材
  42. * @return array
  43. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  44. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  45. */
  46. public function addImageMedia(array $data) {
  47. $dir = $this->root_path.'/'.'image'.'/'.$this->site_id.'/'.date('Ym').'/'.date('d');
  48. $core_upload_service = new CoreUploadService();
  49. $upload_res = $core_upload_service->image($data['file'], $this->site_id, $dir, storage_type: StorageDict::LOCAL);
  50. $data = [
  51. 'site_id' => $this->site_id,
  52. 'type' => WechatMediaDict::IMAGE,
  53. 'file_path' => $upload_res['url']
  54. ];
  55. return (new CoreWechatMediaService())->addMedia($data);
  56. }
  57. /**
  58. * 上传视频资源
  59. * @return array
  60. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  61. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  62. */
  63. public function addVideoMedia(array $data) {
  64. $dir = $this->root_path.'/'.'video'.'/'.$this->site_id.'/'.date('Ym').'/'.date('d');
  65. $core_upload_service = new CoreUploadService();
  66. $upload_res = $core_upload_service->video($data['file'], $this->site_id, $dir, 0, storage_type: StorageDict::LOCAL);
  67. $data = [
  68. 'site_id' => $this->site_id,
  69. 'type' => WechatMediaDict::VIDEO,
  70. 'file_path' => $upload_res['url']
  71. ];
  72. return (new CoreWechatMediaService())->addMedia($data);
  73. }
  74. /**
  75. * 同步草稿箱
  76. * @param int $pages
  77. * @return void
  78. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  79. * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
  80. */
  81. public function syncNewsMedia(int $pages = 0) {
  82. $data = [
  83. 'site_id' => $this->site_id,
  84. 'pages' => $pages
  85. ];
  86. $res = (new CoreWechatMediaService())->syncNewsMedia($data);
  87. if ($pages < $res['total_pages']) {
  88. $pages++;
  89. $this->syncNewsMedia($pages);
  90. }
  91. return true;
  92. }
  93. }