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

111 lines
3.6 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\CouponReceiveType as ReceiveTypeModel;
  6. use App\Models\v3\CouponSetting as SettingModel;
  7. use App\Models\v3\Coupon as CouponModel;
  8. use Dcat\Admin\Contracts\LazyRenderable;
  9. use Dcat\Admin\Traits\LazyWidget;
  10. class CouponPublishForm extends Form implements LazyRenderable
  11. {
  12. use LazyWidget;
  13. /**
  14. * Handle the form request.
  15. *
  16. * @param array $input
  17. *
  18. * @return Response
  19. */
  20. public function handle(array $input)
  21. {
  22. // 获取外部传递参数
  23. $id = $input['id'];
  24. $receiveType = $input['receive_type'];
  25. $coupon = CouponModel::find($id);
  26. if(!$coupon){
  27. return $this->error('优惠券不存在或已删除!');
  28. }
  29. switch($coupon->status){
  30. case 0:
  31. case 3:
  32. $time = time();
  33. // 判断活动时间
  34. if($coupon->end_time <= $time + SettingModel::$publishTime){
  35. return $this->error('活动时间已经接近,请重新设置!');
  36. }
  37. if($coupon->status == 0){
  38. // 删除领取方式
  39. $delRes = ReceiveTypeModel::where('coupon_id',$id)->delete();
  40. if($delRes === false){
  41. return $this->error('发布失败!');
  42. }
  43. // 添加领取方式
  44. $receiveModel = new ReceiveTypeModel();
  45. $receiveModel->coupon_id = $id;
  46. $receiveModel->receive_type = $receiveType;
  47. // 获取一次可领取数量
  48. $receiveNumber = SettingModel::getSettingInfo($receiveType,'value');
  49. $receiveModel->one_receive_number = empty($receiveNumber) ? 1 : $receiveNumber->value;
  50. if(!$receiveModel->save()){
  51. return $this->error('发布失败!');
  52. }
  53. }
  54. $coupon->status = 1;
  55. if($coupon->save()){
  56. return $this->success('发布成功','/coupon');
  57. }
  58. break;
  59. case 1:
  60. return $this->error('优惠券已发布!');
  61. break;
  62. case 2:
  63. return $this->error('优惠券已领完!');
  64. break;
  65. case -1:
  66. return $this->error('优惠券已删除!');
  67. break;
  68. }
  69. return $this->error('发布失败!');
  70. }
  71. /**
  72. * Build a form here.
  73. */
  74. public function form()
  75. {
  76. $id = $this->payload['id'] ?? 0;
  77. $title = $this->payload['title'] ?? '';
  78. $status = $this->payload['status'] ?? -1;
  79. $this->hidden('id')->value($id);
  80. $this->display('title','标题')->value($title);
  81. $receiveTypeInfo = ReceiveTypeModel::getReceiveTypeOne([['coupon_id','=',$id]],'receive_type');
  82. $list = SettingModel::getSettingArray();
  83. $receiveType = empty($receiveTypeInfo->receive_type)? 0 :$receiveTypeInfo->receive_type;
  84. if($status == 0){
  85. $this->select('receive_type','领取方式')->required()->options($list)->value($receiveType);
  86. }else{
  87. $this->select('receive_type','领取方式')->options($list)->value($receiveType)->disable();
  88. }
  89. $this->disableResetButton();
  90. }
  91. /**
  92. * The data of the form.
  93. *
  94. * @return array
  95. */
  96. public function default()
  97. {
  98. return [];
  99. }
  100. }