| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 | 
							- <?php
 
- namespace app\common\service;
 
- use think\File;
 
- use think\facade\Request;
 
- use think\facade\Validate;
 
- use think\facade\Filesystem;
 
- use app\common\service\Service;
 
- use Workerman\Worker;
 
- class FileService extends Service
 
- {
 
-     
 
-     public function uploadImage($path = 'default')
 
-     {
 
-         
 
-         $files = request()->file();
 
-         
 
-         $this->validate($files, Validate::rule(['file' => 'fileSize:2048000|fileExt:jpeg,jpg,png,webp']));
 
-         $file = request()->file('file');
 
-         try {
 
-             
 
-             $path = Filesystem::disk('public')->putFile($path, $file);
 
-         } catch (\Exception $e) {
 
-             throw $this->warpException($e, '上传图片失败:\n');
 
-         }
 
-         
 
-         $path = Request::domain() . getVirRootDir() . '/storage/' . $path;
 
-         return [
 
-             'path' => $path
 
-         ];
 
-     }
 
-     
 
-     public function uploadAttachment($path = 'default')
 
-     {
 
-         
 
-         if (count(Worker::getAllWorkers()) > 0) {
 
-             $file = $_FILES[0]['file_data'];
 
-             
 
-             $tmp_file = sys_get_temp_dir() . '/' . uniqid() . '.' . explode('/', $_FILES[0]['file_type'])[1];
 
-             file_put_contents($tmp_file, $file);
 
-             $file = new File($tmp_file);
 
-             
 
-             $files = ['file' => $file];
 
-             if (
 
-                 !in_array($file->getMime(), [
 
-                     'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 
 
-                     'application/pdf', 
 
-                     'application/msword' 
 
-                 ])
 
-             ) {
 
-                 throw $this->exception('文件mime不符合要求,(请使用pdf,doc,docx)', 777);
 
-             }
 
-         } else {
 
-             
 
-             $files = request()->file();
 
-             $file = request()->file('file');
 
-             $this->validate($files, Validate::rule(['file' => 'fileExt:pdf,doc,docx']));
 
-         }
 
-         try {
 
-             
 
-             $path = Filesystem::disk('public')->putFile($path, $file);
 
-         } catch (\Exception $e) {
 
-             throw $this->warpException($e, '上传图片失败:\n');
 
-         }
 
-         
 
-         $path = Request::domain() . getVirRootDir() . '/storage/' . $path;
 
-         return [
 
-             'path' => $path
 
-         ];
 
-     }
 
-     
 
-     protected static function get_domain()
 
-     {
 
-         $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
 
-         return $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
 
-     }
 
- }
 
 
  |