MediaService.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\wechat\service;
  16. use think\admin\Service;
  17. use think\admin\Storage;
  18. use WeChat\Contracts\MyCurlFile;
  19. /**
  20. * 微信素材管理
  21. * Class MediaService
  22. * @package app\wechat\service
  23. */
  24. class MediaService extends Service
  25. {
  26. /**
  27. * 通过图文ID读取图文信息
  28. * @param mixed $id 本地图文ID
  29. * @param array $map 额外的查询条件
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function news($id, array $map = []): array
  36. {
  37. // 文章主体数据
  38. $where = ['id' => $id, 'is_deleted' => 0];
  39. $data = $this->app->db->name('WechatNews')->where($where)->where($map)->find();
  40. if (empty($data)) return [];
  41. // 文章内容编号
  42. $data['articles'] = [];
  43. $data['articleids'] = str2arr($data['article_id']);
  44. if (empty($data['articleids'])) return $data;
  45. // 文章内容集合
  46. $query = $this->app->db->name('WechatNewsArticle');
  47. $query->whereIn('id', $data['articleids'])->orderField('id', $data['articleids']);
  48. $data['articles'] = $query->withoutField('create_by,create_at')->select()->toArray();
  49. return $data;
  50. }
  51. /**
  52. * 上传图片永久素材
  53. * @param string $url 文件地址
  54. * @param string $type 文件类型
  55. * @param array $video 视频信息
  56. * @return string media_id
  57. * @throws \WeChat\Exceptions\InvalidResponseException
  58. * @throws \WeChat\Exceptions\LocalCacheException
  59. * @throws \think\admin\Exception
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public function upload(string $url, string $type = 'image', array $video = []): string
  65. {
  66. $map = ['md5' => md5($url), 'appid' => WechatService::instance()->getAppid()];
  67. if (($mediaId = $this->app->db->name('WechatMedia')->where($map)->value('media_id'))) return $mediaId;
  68. $result = WechatService::WeChatMedia()->addMaterial(self::_buildCurlFile($url), $type, $video);
  69. data_save('WechatMedia', [
  70. 'local_url' => $url, 'md5' => $map['md5'], 'type' => $type, 'appid' => $map['appid'],
  71. 'media_url' => $result['url'] ?? '', 'media_id' => $result['media_id'],
  72. ], 'type', $map);
  73. return $result['media_id'];
  74. }
  75. /**
  76. * 创建 CURL 文件对象
  77. * @param string $local 文件路径或网络地址
  78. * @return MyCurlFile
  79. * @throws \WeChat\Exceptions\LocalCacheException
  80. */
  81. private function _buildCurlFile(string $local): MyCurlFile
  82. {
  83. if (file_exists($local)) {
  84. return new MyCurlFile($local);
  85. } else {
  86. return new MyCurlFile(Storage::down($local)['file']);
  87. }
  88. }
  89. }