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

125 lines
3.9 KiB

  1. <?php
  2. namespace App\Admin\Forms\v3;
  3. use Dcat\Admin\Widgets\Form;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use App\Models\v3\Coupon as CouponModel;
  6. use Dcat\Admin\Contracts\LazyRenderable;
  7. use Dcat\Admin\Traits\LazyWidget;
  8. class CouponTimeForm extends Form implements LazyRenderable
  9. {
  10. use LazyWidget;
  11. /**
  12. * Handle the form request.
  13. *
  14. * @param array $input
  15. *
  16. * @return Response
  17. */
  18. public function handle(array $input)
  19. {
  20. // 获取外部传递参数
  21. $id = $input['id'];
  22. $inventory = $input['inventory'] ?? 0;
  23. $start_time = $input['start_time'] ?? '';
  24. $end_time = $input['end_time'] ?? '';
  25. $usable_start_time = $input['usable_start_time'] ?? '';
  26. $usable_end_time = $input['usable_end_time'] ?? '';
  27. $coupon = CouponModel::find($id);
  28. if(!$coupon){
  29. return $this->error('优惠券不存在或已删除!');
  30. }
  31. switch($coupon->status){
  32. case 0:
  33. case 1:
  34. case 3:
  35. if(!empty($inventory)){
  36. $coupon->inventory = $inventory;
  37. }
  38. if(!empty($inventory)){
  39. $coupon->start_time = $start_time;
  40. }
  41. if(!empty($inventory)){
  42. $coupon->end_time = $end_time;
  43. }
  44. if(!empty($inventory)){
  45. $coupon->usable_start_time = $usable_start_time;
  46. }
  47. if(!empty($inventory)){
  48. $coupon->usable_end_time = $usable_end_time;
  49. }
  50. if($coupon->save()){
  51. return $this->success('修改成功','/coupon');
  52. }
  53. break;
  54. case 2:
  55. return $this->error('优惠券已领完!');
  56. break;
  57. case -1:
  58. return $this->error('优惠券已删除!');
  59. break;
  60. }
  61. return $this->error('发布失败!');
  62. }
  63. /**
  64. * Build a form here.
  65. */
  66. public function form()
  67. {
  68. $id = $this->payload['id'] ?? 0;
  69. $title = $this->payload['title'] ?? '';
  70. $inventory = $this->payload['inventory'] ?? 0;
  71. $start_time = $this->payload['start_time'] ?? '';
  72. $end_time = $this->payload['end_time'] ?? '';
  73. $usable_start_time = $this->payload['usable_start_time'] ?? '';
  74. $usable_end_time = $this->payload['usable_end_time'] ?? '';
  75. $this->hidden('id')->value($id);
  76. $this->display('title','标题')->value($title);
  77. $this->number('inventory','发放数量')->required()->type('number')->attribute('min', 1)->value($inventory)->default(1);
  78. $this->datetime('start_time','活动开始时间')->required()
  79. ->customFormat(function () use($start_time){
  80. return date('Y-m-d H:i:s',$start_time);
  81. });
  82. $this->datetime('end_time','活动结束时间')->required()
  83. ->customFormat(function () use($end_time){
  84. return date('Y-m-d H:i:s',$end_time);
  85. })
  86. ->rules('after:start_time',[
  87. 'after' => '只能选择活动开始之后的时间'
  88. ]);
  89. $this->datetime('usable_start_time','可用开始时间')->required()
  90. ->customFormat(function () use($usable_start_time){
  91. return date('Y-m-d H:i:s',$usable_start_time);
  92. });
  93. $this->datetime('usable_end_time','可用结束时间')->required()
  94. ->customFormat(function () use($usable_end_time){
  95. return date('Y-m-d H:i:s',$usable_end_time);
  96. })
  97. ->rules('after:end_time',[
  98. 'after' => '只能选择活动结束之后的时间'
  99. ]);
  100. $this->disableResetButton();
  101. }
  102. /**
  103. * The data of the form.
  104. *
  105. * @return array
  106. */
  107. public function default()
  108. {
  109. return [];
  110. }
  111. }