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

58 lines
1.6 KiB

  1. <?php
  2. namespace App\Admin\Extensions\Tools;
  3. use Dcat\Admin\Grid\BatchAction;
  4. use Illuminate\Http\Request;
  5. use App\Models\v3\GoodsActivity as GoodsActivityModel;
  6. class GoodsActivityOnSale extends BatchAction
  7. {
  8. protected $action;
  9. /**
  10. * 批量上下架活动商品
  11. */
  12. // 注意action的构造方法参数一定要给默认值
  13. public function __construct($title = null, $action = 1)
  14. {
  15. $this->title = $title;
  16. $this->action = $action;
  17. }
  18. // 确认弹窗信息
  19. public function confirm()
  20. {
  21. return '您确定要'.$this->title.'已选中的商品吗?';
  22. }
  23. // 处理请求
  24. public function handle(Request $request)
  25. {
  26. // 获取选中的文章ID数组
  27. $keys = $this->getKey();
  28. if(empty($keys) || !is_array($keys)){
  29. return $this->response()->error('请选择商品!');
  30. }
  31. // 获取请求参数
  32. $action = $request->get('action');
  33. $title = $request->get('title');
  34. $actionWhere = $action == 1 ? 0 : 1 ;
  35. $title = $this->title ? $this->title : $title;
  36. // 下架处理
  37. $res = GoodsActivityModel::whereIn('id',$keys)->where('on_sale',$actionWhere)->update(['on_sale'=>$action]);
  38. if($res){
  39. return $this->response()->success($title.'成功')->refresh();
  40. }else{
  41. return $this->response()->error($title.'失败或已'.$title.'!');
  42. }
  43. }
  44. // 设置请求参数
  45. public function parameters()
  46. {
  47. return [
  48. 'action' => $this->action,
  49. 'title' => $this->title,
  50. ];
  51. }
  52. }