FormHelperTrait.php 6.9 KB

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