作者 lyh

gx

@@ -142,8 +142,7 @@ class FileController @@ -142,8 +142,7 @@ class FileController
142 'file.required'=>'必须填写', 142 'file.required'=>'必须填写',
143 ]); 143 ]);
144 $files = $this->request->file('file'); 144 $files = $this->request->file('file');
145 - $size = $files->getSize();  
146 - $file_type = $files->getClientOriginalExtension(); 145 +
147 if (empty($files)) { 146 if (empty($files)) {
148 $this->response('没有上传的文件!', 400); 147 $this->response('没有上传的文件!', 400);
149 } 148 }
@@ -152,6 +151,8 @@ class FileController @@ -152,6 +151,8 @@ class FileController
152 if ($type == 'multi') { 151 if ($type == 'multi') {
153 return $this->multi($files); 152 return $this->multi($files);
154 } else { 153 } else {
  154 + $size = $files->getSize();
  155 + $file_type = $files->getClientOriginalExtension();
155 return $this->single($files,$size,$file_type); 156 return $this->single($files,$size,$file_type);
156 } 157 }
157 } 158 }
@@ -223,7 +224,6 @@ class FileController @@ -223,7 +224,6 @@ class FileController
223 * @time :2023/6/17 16:32 224 * @time :2023/6/17 16:32
224 */ 225 */
225 private function multi($files) { 226 private function multi($files) {
226 - $save_data = [];  
227 $data = []; 227 $data = [];
228 foreach ($files as $file) { 228 foreach ($files as $file) {
229 $fileModel = new File(); 229 $fileModel = new File();
@@ -235,24 +235,27 @@ class FileController @@ -235,24 +235,27 @@ class FileController
235 } 235 }
236 $url = $this->config['root'].'/'.$this->path; 236 $url = $this->config['root'].'/'.$this->path;
237 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension(); 237 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
238 - $res = $file->move($url,$fileName); 238 + //同步数据到cos
  239 + if($this->upload_location == 1){
  240 + $cosService = new CosService();
  241 + $is_cos = 1;//上传到cos
  242 + $cosService->uploadFile($files,$this->path,$fileName);
  243 + }else{
  244 + $is_cos = 0;
  245 + $res = $files->move($url,$fileName);
239 if ($res === false) { 246 if ($res === false) {
240 - return $this->response($file->getError(), Code::USER_ERROR); 247 + return $this->response($files->getError(), Code::USER_ERROR);
241 } 248 }
242 - $save_data[] = [  
243 - 'path' => $this->path.'/'.$fileName,  
244 - 'created_at' => date('Y-m-d H:i:s',time()),  
245 - 'size' => $res->getSize(),  
246 - 'hash' => $hash,  
247 - 'type'=>$files->getClientOriginalExtension(),  
248 - 'refer'=>$this->param['refer']  
249 - ]; 249 + }
  250 + $size = $file->getSize();
  251 + $file_type = $file->getClientOriginalExtension();
  252 + $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
250 $data[] = $hash; 253 $data[] = $hash;
251 } 254 }
252 - $fileModel->insert($save_data);  
253 return $this->response('资源',Code::SUCCESS,['file'=>$data]); 255 return $this->response('资源',Code::SUCCESS,['file'=>$data]);
254 } 256 }
255 257
  258 +
256 /** 259 /**
257 * @param $msg 260 * @param $msg
258 * @param string $code 261 * @param string $code
@@ -41,6 +41,7 @@ class ImageController extends Controller @@ -41,6 +41,7 @@ class ImageController extends Controller
41 public $cache = '';//缓存数据 41 public $cache = '';//缓存数据
42 42
43 public $upload_location = 0;//是否同步到cos 43 public $upload_location = 0;//是否同步到cos
  44 + //上传图片类型
44 public $image_type = [ 45 public $image_type = [
45 1 => 'image_product', 46 1 => 'image_product',
46 2 => 'image_news', 47 2 => 'image_news',
@@ -157,15 +158,15 @@ class ImageController extends Controller @@ -157,15 +158,15 @@ class ImageController extends Controller
157 return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]); 158 return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
158 } 159 }
159 $url = $this->config['root'].$this->path; 160 $url = $this->config['root'].$this->path;
160 - $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();  
161 - //同步数据到cos 161 + $fileName = uniqid().rand(10000,99999).'.'.$image_type;
  162 + //上传到cos
162 if($this->upload_location == 1){ 163 if($this->upload_location == 1){
163 $cosService = new CosService(); 164 $cosService = new CosService();
164 $is_cos = 1;//上传到cos 165 $is_cos = 1;//上传到cos
165 $cosService->uploadFile($files,$this->path,$fileName); 166 $cosService->uploadFile($files,$this->path,$fileName);
166 }else{ 167 }else{
167 $res = $files->move($url,$fileName); 168 $res = $files->move($url,$fileName);
168 - $is_cos = 0;//上传到cos 169 + $is_cos = 0;
169 if ($res === false) { 170 if ($res === false) {
170 return $this->response($files->getError(), Code::USER_ERROR); 171 return $this->response($files->getError(), Code::USER_ERROR);
171 } 172 }
@@ -223,7 +224,7 @@ class ImageController extends Controller @@ -223,7 +224,7 @@ class ImageController extends Controller
223 * @time :2023/6/17 16:31 224 * @time :2023/6/17 16:31
224 */ 225 */
225 private function multi(&$files) { 226 private function multi(&$files) {
226 - $save_data = []; 227 + $save_data = [];//保存数据
227 $data = []; 228 $data = [];
228 foreach ($files as $file) { 229 foreach ($files as $file) {
229 $size = $file->getSize(); 230 $size = $file->getSize();
@@ -243,11 +244,33 @@ class ImageController extends Controller @@ -243,11 +244,33 @@ class ImageController extends Controller
243 $is_cos = 1;//上传到cos 244 $is_cos = 1;//上传到cos
244 $cosService->uploadFile($file,$this->path,$fileName); 245 $cosService->uploadFile($file,$this->path,$fileName);
245 }else{ 246 }else{
  247 + $is_cos = 0;
246 $res = $file->move($url,$fileName); 248 $res = $file->move($url,$fileName);
247 if ($res === false) { 249 if ($res === false) {
248 return $this->response($file->getError(), Code::USER_ERROR); 250 return $this->response($file->getError(), Code::USER_ERROR);
249 } 251 }
250 } 252 }
  253 + //批量存储
  254 + $save_data = $this->saveMysqlAll($save_data,$fileName,$size,$hash,$image_type,$is_cos);
  255 + $data[] = ['image'=>$hash];
  256 + }
  257 + $imageModel->insert($save_data);
  258 + return $this->response('图片资源',Code::SUCCESS,$data);
  259 + }
  260 +
  261 + /**
  262 + * @param $save_data
  263 + * @param $fileName
  264 + * @param $size
  265 + * @param $hash
  266 + * @param $image_type
  267 + * @remark :批量拼接数据
  268 + * @name :saveMysqlAll
  269 + * @author :lyh
  270 + * @method :post
  271 + * @time :2023/7/20 9:26
  272 + */
  273 + public function saveMysqlAll(&$save_data,$fileName,$size,$hash,$image_type,$is_cos){
251 $save_data[] = [ 274 $save_data[] = [
252 'path' => $this->path.'/'.$fileName, 275 'path' => $this->path.'/'.$fileName,
253 'created_at' => date('Y-m-d H:i:s',time()), 276 'created_at' => date('Y-m-d H:i:s',time()),
@@ -258,12 +281,8 @@ class ImageController extends Controller @@ -258,12 +281,8 @@ class ImageController extends Controller
258 'refer'=>$this->param['refer'] ?? 0, 281 'refer'=>$this->param['refer'] ?? 0,
259 'is_cos'=>$is_cos ?? 0 282 'is_cos'=>$is_cos ?? 0
260 ]; 283 ];
261 - $data[] = ['image'=>$hash]; 284 + return $save_data;
262 } 285 }
263 - $imageModel->insert($save_data);  
264 - return $this->response('图片资源',Code::SUCCESS,$data);  
265 - }  
266 -  
267 /** 286 /**
268 * @param $filename 287 * @param $filename
269 * @remark :下载 288 * @remark :下载