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

113 lines
3.5 KiB

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