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

127 lines
4.0 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. $inventory = $input['inventory'] ?? 0;
  26. $start_time = $input['start_time'] ?? '';
  27. $end_time = $input['end_time'] ?? '';
  28. $usable_start_time = $input['usable_start_time'] ?? '';
  29. $usable_end_time = $input['usable_end_time'] ?? '';
  30. $coupon = CouponModel::find($id);
  31. if(!$coupon){
  32. return $this->error('优惠券不存在或已删除!');
  33. }
  34. switch($coupon->status){
  35. case 0:
  36. case 1:
  37. case 3:
  38. if(!empty($inventory)){
  39. $coupon->inventory = $inventory;
  40. }
  41. if(!empty($inventory)){
  42. $coupon->start_time = $start_time;
  43. }
  44. if(!empty($inventory)){
  45. $coupon->end_time = $end_time;
  46. }
  47. if(!empty($inventory)){
  48. $coupon->usable_start_time = $usable_start_time;
  49. }
  50. if(!empty($inventory)){
  51. $coupon->usable_end_time = $usable_end_time;
  52. }
  53. if($coupon->save()){
  54. return $this->success('修改成功','/coupon');
  55. }
  56. break;
  57. case 2:
  58. return $this->error('优惠券已领完!');
  59. break;
  60. case -1:
  61. return $this->error('优惠券已删除!');
  62. break;
  63. }
  64. return $this->error('发布失败!');
  65. }
  66. /**
  67. * Build a form here.
  68. */
  69. public function form()
  70. {
  71. $id = $this->payload['id'] ?? 0;
  72. $title = $this->payload['title'] ?? '';
  73. $inventory = $this->payload['inventory'] ?? 0;
  74. $start_time = $this->payload['start_time'] ?? '';
  75. $end_time = $this->payload['end_time'] ?? '';
  76. $usable_start_time = $this->payload['usable_start_time'] ?? '';
  77. $usable_end_time = $this->payload['usable_end_time'] ?? '';
  78. $this->hidden('id')->value($id);
  79. $this->display('title','标题')->value($title);
  80. $this->number('inventory','发放数量')->required()->type('number')->attribute('min', 1)->value($inventory)->default(1);
  81. $this->datetime('start_time','活动开始时间')->required()
  82. ->customFormat(function () use($start_time){
  83. return date('Y-m-d H:i:s',$start_time);
  84. });
  85. $this->datetime('end_time','活动结束时间')->required()
  86. ->customFormat(function () use($end_time){
  87. return date('Y-m-d H:i:s',$end_time);
  88. })
  89. ->rules('after:start_time',[
  90. 'after' => '只能选择活动开始之后的时间'
  91. ]);
  92. $this->datetime('usable_start_time','可用开始时间')->required()
  93. ->customFormat(function () use($usable_start_time){
  94. return date('Y-m-d H:i:s',$usable_start_time);
  95. });
  96. $this->datetime('usable_end_time','可用结束时间')->required()
  97. ->customFormat(function () use($usable_end_time){
  98. return date('Y-m-d H:i:s',$usable_end_time);
  99. })
  100. ->rules('after:end_time',[
  101. 'after' => '只能选择活动结束之后的时间'
  102. ]);
  103. }
  104. /**
  105. * The data of the form.
  106. *
  107. * @return array
  108. */
  109. public function default()
  110. {
  111. return [];
  112. }
  113. }