MediaService.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.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 library\File;
  16. use think\Db;
  17. use WeChat\Contracts\MyCurlFile;
  18. /**
  19. * 微信素材管理
  20. * Class MediaService
  21. * @package app\wechat\service
  22. */
  23. class MediaService
  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\ModelNotFoundException
  32. * @throws \think\exception\DbException
  33. */
  34. public static function news($id, $where = [])
  35. {
  36. $data = Db::name('WechatNews')->where(['id' => $id])->where($where)->find();
  37. if (empty($data)) return [];
  38. list($data['articles'], $articleIds) = [[], explode(',', $data['article_id'])];
  39. $articles = Db::name('WechatNewsArticle')->whereIn('id', $articleIds)->select();
  40. foreach ($articleIds as $article_id) foreach ($articles as $article) {
  41. if (intval($article['id']) === intval($article_id)) array_push($data['articles'], $article);
  42. unset($article['create_by'], $article['create_at']);
  43. }
  44. return $data;
  45. }
  46. /**
  47. * 上传图片永久素材,返回素材media_id
  48. * @param string $url 文件URL地址
  49. * @param string $type 文件类型
  50. * @param array $videoInfo 视频信息
  51. * @return string|null
  52. * @throws \WeChat\Exceptions\InvalidResponseException
  53. * @throws \WeChat\Exceptions\LocalCacheException
  54. * @throws \think\Exception
  55. * @throws \think\exception\PDOException
  56. */
  57. public static function upload($url, $type = 'image', $videoInfo = [])
  58. {
  59. $where = ['md5' => md5($url), 'appid' => WechatService::getAppid()];
  60. if (($mediaId = Db::name('WechatMedia')->where($where)->value('media_id'))) return $mediaId;
  61. $result = WechatService::WeChatMedia()->addMaterial(self::getServerPath($url), $type, $videoInfo);
  62. data_save('WechatMedia', [
  63. 'local_url' => $url, 'md5' => $where['md5'], 'appid' => WechatService::getAppid(), 'type' => $type,
  64. 'media_url' => isset($result['url']) ? $result['url'] : '', 'media_id' => $result['media_id'],
  65. ], 'type', $where);
  66. return $result['media_id'];
  67. }
  68. /**
  69. * 文件位置处理
  70. * @param string $local
  71. * @return string
  72. * @throws \WeChat\Exceptions\LocalCacheException
  73. */
  74. private static function getServerPath($local)
  75. {
  76. if (file_exists($local)) {
  77. return new MyCurlFile($local);
  78. } else {
  79. return new MyCurlFile(File::down($local)['file']);
  80. }
  81. }
  82. }