链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

131 lines
4.1 KiB

<?php
namespace App\Admin\Forms\v3;
use Dcat\Admin\Widgets\Form;
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 App\Admin\Common\CustomFileController;
class GoodsCopyForm extends Form
{
/**
* Handle the form request.
*
* @param array $input
*
* @return Response
*/
public function handle(array $input)
{
// 获取外部传递参数
$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;
$model->market_id = $markets[$storeId];
$model->category_id = $goods->category_id;
$model->name = $goods->name;
$model->cover_img = $newCoverImg;
$model->goods_unit = $goods->goods_unit;
$model->tags = $goods->tags;
$model->spec = $goods->spec;
$model->details_imgs = $goods->details_imgs;
$model->content = $goods->content;
$model->details = $goods->details;
$model->price = $goods->price;
$model->original_price = $goods->original_price;
$model->vip_price = $goods->vip_price;
$model->start_num = $goods->start_num;
$model->restrict_num = $goods->restrict_num;
$model->is_infinite = $goods->is_infinite;
$model->inventory = $goods->inventory;
$model->on_sale = $goods->on_sale;
$model->sort = $goods->sort;
$model->remark = $goods->remark;
if($model->save() && !empty($goodsBanners)){
$goodsId = $model->getKey();
foreach($goodsBanners as $kb => $vb){
$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);
}
}
};
}
return $this->success('复制成功', '/goods');
}
/**
* Build a form here.
*/
public function form()
{
$id = $this->getKey();
$goods = GoodsModel::select('name')->find($id);
$this->hidden('goods_id')->value($id);
$name = empty($goods->name)?'':$goods->name;
$this->display('name')->value($name);
$stores = StoreModel::getStoreArray();
$this->multipleSelect('store_ids','选择店铺')->required()->options($stores)->help('选择店铺,将当前商品复制到所选店铺。注意选择重复店铺问题!!!');
}
/**
* The data of the form.
*
* @return array
*/
public function default()
{
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;
}
}