MediaService.php 3.5 KB

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