抖音小程序

MaterialService.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\Material;
  5. use App\Support\DouYinApi;
  6. use App\Support\EmailQueue;
  7. use Illuminate\Support\Facades\Storage;
  8. class MaterialService
  9. {
  10. /**
  11. *
  12. * */
  13. public static function uploadMaterial($appId, $type, $file, $videoTitle, $videoDesc, &$failList)
  14. {
  15. $insertData = $failList = [];
  16. if($type == 1) {
  17. $errno = MaterialService::upload($type, $file, $ossUrl);
  18. if($errno) return $errno;
  19. $insertData = [
  20. 'app_id' => $appId, 'resource_type' => $type, 'oss_url' => $ossUrl, 'title' => $videoTitle,
  21. 'desc' => $videoDesc,
  22. ];
  23. } else {
  24. if(!is_array($file)) $file = [$file];
  25. foreach ($file as $datum) {
  26. $errno = MaterialService::upload($type, $datum, $ossUrl);
  27. if($errno) array_push($failList, $datum->getClientOriginalName());
  28. $insertData[] = [
  29. 'app_id' => $appId, 'resource_type' => $type, 'oss_url' => $ossUrl, 'title' => $datum->getClientOriginalName()
  30. ];
  31. }
  32. }
  33. # 存储数据
  34. $result = Material::insert($insertData);
  35. if(!$result) return 2310;
  36. return 0;
  37. }
  38. /**
  39. * 获取素材列表
  40. * */
  41. public static function materialList($appId, $type, $keyword, $page, $pageSize)
  42. {
  43. list($list, $count) = Material::mediaList($appId, $type, $keyword, $page, $pageSize);
  44. return [$list, $count];
  45. }
  46. /**
  47. * 上传素材至OSS
  48. * */
  49. public static function upload($type, $file, &$fileUrl)
  50. {
  51. try{
  52. if (empty($file) || !$file->isValid()) {
  53. return 2311;
  54. }
  55. $originalExtension = strtolower($file->getClientOriginalExtension()); //文件扩展名
  56. if(!$originalExtension) $originalExtension = $file->guessExtension();
  57. $realPath = $file->getRealPath(); //临时文件的绝对路径
  58. $mimeType = $file->getClientMimeType(); // image/jpeg
  59. $fileSize = $file->getClientSize();
  60. switch ($type) {
  61. case 1: // 视频
  62. if (!in_array($originalExtension, ['mp4', 'm3u8'])) {
  63. return 2303;
  64. }
  65. # 视频文件大小限制
  66. if($fileSize > 100 * 1024 * 1024) {
  67. return 2308;
  68. }
  69. break;
  70. case 2: // 图片
  71. if (!in_array($originalExtension, ['jpg', 'png'])) {
  72. Log::logError('上传的图片类型为:', ['type' => $originalExtension, 'info' => $file->guessExtension() ], 'uploadMaterial');
  73. return 2301;
  74. }
  75. if($fileSize > 10 * 1024 * 1024) {
  76. return 2306;
  77. }
  78. // list($width, $height, $imageType, $attr) = getimagesize($realPath);
  79. // if($width * $height > 1555200) {
  80. // return 2312;
  81. // }
  82. break;
  83. }
  84. // 上传文件
  85. $filename = date('YmdHis') . '-' . uniqid() . '.' . $originalExtension;
  86. // 使用我们新建的uploads本地存储空间(目录)
  87. $result = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
  88. if(!$result)
  89. return 2304;
  90. $localFilePath = '../storage/uploads/'.$filename;
  91. # 上传素材到阿里oss
  92. $oss = new OssService('dou-smallapp');
  93. $ossFile = $oss->upload($originalExtension, $localFilePath, $mimeType.'/'.date("Y-m-d",time()));
  94. $fileUrl = $ossFile['oss-request-url'];
  95. @unlink($localFilePath);
  96. } catch (\Exception $e) {
  97. EmailQueue::rPush('素材上传过程发生异常', $e->getTraceAsString(), ['song.shen@kuxuan-inc.com'], '素材上传过程发生异常');
  98. Log::logError('素材上传过程发生异常', [
  99. 'line' => $e->getLine(),
  100. 'msg' => $e->getMessage()
  101. ], 'UploadMaterial-Exception');
  102. return 2305;
  103. }
  104. return 0;
  105. }
  106. public static function uploadMaterialToDouYin($id, $appId, $resourceType, $ossUrl, $title, $desc) {
  107. # 通过链接解析文件后缀
  108. $pathInfo = pathinfo($ossUrl);
  109. $originalExtension = $pathInfo['extension'];
  110. // 上传素材到抖音
  111. $mediaData = DouYinApi::uploadMedia($appId, $resourceType, $ossUrl, $title, $desc, $originalExtension);
  112. $updateData['open_pic_id'] = $mediaData['image_result']['open_pic_id'] ?? '';
  113. $updateData['open_video_id'] = $mediaData['video_result']['open_video_id'] ?? '';
  114. if(empty($updateData['open_pic_id']) && empty($updateData['open_video_id'])) {
  115. return 2305;
  116. }
  117. // 更新系统数据
  118. if(1 == $resourceType) {
  119. $updateData['status'] = 1;
  120. } else {
  121. $updateData['status'] = 2;
  122. }
  123. $res = Material::updateById($id, $updateData);
  124. if(!$res) return 500;
  125. return 0;
  126. }
  127. }