MediaService.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\wechat\service;
  3. use think\admin\Service;
  4. use think\admin\Storage;
  5. use WeChat\Contracts\MyCurlFile;
  6. /**
  7. * 微信素材管理
  8. * Class MediaService
  9. * @package app\wechat\service
  10. */
  11. class MediaService extends Service
  12. {
  13. /**
  14. * 通过图文ID读取图文信息
  15. * @param mixed $id 本地图文ID
  16. * @param array $map 额外的查询条件
  17. * @return array
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public function news($id, array $map = []): array
  23. {
  24. // 文章主体数据
  25. $where = ['id' => $id, 'is_deleted' => 0];
  26. $data = $this->app->db->name('WechatNews')->where($where)->where($map)->find();
  27. if (empty($data)) return [];
  28. // 文章内容编号
  29. $data['articles'] = [];
  30. $data['articleids'] = str2arr($data['article_id']);
  31. if (empty($data['articleids'])) return $data;
  32. // 文章内容集合
  33. $query = $this->app->db->name('WechatNewsArticle');
  34. $query->whereIn('id', $data['articleids'])->orderField('id', $data['articleids']);
  35. $data['articles'] = $query->withoutField('create_by,create_at')->select()->toArray();
  36. return $data;
  37. }
  38. /**
  39. * 上传图片永久素材
  40. * @param string $url 文件地址
  41. * @param string $type 文件类型
  42. * @param array $video 视频信息
  43. * @return string media_id
  44. * @throws \WeChat\Exceptions\InvalidResponseException
  45. * @throws \WeChat\Exceptions\LocalCacheException
  46. * @throws \think\admin\Exception
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function upload(string $url, string $type = 'image', array $video = []): string
  52. {
  53. $map = ['md5' => md5($url), 'appid' => WechatService::instance()->getAppid()];
  54. if (($mediaId = $this->app->db->name('WechatMedia')->where($map)->value('media_id'))) return $mediaId;
  55. $result = WechatService::WeChatMedia()->addMaterial(self::_buildCurlFile($url), $type, $video);
  56. data_save('WechatMedia', [
  57. 'local_url' => $url, 'md5' => $map['md5'], 'type' => $type, 'appid' => $map['appid'],
  58. 'media_url' => $result['url'] ?? '', 'media_id' => $result['media_id'],
  59. ], 'type', $map);
  60. return $result['media_id'];
  61. }
  62. /**
  63. * 创建 CURL 文件对象
  64. * @param string $local 文件路径或网络地址
  65. * @return MyCurlFile
  66. * @throws \WeChat\Exceptions\LocalCacheException
  67. */
  68. private function _buildCurlFile(string $local): MyCurlFile
  69. {
  70. if (file_exists($local)) {
  71. return new MyCurlFile($local);
  72. } else {
  73. return new MyCurlFile(Storage::down($local)['file']);
  74. }
  75. }
  76. }