FileUploadService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Service;
  3. use App\Service\Weixin\WeixinService;
  4. use Illuminate\Support\Facades\Storage;
  5. class FileUploadService extends Service
  6. {
  7. //文件类型
  8. const FILE_TYPE = [
  9. 'txt',
  10. 'jpg',
  11. 'png',
  12. 'gif',
  13. 'jpeg',
  14. 'zip',
  15. 'rar',
  16. 'xlsx',
  17. 'xls',
  18. 'xml'
  19. ];
  20. const tmp_dir = 'kq';
  21. const tmp_dir_1 = 'upload_occ';
  22. const string = '/image/';
  23. const string_1 = '/api/uploadFiles/';
  24. const string2 = 'qyadmin|';
  25. const string3 = 'qyadmin/';
  26. public function uploadFileForKq($file){
  27. if(empty($file)) return [false, '请上传文件'];
  28. // 获取文件相关信息
  29. $ext = $file->getClientOriginalExtension(); // 扩展名
  30. $realPath = $file->getRealPath(); //临时文件的绝对路径
  31. $ext = strtolower($ext);
  32. if (! in_array($ext, self::FILE_TYPE)){
  33. $str = '文件格式为:';
  34. foreach (self::FILE_TYPE as $value){
  35. $str.= $value . ' ' ;
  36. }
  37. return [false,$str];
  38. }
  39. //文件名
  40. $file_name = date("Ymd").time().rand(1000,9999);
  41. $filename = $file_name . '.' . $ext;
  42. $dir = self::tmp_dir . '/' . $filename;
  43. Storage::disk('public')->put($dir, file_get_contents($realPath));
  44. return [true, self::string . $filename];
  45. }
  46. public function uploadFile($file){
  47. if(empty($file)) return [false, '请上传文件'];
  48. // 获取文件相关信息
  49. $ext = $file->getClientOriginalExtension(); // 扩展名
  50. $realPath = $file->getRealPath(); //临时文件的绝对路径
  51. $ext = strtolower($ext);
  52. if (! in_array($ext, self::FILE_TYPE)){
  53. $str = '文件格式为:';
  54. foreach (self::FILE_TYPE as $value){
  55. $str.= $value . ' ' ;
  56. }
  57. return [false,$str];
  58. }
  59. $date = date("Y-m-d");
  60. //文件名
  61. $file_name = date("Ymd").time().rand(1000,9999);
  62. $filename = $file_name.'.' . $ext;
  63. $dir = self::tmp_dir_1 . '/' . $date . '/' . $filename;
  64. Storage::disk('public')->put($dir, file_get_contents($realPath));
  65. return [true, self::string_1 . self::string2 . $filename];
  66. }
  67. //获取文件的位置oss
  68. public function getFileShow($file_name,$expired = 3500){
  69. $path = "";
  70. if(empty($file_name)) return $path;
  71. if(strpos($file_name, FileUploadService::string_1 . FileUploadService::string2) !== false){
  72. $file_name = str_replace(FileUploadService::string_1 . FileUploadService::string2,'', $file_name);
  73. $timestamp = substr($file_name, 0, 8); // 截取前八位数字
  74. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  75. $date = $date->format('Y-m-d');
  76. $savePath = self::string3 . $date . '/' . $file_name;
  77. list($status,$path) = (new OssService())->getTemporaryUrl($savePath,$expired);
  78. }
  79. return $path;
  80. }
  81. public function uploadFileLocal($file){
  82. if(empty($file)) return [false, '请上传文件'];
  83. // 获取文件相关信息
  84. $ext = $file->getClientOriginalExtension(); // 扩展名
  85. $realPath = $file->getRealPath(); //临时文件的绝对路径
  86. $ext = strtolower($ext);
  87. if (! in_array($ext, self::FILE_TYPE)){
  88. $str = '文件格式为:';
  89. foreach (self::FILE_TYPE as $value){
  90. $str.= $value . ' ' ;
  91. }
  92. return [false,$str];
  93. }
  94. // 上传文件
  95. $file_name = date("Ymd").time().rand(1000,9999);
  96. $filename = $file_name.'.' . $ext;
  97. // 使用我们新建的uploads本地存储空间(目录)
  98. Storage::disk('public')->put('upload_files/'.$filename, file_get_contents($realPath));
  99. return [true, self::string . $filename];
  100. }
  101. public function createOssUpload($file){
  102. if(! is_array($file) && empty($file)) return;
  103. foreach ($file as $filename){
  104. $filename = str_replace(FileUploadService::string_1.FileUploadService::string2,'', $filename);
  105. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  106. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  107. $date = $date->format('Y-m-d');
  108. $dir = FileUploadService::tmp_dir_1 . '/' . $date . '/' . $filename;
  109. $realPath = storage_path() . "/app/public/" . $dir;
  110. $savePath = self::string3 . $date . '/' . $filename;
  111. list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
  112. if($status) Storage::disk('public')->delete($dir);
  113. }
  114. }
  115. public function createOssUploadOld($file){
  116. if(! is_array($file) && empty($file)) return;
  117. foreach ($file as $filename){
  118. if(strpos($filename, FileUploadService::string_1.FileUploadService::string2) !== false){
  119. $filename = str_replace(FileUploadService::string_1.FileUploadService::string2,'',$filename);
  120. $timestamp = substr($filename, 0, 8); // 截取前八位数字
  121. $date = \DateTime::createFromFormat('Ymd', $timestamp);
  122. $date = $date->format('Y-m-d');
  123. $delPath = self::string3 . $date . '/' . $filename;
  124. list($status,$msg) = (new OssService())->deleteFile($delPath);
  125. // if(! $status) return [false , $msg];
  126. }
  127. }
  128. }
  129. public function delLocalPublicFile($filename){
  130. $path = storage_path('app/public/kq/' . $filename);
  131. // 将本地路径转换为存储路径
  132. $storagePath = str_replace(storage_path('app/public/'), '', $path);
  133. // 检查文件是否存在
  134. if (Storage::disk('public')->exists($storagePath)) {
  135. // 文件存在,可以进行删除操作
  136. Storage::disk('public')->delete($storagePath);
  137. }
  138. }
  139. public function createOssUploadWx($file){
  140. if(! is_array($file) && empty($file)) return;
  141. foreach ($file as $filename){
  142. $dir = WeixinService::dir . $filename;
  143. $realPath = storage_path() . "/app/public/" . $dir;
  144. $savePath = self::string3 . WeixinService::dir . $filename;
  145. list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
  146. if($status) Storage::disk('public')->delete($dir);
  147. }
  148. }
  149. public function createOssDeleteWx($file){
  150. if(! is_array($file) && empty($file)) return;
  151. foreach ($file as $filename){
  152. $delPath = self::string3 . WeixinService::dir . $filename;
  153. list($status,$msg) = (new OssService())->deleteFile($delPath);
  154. }
  155. }
  156. //获取文件的位置oss
  157. public function getFileShowWx($file_name,$expired = 3500){
  158. $path = "";
  159. if(empty($file_name)) return $path;
  160. $savePath = self::string3 . WeixinService::dir . $file_name;
  161. list($status,$path) = (new OssService())->getTemporaryUrl($savePath,$expired);
  162. return $path;
  163. }
  164. }