链街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.5 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. $activitySetting = [
  33. 'index_activity_type' => $input['index_activity_type'] ?? 1
  34. ];
  35. switch($activitySetting['index_activity_type']){
  36. case 1:
  37. $setting = [
  38. 'buy_num_limit' => $input['buy_num_limit_1'] ?? 0,
  39. 'banner' => $input['banner_1'] ?? '',
  40. 'show_type' => $input['show_type_1'] ?? 0,
  41. 'index_num' => $input['index_num_1'] ?? 6,
  42. 'index_image' => $input['index_image_1'] ?? '',
  43. 'index_url' => $input['index_url_1'] ?? '',
  44. ];
  45. $data = [
  46. 'flash_sale' => $setting['buy_num_limit'],
  47. ];
  48. break;
  49. case 2:
  50. $setting = [
  51. 'buy_num_limit' => $input['buy_num_limit_2'] ?? 0,
  52. 'banner' => $input['banner_2'] ?? ''
  53. ];
  54. $data = [
  55. 'group_buy' => $setting['buy_num_limit'],
  56. ];
  57. break;
  58. case 3:
  59. $setting = [
  60. 'buy_num_limit' => $input['buy_num_limit_3'] ?? 0,
  61. 'banner' => $input['banner_3'] ?? ''
  62. ];
  63. $data = [
  64. 'new_product' => $setting['buy_num_limit'],
  65. ];
  66. break;
  67. default:
  68. return $this->error('请选择正确的首页展示的活动类型!');
  69. break;
  70. }
  71. $activitySetting[$this->indexOptionsValue[$activitySetting['index_activity_type']]] = json_encode($setting);
  72. $res1 = Redis::hmset(RedisKey::ACTIVITY_TYPE_LIMIT_NUMS , $data);
  73. $res2 = Redis::hmset(RedisKey::ACTIVITY_TYPE_SETTING , $activitySetting);
  74. if($res1 && $res2){
  75. return $this->success('修改成功','/goods_activity');
  76. }else{
  77. return $this->error('修改失败');
  78. }
  79. }
  80. /**
  81. * Build a form here.
  82. */
  83. public function form()
  84. {
  85. $data = Redis::hgetall(RedisKey::ACTIVITY_TYPE_LIMIT_NUMS);
  86. $setting = Redis::hgetall(RedisKey::ACTIVITY_TYPE_SETTING);
  87. $limitData = [
  88. 1 => isset($data['flash_sale']) ? $data['flash_sale']:0,
  89. 2 => isset($data['group_buy']) ? $data['group_buy']:0,
  90. 3 => isset($data['new_product']) ? $data['new_product']:0,
  91. ];
  92. $settingData = [
  93. 'index_activity_type' => $setting['index_activity_type'] ?? 1,
  94. 1 => isset($setting['flash_sale']) ? json_decode($setting['flash_sale'],true) : [],
  95. 2 => isset($setting['group_buy']) ? json_decode($setting['group_buy'],true) : [],
  96. 3 => isset($setting['new_product']) ? json_decode($setting['new_product'],true) : [],
  97. ];
  98. $this->select('index_activity_type','首页展示的活动类型')
  99. ->when([1],function(Form $form) use($limitData,$settingData){
  100. $buyNumLimit = $limitData[1] ?? 0;
  101. $sData = $settingData[1] ?? [];
  102. $form->number('buy_num_limit_1','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  103. $form->image('banner_1','banner')->autoUpload()->saveFullUrl()
  104. ->customFormat(function() use($sData){
  105. return [$sData['banner'] ?? ''];
  106. })->width(3);
  107. $form->radio('show_type_1','展示形式')
  108. ->when([1],function(Form $form) use($sData){
  109. $form->number('index_num_1','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
  110. })
  111. ->when([2],function(Form $form) use($sData){
  112. $form->image('index_image_1','首页大图')->autoUpload()->saveFullUrl()
  113. ->customFormat(function() use($sData){
  114. return [$sData['index_image'] ?? ''];
  115. })->width(3);
  116. $form->url('index_url_1','跳转链接')->value($sData['index_url'] ?? '')->width(4);
  117. })
  118. ->options($form->typeOptions)
  119. ->value($sData['show_type'] ?? 1)
  120. ->default(1);
  121. })
  122. ->when([2],function(Form $form) use($limitData,$settingData){
  123. $buyNumLimit = $limitData[2] ?? 0;
  124. $sData = $settingData[2] ?? [];
  125. $form->number('buy_num_limit_2','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  126. $form->image('banner_2','banner')->autoUpload()->saveFullUrl()
  127. ->customFormat(function() use($sData){
  128. return [$sData['banner'] ?? ''];
  129. })->width(3);
  130. })
  131. ->when([3],function(Form $form) use($limitData,$settingData){
  132. $buyNumLimit = $limitData[3] ?? 0;
  133. $sData = $settingData[3] ?? [];
  134. $form->number('buy_num_limit_3','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  135. $form->image('banner_3','banner')->autoUpload()->saveFullUrl()
  136. ->customFormat(function() use($sData){
  137. return [$sData['banner'] ?? ''];
  138. })->width(3);
  139. })
  140. ->options($this->indexOptions)->value($settingData['index_activity_type'] ?? 1)->default(1)->width(4);
  141. }
  142. /**
  143. * The data of the form.
  144. *
  145. * @return array
  146. */
  147. public function default()
  148. {
  149. return [];
  150. }
  151. }