链街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.
 
 
 
 

216 lines
9.4 KiB

<?php
namespace App\Admin\Forms\v3;
use Dcat\Admin\Widgets\Form;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Redis;
use App\Libs\Redis\RedisKey;
class GoodsActivitySettingForm extends Form
{
protected $typeOptions = [
1 => '列表轮播',
2 => '大图片',
];
protected $typeOptionsValue = [
1 => 'list',
2 => 'image',
];
protected $indexOptions = [
1 => '秒杀',
2 => '团购',
3 => '新品'
];
protected $indexOptionsValue = [
0 => 'default',
1 => 'flash_sale',
2 => 'group_buy',
3 => 'new_product'
];
/**
* Handle the form request.
*
* @param array $input
*
* @return Response
*/
public function handle(array $input)
{
$activitySetting = [
'index_activity_type' => $this->indexOptionsValue[$input['index_activity_type']] ?? $this->indexOptionsValue[0]
];
switch($activitySetting['index_activity_type']){
case 'flash_sale':
$setting = [
'buy_num_limit' => $input['buy_num_limit_1'] ?? 0,
'banner' => $input['banner_1'] ?? '',
'show_type' => $this->typeOptionsValue[$input['show_type_1']] ?? $this->typeOptionsValue[1],
'index_num_limit' => $input['index_num_1'] ?? 6,
'index_image' => $input['index_image_1'] ?? '',
'index_url' => $input['index_url_1'] ?? '',
];
$data = [
'flash_sale' => $setting['buy_num_limit'],
];
break;
case 'group_buy':
$setting = [
'buy_num_limit' => $input['buy_num_limit_2'] ?? 0,
'banner' => $input['banner_2'] ?? '',
'show_type' => $this->typeOptionsValue[$input['show_type_2']] ?? $this->typeOptionsValue[1],
'index_num_limit' => $input['index_num_2'] ?? 6,
'index_image' => $input['index_image_2'] ?? '',
'index_url' => $input['index_url_2'] ?? '',
];
$data = [
'group_buy' => $setting['buy_num_limit'],
];
break;
case 'new_product':
$setting = [
'buy_num_limit' => $input['buy_num_limit_3'] ?? 0,
'banner' => $input['banner_3'] ?? '',
'show_type' => $this->typeOptionsValue[$input['show_type_3']] ?? $this->typeOptionsValue[1],
'index_num_limit' => $input['index_num_3'] ?? 6,
'index_image' => $input['index_image_3'] ?? '',
'index_url' => $input['index_url_3'] ?? '',
];
$data = [
'new_product' => $setting['buy_num_limit'],
];
break;
default:
return $this->error('请选择正确的首页展示的活动类型!');
break;
}
$activitySetting[$activitySetting['index_activity_type']] = json_encode($setting);
$res1 = Redis::hmset(RedisKey::ACTIVITY_TYPE_LIMIT_NUMS , $data);
$res2 = Redis::hmset(RedisKey::ACTIVITY_TYPE_SETTING , $activitySetting);
if($res1 && $res2){
return $this->success('修改成功','/goods_activity');
}else{
return $this->error('修改失败');
}
}
/**
* Build a form here.
*/
public function form()
{
$data = Redis::hgetall(RedisKey::ACTIVITY_TYPE_LIMIT_NUMS);
$setting = Redis::hgetall(RedisKey::ACTIVITY_TYPE_SETTING);
$indexOptionsKey = array_flip($this->indexOptionsValue);
$typeOptionsKey = array_flip($this->typeOptionsValue);
$limitData = [
1 => isset($data['flash_sale']) ? $data['flash_sale']:0,
2 => isset($data['group_buy']) ? $data['group_buy']:0,
3 => isset($data['new_product']) ? $data['new_product']:0,
];
$settingData = [
'index_activity_type' => $indexOptionsKey[$setting['index_activity_type']] ?? 1,
1 => isset($setting['flash_sale']) ? json_decode($setting['flash_sale'],true) : [],
2 => isset($setting['group_buy']) ? json_decode($setting['group_buy'],true) : [],
3 => isset($setting['new_product']) ? json_decode($setting['new_product'],true) : [],
];
$this->select('index_activity_type','首页展示的活动类型')
->when([1],function(Form $form) use($limitData,$settingData,$typeOptionsKey){
$buyNumLimit = $limitData[1] ?? 0;
$sData = $settingData[1] ?? [];
$showType = $sData['show_type'] ?? 'list';
$form->number('buy_num_limit_1','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
$form->image('banner_1','banner')->autoUpload()->saveFullUrl()
->customFormat(function() use($sData){
return [$sData['banner'] ?? ''];
})->width(3);
$form->radio('show_type_1','展示形式')
->when([1],function(Form $form) use($sData){
$form->number('index_num_1','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
})
->when([2],function(Form $form) use($sData){
$form->image('index_image_1','首页大图')->autoUpload()->saveFullUrl()
->customFormat(function() use($sData){
return [$sData['index_image'] ?? ''];
})->width(3);
$form->url('index_url_1','跳转链接')->value($sData['index_url'] ?? '')->width(4);
})
->options($form->typeOptions)
->value($typeOptionsKey[$showType] ?? 1)
->default(1);
})
->when([2],function(Form $form) use($limitData,$settingData,$typeOptionsKey){
$buyNumLimit = $limitData[2] ?? 0;
$sData = $settingData[2] ?? [];
$showType = $sData['show_type'] ?? 'list';
$form->number('buy_num_limit_2','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
$form->image('banner_2','banner')->autoUpload()->saveFullUrl()
->customFormat(function() use($sData){
return [$sData['banner'] ?? ''];
})->width(3);
$form->radio('show_type_2','展示形式')
->when([1],function(Form $form) use($sData){
$form->number('index_num_2','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
})
->when([2],function(Form $form) use($sData){
$form->image('index_image_2','首页大图')->autoUpload()->saveFullUrl()
->customFormat(function() use($sData){
return [$sData['index_image'] ?? ''];
})->width(3);
$form->url('index_url_2','跳转链接')->value($sData['index_url'] ?? '')->width(4);
})
->options($form->typeOptions)
->value($typeOptionsKey[$showType] ?? 1)
->default(1);
})
->when([3],function(Form $form) use($limitData,$settingData,$typeOptionsKey){
$buyNumLimit = $limitData[3] ?? 0;
$sData = $settingData[3] ?? [];
$showType = $sData['show_type'] ?? 'list';
$form->number('buy_num_limit_3','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
$form->image('banner_3','banner')->autoUpload()->saveFullUrl()
->customFormat(function() use($sData){
return [$sData['banner'] ?? ''];
})->width(3);
$form->radio('show_type_3','展示形式')
->when([1],function(Form $form) use($sData){
$form->number('index_num_3','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
})
->when([2],function(Form $form) use($sData){
$form->image('index_image_3','首页大图')->autoUpload()->saveFullUrl()
->customFormat(function() use($sData){
return [$sData['index_image'] ?? ''];
})->width(3);
$form->url('index_url_3','跳转链接')->value($sData['index_url'] ?? '')->width(4);
})
->options($form->typeOptions)
->value($typeOptionsKey[$showType] ?? 1)
->default(1);
})
->options($this->indexOptions)->value($settingData['index_activity_type'] ?? 1)->default(1)->width(4);
}
/**
* The data of the form.
*
* @return array
*/
public function default()
{
return [];
}
}