Browse Source

商品复制--复制商品时,图片也重新复制。因为复制商品图片oss同源问题会导致一个商品的图片删除会导致其他商品的图片也被删除。

master
liangyuyan 5 years ago
parent
commit
5a597b56e7
  1. 75
      app/Admin/Common/CustomFileController.php
  2. 44
      app/Admin/Controllers/v3/CustomFileController.php
  3. 46
      app/Admin/Forms/v3/GoodsActivityCopyForm.php
  4. 49
      app/Admin/Forms/v3/GoodsCopyForm.php

75
app/Admin/Common/CustomFileController.php

@ -0,0 +1,75 @@
<?php
namespace App\Admin\Common;
use Dcat\Admin\Traits\HasUploadedFile;
class CustomFileController
{
/**
* 自定义上传
*/
use HasUploadedFile;
public function handle()
{
$disk = $this->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' => '文件复制失败'];
}
}

44
app/Admin/Controllers/v3/CustomFileController.php

@ -1,44 +0,0 @@
<?php
namespace App\Admin\Controllers\v3;
use Dcat\Admin\Traits\HasUploadedFile;
class CustomFileController
{
/**
* 自定义上传
*/
use HasUploadedFile;
public function handle()
{
$disk = $this->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('文件上传失败');
}
}

46
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);
}
}
};
}
@ -132,4 +136,22 @@ 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;
}
}

49
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
{
@ -25,11 +25,15 @@ 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;
}
}
Loading…
Cancel
Save