| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Module\Activity\AdminControllers\Actions;
- use App\Module\Activity\Enums\ACTIVITY_STATUS;
- use App\Module\Activity\Models\ActivityConfig;
- use Dcat\Admin\Grid\RowAction;
- use Illuminate\Http\Request;
- /**
- * 结束活动操作
- */
- class EndActivityAction extends RowAction
- {
- /**
- * 操作按钮标题
- *
- * @var string
- */
- protected $title = '<i class="fa fa-stop"></i> 结束';
- /**
- * 判断是否允许显示此操作
- *
- * @return bool
- */
- public function allowed()
- {
- return $this->row->status === ACTIVITY_STATUS::IN_PROGRESS;
- }
- /**
- * 处理请求
- *
- * @param Request $request
- * @return mixed
- */
- public function handle(Request $request)
- {
- try {
- $id = $this->getKey();
-
- $activity = ActivityConfig::findOrFail($id);
-
- if ($activity->status !== ACTIVITY_STATUS::IN_PROGRESS) {
- return $this->response()->error('只有进行中的活动才能结束');
- }
-
- $activity->status = ACTIVITY_STATUS::ENDED;
- $activity->save();
-
- // 触发活动状态变更事件
- event(new \App\Module\Activity\Events\ActivityStatusChangedEvent(
- $activity->id,
- ACTIVITY_STATUS::IN_PROGRESS,
- ACTIVITY_STATUS::ENDED
- ));
-
- return $this->response()
- ->success("活动 [{$activity->name}] 已结束")
- ->refresh();
- } catch (\Exception $e) {
- return $this->response()
- ->error('操作失败: ' . $e->getMessage());
- }
- }
- /**
- * 确认信息
- *
- * @return array|string|void
- */
- public function confirm()
- {
- return ['确定要结束此活动吗?', '结束后用户将无法继续参与活动,但仍可查看活动信息'];
- }
- }
|