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

244 lines
11 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. $imageUrl = config('filesystems.disks.oss.img_host');
  41. switch($activitySetting['index_activity_type']){
  42. case 'flash_sale':
  43. $banner1 = $input['banner_1'];
  44. $bLen = empty($banner1) ? 1 : strrpos($banner1,'http');
  45. $banner = $bLen !== false ? $banner1 : $imageUrl.'/'.$banner1 ;
  46. $image1 = $input['index_image_1'];
  47. $iLen = empty($image1) ? 1 : strrpos($image1,'http');
  48. $image = $iLen !== false ? $image1 : $imageUrl.'/'.$image1 ;
  49. $setting = [
  50. 'buy_num_limit' => $input['buy_num_limit_1'] ?? 0,
  51. 'banner' => $banner,
  52. 'show_type' => $this->typeOptionsValue[$input['show_type_1']] ?? $this->typeOptionsValue[1],
  53. 'index_num_limit' => $input['index_num_1'] ?? 6,
  54. 'index_image' => $image,
  55. 'index_url' => $input['index_url_1'] ?? '',
  56. ];
  57. $data = [
  58. 'flash_sale' => $setting['buy_num_limit'],
  59. ];
  60. break;
  61. case 'group_buy':
  62. $banner2 = $input['banner_2'];
  63. $bLen = empty($banner2) ? 2 : strrpos($banner2,'http');
  64. $banner = $bLen !== false ? $banner2 : $imageUrl.'/'.$banner2 ;
  65. $image2 = $input['index_image_2'];
  66. $iLen = empty($image2) ? 2 : strrpos($image2,'http');
  67. $image = $iLen !== false ? $image2 : $imageUrl.'/'.$image2 ;
  68. $setting = [
  69. 'buy_num_limit' => $input['buy_num_limit_2'] ?? 0,
  70. 'banner' => $banner,
  71. 'show_type' => $this->typeOptionsValue[$input['show_type_2']] ?? $this->typeOptionsValue[1],
  72. 'index_num_limit' => $input['index_num_2'] ?? 6,
  73. 'index_image' => $image,
  74. 'index_url' => $input['index_url_2'] ?? '',
  75. ];
  76. $data = [
  77. 'group_buy' => $setting['buy_num_limit'],
  78. ];
  79. break;
  80. case 'new_product':
  81. $banner3 = $input['banner_3'];
  82. $bLen = empty($banner3) ? 3 : strrpos($banner3,'http');
  83. $banner = $bLen !== false ? $banner3 : $imageUrl.'/'.$banner3 ;
  84. $image3 = $input['index_image_3'];
  85. $iLen = empty($image3) ? 3 : strrpos($image3,'http');
  86. $image = $iLen !== false ? $image3 : $imageUrl.'/'.$image3 ;
  87. $setting = [
  88. 'buy_num_limit' => $input['buy_num_limit_3'] ?? 0,
  89. 'banner' => $banner,
  90. 'show_type' => $this->typeOptionsValue[$input['show_type_3']] ?? $this->typeOptionsValue[1],
  91. 'index_num_limit' => $input['index_num_3'] ?? 6,
  92. 'index_image' => $image,
  93. 'index_url' => $input['index_url_3'] ?? '',
  94. ];
  95. $data = [
  96. 'new_product' => $setting['buy_num_limit'],
  97. ];
  98. break;
  99. default:
  100. return $this->error('请选择正确的首页展示的活动类型!');
  101. break;
  102. }
  103. if($setting['show_type'] == 'image' && empty($setting['index_image'])){
  104. return $this->error('请上传首页大图!');
  105. }else if($setting['show_type'] == 'image' && empty($setting['index_url'])){
  106. return $this->error('请填写跳转链接');
  107. }
  108. $activitySetting[$activitySetting['index_activity_type']] = json_encode($setting);
  109. $res1 = Redis::hmset(RedisKey::ACTIVITY_TYPE_LIMIT_NUMS , $data);
  110. $res2 = Redis::hmset(RedisKey::ACTIVITY_TYPE_SETTING , $activitySetting);
  111. if($res1 && $res2){
  112. return $this->success('修改成功','/goods_activity_setting');
  113. }else{
  114. return $this->error('修改失败');
  115. }
  116. }
  117. /**
  118. * Build a form here.
  119. */
  120. public function form()
  121. {
  122. $data = Redis::hgetall(RedisKey::ACTIVITY_TYPE_LIMIT_NUMS);
  123. $setting = Redis::hgetall(RedisKey::ACTIVITY_TYPE_SETTING);
  124. $indexOptionsKey = array_flip($this->indexOptionsValue);
  125. $typeOptionsKey = array_flip($this->typeOptionsValue);
  126. $limitData = [
  127. 1 => isset($data['flash_sale']) ? $data['flash_sale']:0,
  128. 2 => isset($data['group_buy']) ? $data['group_buy']:0,
  129. 3 => isset($data['new_product']) ? $data['new_product']:0,
  130. ];
  131. $type = $setting['index_activity_type'] ?? 'flash_sale';
  132. $settingData = [
  133. 'index_activity_type' => $indexOptionsKey[$type] ?? 1,
  134. 1 => isset($setting['flash_sale']) ? json_decode($setting['flash_sale'],true) : [],
  135. 2 => isset($setting['group_buy']) ? json_decode($setting['group_buy'],true) : [],
  136. 3 => isset($setting['new_product']) ? json_decode($setting['new_product'],true) : [],
  137. ];
  138. $this->select('index_activity_type','首页展示的活动类型')
  139. ->when([1],function(Form $form) use($limitData,$settingData,$typeOptionsKey){
  140. $buyNumLimit = $limitData[1] ?? 0;
  141. $sData = $settingData[1] ?? [];
  142. $showType = $sData['show_type'] ?? 'list';
  143. $form->number('buy_num_limit_1','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  144. $form->image('banner_1','banner')->required()->autoUpload()
  145. ->customFormat(function() use($sData){
  146. return [$sData['banner'] ?? ''];
  147. })->width(3);
  148. $form->radio('show_type_1','展示形式')
  149. ->when([1],function(Form $form) use($sData){
  150. $form->number('index_num_1','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
  151. })
  152. ->when([2],function(Form $form) use($sData){
  153. $form->image('index_image_1','首页大图')->autoUpload()
  154. ->customFormat(function() use($sData){
  155. return [$sData['index_image'] ?? ''];
  156. })->width(3);
  157. $form->text('index_url_1','跳转链接')->value($sData['index_url'] ?? '')->width(4);
  158. })
  159. ->options($form->typeOptions)
  160. ->value($typeOptionsKey[$showType] ?? 1)
  161. ->default(1);
  162. })
  163. ->when([2],function(Form $form) use($limitData,$settingData,$typeOptionsKey){
  164. $buyNumLimit = $limitData[2] ?? 0;
  165. $sData = $settingData[2] ?? [];
  166. $showType = $sData['show_type'] ?? 'list';
  167. $form->number('buy_num_limit_2','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  168. $form->image('banner_2','banner')->required()->autoUpload()
  169. ->customFormat(function() use($sData){
  170. return [$sData['banner'] ?? ''];
  171. })->width(3);
  172. $form->radio('show_type_2','展示形式')
  173. ->when([1],function(Form $form) use($sData){
  174. $form->number('index_num_2','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
  175. })
  176. ->when([2],function(Form $form) use($sData){
  177. $form->image('index_image_2','首页大图')->autoUpload()
  178. ->customFormat(function() use($sData){
  179. return [$sData['index_image'] ?? ''];
  180. })->width(3);
  181. $form->text('index_url_2','跳转链接')->value($sData['index_url'] ?? '')->width(4);
  182. })
  183. ->options($form->typeOptions)
  184. ->value($typeOptionsKey[$showType] ?? 1)
  185. ->default(1);
  186. })
  187. ->when([3],function(Form $form) use($limitData,$settingData,$typeOptionsKey){
  188. $buyNumLimit = $limitData[3] ?? 0;
  189. $sData = $settingData[3] ?? [];
  190. $showType = $sData['show_type'] ?? 'list';
  191. $form->number('buy_num_limit_3','购买数量限制')->attribute('min', 0)->default(1)->value($buyNumLimit)->width(6)->help('同一活动类型的商品,单笔订单可购买商品个数');
  192. $form->image('banner_3','banner')->required()->autoUpload()
  193. ->customFormat(function() use($sData){
  194. return [$sData['banner'] ?? ''];
  195. })->width(3);
  196. $form->radio('show_type_3','展示形式')
  197. ->when([1],function(Form $form) use($sData){
  198. $form->number('index_num_3','首页显示数量')->value($sData['index_num'] ?? 6)->min(1)->default(6)->width(6);
  199. })
  200. ->when([2],function(Form $form) use($sData){
  201. $form->image('index_image_3','首页大图')->autoUpload()
  202. ->customFormat(function() use($sData){
  203. return [$sData['index_image'] ?? ''];
  204. })->width(3);
  205. $form->text('index_url_3','跳转链接')->value($sData['index_url'] ?? '')->width(4);
  206. })
  207. ->options($form->typeOptions)
  208. ->value($typeOptionsKey[$showType] ?? 1)
  209. ->default(1);
  210. })
  211. ->options($this->indexOptions)->value($settingData['index_activity_type'] ?? 1)->default(1)->width(4);
  212. }
  213. /**
  214. * The data of the form.
  215. *
  216. * @return array
  217. */
  218. public function default()
  219. {
  220. return [];
  221. }
  222. }