作者 lyh

gx

@@ -113,7 +113,6 @@ class FileController @@ -113,7 +113,6 @@ class FileController
113 } 113 }
114 114
115 /** 115 /**
116 - * @param $files  
117 * @remark :单文件上传 116 * @remark :单文件上传
118 * @name :single 117 * @name :single
119 * @author :lyh 118 * @author :lyh
@@ -126,7 +125,7 @@ class FileController @@ -126,7 +125,7 @@ class FileController
126 $fileModel = new File(); 125 $fileModel = new File();
127 $file_hash = $fileModel->read(['hash'=>$hash]); 126 $file_hash = $fileModel->read(['hash'=>$hash]);
128 if($file_hash !== false){ 127 if($file_hash !== false){
129 - return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]); 128 + return $this->response('资源',Code::SUCCESS,$this->responseData($fileModel,$hash));
130 } 129 }
131 $url = $this->config['root'].$this->path; 130 $url = $this->config['root'].$this->path;
132 $fileName = uniqid().rand(10000,99999).'.'.$file_type; 131 $fileName = uniqid().rand(10000,99999).'.'.$file_type;
@@ -141,7 +140,7 @@ class FileController @@ -141,7 +140,7 @@ class FileController
141 } 140 }
142 } 141 }
143 $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location); 142 $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location);
144 - return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]); 143 + return $this->response('资源',Code::SUCCESS,$this->responseData($fileModel,$hash));
145 } 144 }
146 145
147 /** 146 /**
@@ -169,7 +168,6 @@ class FileController @@ -169,7 +168,6 @@ class FileController
169 } 168 }
170 169
171 /** 170 /**
172 - * @param $files  
173 * @remark :多文件上传(暂时未用) 171 * @remark :多文件上传(暂时未用)
174 * @name :multi 172 * @name :multi
175 * @author :lyh 173 * @author :lyh
@@ -183,11 +181,12 @@ class FileController @@ -183,11 +181,12 @@ class FileController
183 $hash = hash_file('md5', $file->getPathname()); 181 $hash = hash_file('md5', $file->getPathname());
184 $file_hash = $fileModel->read(['hash'=>$hash]); 182 $file_hash = $fileModel->read(['hash'=>$hash]);
185 if($file_hash !== false){ 183 if($file_hash !== false){
186 - $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]; 184 + $data[] = $this->responseData($fileModel,$hash);
187 continue; 185 continue;
188 } 186 }
189 $url = $this->config['root'].'/'.$this->path; 187 $url = $this->config['root'].'/'.$this->path;
190 - $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension(); 188 + $file_type = $file->getClientOriginalExtension();
  189 + $fileName = uniqid().rand(10000,99999).'.'.$file_type;
191 //同步数据到cos 190 //同步数据到cos
192 if($this->upload_location == 1){ 191 if($this->upload_location == 1){
193 $cosService = new CosService(); 192 $cosService = new CosService();
@@ -199,20 +198,14 @@ class FileController @@ -199,20 +198,14 @@ class FileController
199 } 198 }
200 } 199 }
201 $size = $file->getSize(); 200 $size = $file->getSize();
202 - $file_type = $file->getClientOriginalExtension();  
203 $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location); 201 $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location);
204 - $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]; 202 + $data[] = $this->responseData($fileModel,$hash);
205 } 203 }
206 - return $this->response('资源',Code::SUCCESS,$data); 204 + $this->response('资源',Code::SUCCESS,$data);
207 } 205 }
208 206
209 207
210 /** 208 /**
211 - * @param $msg  
212 - * @param string $code  
213 - * @param $data  
214 - * @param $result_code  
215 - * @param $type  
216 * @remark :统一返回接口 209 * @remark :统一返回接口
217 * @name :response 210 * @name :response
218 * @author :lyh 211 * @author :lyh
@@ -325,4 +318,20 @@ class FileController @@ -325,4 +318,20 @@ class FileController
325 } 318 }
326 $this->response('success',Code::SUCCESS,$lists); 319 $this->response('success',Code::SUCCESS,$lists);
327 } 320 }
  321 +
  322 + /**
  323 + * @remark :统一返回参数处理
  324 + * @name :responseData
  325 + * @author :lyh
  326 + * @method :post
  327 + * @time :2023/7/26 13:41
  328 + */
  329 + public function responseData(&$fileModel,$hash){
  330 + $data = [
  331 + 'file'=>$hash,
  332 + 'file_link'=>$this->getFileUrl($fileModel,$hash),
  333 + 'file_download'=>url('a/download/files?hash='.$hash),
  334 + ];
  335 + return $data;
  336 + }
328 } 337 }
@@ -151,7 +151,7 @@ class ImageController extends Controller @@ -151,7 +151,7 @@ class ImageController extends Controller
151 $imageModel = new ImageModel(); 151 $imageModel = new ImageModel();
152 $image_hash = $imageModel->read(['hash'=>$hash]); 152 $image_hash = $imageModel->read(['hash'=>$hash]);
153 if($image_hash !== false){ 153 if($image_hash !== false){
154 - return $this->response('图片资源',Code::SUCCESS,['image'=>$hash,'image_link'=>$this->getImageUrl($hash)]); 154 + return $this->response('图片资源',Code::SUCCESS,$this->responseData($hash));
155 } 155 }
156 $url = $this->config['root'].$this->path; 156 $url = $this->config['root'].$this->path;
157 $fileName = uniqid().rand(10000,99999).'.'.$image_type; 157 $fileName = uniqid().rand(10000,99999).'.'.$image_type;
@@ -166,7 +166,7 @@ class ImageController extends Controller @@ -166,7 +166,7 @@ class ImageController extends Controller
166 } 166 }
167 } 167 }
168 $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location); 168 $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
169 - return $this->response('图片资源',Code::SUCCESS,['image'=>$hash,'image_link'=>$this->getImageUrl($hash)]); 169 + return $this->response('图片资源',Code::SUCCESS,$this->responseData($hash));
170 } 170 }
171 171
172 /** 172 /**
@@ -226,7 +226,7 @@ class ImageController extends Controller @@ -226,7 +226,7 @@ class ImageController extends Controller
226 $hash = hash_file('md5', $file->getPathname()); 226 $hash = hash_file('md5', $file->getPathname());
227 $image_hash = $imageModel->read(['hash'=>$hash]); 227 $image_hash = $imageModel->read(['hash'=>$hash]);
228 if($image_hash !== false){ 228 if($image_hash !== false){
229 - $data[] = ['image'=>$hash,'image_link'=>$this->getImageUrl($hash)]; 229 + $data[] = $this->responseData($hash);
230 continue; 230 continue;
231 } 231 }
232 $url = $this->config['root'].$this->path; 232 $url = $this->config['root'].$this->path;
@@ -238,14 +238,14 @@ class ImageController extends Controller @@ -238,14 +238,14 @@ class ImageController extends Controller
238 }else{ 238 }else{
239 $res = $file->move($url,$fileName); 239 $res = $file->move($url,$fileName);
240 if ($res === false) { 240 if ($res === false) {
241 - return $this->response($file->getError(), Code::USER_ERROR); 241 + $this->response($file->getError(), Code::USER_ERROR);
242 } 242 }
243 } 243 }
244 //批量存储 244 //批量存储
245 $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location); 245 $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
246 - $data[] = ['image'=>$hash,'image_link'=>$this->getImageUrl($hash)]; 246 + $data[] = $this->responseData($hash);
247 } 247 }
248 - return $this->response('图片资源',Code::SUCCESS,$data); 248 + $this->response('图片资源',Code::SUCCESS,$data);
249 } 249 }
250 250
251 /** 251 /**
@@ -257,7 +257,6 @@ class ImageController extends Controller @@ -257,7 +257,6 @@ class ImageController extends Controller
257 * @time :2023/7/19 17:59 257 * @time :2023/7/19 17:59
258 */ 258 */
259 public function download(){ 259 public function download(){
260 - $this->setHeader();  
261 $imageModel = new ImageModel(); 260 $imageModel = new ImageModel();
262 $info = $imageModel->read(['hash' => $this->param['hash']]); 261 $info = $imageModel->read(['hash' => $this->param['hash']]);
263 if ($info === false) { 262 if ($info === false) {
@@ -281,25 +280,7 @@ class ImageController extends Controller @@ -281,25 +280,7 @@ class ImageController extends Controller
281 readfile($fileUrl); 280 readfile($fileUrl);
282 } 281 }
283 282
284 - public function setHeader(){  
285 - // 设置跨域请求头部  
286 - header('Access-Control-Allow-Origin: *');  
287 - header('Access-Control-Allow-Methods: GET');  
288 - header('Access-Control-Allow-Headers: Content-Type');  
289 - // 处理预检请求  
290 - if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {  
291 - header('Access-Control-Allow-Origin: *');  
292 - header('Access-Control-Allow-Methods: GET');  
293 - header('Access-Control-Allow-Headers: Content-Type');  
294 - exit;  
295 - }  
296 - }  
297 /** 283 /**
298 - * @param $msg  
299 - * @param string $code  
300 - * @param $data  
301 - * @param $result_code  
302 - * @param $type  
303 * @remark :统一返回 284 * @remark :统一返回
304 * @name :response 285 * @name :response
305 * @author :lyh 286 * @author :lyh
@@ -381,4 +362,20 @@ class ImageController extends Controller @@ -381,4 +362,20 @@ class ImageController extends Controller
381 } 362 }
382 return $url; 363 return $url;
383 } 364 }
  365 +
  366 + /**
  367 + * @remark :统一返回参数处理
  368 + * @name :responseData
  369 + * @author :lyh
  370 + * @method :post
  371 + * @time :2023/7/26 13:41
  372 + */
  373 + public function responseData($hash){
  374 + $data = [
  375 + 'image'=>$hash,
  376 + 'image_link'=>$this->getImageUrl($hash),
  377 + 'image_download'=>url('a/download/images?hash='.$hash),
  378 + ];
  379 + return $data;
  380 + }
384 } 381 }