MediaService.php 3.7 KB

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