作者 lyh

gx

... ... @@ -113,7 +113,6 @@ class FileController
}
/**
* @param $files
* @remark :单文件上传
* @name :single
* @author :lyh
... ... @@ -126,7 +125,7 @@ class FileController
$fileModel = new File();
$file_hash = $fileModel->read(['hash'=>$hash]);
if($file_hash !== false){
return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]);
return $this->response('资源',Code::SUCCESS,$this->responseData($fileModel,$hash));
}
$url = $this->config['root'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$file_type;
... ... @@ -141,7 +140,7 @@ class FileController
}
}
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location);
return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]);
return $this->response('资源',Code::SUCCESS,$this->responseData($fileModel,$hash));
}
/**
... ... @@ -169,7 +168,6 @@ class FileController
}
/**
* @param $files
* @remark :多文件上传(暂时未用)
* @name :multi
* @author :lyh
... ... @@ -183,11 +181,12 @@ class FileController
$hash = hash_file('md5', $file->getPathname());
$file_hash = $fileModel->read(['hash'=>$hash]);
if($file_hash !== false){
$data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)];
$data[] = $this->responseData($fileModel,$hash);
continue;
}
$url = $this->config['root'].'/'.$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
$file_type = $file->getClientOriginalExtension();
$fileName = uniqid().rand(10000,99999).'.'.$file_type;
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
... ... @@ -199,20 +198,14 @@ class FileController
}
}
$size = $file->getSize();
$file_type = $file->getClientOriginalExtension();
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location);
$data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)];
$data[] = $this->responseData($fileModel,$hash);
}
return $this->response('资源',Code::SUCCESS,$data);
$this->response('资源',Code::SUCCESS,$data);
}
/**
* @param $msg
* @param string $code
* @param $data
* @param $result_code
* @param $type
* @remark :统一返回接口
* @name :response
* @author :lyh
... ... @@ -325,4 +318,20 @@ class FileController
}
$this->response('success',Code::SUCCESS,$lists);
}
/**
* @remark :统一返回参数处理
* @name :responseData
* @author :lyh
* @method :post
* @time :2023/7/26 13:41
*/
public function responseData(&$fileModel,$hash){
$data = [
'file'=>$hash,
'file_link'=>$this->getFileUrl($fileModel,$hash),
'file_download'=>url('a/download/files?hash='.$hash),
];
return $data;
}
}
... ...
... ... @@ -151,7 +151,7 @@ class ImageController extends Controller
$imageModel = new ImageModel();
$image_hash = $imageModel->read(['hash'=>$hash]);
if($image_hash !== false){
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash,'image_link'=>$this->getImageUrl($hash)]);
return $this->response('图片资源',Code::SUCCESS,$this->responseData($hash));
}
$url = $this->config['root'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$image_type;
... ... @@ -166,7 +166,7 @@ class ImageController extends Controller
}
}
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash,'image_link'=>$this->getImageUrl($hash)]);
return $this->response('图片资源',Code::SUCCESS,$this->responseData($hash));
}
/**
... ... @@ -226,7 +226,7 @@ class ImageController extends Controller
$hash = hash_file('md5', $file->getPathname());
$image_hash = $imageModel->read(['hash'=>$hash]);
if($image_hash !== false){
$data[] = ['image'=>$hash,'image_link'=>$this->getImageUrl($hash)];
$data[] = $this->responseData($hash);
continue;
}
$url = $this->config['root'].$this->path;
... ... @@ -238,14 +238,14 @@ class ImageController extends Controller
}else{
$res = $file->move($url,$fileName);
if ($res === false) {
return $this->response($file->getError(), Code::USER_ERROR);
$this->response($file->getError(), Code::USER_ERROR);
}
}
//批量存储
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
$data[] = ['image'=>$hash,'image_link'=>$this->getImageUrl($hash)];
$data[] = $this->responseData($hash);
}
return $this->response('图片资源',Code::SUCCESS,$data);
$this->response('图片资源',Code::SUCCESS,$data);
}
/**
... ... @@ -257,7 +257,6 @@ class ImageController extends Controller
* @time :2023/7/19 17:59
*/
public function download(){
$this->setHeader();
$imageModel = new ImageModel();
$info = $imageModel->read(['hash' => $this->param['hash']]);
if ($info === false) {
... ... @@ -281,25 +280,7 @@ class ImageController extends Controller
readfile($fileUrl);
}
public function setHeader(){
// 设置跨域请求头部
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-Type');
// 处理预检请求
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-Type');
exit;
}
}
/**
* @param $msg
* @param string $code
* @param $data
* @param $result_code
* @param $type
* @remark :统一返回
* @name :response
* @author :lyh
... ... @@ -381,4 +362,20 @@ class ImageController extends Controller
}
return $url;
}
/**
* @remark :统一返回参数处理
* @name :responseData
* @author :lyh
* @method :post
* @time :2023/7/26 13:41
*/
public function responseData($hash){
$data = [
'image'=>$hash,
'image_link'=>$this->getImageUrl($hash),
'image_download'=>url('a/download/images?hash='.$hash),
];
return $data;
}
}
... ...