diff --git a/app/Admin/Common/CustomFileController.php b/app/Admin/Common/CustomFileController.php index e193601..2fad113 100644 --- a/app/Admin/Common/CustomFileController.php +++ b/app/Admin/Common/CustomFileController.php @@ -72,4 +72,29 @@ class CustomFileController ? ['status' => true , 'msg' => '文件复制成功', 'path' => $path, 'url' => $disk->url($path)] : ['status' => false , 'msg' => '文件复制失败']; } + + /** + * 上传文件 + * @param $imageDir 存储文件夹 + * @param $filePath 文件相对路径 + */ + public function autoUploadFile($imageDir, $file) + { + $disk = $this->disk('oss'); + + // 图片拓展名 + $fileOriginalExtension = pathinfo($file)['extension']; + + $fileName = md5(time().rand(0000,9999)); + + $dir = $imageDir; + $newName = 'upload_'.$fileName.'.'.$fileOriginalExtension; + $path = "{$dir}/$newName"; + + $result = $disk->putFileAs($dir, $file, $newName); + + return $result + ? ['status' => true , 'msg' => '文件上传成功', 'path' => $path, 'url' => $disk->url($path)] + : ['status' => false , 'msg' => '文件上传失败']; + } } \ No newline at end of file diff --git a/app/Admin/Forms/v3/GoodsCopyForm.php b/app/Admin/Forms/v3/GoodsCopyForm.php index 9ee63ff..5fd17b4 100644 --- a/app/Admin/Forms/v3/GoodsCopyForm.php +++ b/app/Admin/Forms/v3/GoodsCopyForm.php @@ -112,15 +112,22 @@ class GoodsCopyForm extends Form /** * 复制的商品图片 * @param $ossImageDir oss 存储文件夹 - * @param $imagePath 图片绝对路径 + * @param $imagePath 图片相对路径 */ public function copyImage($ossImageDir, $imagePath) { $return = ''; - $oldCoverImg = $imagePath; - $customFile = new CustomFileController(); - $result = $customFile->autoCopyFile($ossImageDir, $oldCoverImg); + + // 判断图片是否包含http + if(stripos($imagePath,'http') !== false && @getimagesize($imagePath)){ + // 上传模式 + $result = $customFile->autoUploadFile($ossImageDir, $imagePath); + }else{ + // 复制模式 + $result = $customFile->autoCopyFile($ossImageDir, $imagePath); + }; + if($result['status']){ $return = $result['path']; }