| 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 CloseActivityAction extends RowAction
- {
- /**
- * 操作按钮标题
- *
- * @var string
- */
- protected $title = '<i class="fa fa-times"></i> 关闭';
- /**
- * 判断是否允许显示此操作
- *
- * @return bool
- */
- public function allowed()
- {
- return $this->row->status === ACTIVITY_STATUS::ENDED;
- }
- /**
- * 处理请求
- *
- * @param Request $request
- * @return mixed
- */
- public function handle(Request $request)
- {
- try {
- $id = $this->getKey();
-
- $activity = ActivityConfig::findOrFail($id);
-
- if ($activity->status !== ACTIVITY_STATUS::ENDED) {
- return $this->response()->error('只有已结束的活动才能关闭');
- }
-
- $activity->status = ACTIVITY_STATUS::CLOSED;
- $activity->save();
-
- // 触发活动状态变更事件
- event(new \App\Module\Activity\Events\ActivityStatusChangedEvent(
- $activity->id,
- ACTIVITY_STATUS::ENDED,
- ACTIVITY_STATUS::CLOSED
- ));
-
- return $this->response()
- ->success("活动 [{$activity->name}] 已关闭")
- ->refresh();
- } catch (\Exception $e) {
- return $this->response()
- ->error('操作失败: ' . $e->getMessage());
- }
- }
- /**
- * 确认信息
- *
- * @return array|string|void
- */
- public function confirm()
- {
- return ['确定要关闭此活动吗?', '关闭后活动将完全不可见,用户无法查看活动信息'];
- }
- }
|