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

168 lines
6.8 KiB

  1. <?php
  2. namespace App\Admin\Forms\v3;
  3. use Dcat\Admin\Widgets\Form;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Illuminate\Support\Facades\Redis;
  6. use App\Libs\Redis\RedisKey;
  7. class GoodsActivitySettingForm extends Form
  8. {
  9. protected $typeOptions = [
  10. 1 => '列表轮播',
  11. 2 => '大图片',
  12. ];
  13. protected $indexOptions = [
  14. 1 => '秒杀',
  15. 2 => '团购',
  16. 3 => '新品'
  17. ];
  18. protected $indexOptionsValue = [
  19. 1 => 'flash_sale',
  20. 2 => 'group_buy',
  21. 3 => 'new_product'
  22. ];
  23. /**
  24. * Handle the form request.
  25. *
  26. * @param array $input
  27. *
  28. * @return Response
  29. */
  30. public function handle(array $input)
  31. {
  32. // 获取外部传递参数
  33. // $data = [
  34. // 'flash_sale' => $input['flash_sale'] ?? 0,
  35. // 'group_buy' => $input['group_buy'] ?? 0,
  36. // 'new_product' => $input['new_product'] ?? 0,
  37. // ];
  38. $activitySetting = [
  39. 'index_activity_type' => $input['index_activity_type'] ?? 1
  40. ];
  41. $setting = [
  42. 'buy_num_limit' => $input['buy_num_limit'] ?? 6,
  43. ];
  44. switch($activitySetting['index_activity_type']){
  45. case 1:
  46. $setting = [
  47. 'banner' => $input['banner'] ?? '',
  48. 'show_type' => $input['show_type'] ?? 1,
  49. 'index_num' => $input['index_num'] ?? 6,
  50. 'index_image' => $input['index_image'] ?? '',
  51. 'index_url' => $input['index_url'] ?? '',
  52. ];
  53. $data = [
  54. 'flash_sale' => $setting['buy_num_limit'] ?? 0,
  55. ];
  56. break;
  57. case 2:
  58. $setting = [
  59. 'banner' => $input['banner'] ?? '',
  60. ];
  61. $data = [
  62. 'group_buy' => $setting['buy_num_limit'] ?? 0,
  63. ];
  64. break;
  65. case 3:
  66. $setting = [
  67. 'banner' => $input['banner'] ?? '',
  68. ];
  69. $data = [
  70. 'new_product' => $setting['buy_num_limit'] ?? 0,
  71. ];
  72. break;
  73. default:
  74. return $this->error('请选择正确的首页展示的活动类型!');
  75. break;
  76. }
  77. // var_dump($setting);
  78. $activitySetting[$this->indexOptionsValue[$activitySetting['index_activity_type']]] = json_encode($setting);
  79. dd([$activitySetting['index_activity_type'],json_decode($activitySetting[$this->indexOptionsValue[$activitySetting['index_activity_type']]])]);
  80. $res1 = Redis::hmset(RedisKey::ACTIVITY_TYPE_LIMIT_NUMS , $data);
  81. $res2 = Redis::hmset(RedisKey::ACTIVITY_TYPE_SETTING , $activitySetting);
  82. if($res1 && $res2){
  83. return $this->success('修改成功','/goods_activity');
  84. }else{
  85. return $this->error('修改失败');
  86. }
  87. }
  88. /**
  89. * Build a form here.
  90. */
  91. public function form()
  92. {
  93. $data = Redis::hgetall(RedisKey::ACTIVITY_TYPE_LIMIT_NUMS);
  94. $setting = Redis::hgetall(RedisKey::ACTIVITY_TYPE_SETTING);
  95. // dd([$data,$setting]);
  96. $limitData = [
  97. 1 => isset($data['flash_sale']) ? $data['flash_sale']:0,
  98. 2 => isset($data['group_buy']) ? $data['group_buy']:0,
  99. 3 => isset($data['new_product']) ? $data['new_product']:0,
  100. ];
  101. $settingData = [
  102. 'index_activity_type' => isset($setting['index_activity_type']) ?? 1,
  103. 1 => isset($setting['flash_sale']) ? json_decode($setting['flash_sale']) : [],
  104. 2 => isset($setting['group_buy']) ? json_decode($setting['group_buy']) : [],
  105. 3 => isset($setting['new_product']) ? json_decode($setting['new_product']) : [],
  106. ];
  107. $this->select('index_activity_type','首页展示的活动类型')
  108. ->when([1],function(Form $form) use($limitData,$settingData){
  109. $buyNumLimit = $limitData[1] ?? 0;
  110. $sData = $settingData[1] ?? [];
  111. $form->number('buy_num_limit','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  112. $form->image('banner','banner')->value($sData['banner'] ?? '')->autoUpload()->saveFullUrl()->width(3);
  113. $form->radio('show_type','展示形式')
  114. ->when([1],function(Form $form) use($sData){
  115. $form->number('index_num','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
  116. })
  117. ->when([2],function(Form $form) use($sData){
  118. $form->image('index_image','首页大图')->value($sData['index_image'] ?? '')->autoUpload()->saveFullUrl()->width(3);
  119. $form->url('index_url','跳转链接')->value($sData['index_url'] ?? '')->width(4);
  120. })
  121. ->options($form->typeOptions)
  122. ->value($sData['show_type'] ?? 1)
  123. ->default(1);
  124. })
  125. ->when([2],function(Form $form) use($limitData,$settingData){
  126. $buyNumLimit = $limitData[2] ?? 0;
  127. $sData = $settingData[1] ?? [];
  128. $form->number('buy_num_limit','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  129. $form->image('banner','banner')->value($sData['banner'] ?? '')->autoUpload()->saveFullUrl()->width(3);
  130. })
  131. ->when([3],function(Form $form) use($limitData,$settingData){
  132. $buyNumLimit = $limitData[3] ?? 0;
  133. $sData = $settingData[1] ?? [];
  134. $form->number('buy_num_limit','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  135. $form->image('banner','banner')->value($sData['banner'] ?? '')->autoUpload()->saveFullUrl()->width(3);
  136. })
  137. ->options($this->indexOptions)->value($settingData['index_activity_type'] ?? 1)->default(1)->width(4);
  138. // $this->display(RedisKey::ACTIVITY_TYPE_SETTING , '设置')->value('同一活动类型的商品,单笔订单可购买商品种数');
  139. // $this->number('flash_sale','秒杀')->attribute('min', 0)->default(1)->value($flashSale);
  140. // $this->number('group_buy','团购')->attribute('min', 0)->default(1)->value($groupBuy);
  141. // $this->number('new_product','新品')->attribute('min', 0)->default(1)->value($newProduct);
  142. }
  143. /**
  144. * The data of the form.
  145. *
  146. * @return array
  147. */
  148. public function default()
  149. {
  150. return [];
  151. }
  152. }