作者 lyh

gx

@@ -38,8 +38,9 @@ class FileController @@ -38,8 +38,9 @@ class FileController
38 public $cache = '';//缓存数据 38 public $cache = '';//缓存数据
39 public $upload_location = 1; 39 public $upload_location = 1;
40 public $file_type = [ 40 public $file_type = [
41 - 1 => 'file_other',  
42 - 0 => 'file' 41 + 2 => 'other',//其他
  42 + 1 => 'video',//视频
  43 + 0 => 'file'//文件
43 ]; 44 ];
44 public function __construct() 45 public function __construct()
45 { 46 {
@@ -169,7 +170,7 @@ class FileController @@ -169,7 +170,7 @@ class FileController
169 $hash = hash_file('md5', $files->getPathname()); 170 $hash = hash_file('md5', $files->getPathname());
170 //查看文件是否存在 171 //查看文件是否存在
171 $fileModel = new File(); 172 $fileModel = new File();
172 - $file_hash = $fileModel->read(['hash'=>$hash]); 173 + $file_hash = $fileModel->read(['hash'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]);
173 if($file_hash !== false){ 174 if($file_hash !== false){
174 return $this->response('资源',Code::SUCCESS,['file'=>$hash]); 175 return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
175 } 176 }
@@ -178,17 +179,15 @@ class FileController @@ -178,17 +179,15 @@ class FileController
178 //同步数据到cos 179 //同步数据到cos
179 if($this->upload_location == 1){ 180 if($this->upload_location == 1){
180 $cosService = new CosService(); 181 $cosService = new CosService();
181 - $is_cos = 1;//上传到cos  
182 $cosService->uploadFile($files,$this->path,$fileName); 182 $cosService->uploadFile($files,$this->path,$fileName);
183 }else{ 183 }else{
184 - $is_cos = 0;  
185 $res = $files->move($url,$fileName); 184 $res = $files->move($url,$fileName);
186 if ($res === false) { 185 if ($res === false) {
187 return $this->response($files->getError(), Code::USER_ERROR); 186 return $this->response($files->getError(), Code::USER_ERROR);
188 } 187 }
189 } 188 }
190 - $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);  
191 - return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($hash)]); 189 + $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location);
  190 + return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]);
192 } 191 }
193 192
194 /** 193 /**
@@ -230,7 +229,7 @@ class FileController @@ -230,7 +229,7 @@ class FileController
230 $hash = hash_file('md5', $file->getPathname()); 229 $hash = hash_file('md5', $file->getPathname());
231 $file_hash = $fileModel->read(['hash'=>$hash]); 230 $file_hash = $fileModel->read(['hash'=>$hash]);
232 if($file_hash !== false){ 231 if($file_hash !== false){
233 - $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($hash)]; 232 + $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)];
234 continue; 233 continue;
235 } 234 }
236 $url = $this->config['root'].'/'.$this->path; 235 $url = $this->config['root'].'/'.$this->path;
@@ -238,10 +237,8 @@ class FileController @@ -238,10 +237,8 @@ class FileController
238 //同步数据到cos 237 //同步数据到cos
239 if($this->upload_location == 1){ 238 if($this->upload_location == 1){
240 $cosService = new CosService(); 239 $cosService = new CosService();
241 - $is_cos = 1;//上传到cos  
242 $cosService->uploadFile($files,$this->path,$fileName); 240 $cosService->uploadFile($files,$this->path,$fileName);
243 }else{ 241 }else{
244 - $is_cos = 0;  
245 $res = $files->move($url,$fileName); 242 $res = $files->move($url,$fileName);
246 if ($res === false) { 243 if ($res === false) {
247 return $this->response($files->getError(), Code::USER_ERROR); 244 return $this->response($files->getError(), Code::USER_ERROR);
@@ -249,8 +246,8 @@ class FileController @@ -249,8 +246,8 @@ class FileController
249 } 246 }
250 $size = $file->getSize(); 247 $size = $file->getSize();
251 $file_type = $file->getClientOriginalExtension(); 248 $file_type = $file->getClientOriginalExtension();
252 - $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);  
253 - $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($hash)]; 249 + $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location);
  250 + $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)];
254 } 251 }
255 return $this->response('资源',Code::SUCCESS,$data); 252 return $this->response('资源',Code::SUCCESS,$data);
256 } 253 }
@@ -288,23 +285,26 @@ class FileController @@ -288,23 +285,26 @@ class FileController
288 * @method :post 285 * @method :post
289 * @time :2023/6/26 16:28 286 * @time :2023/6/26 16:28
290 */ 287 */
291 - public function downLoad($hash){ 288 + public function downLoad(){
292 $file_model = new File(); 289 $file_model = new File();
293 - $info = $file_model->read(['hash' => $hash]); 290 + $info = $file_model->read(['hash' => $this->param['hash']]);
294 if ($info === false) { 291 if ($info === false) {
295 $this->response('指定文件不存在!', Code::USER_ERROR); 292 $this->response('指定文件不存在!', Code::USER_ERROR);
296 } 293 }
297 - $path = $this->config['url'].'/'.$info['path'];  
298 - if (!is_file($path)) {  
299 - $this->response('指定文件已被系统删除!', Code::USER_ERROR); 294 + if($info['is_cos'] == 1){
  295 +
  296 + }else{
  297 + $fileUrl = $this->config['root'].'/'.$info['path'];
  298 + if (!is_file($fileUrl)) {
  299 + $this->response('指定文件已被系统删除!', Code::USER_ERROR);
  300 + }
  301 + $fileName = 'downloaded_file'.'.'.$info['type']; // 要保存的文件名
  302 + // 设置响应头
  303 + header('Content-Type: application/octet-stream');
  304 + header('Content-Disposition: attachment; filename="' . $fileName . '"');
  305 + // 下载文件
  306 + readfile($fileUrl);
300 } 307 }
301 - $fileUrl = url('upload'.$path); // 文件的 URL  
302 - $fileName = 'downloaded_file'.'.'.$info['type']; // 要保存的文件名  
303 - // 设置响应头  
304 - header('Content-Type: application/octet-stream');  
305 - header('Content-Disposition: attachment; filename="' . $fileName . '"');  
306 - // 下载文件  
307 - readfile($fileUrl);  
308 } 308 }
309 309
310 /** 310 /**
@@ -340,8 +340,8 @@ class FileController @@ -340,8 +340,8 @@ class FileController
340 * @method :post 340 * @method :post
341 * @time :2023/7/20 16:46 341 * @time :2023/7/20 16:46
342 */ 342 */
343 - public function getFileUrl($hash){  
344 - $fileModel = new File(); 343 + public function getFileUrl(&$fileModel,$hash){
  344 +
345 $info = $fileModel->read(['hash'=>$hash]); 345 $info = $fileModel->read(['hash'=>$hash]);
346 if($info['is_cos'] == 1){ 346 if($info['is_cos'] == 1){
347 $cos = new CosService(); 347 $cos = new CosService();
@@ -351,4 +351,21 @@ class FileController @@ -351,4 +351,21 @@ class FileController
351 } 351 }
352 return $url; 352 return $url;
353 } 353 }
  354 +
  355 + /**
  356 + * @remark :获取所有视频
  357 + * @name :getImageList
  358 + * @author :lyh
  359 + * @method :post
  360 + * @time :2023/6/29 11:48
  361 + */
  362 + public function getFileList(){
  363 + $fileModel = new File();
  364 + $lists = $fileModel->list(['refer'=>1],$order = 'id',['id','hash','type','path','created_at']);
  365 + foreach ($lists as $k => $v){
  366 + $v['file_link'] = $this->getFileUrl($fileModel,$v['hash']);
  367 + $lists[$k] = $v;
  368 + }
  369 + $this->response('success',Code::SUCCESS,$lists);
  370 + }
354 } 371 }
@@ -43,7 +43,7 @@ class CategoryLogic extends BaseLogic @@ -43,7 +43,7 @@ class CategoryLogic extends BaseLogic
43 43
44 public function getInfo($id) 44 public function getInfo($id)
45 { 45 {
46 - $info = parent::getInfo($id); 46 + $info = $this->model->read(['id'=>$id]);
47 $info['url'] = $this->getProjectDomain() . $info['route'] ; 47 $info['url'] = $this->getProjectDomain() . $info['route'] ;
48 $info['image_link'] = $this->getImageUrl($info['image']); 48 $info['image_link'] = $this->getImageUrl($info['image']);
49 return $this->success($info); 49 return $this->success($info);
@@ -60,4 +60,24 @@ class CosService @@ -60,4 +60,24 @@ class CosService
60 $imageUrl = $cosClient->getObjectUrl($cos['bucket'], trim($image_name,'/'), '+10 years'); 60 $imageUrl = $cosClient->getObjectUrl($cos['bucket'], trim($image_name,'/'), '+10 years');
61 return $imageUrl; 61 return $imageUrl;
62 } 62 }
  63 +
  64 + /**
  65 + * @remark :下载文件
  66 + * @name :downloadFile
  67 + * @author :lyh
  68 + * @method :post
  69 + * @time :2023/7/21 14:56
  70 + */
  71 + public function downloadFile($file_name){
  72 + $cos = config('filesystems.disks.cos');
  73 + $cosClient = new Client([
  74 + 'region' => $cos['region'],
  75 + 'credentials' => [
  76 + 'secretId' => $cos['credentials']['secretId'],
  77 + 'secretKey' => $cos['credentials']['secretKey'],
  78 + ],
  79 + ]);
  80 + $saveAs = 'C:\Users\\' . get_current_user() . '\Desktop\filename.ext';
  81 + return $cosClient->getObject(['Bucket' => $cos['bucket'], 'Key' => trim($file_name,'/'), 'SaveAs' => $saveAs]);
  82 + }
63 } 83 }
@@ -208,17 +208,20 @@ Route::middleware(['bloginauth'])->group(function () { @@ -208,17 +208,20 @@ Route::middleware(['bloginauth'])->group(function () {
208 }); 208 });
209 209
210 //文件操作 210 //文件操作
211 - Route::prefix('file')->group(function () {  
212 - Route::post('/upload', [\App\Http\Controllers\Bside\FileController::class, 'upload'])->name('file_upload');  
213 - Route::get('/lists', [\App\Http\Controllers\Bside\FileController::class, 'lists'])->name('file_lists');  
214 - }); 211 +// Route::prefix('file')->group(function () {
  212 +// Route::post('/upload', [\App\Http\Controllers\Bside\FileController::class, 'upload'])->name('file_upload');
  213 +// Route::get('/lists', [\App\Http\Controllers\Bside\FileController::class, 'lists'])->name('file_lists');
  214 +// });
215 //图片操作 215 //图片操作
216 Route::prefix('images')->group(function () { 216 Route::prefix('images')->group(function () {
217 Route::post('/upload', [\App\Http\Controllers\File\ImageController::class, 'upload'])->name('image_upload'); 217 Route::post('/upload', [\App\Http\Controllers\File\ImageController::class, 'upload'])->name('image_upload');
  218 + Route::any('/getImageList', [\App\Http\Controllers\File\ImageController::class, 'getImageList'])->name('image_getImageList');
218 }); 219 });
219 //文件操作 220 //文件操作
220 Route::prefix('files')->group(function () { 221 Route::prefix('files')->group(function () {
221 Route::post('/upload', [\App\Http\Controllers\File\FileController::class, 'upload'])->name('files_upload'); 222 Route::post('/upload', [\App\Http\Controllers\File\FileController::class, 'upload'])->name('files_upload');
  223 + Route::any('/getFileList', [\App\Http\Controllers\File\FileController::class, 'getFileList'])->name('files_getFileList');
  224 + Route::any('/downLoad', [\App\Http\Controllers\File\FileController::class, 'downLoad'])->name('files_downLoad');
222 }); 225 });
223 //精准询盘 226 //精准询盘
224 Route::prefix('inquiry')->group(function () { 227 Route::prefix('inquiry')->group(function () {
@@ -334,7 +337,6 @@ Route::middleware(['bloginauth'])->group(function () { @@ -334,7 +337,6 @@ Route::middleware(['bloginauth'])->group(function () {
334 Route::group([], function () { 337 Route::group([], function () {
335 Route::any('/login', [\App\Http\Controllers\Bside\ComController::class, 'login'])->name('login'); 338 Route::any('/login', [\App\Http\Controllers\Bside\ComController::class, 'login'])->name('login');
336 Route::any('/stringTranslation', [\App\Http\Controllers\Bside\ComController::class, 'stringTranslation'])->name('com_stringTranslation'); 339 Route::any('/stringTranslation', [\App\Http\Controllers\Bside\ComController::class, 'stringTranslation'])->name('com_stringTranslation');
337 - Route::any('/getImageList', [\App\Http\Controllers\File\ImageController::class, 'getImageList'])->name('image_getImageList');  
338 Route::any('/sendLoginSms', [\App\Http\Controllers\Bside\ComController::class, 'sendLoginSms'])->name('sendLoginSms'); 340 Route::any('/sendLoginSms', [\App\Http\Controllers\Bside\ComController::class, 'sendLoginSms'])->name('sendLoginSms');
339 Route::get('/file/download', [\App\Http\Controllers\Bside\FileController::class, 'download'])->name('file_download'); 341 Route::get('/file/download', [\App\Http\Controllers\Bside\FileController::class, 'download'])->name('file_download');
340 Route::any('/image/{hash}/{w?}/{h?}', [\App\Http\Controllers\File\ImageController::class, 'index'])->name('image_show'); 342 Route::any('/image/{hash}/{w?}/{h?}', [\App\Http\Controllers\File\ImageController::class, 'index'])->name('image_show');