StartActivityAction.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Module\Activity\AdminControllers\Actions;
  3. use App\Module\Activity\Enums\ACTIVITY_STATUS;
  4. use App\Module\Activity\Models\ActivityConfig;
  5. use Dcat\Admin\Grid\RowAction;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 开始活动操作
  9. */
  10. class StartActivityAction extends RowAction
  11. {
  12. /**
  13. * 操作按钮标题
  14. *
  15. * @var string
  16. */
  17. protected $title = '<i class="fa fa-play"></i> 开始';
  18. /**
  19. * 判断是否允许显示此操作
  20. *
  21. * @return bool
  22. */
  23. public function allowed()
  24. {
  25. return $this->row->status === ACTIVITY_STATUS::NOT_STARTED;
  26. }
  27. /**
  28. * 处理请求
  29. *
  30. * @param Request $request
  31. * @return mixed
  32. */
  33. public function handle(Request $request)
  34. {
  35. try {
  36. $id = $this->getKey();
  37. $activity = ActivityConfig::findOrFail($id);
  38. if ($activity->status !== ACTIVITY_STATUS::NOT_STARTED) {
  39. return $this->response()->error('只有未开始的活动才能开始');
  40. }
  41. $activity->status = ACTIVITY_STATUS::IN_PROGRESS;
  42. $activity->save();
  43. // 触发活动状态变更事件
  44. event(new \App\Module\Activity\Events\ActivityStatusChangedEvent(
  45. $activity->id,
  46. ACTIVITY_STATUS::NOT_STARTED,
  47. ACTIVITY_STATUS::IN_PROGRESS
  48. ));
  49. return $this->response()
  50. ->success("活动 [{$activity->name}] 已开始")
  51. ->refresh();
  52. } catch (\Exception $e) {
  53. return $this->response()
  54. ->error('操作失败: ' . $e->getMessage());
  55. }
  56. }
  57. /**
  58. * 确认信息
  59. *
  60. * @return array|string|void
  61. */
  62. public function confirm()
  63. {
  64. return ['确定要开始此活动吗?', '开始后活动将对用户可见并可参与'];
  65. }
  66. }