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

216 lines
9.4 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 $typeOptionsValue = [
  14. 1 => 'list',
  15. 2 => 'image',
  16. ];
  17. protected $indexOptions = [
  18. 1 => '秒杀',
  19. 2 => '团购',
  20. 3 => '新品'
  21. ];
  22. protected $indexOptionsValue = [
  23. 0 => 'default',
  24. 1 => 'flash_sale',
  25. 2 => 'group_buy',
  26. 3 => 'new_product'
  27. ];
  28. /**
  29. * Handle the form request.
  30. *
  31. * @param array $input
  32. *
  33. * @return Response
  34. */
  35. public function handle(array $input)
  36. {
  37. $activitySetting = [
  38. 'index_activity_type' => $this->indexOptionsValue[$input['index_activity_type']] ?? $this->indexOptionsValue[0]
  39. ];
  40. switch($activitySetting['index_activity_type']){
  41. case 'flash_sale':
  42. $setting = [
  43. 'buy_num_limit' => $input['buy_num_limit_1'] ?? 0,
  44. 'banner' => $input['banner_1'] ?? '',
  45. 'show_type' => $this->typeOptionsValue[$input['show_type_1']] ?? $this->typeOptionsValue[1],
  46. 'index_num_limit' => $input['index_num_1'] ?? 6,
  47. 'index_image' => $input['index_image_1'] ?? '',
  48. 'index_url' => $input['index_url_1'] ?? '',
  49. ];
  50. $data = [
  51. 'flash_sale' => $setting['buy_num_limit'],
  52. ];
  53. break;
  54. case 'group_buy':
  55. $setting = [
  56. 'buy_num_limit' => $input['buy_num_limit_2'] ?? 0,
  57. 'banner' => $input['banner_2'] ?? '',
  58. 'show_type' => $this->typeOptionsValue[$input['show_type_2']] ?? $this->typeOptionsValue[1],
  59. 'index_num_limit' => $input['index_num_2'] ?? 6,
  60. 'index_image' => $input['index_image_2'] ?? '',
  61. 'index_url' => $input['index_url_2'] ?? '',
  62. ];
  63. $data = [
  64. 'group_buy' => $setting['buy_num_limit'],
  65. ];
  66. break;
  67. case 'new_product':
  68. $setting = [
  69. 'buy_num_limit' => $input['buy_num_limit_3'] ?? 0,
  70. 'banner' => $input['banner_3'] ?? '',
  71. 'show_type' => $this->typeOptionsValue[$input['show_type_3']] ?? $this->typeOptionsValue[1],
  72. 'index_num_limit' => $input['index_num_3'] ?? 6,
  73. 'index_image' => $input['index_image_3'] ?? '',
  74. 'index_url' => $input['index_url_3'] ?? '',
  75. ];
  76. $data = [
  77. 'new_product' => $setting['buy_num_limit'],
  78. ];
  79. break;
  80. default:
  81. return $this->error('请选择正确的首页展示的活动类型!');
  82. break;
  83. }
  84. $activitySetting[$activitySetting['index_activity_type']] = json_encode($setting);
  85. $res1 = Redis::hmset(RedisKey::ACTIVITY_TYPE_LIMIT_NUMS , $data);
  86. $res2 = Redis::hmset(RedisKey::ACTIVITY_TYPE_SETTING , $activitySetting);
  87. if($res1 && $res2){
  88. return $this->success('修改成功','/goods_activity');
  89. }else{
  90. return $this->error('修改失败');
  91. }
  92. }
  93. /**
  94. * Build a form here.
  95. */
  96. public function form()
  97. {
  98. $data = Redis::hgetall(RedisKey::ACTIVITY_TYPE_LIMIT_NUMS);
  99. $setting = Redis::hgetall(RedisKey::ACTIVITY_TYPE_SETTING);
  100. $indexOptionsKey = array_flip($this->indexOptionsValue);
  101. $typeOptionsKey = array_flip($this->typeOptionsValue);
  102. $limitData = [
  103. 1 => isset($data['flash_sale']) ? $data['flash_sale']:0,
  104. 2 => isset($data['group_buy']) ? $data['group_buy']:0,
  105. 3 => isset($data['new_product']) ? $data['new_product']:0,
  106. ];
  107. $settingData = [
  108. 'index_activity_type' => $indexOptionsKey[$setting['index_activity_type']] ?? 1,
  109. 1 => isset($setting['flash_sale']) ? json_decode($setting['flash_sale'],true) : [],
  110. 2 => isset($setting['group_buy']) ? json_decode($setting['group_buy'],true) : [],
  111. 3 => isset($setting['new_product']) ? json_decode($setting['new_product'],true) : [],
  112. ];
  113. $this->select('index_activity_type','首页展示的活动类型')
  114. ->when([1],function(Form $form) use($limitData,$settingData,$typeOptionsKey){
  115. $buyNumLimit = $limitData[1] ?? 0;
  116. $sData = $settingData[1] ?? [];
  117. $showType = $sData['show_type'] ?? 'list';
  118. $form->number('buy_num_limit_1','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  119. $form->image('banner_1','banner')->autoUpload()->saveFullUrl()
  120. ->customFormat(function() use($sData){
  121. return [$sData['banner'] ?? ''];
  122. })->width(3);
  123. $form->radio('show_type_1','展示形式')
  124. ->when([1],function(Form $form) use($sData){
  125. $form->number('index_num_1','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
  126. })
  127. ->when([2],function(Form $form) use($sData){
  128. $form->image('index_image_1','首页大图')->autoUpload()->saveFullUrl()
  129. ->customFormat(function() use($sData){
  130. return [$sData['index_image'] ?? ''];
  131. })->width(3);
  132. $form->url('index_url_1','跳转链接')->value($sData['index_url'] ?? '')->width(4);
  133. })
  134. ->options($form->typeOptions)
  135. ->value($typeOptionsKey[$showType] ?? 1)
  136. ->default(1);
  137. })
  138. ->when([2],function(Form $form) use($limitData,$settingData,$typeOptionsKey){
  139. $buyNumLimit = $limitData[2] ?? 0;
  140. $sData = $settingData[2] ?? [];
  141. $showType = $sData['show_type'] ?? 'list';
  142. $form->number('buy_num_limit_2','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  143. $form->image('banner_2','banner')->autoUpload()->saveFullUrl()
  144. ->customFormat(function() use($sData){
  145. return [$sData['banner'] ?? ''];
  146. })->width(3);
  147. $form->radio('show_type_2','展示形式')
  148. ->when([1],function(Form $form) use($sData){
  149. $form->number('index_num_2','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
  150. })
  151. ->when([2],function(Form $form) use($sData){
  152. $form->image('index_image_2','首页大图')->autoUpload()->saveFullUrl()
  153. ->customFormat(function() use($sData){
  154. return [$sData['index_image'] ?? ''];
  155. })->width(3);
  156. $form->url('index_url_2','跳转链接')->value($sData['index_url'] ?? '')->width(4);
  157. })
  158. ->options($form->typeOptions)
  159. ->value($typeOptionsKey[$showType] ?? 1)
  160. ->default(1);
  161. })
  162. ->when([3],function(Form $form) use($limitData,$settingData,$typeOptionsKey){
  163. $buyNumLimit = $limitData[3] ?? 0;
  164. $sData = $settingData[3] ?? [];
  165. $showType = $sData['show_type'] ?? 'list';
  166. $form->number('buy_num_limit_3','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  167. $form->image('banner_3','banner')->autoUpload()->saveFullUrl()
  168. ->customFormat(function() use($sData){
  169. return [$sData['banner'] ?? ''];
  170. })->width(3);
  171. $form->radio('show_type_3','展示形式')
  172. ->when([1],function(Form $form) use($sData){
  173. $form->number('index_num_3','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
  174. })
  175. ->when([2],function(Form $form) use($sData){
  176. $form->image('index_image_3','首页大图')->autoUpload()->saveFullUrl()
  177. ->customFormat(function() use($sData){
  178. return [$sData['index_image'] ?? ''];
  179. })->width(3);
  180. $form->url('index_url_3','跳转链接')->value($sData['index_url'] ?? '')->width(4);
  181. })
  182. ->options($form->typeOptions)
  183. ->value($typeOptionsKey[$showType] ?? 1)
  184. ->default(1);
  185. })
  186. ->options($this->indexOptions)->value($settingData['index_activity_type'] ?? 1)->default(1)->width(4);
  187. }
  188. /**
  189. * The data of the form.
  190. *
  191. * @return array
  192. */
  193. public function default()
  194. {
  195. return [];
  196. }
  197. }