Browse Source

Merge branch 'goods_copy_bug_1106'

master
liangyuyan 5 years ago
parent
commit
0c1a464176
  1. 75
      app/Admin/Common/CustomFileController.php
  2. 48
      app/Admin/Forms/v3/GoodsActivityCopyForm.php
  3. 48
      app/Admin/Forms/v3/GoodsCopyForm.php
  4. 3
      app/Admin/routes.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' => '文件复制失败'];
}
}

48
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;
}
}

48
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);
}
}
};
}
@ -102,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;
}
}

3
app/Admin/routes.php

@ -93,8 +93,9 @@ 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');
});
Loading…
Cancel
Save