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.
125 lines
3.9 KiB
125 lines
3.9 KiB
<?php
|
|
|
|
namespace App\Admin\Forms\v3;
|
|
|
|
use Dcat\Admin\Widgets\Form;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use App\Models\v3\Coupon as CouponModel;
|
|
use Dcat\Admin\Contracts\LazyRenderable;
|
|
use Dcat\Admin\Traits\LazyWidget;
|
|
|
|
class CouponTimeForm extends Form implements LazyRenderable
|
|
{
|
|
use LazyWidget;
|
|
/**
|
|
* Handle the form request.
|
|
*
|
|
* @param array $input
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function handle(array $input)
|
|
{
|
|
// 获取外部传递参数
|
|
$id = $input['id'];
|
|
$inventory = $input['inventory'] ?? 0;
|
|
$start_time = $input['start_time'] ?? '';
|
|
$end_time = $input['end_time'] ?? '';
|
|
$usable_start_time = $input['usable_start_time'] ?? '';
|
|
$usable_end_time = $input['usable_end_time'] ?? '';
|
|
|
|
$coupon = CouponModel::find($id);
|
|
if(!$coupon){
|
|
return $this->error('优惠券不存在或已删除!');
|
|
}
|
|
|
|
switch($coupon->status){
|
|
case 0:
|
|
case 1:
|
|
case 3:
|
|
if(!empty($inventory)){
|
|
$coupon->inventory = $inventory;
|
|
}
|
|
if(!empty($inventory)){
|
|
$coupon->start_time = $start_time;
|
|
}
|
|
if(!empty($inventory)){
|
|
$coupon->end_time = $end_time;
|
|
}
|
|
if(!empty($inventory)){
|
|
$coupon->usable_start_time = $usable_start_time;
|
|
}
|
|
if(!empty($inventory)){
|
|
$coupon->usable_end_time = $usable_end_time;
|
|
}
|
|
|
|
if($coupon->save()){
|
|
return $this->success('修改成功','/coupon');
|
|
}
|
|
break;
|
|
case 2:
|
|
return $this->error('优惠券已领完!');
|
|
break;
|
|
case -1:
|
|
return $this->error('优惠券已删除!');
|
|
break;
|
|
}
|
|
return $this->error('发布失败!');
|
|
}
|
|
|
|
/**
|
|
* Build a form here.
|
|
*/
|
|
public function form()
|
|
{
|
|
$id = $this->payload['id'] ?? 0;
|
|
$title = $this->payload['title'] ?? '';
|
|
$inventory = $this->payload['inventory'] ?? 0;
|
|
|
|
$start_time = $this->payload['start_time'] ?? '';
|
|
$end_time = $this->payload['end_time'] ?? '';
|
|
$usable_start_time = $this->payload['usable_start_time'] ?? '';
|
|
$usable_end_time = $this->payload['usable_end_time'] ?? '';
|
|
|
|
$this->hidden('id')->value($id);
|
|
$this->display('title','标题')->value($title);
|
|
|
|
$this->number('inventory','发放数量')->required()->type('number')->attribute('min', 1)->value($inventory)->default(1);
|
|
|
|
$this->datetime('start_time','活动开始时间')->required()
|
|
->customFormat(function () use($start_time){
|
|
return date('Y-m-d H:i:s',$start_time);
|
|
});
|
|
|
|
$this->datetime('end_time','活动结束时间')->required()
|
|
->customFormat(function () use($end_time){
|
|
return date('Y-m-d H:i:s',$end_time);
|
|
})
|
|
->rules('after:start_time',[
|
|
'after' => '只能选择活动开始之后的时间'
|
|
]);
|
|
$this->datetime('usable_start_time','可用开始时间')->required()
|
|
->customFormat(function () use($usable_start_time){
|
|
return date('Y-m-d H:i:s',$usable_start_time);
|
|
});
|
|
$this->datetime('usable_end_time','可用结束时间')->required()
|
|
->customFormat(function () use($usable_end_time){
|
|
return date('Y-m-d H:i:s',$usable_end_time);
|
|
})
|
|
->rules('after:end_time',[
|
|
'after' => '只能选择活动结束之后的时间'
|
|
]);
|
|
$this->disableResetButton();
|
|
}
|
|
|
|
/**
|
|
* The data of the form.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function default()
|
|
{
|
|
return [];
|
|
}
|
|
|
|
}
|