FormHelperTrait.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Module\Game\AdminControllers\Helper;
  3. use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
  4. use App\Module\Game\Enums\REWARD_TYPE;
  5. use App\Module\Game\Models\GameRewardGroup;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Form\Field;
  8. /**
  9. * 表单辅助特性
  10. *
  11. * 提供游戏模块后台控制器的表单构建功能的具体实现
  12. */
  13. trait FormHelperTrait
  14. {
  15. /**
  16. * 添加奖励类型选择
  17. *
  18. * @param string $field 字段名
  19. * @param string $label 标签名
  20. * @return Field\Select
  21. */
  22. public function selectRewardType(string $field = 'reward_type', string $label = '奖励类型'): Field\Select
  23. {
  24. return $this->form->select($field, $label)->options(REWARD_TYPE::getAll())->required();
  25. }
  26. /**
  27. * 添加奖励来源类型选择
  28. *
  29. * @param string $field 字段名
  30. * @param string $label 标签名
  31. * @return Field\Select
  32. */
  33. public function selectRewardSourceType(string $field = 'source_type', string $label = '奖励来源'): Field\Select
  34. {
  35. return $this->form->select($field, $label)->options(REWARD_SOURCE_TYPE::getAll())->required();
  36. }
  37. /**
  38. * 添加奖励组选择
  39. *
  40. * @param string $field 字段名
  41. * @param string $label 标签名
  42. * @return Field\Select
  43. */
  44. public function selectRewardGroup(string $field = 'group_id', string $label = '奖励组'): Field\Select
  45. {
  46. $options = GameRewardGroup::pluck('name', 'id')->toArray();
  47. return $this->form->select($field, $label)->options($options)->required();
  48. }
  49. /**
  50. * 添加是否随机发放开关
  51. *
  52. * @param string $field 字段名
  53. * @param string $label 标签名
  54. * @return Field\Switch
  55. */
  56. public function switchIsRandom(string $field = 'is_random', string $label = '随机发放'): Field\Switch
  57. {
  58. return $this->form->switch($field, $label)->default(0);
  59. }
  60. /**
  61. * 添加随机数量输入
  62. *
  63. * @param string $field 字段名
  64. * @param string $label 标签名
  65. * @return Field\Number
  66. */
  67. public function numberRandomCount(string $field = 'random_count', string $label = '随机数量'): Field\Number
  68. {
  69. return $this->form->number($field, $label)->default(1)->min(1)->help('随机发放时的奖励数量,仅当随机发放开启时有效');
  70. }
  71. /**
  72. * 添加是否必中开关
  73. *
  74. * @param string $field 字段名
  75. * @param string $label 标签名
  76. * @return Field\Switch
  77. */
  78. public function switchIsGuaranteed(string $field = 'is_guaranteed', string $label = '必中'): Field\Switch
  79. {
  80. return $this->form->switch($field, $label)->default(0);
  81. }
  82. /**
  83. * 添加权重输入
  84. *
  85. * @param string $field 字段名
  86. * @param string $label 标签名
  87. * @return Field\Number
  88. */
  89. public function numberWeight(string $field = 'weight', string $label = '权重'): Field\Number
  90. {
  91. return $this->form->number($field, $label)->default(100)->min(1)->help('权重越高,被选中的概率越大');
  92. }
  93. /**
  94. * 添加目标ID输入
  95. *
  96. * @param string $field 字段名
  97. * @param string $label 标签名
  98. * @return Field\Text
  99. */
  100. public function textTargetId(string $field = 'target_id', string $label = '目标ID'): Field\Text
  101. {
  102. return $this->form->text($field, $label)->required()->help('根据奖励类型,填写对应的物品ID、货币ID等');
  103. }
  104. /**
  105. * 添加参数1输入
  106. *
  107. * @param string $field 字段名
  108. * @param string $label 标签名
  109. * @return Field\Text
  110. */
  111. public function textParam1(string $field = 'param1', string $label = '参数1'): Field\Text
  112. {
  113. return $this->form->text($field, $label)->default(0)->help('可选参数,根据奖励类型使用');
  114. }
  115. /**
  116. * 添加参数2输入
  117. *
  118. * @param string $field 字段名
  119. * @param string $label 标签名
  120. * @return Field\Text
  121. */
  122. public function textParam2(string $field = 'param2', string $label = '参数2'): Field\Text
  123. {
  124. return $this->form->text($field, $label)->default(0)->help('可选参数,根据奖励类型使用');
  125. }
  126. /**
  127. * 添加数量输入
  128. *
  129. * @param string $field 字段名
  130. * @param string $label 标签名
  131. * @return Field\Number
  132. */
  133. public function numberQuantity(string $field = 'quantity', string $label = '数量'): Field\Number
  134. {
  135. return $this->form->number($field, $label)->default(1)->min(1)->required();
  136. }
  137. /**
  138. * 添加奖励项表格
  139. *
  140. * @param string $field 字段名
  141. * @param string $label 标签名
  142. * @return Field\Table
  143. */
  144. public function tableRewardItems(string $field = 'items', string $label = '奖励项'): Field\Table
  145. {
  146. return $this->form->table($field, $label, function (Form\NestedForm $table) {
  147. $table->select('reward_type', '奖励类型')->options(REWARD_TYPE::getAll())->required();
  148. $table->text('target_id', '目标ID')->required()->help('根据奖励类型,填写对应的物品ID、货币ID等');
  149. $table->text('param1', '参数1')->default(0)->help('可选参数,根据奖励类型使用');
  150. $table->text('param2', '参数2')->default(0)->help('可选参数,根据奖励类型使用');
  151. $table->number('quantity', '数量')->default(1)->min(1)->required();
  152. $table->number('weight', '权重')->default(100)->min(1);
  153. $table->switch('is_guaranteed', '必中')->default(0);
  154. });
  155. }
  156. }