| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- namespace App\Module\Activity\AdminControllers\Helper;
- use App\Module\Activity\Enums\ACTIVITY_STATUS;
- use App\Module\Activity\Enums\ACTIVITY_TYPE;
- use App\Module\Activity\Enums\CONDITION_TYPE;
- use App\Module\Activity\Enums\PARTICIPATION_STATUS;
- use App\Module\Activity\Enums\REWARD_STATUS;
- use App\Module\Game\Enums\REWARD_TYPE;
- use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
- use App\Module\Game\AdminControllers\LazyRenderable\RewardGroupLazyRenderable;
- use Dcat\Admin\Form;
- use Dcat\Admin\Form\Field;
- /**
- * 表单辅助特性
- *
- * 提供活动模块后台控制器的表单构建功能的具体实现
- */
- trait FormHelperTrait
- {
- /**
- * 添加活动状态选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectActivityStatus(string $field = 'status', string $label = '活动状态'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(ACTIVITY_STATUS::getAll())
- ->default(ACTIVITY_STATUS::NOT_STARTED->value)
- ->required();
- }
- /**
- * 添加活动类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectActivityType(string $field = 'type', string $label = '活动类型'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(ACTIVITY_TYPE::getAll())
- ->required();
- }
- /**
- * 添加条件类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectConditionType(string $field = 'condition_type', string $label = '条件类型'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(CONDITION_TYPE::getAll())
- ->required();
- }
- /**
- * 添加参与状态选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectParticipationStatus(string $field = 'completion_status', string $label = '参与状态'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(PARTICIPATION_STATUS::getAll())
- ->default(PARTICIPATION_STATUS::IN_PROGRESS->value)
- ->required();
- }
- /**
- * 添加奖励状态选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectRewardStatus(string $field = 'reward_status', string $label = '奖励状态'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(REWARD_STATUS::getAll())
- ->default(REWARD_STATUS::NOT_CLAIMED->value)
- ->required();
- }
- /**
- * 添加奖励类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectRewardType(string $field = 'reward_type', string $label = '奖励类型'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(REWARD_TYPE::getAll())
- ->required();
- }
- /**
- * 添加奖励来源类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectRewardSourceType(string $field = 'source_type', string $label = '奖励来源'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(REWARD_SOURCE_TYPE::getValueDescription())
- ->default(REWARD_SOURCE_TYPE::ACTIVITY->value)
- ->required();
- }
- /**
- * 添加活动时间范围选择
- *
- * @param string $startField 开始时间字段
- * @param string $endField 结束时间字段
- * @param string $label 标签名
- * @return Field\DateRange
- */
- public function dateTimeRangeActivity(string $startField = 'start_time', string $endField = 'end_time', string $label = '活动时间'): Field\DateRange
- {
- return $this->form->dateRange($startField, $endField, $label)
- ->datetime()
- ->required();
- }
- /**
- * 添加活动配置JSON编辑器
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Editor
- */
- public function jsonEditorActivityConfig(string $field = 'config', string $label = '活动配置'): Field\Editor
- {
- return $this->form->editor($field, $label)
- ->help('请输入有效的JSON格式配置');
- }
- /**
- * 添加活动条件JSON编辑器
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Editor
- */
- public function jsonEditorActivityConditions(string $field = 'conditions', string $label = '活动条件'): Field\Editor
- {
- return $this->form->editor($field, $label)
- ->help('请输入有效的JSON格式条件配置');
- }
- /**
- * 添加活动奖励JSON编辑器
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Editor
- */
- public function jsonEditorActivityRewards(string $field = 'rewards', string $label = '活动奖励'): Field\Editor
- {
- return $this->form->editor($field, $label)
- ->help('请输入有效的JSON格式奖励配置');
- }
- /**
- * 添加活动条件表格
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Table
- */
- public function tableActivityConditions(string $field = 'conditions', string $label = '活动条件'): Field\Table
- {
- return $this->form->table($field, $label, function (Field\Table\InlineTable $table) {
- $table->select('type', '条件类型')
- ->options(CONDITION_TYPE::getAll())
- ->required();
- $table->text('target_id', '目标ID')
- ->required();
- $table->number('target_value', '目标值')
- ->min(0)
- ->required();
- $table->text('description', '描述');
- });
- }
- /**
- * 添加活动奖励表格
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Table
- */
- public function tableActivityRewards(string $field = 'rewards', string $label = '活动奖励'): Field\Table
- {
- return $this->form->table($field, $label, function (Field\Table\InlineTable $table) {
- $table->select('type', '奖励类型')
- ->options(REWARD_TYPE::getAll())
- ->required();
- $table->text('reward_id', '奖励ID')
- ->required();
- $table->number('quantity', '数量')
- ->min(1)
- ->default(1)
- ->required();
- $table->text('description', '描述');
- });
- }
- /**
- * 添加奖励组选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\SelectTable
- */
- public function selectRewardGroup(string $field = 'reward_group_id', string $label = '奖励组'): Field\SelectTable
- {
- $table = RewardGroupLazyRenderable::make();
- return $this->form->selectTable($field, $label)
- ->from($table)
- ->model($table->getModel(), 'id', 'name');
- }
- /**
- * 添加活动名称输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Text
- */
- public function textActivityName(string $field = 'name', string $label = '活动名称'): Field\Text
- {
- return $this->form->text($field, $label)
- ->required()
- ->rules('required|max:50');
- }
- /**
- * 添加活动描述输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Textarea
- */
- public function textareaActivityDescription(string $field = 'description', string $label = '活动描述'): Field\Textarea
- {
- return $this->form->textarea($field, $label)
- ->rows(3)
- ->rules('max:200');
- }
- /**
- * 添加活动图标上传
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Image
- */
- public function imageActivityIcon(string $field = 'icon', string $label = '活动图标'): Field\Image
- {
- return $this->form->image($field, $label)
- ->autoUpload()
- ->uniqueName()
- ->help('建议尺寸: 200x200px');
- }
- /**
- * 添加活动背景图上传
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Image
- */
- public function imageActivityBackground(string $field = 'background', string $label = '活动背景图'): Field\Image
- {
- return $this->form->image($field, $label)
- ->autoUpload()
- ->uniqueName()
- ->help('建议尺寸: 1920x1080px');
- }
- /**
- * 添加活动排序输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Number
- */
- public function numberActivitySort(string $field = 'sort', string $label = '排序'): Field\Number
- {
- return $this->form->number($field, $label)
- ->default(0)
- ->help('数字越小越靠前');
- }
- /**
- * 添加活动是否显示开关
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Switch
- */
- public function switchActivityVisible(string $field = 'is_visible', string $label = '是否显示'): Field\Switch
- {
- return $this->form->switch($field, $label)
- ->default(true);
- }
- }
|