MediaService.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\wechat\service;
  14. use library\File;
  15. use think\Db;
  16. use WeChat\Contracts\MyCurlFile;
  17. /**
  18. * 微信素材管理
  19. * Class MediaService
  20. * @package app\wechat\service
  21. */
  22. class MediaService
  23. {
  24. /**
  25. * 通过图文ID读取图文信息
  26. * @param integer $id 本地图文ID
  27. * @param array $where 额外的查询条件
  28. * @return array
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @throws \think\exception\DbException
  32. */
  33. public static function news($id, $where = [])
  34. {
  35. $data = Db::name('WechatNews')->where(['id' => $id])->where($where)->find();
  36. list($data['articles'], $articleIds) = [[], explode(',', $data['article_id'])];
  37. $articles = Db::name('WechatNewsArticle')->whereIn('id', $articleIds)->select();
  38. foreach ($articleIds as $article_id) foreach ($articles as $article) {
  39. if (intval($article['id']) === intval($article_id)) array_push($data['articles'], $article);
  40. unset($article['create_by'], $article['create_at']);
  41. }
  42. return $data;
  43. }
  44. /**
  45. * 上传图片永久素材,返回素材media_id
  46. * @param string $url 文件URL地址
  47. * @param string $type 文件类型
  48. * @param array $videoInfo 视频信息
  49. * @return string|null
  50. * @throws \WeChat\Exceptions\InvalidResponseException
  51. * @throws \WeChat\Exceptions\LocalCacheException
  52. * @throws \think\Exception
  53. * @throws \think\exception\PDOException
  54. */
  55. public static function upload($url, $type = 'image', $videoInfo = [])
  56. {
  57. $where = ['md5' => md5($url), 'appid' => WechatService::getAppid()];
  58. if (($mediaId = Db::name('WechatMedia')->where($where)->value('media_id'))) return $mediaId;
  59. $result = WechatService::WeChatMedia()->addMaterial(self::getServerPath($url), $type, $videoInfo);
  60. data_save('WechatMedia', [
  61. 'local_url' => $url, 'md5' => $where['md5'], 'appid' => WechatService::getAppid(), 'type' => $type,
  62. 'media_url' => isset($result['url']) ? $result['url'] : '', 'media_id' => $result['media_id'],
  63. ], 'type', $where);
  64. return $result['media_id'];
  65. }
  66. /**
  67. * 文件位置处理
  68. * @param string $local
  69. * @return string
  70. * @throws \WeChat\Exceptions\LocalCacheException
  71. */
  72. private static function getServerPath($local)
  73. {
  74. if (file_exists($local)) return new MyCurlFile($local);
  75. return new MyCurlFile(File::down($local)['file']);
  76. }
  77. }