diff --git a/app/Admin/Common/CustomFileController.php b/app/Admin/Common/CustomFileController.php new file mode 100644 index 0000000..e193601 --- /dev/null +++ b/app/Admin/Common/CustomFileController.php @@ -0,0 +1,75 @@ +disk('oss'); + + // 判断是否是删除文件请求 + if ($this->isDeleteRequest()) { + // 删除文件并响应 + return $this->deleteFileAndResponse($disk); + } + + // 获取上传的文件 + $file = $this->file(); + + // 获取上传的字段名称 + // $column = $this->uploader()->upload_column; + + // 图片拓展名 + // $fileOriginalExtension = '.png'; + + $fileName = md5(time().rand(0000,9999)); + + $dir = 'mp_images'; + $newName = date('Y-m-d-').$fileName.'.'.$file->getClientOriginalExtension(); + + $result = $disk->putFileAs($dir, $file, $newName); + + $path = "{$dir}/$newName"; + + return $result + ? $this->responseUploaded($path, $disk->url($path)) + : $this->responseErrorMessage('文件上传失败'); + } + + /** + * 复制文件 + * @param $imageDir 存储文件夹 + * @param $filePath 文件相对路径 + */ + public function autoCopyFile($imageDir, $file) + { + $disk = $this->disk('oss'); + + $result = false; + if($disk->exists($file)){ + + // 图片拓展名 + $fileOriginalExtension = pathinfo($file)['extension']; + + $fileName = md5(time().rand(0000,9999)); + + $dir = $imageDir; + $newName = 'copy_'.$fileName.'.'.$fileOriginalExtension; + $path = "{$dir}/$newName"; + + $result = $disk->copy($file, $path); + }; + + 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/Controllers/v3/CustomFileController.php b/app/Admin/Controllers/v3/CustomFileController.php deleted file mode 100644 index e3b5085..0000000 --- a/app/Admin/Controllers/v3/CustomFileController.php +++ /dev/null @@ -1,44 +0,0 @@ -disk('oss'); - - // 判断是否是删除文件请求 - if ($this->isDeleteRequest()) { - // 删除文件并响应 - return $this->deleteFileAndResponse($disk); - } - - // 获取上传的文件 - $file = $this->file(); - var_dump($file); -dd($file); - // 获取上传的字段名称 - $column = $this->uploader()->upload_column; - - $fileName = md5(time()); - - $dir = 'mp_images'; - $newName = date('Y-m-d').$fileName.'.'.$file->getClientOriginalExtension(); - - $result = $disk->putFileAs($dir, $file, $newName); - - $path = "{$dir}/$newName"; - - return $result - ? $this->responseUploaded($path, $disk->url($path)) - : $this->responseErrorMessage('文件上传失败'); - } -} \ No newline at end of file diff --git a/app/Admin/Forms/v3/GoodsActivityCopyForm.php b/app/Admin/Forms/v3/GoodsActivityCopyForm.php index d4a24dc..5a9f282 100644 --- a/app/Admin/Forms/v3/GoodsActivityCopyForm.php +++ b/app/Admin/Forms/v3/GoodsActivityCopyForm.php @@ -4,6 +4,7 @@ */ namespace App\Admin\Forms\v3; +use App\Admin\Common\CustomFileController; use Dcat\Admin\Traits\LazyWidget; use Dcat\Admin\Widgets\Form; use Dcat\Admin\Contracts\LazyRenderable; @@ -38,12 +39,12 @@ class GoodsActivityCopyForm extends Form implements LazyRenderable if($expireTime <= time()){ $this->error('活动结束时间必须大于当前时间!'); } - + $ossImageDir = config('admin.upload.directory.image'); $goods = GoodsModel::find($goodsId); $goodsBanners = GoodsBannerModel::where('goods_id',$goodsId)->get(); $markets = StoreModel::whereIn('id',$storeIds)->pluck('market_id','id'); foreach($storeIds as $key =>$storeId){ - + $newCoverImg = $this->copyImage($ossImageDir,$goods->cover_img); $marketId = $markets[$storeId]??0; $model = new GoodsModel(); @@ -61,7 +62,7 @@ class GoodsActivityCopyForm extends Form implements LazyRenderable $model->category_id = $goods->category_id; $model->name = $goods->name; - $model->cover_img = $goods->cover_img; + $model->cover_img = $newCoverImg; $model->goods_unit = $goods->goods_unit; $model->tags = $goods->tags; @@ -85,15 +86,18 @@ class GoodsActivityCopyForm extends Form implements LazyRenderable if($model->save() && !empty($goodsBanners)){ $goodsId = $model->getKey(); foreach($goodsBanners as $kb => $vb){ - $banners = [ - 'goods_id' => $goodsId, - 'type' => $vb->type, - 'path' => $vb->path, - 'sort' => $vb->sort, - 'created_at' => time(), - 'updated_at' => time(), - ]; - GoodsBannerModel::insert($banners); + $newBanner = $this->copyImage($ossImageDir, $vb->path); + if(!empty($newBanner)){ + $banners = [ + 'goods_id' => $goodsId, + 'type' => $vb->type, + 'path' => $newBanner, + 'sort' => $vb->sort, + 'created_at' => time(), + 'updated_at' => time(), + ]; + GoodsBannerModel::insert($banners); + } } }; } @@ -131,5 +135,23 @@ class GoodsActivityCopyForm extends Form implements LazyRenderable { return []; } - + + /** + * 复制的商品图片 + * @param $ossImageDir oss 存储文件夹 + * @param $imagePath 图片绝对路径 + */ + public function copyImage($ossImageDir, $imagePath) + { + $return = ''; + $oldCoverImg = $imagePath; + + $customFile = new CustomFileController(); + $result = $customFile->autoCopyFile($ossImageDir, $oldCoverImg); + if($result['status']){ + $return = $result['path']; + } + + return $return; + } } diff --git a/app/Admin/Forms/v3/GoodsCopyForm.php b/app/Admin/Forms/v3/GoodsCopyForm.php index 73f1cb4..9ee63ff 100644 --- a/app/Admin/Forms/v3/GoodsCopyForm.php +++ b/app/Admin/Forms/v3/GoodsCopyForm.php @@ -7,7 +7,7 @@ use Symfony\Component\HttpFoundation\Response; use App\Models\v3\Store as StoreModel; use App\Models\v3\Goods as GoodsModel; use App\Models\v3\GoodsBanners as GoodsBannerModel; -use Illuminate\Support\Facades\DB; +use App\Admin\Common\CustomFileController; class GoodsCopyForm extends Form { @@ -24,12 +24,16 @@ class GoodsCopyForm extends Form // 获取外部传递参数 $goodsId = $input['goods_id']; $storeIds= $input['store_ids']; + + $ossImageDir = config('admin.upload.directory.image'); $goods = GoodsModel::find($goodsId); $goodsBanners = GoodsBannerModel::where('goods_id',$goodsId)->get(); $markets = StoreModel::whereIn('id',$storeIds)->pluck('market_id','id'); foreach($storeIds as $key =>$value){ $storeId = $value; + $newCoverImg = $this->copyImage($ossImageDir,$goods->cover_img); + $model = new GoodsModel(); $model->store_id = $storeId; @@ -37,7 +41,7 @@ class GoodsCopyForm extends Form $model->category_id = $goods->category_id; $model->name = $goods->name; - $model->cover_img = $goods->cover_img; + $model->cover_img = $newCoverImg; $model->goods_unit = $goods->goods_unit; $model->tags = $goods->tags; @@ -62,15 +66,18 @@ class GoodsCopyForm extends Form if($model->save() && !empty($goodsBanners)){ $goodsId = $model->getKey(); foreach($goodsBanners as $kb => $vb){ - $banners = [ - 'goods_id' => $goodsId, - 'type' => $vb->type, - 'path' => $vb->path, - 'sort' => $vb->sort, - 'created_at' => time(), - 'updated_at' => time(), - ]; - GoodsBannerModel::insert($banners); + $newBanner = $this->copyImage($ossImageDir, $vb->path); + if(!empty($newBanner)){ + $banners = [ + 'goods_id' => $goodsId, + 'type' => $vb->type, + 'path' => $newBanner, + 'sort' => $vb->sort, + 'created_at' => time(), + 'updated_at' => time(), + ]; + GoodsBannerModel::insert($banners); + } } }; } @@ -90,7 +97,6 @@ class GoodsCopyForm extends Form $this->display('name')->value($name); $stores = StoreModel::getStoreArray(); $this->multipleSelect('store_ids','选择店铺')->required()->options($stores)->help('选择店铺,将当前商品复制到所选店铺。注意选择重复店铺问题!!!'); - $this->image('image','测试')->url('/custom_file'); } /** @@ -103,4 +109,23 @@ class GoodsCopyForm extends Form return []; } + /** + * 复制的商品图片 + * @param $ossImageDir oss 存储文件夹 + * @param $imagePath 图片绝对路径 + */ + public function copyImage($ossImageDir, $imagePath) + { + $return = ''; + $oldCoverImg = $imagePath; + + $customFile = new CustomFileController(); + $result = $customFile->autoCopyFile($ossImageDir, $oldCoverImg); + if($result['status']){ + $return = $result['path']; + } + + return $return; + } + }