FileService.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/2/17
  8. * Time: 15:10
  9. */
  10. namespace app\backend\modules\upload\services;
  11. use app\common\services\Utils;
  12. use app\platform\modules\system\models\SystemSetting;
  13. /**
  14. * 需要一个上传文件(图片、视频、音频、pdf等)的接口,未看到有相应的接口,写个服务先
  15. * Class FileService
  16. * @package app\backend\modules\upload\services
  17. */
  18. class FileService
  19. {
  20. /**
  21. * @var \Illuminate\Http\UploadedFile
  22. */
  23. private $file;
  24. private $fileName;
  25. private $basePath;
  26. private $uploadPath;
  27. private $remote;
  28. public $maxSize = 5 * 1024 * 1024;
  29. public $extLimit = null;
  30. public function setFile(\Illuminate\Http\UploadedFile $file)
  31. {
  32. $this->file = $file;
  33. }
  34. /**
  35. * @return array|\Illuminate\Http\UploadedFile|\Illuminate\Http\UploadedFile[]|null
  36. * @throws \Exception
  37. */
  38. private function getFile()
  39. {
  40. if (!isset($this->file)) {
  41. $this->file = request()->file('file');
  42. if (!$this->file) {
  43. throw new \Exception('请传入正确文件参数.');
  44. }
  45. }
  46. return $this->file;
  47. }
  48. public function setFileName($fileName)
  49. {
  50. $this->fileName = $fileName;
  51. }
  52. /**
  53. * 文件名
  54. * @return string
  55. * @throws \Exception
  56. */
  57. private function getFileName()
  58. {
  59. if (!$this->fileName) {
  60. $ext = $this->getFile()->getClientOriginalExtension() ? : 'jpg'; //文件后缀
  61. $this->fileName = md5($this->getFile()->getClientOriginalName() . date('Y-m-d') . str_random(6)) . '.' . $ext;
  62. }
  63. return $this->fileName;
  64. }
  65. public function setUploadPath($uploadPath)
  66. {
  67. $this->uploadPath = $uploadPath;
  68. }
  69. private function getUploadPath()
  70. {
  71. if (!isset($this->uploadPath)) {
  72. $this->uploadPath = '';
  73. }
  74. return $this->uploadPath;
  75. }
  76. private function getBasePath()
  77. {
  78. if (!isset($this->basePath)) {
  79. if (config('app.framework') == 'platform') {
  80. $this->basePath = base_path('static/upload');
  81. } else {
  82. $this->basePath = dirname(dirname(base_path())).'/attachment';
  83. }
  84. }
  85. return $this->basePath;
  86. }
  87. private function getRemote()
  88. {
  89. if (!isset($this->remote)) {
  90. if (config('app.framework') == 'platform') {
  91. $this->remote = SystemSetting::settingLoad('remote', 'system_remote');
  92. } else {
  93. //全局配置
  94. global $_W;
  95. //公众号独立配置信息 优先使用公众号独立配置
  96. $uni_setting = app('WqUniSetting')->get()->toArray();
  97. if (!empty($uni_setting['remote']) && iunserializer($uni_setting['remote'])['type'] != 0) {
  98. $setting['remote'] = iunserializer($uni_setting['remote']);
  99. $this->remote = $setting['remote'];
  100. } else {
  101. $this->remote = $_W['setting']['remote'];
  102. }
  103. }
  104. }
  105. return $this->remote;
  106. }
  107. /**
  108. * 文件验证
  109. * @throws \Exception
  110. */
  111. private function verifyFile()
  112. {
  113. if (!$this->getFile()->isValid()) {
  114. throw new \Exception('文件上传失败');
  115. }
  116. if ($this->extLimit && is_array($this->extLimit) && !in_array($this->getFile()->getClientOriginalExtension(),$this->extLimit)) {
  117. throw new \Exception('文件不符合类型');
  118. }
  119. if ($this->getFile()->getSize() > $this->maxSize) {
  120. throw new \Exception('文件上传超过大小限制'.($this->maxSize/1024/1024).'M');
  121. }
  122. }
  123. /**
  124. * 上传文件
  125. * @throws \Exception
  126. */
  127. public function upload()
  128. {
  129. $this->verifyFile();
  130. // 获取文件相关信息
  131. $realPath = $this->getFile()->getRealPath(); //临时文件的绝对路径
  132. Utils::mkdirs($this->getBasePath() . '/' . $this->getUploadPath());
  133. $result = file_put_contents($this->getBasePath() . '/' . $this->getUploadPath() . '/' . $this->getFileName(),file_get_contents($realPath));
  134. if (!$result){
  135. throw new \Exception('上传失败');
  136. }
  137. if ($this->getRemote()['type'] != 0) {//远程附件
  138. if (config('app.framework') == 'platform') {
  139. $res = file_remote_upload($this->getUploadPath() . '/' . $this->getFileName(), true, $this->getRemote());
  140. } else {
  141. $res = file_remote_upload_wq($this->getUploadPath() . '/' . $this->getFileName(), true, $this->getRemote());
  142. }
  143. if ($res && in_array($res['errno'],[1,-1])) {
  144. throw new \Exception('上传失败:'.$res['message']);
  145. }
  146. }
  147. return $this->getFileUrl();
  148. }
  149. /**
  150. * @return bool|string
  151. * @throws \Exception
  152. */
  153. private function getFileUrl()
  154. {
  155. return $this->getUploadPath() . '/' . $this->getFileName();
  156. }
  157. }