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.
106 lines
3.2 KiB
106 lines
3.2 KiB
<?php
|
|
|
|
namespace App\Admin\Forms\v3;
|
|
|
|
use App\Models\v3\GoodsBanners;
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use App\Models\v3\Goods as GoodsModel;
|
|
use App\Models\v3\GoodsBanners as GoodsBannerModel;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class GoodsImageForm extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
|
|
protected $search = '';
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
// 获取外部传递参数
|
|
$goodsId = $input['goods_id'];
|
|
$coverImg= $input['cover_img'];
|
|
$bannerImg= $input['img_banner'];
|
|
|
|
if($goodsId > 0 && !empty($coverImg)){
|
|
$data = ['cover_img' => $coverImg];
|
|
$res1 = GoodsModel::where('id',$goodsId)->update($data);
|
|
}
|
|
if($goodsId > 0 && !empty($bannerImg)){
|
|
$banner = GoodsBannerModel::where('goods_id',$goodsId)->delete();
|
|
|
|
foreach($bannerImg as $value){
|
|
$banner = new GoodsBanners();
|
|
$banner->goods_id = $goodsId;
|
|
$banner->type = 1;
|
|
$banner->created_at = time();
|
|
$banner->updated_at = time();
|
|
$banner->path = $value;
|
|
$res2 = $banner->save();
|
|
}
|
|
}
|
|
|
|
if((isset($res1) && $res1) || (isset($res2) && $res2)){
|
|
return $this->success('修改成功','/goods?'.$this->getSearch($this->search));
|
|
}
|
|
return $this->error('未做任何修改');
|
|
}
|
|
|
|
/**
|
|
* Build a form here.
|
|
*/
|
|
public function form()
|
|
{
|
|
$id = $this->payload['id'] ?? 0;
|
|
$goodName = $this->payload['name'] ?? '';
|
|
$coverImg = $this->payload['cover_img'] ?? '';
|
|
$this->search = $this->payload['search'] ?? '';
|
|
// $goods = GoodsModel::select('name','cover_img')->find($id);
|
|
// $goodName = empty($goods->name)?'':$goods->name;
|
|
// $coverImg = empty($goods->cover_img) ? '':$goods->cover_img;
|
|
$this->hidden('goods_id')->value($id);
|
|
$this->display('name','商品名称')->value($goodName);
|
|
$this->image('cover_img','封面图')->customFormat(function($cover_img) use($coverImg){
|
|
return [$coverImg];
|
|
})->autoUpload();
|
|
$this->multipleImage('img_banner','轮播图')->customFormat(function($img_banner) use($id){
|
|
if(!$id){
|
|
return ;
|
|
}
|
|
$banners = GoodsBanners::where('goods_id',$id)->pluck('path')->toArray();
|
|
return empty($banners) ? [] : $banners;
|
|
})->autoUpload();
|
|
$this->disableResetButton();
|
|
}
|
|
|
|
/**
|
|
* The data of the form.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function default()
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function getSearch($data)
|
|
{
|
|
$return = '';
|
|
if(is_array($data) && count($data) > 0){
|
|
unset($data['_pjax']);
|
|
foreach($data as $key => $value){
|
|
$return .= $key.'='.$value.'&';
|
|
}
|
|
}
|
|
return $return;
|
|
}
|
|
|
|
}
|