From 3b39cdd51e4105690fce8e93a6725b905ee06925 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Fri, 6 Nov 2020 18:12:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/v3/CustomFileController.php | 44 +++++++++++++++++++ app/Admin/Forms/v3/GoodsCopyForm.php | 1 + app/Admin/routes.php | 4 +- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 app/Admin/Controllers/v3/CustomFileController.php diff --git a/app/Admin/Controllers/v3/CustomFileController.php b/app/Admin/Controllers/v3/CustomFileController.php new file mode 100644 index 0000000..e3b5085 --- /dev/null +++ b/app/Admin/Controllers/v3/CustomFileController.php @@ -0,0 +1,44 @@ +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/GoodsCopyForm.php b/app/Admin/Forms/v3/GoodsCopyForm.php index aac8d66..73f1cb4 100644 --- a/app/Admin/Forms/v3/GoodsCopyForm.php +++ b/app/Admin/Forms/v3/GoodsCopyForm.php @@ -90,6 +90,7 @@ 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'); } /** diff --git a/app/Admin/routes.php b/app/Admin/routes.php index bccdd3d..6fe9bd9 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -93,8 +93,10 @@ Route::group([ // 活动商品统计 $router->any('/goods_activity_report', 'v3\GoodsActivityReportController@index'); $router->any('/goods_activity_export', 'v3\GoodsActivityReportController@export'); - //普通商品统计 + // 普通商品统计 $router->resource('/goods_report', 'v3\GoodsReportController'); $router->any('/goods_report_export', 'v3\GoodsReportController@export'); + // 自定义上传文件 + $router->any('/custom_file', 'v3\CustomFileController@handle'); }); From 5a597b56e78d1e272fc327586e2584256cdb69a7 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Mon, 9 Nov 2020 15:38:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=95=86=E5=93=81=E5=A4=8D=E5=88=B6--?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=E5=95=86=E5=93=81=E6=97=B6=EF=BC=8C=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E4=B9=9F=E9=87=8D=E6=96=B0=E5=A4=8D=E5=88=B6=E3=80=82?= =?UTF-8?q?=E5=9B=A0=E4=B8=BA=E5=A4=8D=E5=88=B6=E5=95=86=E5=93=81=E5=9B=BE?= =?UTF-8?q?=E7=89=87oss=E5=90=8C=E6=BA=90=E9=97=AE=E9=A2=98=E4=BC=9A?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E4=B8=80=E4=B8=AA=E5=95=86=E5=93=81=E7=9A=84?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=88=A0=E9=99=A4=E4=BC=9A=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=85=B6=E4=BB=96=E5=95=86=E5=93=81=E7=9A=84=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E4=B9=9F=E8=A2=AB=E5=88=A0=E9=99=A4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Common/CustomFileController.php | 75 +++++++++++++++++++ .../Controllers/v3/CustomFileController.php | 44 ----------- app/Admin/Forms/v3/GoodsActivityCopyForm.php | 48 ++++++++---- app/Admin/Forms/v3/GoodsCopyForm.php | 49 +++++++++--- 4 files changed, 147 insertions(+), 69 deletions(-) create mode 100644 app/Admin/Common/CustomFileController.php delete mode 100644 app/Admin/Controllers/v3/CustomFileController.php 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; + } + } From b60cb17d88082d334ff58474aa129ab5d7d97da4 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Mon, 9 Nov 2020 15:45:04 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=9B=BE=E7=89=87=E8=B7=AF=E7=94=B1-=E6=9A=82?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/routes.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Admin/routes.php b/app/Admin/routes.php index 6fe9bd9..81605a6 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -96,7 +96,6 @@ Route::group([ // 普通商品统计 $router->resource('/goods_report', 'v3\GoodsReportController'); $router->any('/goods_report_export', 'v3\GoodsReportController@export'); - // 自定义上传文件 - $router->any('/custom_file', 'v3\CustomFileController@handle'); + });