| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <?php
- namespace App\Module\Task\AdminControllers\Helper;
- use App\Module\Task\Enums\RESET_TYPE;
- use App\Module\Task\Enums\REWARD_TYPE;
- use App\Module\Task\Enums\TASK_STATUS;
- use App\Module\Task\Enums\TASK_TYPE;
- use App\Module\Task\Models\Task;
- use App\Module\Task\Models\TaskCategory;
- use App\Module\Task\Models\TaskCondition;
- use Dcat\Admin\Form;
- /**
- * 表单辅助特性
- *
- * 提供任务模块后台控制器的表单构建功能
- */
- trait FormHelperTrait
- {
- /**
- * 任务分类选择
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Select
- */
- public function selectTaskCategory(string $field = 'category_id', string $label = '任务分类')
- {
- return $this->form->select($field, $label)->options(function () {
- return TaskCategory::pluck('name', 'id');
- })->required();
- }
-
- /**
- * 任务类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Select
- */
- public function selectTaskType(string $field = 'type', string $label = '任务类型')
- {
- return $this->form->select($field, $label)->options(TASK_TYPE::getOptions())->required();
- }
-
- /**
- * 重置类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Select
- */
- public function selectResetType(string $field = 'reset_type', string $label = '重置类型')
- {
- return $this->form->select($field, $label)->options(RESET_TYPE::getOptions())->default('none');
- }
-
- /**
- * 任务状态选择
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Select
- */
- public function selectTaskStatus(string $field = 'status', string $label = '任务状态')
- {
- return $this->form->select($field, $label)->options(TASK_STATUS::getOptions())->default(TASK_STATUS::NOT_ACCEPTED->value);
- }
-
- /**
- * 奖励类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Select
- */
- public function selectRewardType(string $field = 'reward_type', string $label = '奖励类型')
- {
- return $this->form->select($field, $label)->options(REWARD_TYPE::getOptions())->required();
- }
-
- /**
- * 条件类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Select
- */
- public function selectConditionType(string $field = 'condition_type', string $label = '条件类型')
- {
- return $this->form->select($field, $label)->options([
- 'prerequisite' => '前置条件',
- 'progress' => '进度条件',
- ])->default('progress')->required();
- }
-
- /**
- * 任务选择
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Select
- */
- public function selectTask(string $field = 'task_id', string $label = '任务')
- {
- return $this->form->select($field, $label)->options(function () {
- return Task::pluck('name', 'id');
- })->required();
- }
-
- /**
- * 条件选择
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Select
- */
- public function selectCondition(string $field = 'condition_id', string $label = '条件')
- {
- return $this->form->select($field, $label)->options(function () {
- return TaskCondition::pluck('name', 'id');
- })->required();
- }
-
- /**
- * 前置任务选择
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\MultipleSelect
- */
- public function multipleSelectPrerequisiteTasks(string $field = 'prerequisite_tasks', string $label = '前置任务')
- {
- return $this->form->multipleSelect($field, $label)->options(function () {
- return Task::pluck('name', 'id');
- })->help('选择完成后才能接取此任务的前置任务');
- }
-
- /**
- * 是否激活开关
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Switch
- */
- public function switchIsActive(string $field = 'is_active', string $label = '是否激活')
- {
- return $this->form->switch($field, $label)->default(1);
- }
-
- /**
- * 是否必要条件开关
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Switch
- */
- public function switchIsRequired(string $field = 'is_required', string $label = '是否必要条件')
- {
- return $this->form->switch($field, $label)->default(1)->help('是否为必须完成的条件');
- }
-
- /**
- * 排序权重
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Number
- */
- public function numberSortOrder(string $field = 'sort_order', string $label = '排序权重')
- {
- return $this->form->number($field, $label)->default(0)->help('数值越大越靠前');
- }
-
- /**
- * 所需等级
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Number
- */
- public function numberLevelRequired(string $field = 'level_required', string $label = '所需等级')
- {
- return $this->form->number($field, $label)->default(1)->min(1)->help('接取任务所需的最低等级');
- }
-
- /**
- * 时间限制
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Number
- */
- public function numberTimeLimit(string $field = 'time_limit', string $label = '时间限制(秒)')
- {
- return $this->form->number($field, $label)->help('任务完成的时间限制,为空表示无限制');
- }
-
- /**
- * 最大完成次数
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Number
- */
- public function numberMaxCompletions(string $field = 'max_completions', string $label = '最大完成次数')
- {
- return $this->form->number($field, $label)->help('任务可完成的最大次数,为空表示无限制');
- }
-
- /**
- * 目标值
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Number
- */
- public function numberTargetValue(string $field = 'target_value', string $label = '目标值')
- {
- return $this->form->number($field, $label)->default(1)->min(1)->required()->help('完成条件所需的目标值');
- }
-
- /**
- * 奖励数量
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Number
- */
- public function numberQuantity(string $field = 'quantity', string $label = '奖励数量')
- {
- return $this->form->number($field, $label)->default(1)->min(1)->required();
- }
-
- /**
- * 进度值
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Number
- */
- public function numberProgress(string $field = 'progress', string $label = '进度')
- {
- return $this->form->number($field, $label)->default(0)->min(0)->max(100)->help('任务完成进度(0-100)');
- }
-
- /**
- * JSON字段
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @param string $help 帮助文本
- * @return \Dcat\Admin\Form\Field\Textarea
- */
- public function json(string $field, string $label, string $help = '')
- {
- return $this->form->textarea($field, $label)
- ->rows(3)
- ->help($help ?: "{$label},JSON格式,例如:{\"key\": \"value\"}")
- ->rules('nullable|json');
- }
-
- /**
- * 任务奖励嵌套表单
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\NestedForm
- */
- public function hasRewards(string $field = 'rewards', string $label = '任务奖励')
- {
- return $this->form->hasMany($field, $label, function (Form\NestedForm $form) {
- $form->select('reward_type', '奖励类型')->options(REWARD_TYPE::getOptions())->required();
- $form->text('reward_param1', '奖励参数1')->required()->help('如物品类型、货币类型等');
- $form->text('reward_param2', '奖励参数2')->required()->help('如物品ID、货币ID等');
- $form->number('quantity', '奖励数量')->default(1)->min(1)->required();
- $form->textarea('extra_data', '额外数据')->rows(2)->help('奖励相关的额外数据,JSON格式');
- $form->number('sort_order', '排序权重')->default(0)->help('数值越大越靠前');
- });
- }
-
- /**
- * 任务接取消耗嵌套表单
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\NestedForm
- */
- public function hasCosts(string $field = 'costs', string $label = '任务接取消耗')
- {
- return $this->form->hasMany($field, $label, function (Form\NestedForm $form) {
- $form->text('cost_type', '消耗类型')->required()->help('如物品、货币等');
- $form->text('cost_param1', '消耗参数1')->required()->help('如物品类型、货币类型等');
- $form->text('cost_param2', '消耗参数2')->required()->help('如物品ID、货币ID等');
- $form->number('quantity', '消耗数量')->default(1)->min(1)->required();
- $form->textarea('extra_data', '额外数据')->rows(2)->help('消耗相关的额外数据,JSON格式');
- });
- }
-
- /**
- * 任务达成条件嵌套表单
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\NestedForm
- */
- public function hasAchievementConditions(string $field = 'achievementConditions', string $label = '任务达成条件')
- {
- return $this->form->hasMany($field, $label, function (Form\NestedForm $form) {
- $form->select('condition_id', '条件')->options(function () {
- return TaskCondition::pluck('name', 'id');
- })->required();
- $form->select('condition_type', '条件类型')->options([
- 'prerequisite' => '前置条件',
- 'progress' => '进度条件',
- ])->default('progress')->required();
- $form->textarea('params', '条件参数')->rows(2)->help('条件相关的参数,JSON格式');
- $form->number('target_value', '目标值')->default(1)->min(1)->required()->help('完成条件所需的目标值');
- $form->switch('is_required', '是否必要条件')->default(1)->help('是否为必须完成的条件');
- $form->number('sort_order', '排序权重')->default(0)->help('数值越大越靠前');
- });
- }
- /**
- * 自动接取任务开关
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Switch
- */
- public function switchAutoAccept(string $field = 'auto_accept', string $label = '自动接取')
- {
- return $this->form->switch($field, $label)->default(0)->help('开启后,符合条件的用户将自动接取此任务');
- }
- /**
- * 自动完成任务开关
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Switch
- */
- public function switchAutoComplete(string $field = 'auto_complete', string $label = '自动完成')
- {
- return $this->form->switch($field, $label)->default(0)->help('开启后,任务条件达成时将自动完成任务');
- }
- /**
- * 自动发放奖励开关
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Switch
- */
- public function switchAutoReward(string $field = 'auto_reward', string $label = '自动发放奖励')
- {
- return $this->form->switch($field, $label)->default(0)->help('开启后,任务完成时将自动发放奖励');
- }
- /**
- * 自动重置任务开关
- *
- * @param string $field 字段名
- * @param string $label 标签
- * @return Form\Field\Switch
- */
- public function switchAutoReset(string $field = 'auto_reset', string $label = '自动重置')
- {
- return $this->form->switch($field, $label)->default(0)->help('开启后,任务完成后将根据重置类型自动重置');
- }
- }
|