|
|
@@ -0,0 +1,337 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\Activity\AdminControllers;
|
|
|
+
|
|
|
+use App\Module\Activity\Enums\ACTIVITY_STATUS;
|
|
|
+use App\Module\Activity\Enums\ACTIVITY_TYPE;
|
|
|
+use App\Module\Activity\Models\ActivityConfig;
|
|
|
+use App\Module\Activity\Repositorys\ActivityConfigRepository;
|
|
|
+use App\Module\Game\Services\RewardService;
|
|
|
+use Dcat\Admin\Form;
|
|
|
+use Dcat\Admin\Grid;
|
|
|
+use Dcat\Admin\Show;
|
|
|
+use Dcat\Admin\Http\Controllers\AdminController;
|
|
|
+use Dcat\Admin\Layout\Content;
|
|
|
+use Dcat\Admin\Widgets\Card;
|
|
|
+use Dcat\Admin\Widgets\Table;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 活动管理控制器
|
|
|
+ */
|
|
|
+class ActivityController extends AdminController
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 标题
|
|
|
+ *
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ protected function title()
|
|
|
+ {
|
|
|
+ return '活动管理';
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建表格
|
|
|
+ *
|
|
|
+ * @return Grid
|
|
|
+ */
|
|
|
+ protected function grid()
|
|
|
+ {
|
|
|
+ return Grid::make(new ActivityConfigRepository(), function (Grid $grid) {
|
|
|
+ $grid->column('id', 'ID')->sortable();
|
|
|
+ $grid->column('name', '活动名称');
|
|
|
+ $grid->column('type', '活动类型')->display(function ($type) {
|
|
|
+ return ACTIVITY_TYPE::getName($type);
|
|
|
+ });
|
|
|
+ $grid->column('start_time', '开始时间')->sortable();
|
|
|
+ $grid->column('end_time', '结束时间')->sortable();
|
|
|
+ $grid->column('status', '状态')->display(function ($status) {
|
|
|
+ return ACTIVITY_STATUS::getName($status);
|
|
|
+ })->label([
|
|
|
+ ACTIVITY_STATUS::NOT_STARTED => 'default',
|
|
|
+ ACTIVITY_STATUS::IN_PROGRESS => 'success',
|
|
|
+ ACTIVITY_STATUS::ENDED => 'warning',
|
|
|
+ ACTIVITY_STATUS::CLOSED => 'danger',
|
|
|
+ ]);
|
|
|
+ $grid->column('display_order', '显示顺序')->sortable();
|
|
|
+ $grid->column('reward_group_id', '奖励组ID');
|
|
|
+ $grid->column('reward_group_code', '奖励组编码');
|
|
|
+ $grid->column('created_at', '创建时间');
|
|
|
+ $grid->column('updated_at', '更新时间');
|
|
|
+
|
|
|
+ $grid->filter(function (Grid\Filter $filter) {
|
|
|
+ $filter->equal('id', 'ID');
|
|
|
+ $filter->like('name', '活动名称');
|
|
|
+ $filter->equal('type', '活动类型')->select(ACTIVITY_TYPE::getAll());
|
|
|
+ $filter->equal('status', '状态')->select(ACTIVITY_STATUS::getAll());
|
|
|
+ $filter->between('start_time', '开始时间')->datetime();
|
|
|
+ $filter->between('end_time', '结束时间')->datetime();
|
|
|
+ });
|
|
|
+
|
|
|
+ $grid->actions(function (Grid\Displayers\Actions $actions) {
|
|
|
+ // 添加复制按钮
|
|
|
+ $actions->append('<a href="' . admin_url('activity/activities/' . $actions->getKey() . '/duplicate') . '"><i class="fa fa-copy"></i> 复制</a>');
|
|
|
+
|
|
|
+ // 添加更新状态按钮
|
|
|
+ if ($actions->row->status === ACTIVITY_STATUS::NOT_STARTED) {
|
|
|
+ $actions->append('<a href="' . admin_url('activity/activities/' . $actions->getKey() . '/start') . '"><i class="fa fa-play"></i> 开始</a>');
|
|
|
+ } elseif ($actions->row->status === ACTIVITY_STATUS::IN_PROGRESS) {
|
|
|
+ $actions->append('<a href="' . admin_url('activity/activities/' . $actions->getKey() . '/end') . '"><i class="fa fa-stop"></i> 结束</a>');
|
|
|
+ } elseif ($actions->row->status === ACTIVITY_STATUS::ENDED) {
|
|
|
+ $actions->append('<a href="' . admin_url('activity/activities/' . $actions->getKey() . '/close') . '"><i class="fa fa-times"></i> 关闭</a>');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建详情页
|
|
|
+ *
|
|
|
+ * @param mixed $id
|
|
|
+ * @return Show
|
|
|
+ */
|
|
|
+ protected function detail($id)
|
|
|
+ {
|
|
|
+ return Show::make($id, new ActivityConfigRepository(), function (Show $show) {
|
|
|
+ $show->field('id', 'ID');
|
|
|
+ $show->field('name', '活动名称');
|
|
|
+ $show->field('type', '活动类型')->as(function ($type) {
|
|
|
+ return ACTIVITY_TYPE::getName($type);
|
|
|
+ });
|
|
|
+ $show->field('description', '活动描述');
|
|
|
+ $show->field('start_time', '开始时间');
|
|
|
+ $show->field('end_time', '结束时间');
|
|
|
+ $show->field('status', '状态')->as(function ($status) {
|
|
|
+ return ACTIVITY_STATUS::getName($status);
|
|
|
+ });
|
|
|
+ $show->field('display_order', '显示顺序');
|
|
|
+ $show->field('icon', '活动图标')->image();
|
|
|
+ $show->field('banner', '活动横幅')->image();
|
|
|
+ $show->field('reward_group_id', '奖励组ID');
|
|
|
+ $show->field('reward_group_code', '奖励组编码');
|
|
|
+ $show->field('config_params', '配置参数')->json();
|
|
|
+ $show->field('created_at', '创建时间');
|
|
|
+ $show->field('updated_at', '更新时间');
|
|
|
+
|
|
|
+ // 显示活动条件
|
|
|
+ $show->divider();
|
|
|
+ $show->field('条件')->as(function () {
|
|
|
+ $conditions = $this->conditions()->get();
|
|
|
+ if ($conditions->isEmpty()) {
|
|
|
+ return '无条件';
|
|
|
+ }
|
|
|
+
|
|
|
+ $headers = ['ID', '条件类型', '参与条件', '完成条件', '显示顺序'];
|
|
|
+ $rows = [];
|
|
|
+
|
|
|
+ foreach ($conditions as $condition) {
|
|
|
+ $rows[] = [
|
|
|
+ $condition->id,
|
|
|
+ $condition->getConditionTypeName(),
|
|
|
+ $condition->is_participation_condition ? '是' : '否',
|
|
|
+ $condition->is_completion_condition ? '是' : '否',
|
|
|
+ $condition->display_order
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return Card::make(
|
|
|
+ Table::make($headers, $rows)
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ // 显示奖励信息
|
|
|
+ $show->divider();
|
|
|
+ $show->field('奖励信息')->as(function () {
|
|
|
+ if (!$this->reward_group_id && !$this->reward_group_code) {
|
|
|
+ return '未配置奖励';
|
|
|
+ }
|
|
|
+
|
|
|
+ $rewardInfo = '奖励组ID: ' . ($this->reward_group_id ?: '无') . '<br>';
|
|
|
+ $rewardInfo .= '奖励组编码: ' . ($this->reward_group_code ?: '无') . '<br>';
|
|
|
+
|
|
|
+ // 这里可以调用奖励组服务获取更详细的奖励信息
|
|
|
+ // 由于没有实际的奖励组服务,这里只返回基本信息
|
|
|
+ return $rewardInfo;
|
|
|
+ })->unescape();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建表单
|
|
|
+ *
|
|
|
+ * @return Form
|
|
|
+ */
|
|
|
+ protected function form()
|
|
|
+ {
|
|
|
+ return Form::make(new ActivityConfigRepository(), function (Form $form) {
|
|
|
+ $form->display('id', 'ID');
|
|
|
+ $form->text('name', '活动名称')->required();
|
|
|
+ $form->select('type', '活动类型')->options(ACTIVITY_TYPE::getAll())->required();
|
|
|
+ $form->textarea('description', '活动描述');
|
|
|
+ $form->datetime('start_time', '开始时间')->required();
|
|
|
+ $form->datetime('end_time', '结束时间')->required();
|
|
|
+ $form->select('status', '状态')->options(ACTIVITY_STATUS::getAll())->default(ACTIVITY_STATUS::NOT_STARTED);
|
|
|
+ $form->number('display_order', '显示顺序')->default(0);
|
|
|
+ $form->image('icon', '活动图标')->autoUpload();
|
|
|
+ $form->image('banner', '活动横幅')->autoUpload();
|
|
|
+
|
|
|
+ // 奖励组选择
|
|
|
+ $form->divider('奖励配置');
|
|
|
+ $form->number('reward_group_id', '奖励组ID')->help('填写奖励组ID或奖励组编码,两者选其一即可');
|
|
|
+ $form->text('reward_group_code', '奖励组编码')->help('填写奖励组ID或奖励组编码,两者选其一即可');
|
|
|
+
|
|
|
+ // 活动条件
|
|
|
+ $form->divider('活动条件');
|
|
|
+ $form->hasMany('conditions', '条件', function (Form\NestedForm $form) {
|
|
|
+ $form->select('condition_type', '条件类型')->options(\App\Module\Activity\Enums\CONDITION_TYPE::getAll())->required();
|
|
|
+ $form->textarea('condition_params', '条件参数')->help('JSON格式,根据条件类型填写不同的参数');
|
|
|
+ $form->switch('is_participation_condition', '参与条件')->default(false);
|
|
|
+ $form->switch('is_completion_condition', '完成条件')->default(false);
|
|
|
+ $form->number('display_order', '显示顺序')->default(0);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 高级配置
|
|
|
+ $form->divider('高级配置');
|
|
|
+ $form->textarea('config_params', '配置参数')->help('JSON格式,用于存储活动特定的配置参数');
|
|
|
+
|
|
|
+ $form->display('created_at', '创建时间');
|
|
|
+ $form->display('updated_at', '更新时间');
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 复制活动
|
|
|
+ *
|
|
|
+ * @param int $id
|
|
|
+ * @param Content $content
|
|
|
+ * @return Content
|
|
|
+ */
|
|
|
+ public function duplicate($id, Content $content)
|
|
|
+ {
|
|
|
+ $activity = ActivityConfig::with('conditions')->findOrFail($id);
|
|
|
+
|
|
|
+ // 复制活动基本信息
|
|
|
+ $newActivity = $activity->replicate();
|
|
|
+ $newActivity->name = $activity->name . ' (复制)';
|
|
|
+ $newActivity->status = ACTIVITY_STATUS::NOT_STARTED;
|
|
|
+ $newActivity->save();
|
|
|
+
|
|
|
+ // 复制活动条件
|
|
|
+ foreach ($activity->conditions as $condition) {
|
|
|
+ $newCondition = $condition->replicate();
|
|
|
+ $newCondition->activity_id = $newActivity->id;
|
|
|
+ $newCondition->save();
|
|
|
+ }
|
|
|
+
|
|
|
+ return $content
|
|
|
+ ->title($this->title())
|
|
|
+ ->description('复制活动')
|
|
|
+ ->body(admin_success('复制成功', "已成功复制活动 [{$newActivity->name}]"))
|
|
|
+ ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('activity/activities') . "'; }, 2000);</script>");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开始活动
|
|
|
+ *
|
|
|
+ * @param int $id
|
|
|
+ * @param Content $content
|
|
|
+ * @return Content
|
|
|
+ */
|
|
|
+ public function start($id, Content $content)
|
|
|
+ {
|
|
|
+ $activity = ActivityConfig::findOrFail($id);
|
|
|
+
|
|
|
+ if ($activity->status !== ACTIVITY_STATUS::NOT_STARTED) {
|
|
|
+ return $content
|
|
|
+ ->title($this->title())
|
|
|
+ ->description('开始活动')
|
|
|
+ ->body(admin_error('操作失败', '只有未开始的活动才能开始'));
|
|
|
+ }
|
|
|
+
|
|
|
+ $activity->status = ACTIVITY_STATUS::IN_PROGRESS;
|
|
|
+ $activity->save();
|
|
|
+
|
|
|
+ // 触发活动状态变更事件
|
|
|
+ event(new \App\Module\Activity\Events\ActivityStatusChangedEvent(
|
|
|
+ $activity->id,
|
|
|
+ ACTIVITY_STATUS::NOT_STARTED,
|
|
|
+ ACTIVITY_STATUS::IN_PROGRESS
|
|
|
+ ));
|
|
|
+
|
|
|
+ return $content
|
|
|
+ ->title($this->title())
|
|
|
+ ->description('开始活动')
|
|
|
+ ->body(admin_success('操作成功', "活动 [{$activity->name}] 已开始"))
|
|
|
+ ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('activity/activities') . "'; }, 2000);</script>");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 结束活动
|
|
|
+ *
|
|
|
+ * @param int $id
|
|
|
+ * @param Content $content
|
|
|
+ * @return Content
|
|
|
+ */
|
|
|
+ public function end($id, Content $content)
|
|
|
+ {
|
|
|
+ $activity = ActivityConfig::findOrFail($id);
|
|
|
+
|
|
|
+ if ($activity->status !== ACTIVITY_STATUS::IN_PROGRESS) {
|
|
|
+ return $content
|
|
|
+ ->title($this->title())
|
|
|
+ ->description('结束活动')
|
|
|
+ ->body(admin_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 $content
|
|
|
+ ->title($this->title())
|
|
|
+ ->description('结束活动')
|
|
|
+ ->body(admin_success('操作成功', "活动 [{$activity->name}] 已结束"))
|
|
|
+ ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('activity/activities') . "'; }, 2000);</script>");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关闭活动
|
|
|
+ *
|
|
|
+ * @param int $id
|
|
|
+ * @param Content $content
|
|
|
+ * @return Content
|
|
|
+ */
|
|
|
+ public function close($id, Content $content)
|
|
|
+ {
|
|
|
+ $activity = ActivityConfig::findOrFail($id);
|
|
|
+
|
|
|
+ if ($activity->status !== ACTIVITY_STATUS::ENDED) {
|
|
|
+ return $content
|
|
|
+ ->title($this->title())
|
|
|
+ ->description('关闭活动')
|
|
|
+ ->body(admin_error('操作失败', '只有已结束的活动才能关闭'));
|
|
|
+ }
|
|
|
+
|
|
|
+ $activity->status = ACTIVITY_STATUS::CLOSED;
|
|
|
+ $activity->save();
|
|
|
+
|
|
|
+ // 触发活动状态变更事件
|
|
|
+ event(new \App\Module\Activity\Events\ActivityStatusChangedEvent(
|
|
|
+ $activity->id,
|
|
|
+ ACTIVITY_STATUS::ENDED,
|
|
|
+ ACTIVITY_STATUS::CLOSED
|
|
|
+ ));
|
|
|
+
|
|
|
+ return $content
|
|
|
+ ->title($this->title())
|
|
|
+ ->description('关闭活动')
|
|
|
+ ->body(admin_success('操作成功', "活动 [{$activity->name}] 已关闭"))
|
|
|
+ ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('activity/activities') . "'; }, 2000);</script>");
|
|
|
+ }
|
|
|
+}
|