FormHelperTrait.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace App\Module\Activity\AdminControllers\Helper;
  3. use App\Module\Activity\Enums\ACTIVITY_STATUS;
  4. use App\Module\Activity\Enums\ACTIVITY_TYPE;
  5. use App\Module\Activity\Enums\CONDITION_TYPE;
  6. use App\Module\Activity\Enums\PARTICIPATION_STATUS;
  7. use App\Module\Activity\Enums\REWARD_STATUS;
  8. use App\Module\Game\Enums\REWARD_TYPE;
  9. use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
  10. use App\Module\Game\AdminControllers\LazyRenderable\RewardGroupLazyRenderable;
  11. use Dcat\Admin\Form;
  12. use Dcat\Admin\Form\Field;
  13. /**
  14. * 表单辅助特性
  15. *
  16. * 提供活动模块后台控制器的表单构建功能的具体实现
  17. */
  18. trait FormHelperTrait
  19. {
  20. /**
  21. * 添加活动状态选择
  22. *
  23. * @param string $field 字段名
  24. * @param string $label 标签名
  25. * @return Field\Select
  26. */
  27. public function selectActivityStatus(string $field = 'status', string $label = '活动状态'): Field\Select
  28. {
  29. return $this->form->select($field, $label)
  30. ->options(ACTIVITY_STATUS::getAll())
  31. ->default(ACTIVITY_STATUS::NOT_STARTED->value)
  32. ->required();
  33. }
  34. /**
  35. * 添加活动类型选择
  36. *
  37. * @param string $field 字段名
  38. * @param string $label 标签名
  39. * @return Field\Select
  40. */
  41. public function selectActivityType(string $field = 'type', string $label = '活动类型'): Field\Select
  42. {
  43. return $this->form->select($field, $label)
  44. ->options(ACTIVITY_TYPE::getAll())
  45. ->required();
  46. }
  47. /**
  48. * 添加条件类型选择
  49. *
  50. * @param string $field 字段名
  51. * @param string $label 标签名
  52. * @return Field\Select
  53. */
  54. public function selectConditionType(string $field = 'condition_type', string $label = '条件类型'): Field\Select
  55. {
  56. return $this->form->select($field, $label)
  57. ->options(CONDITION_TYPE::getAll())
  58. ->required();
  59. }
  60. /**
  61. * 添加参与状态选择
  62. *
  63. * @param string $field 字段名
  64. * @param string $label 标签名
  65. * @return Field\Select
  66. */
  67. public function selectParticipationStatus(string $field = 'completion_status', string $label = '参与状态'): Field\Select
  68. {
  69. return $this->form->select($field, $label)
  70. ->options(PARTICIPATION_STATUS::getAll())
  71. ->default(PARTICIPATION_STATUS::IN_PROGRESS->value)
  72. ->required();
  73. }
  74. /**
  75. * 添加奖励状态选择
  76. *
  77. * @param string $field 字段名
  78. * @param string $label 标签名
  79. * @return Field\Select
  80. */
  81. public function selectRewardStatus(string $field = 'reward_status', string $label = '奖励状态'): Field\Select
  82. {
  83. return $this->form->select($field, $label)
  84. ->options(REWARD_STATUS::getAll())
  85. ->default(REWARD_STATUS::NOT_CLAIMED->value)
  86. ->required();
  87. }
  88. /**
  89. * 添加奖励类型选择
  90. *
  91. * @param string $field 字段名
  92. * @param string $label 标签名
  93. * @return Field\Select
  94. */
  95. public function selectRewardType(string $field = 'reward_type', string $label = '奖励类型'): Field\Select
  96. {
  97. return $this->form->select($field, $label)
  98. ->options(REWARD_TYPE::getAll())
  99. ->required();
  100. }
  101. /**
  102. * 添加奖励来源类型选择
  103. *
  104. * @param string $field 字段名
  105. * @param string $label 标签名
  106. * @return Field\Select
  107. */
  108. public function selectRewardSourceType(string $field = 'source_type', string $label = '奖励来源'): Field\Select
  109. {
  110. return $this->form->select($field, $label)
  111. ->options(REWARD_SOURCE_TYPE::getAll())
  112. ->default(REWARD_SOURCE_TYPE::ACTIVITY->value)
  113. ->required();
  114. }
  115. /**
  116. * 添加活动时间范围选择
  117. *
  118. * @param string $startField 开始时间字段
  119. * @param string $endField 结束时间字段
  120. * @param string $label 标签名
  121. * @return Field\DateRange
  122. */
  123. public function dateTimeRangeActivity(string $startField = 'start_time', string $endField = 'end_time', string $label = '活动时间'): Field\DateRange
  124. {
  125. return $this->form->dateRange($startField, $endField, $label)
  126. ->datetime()
  127. ->required();
  128. }
  129. /**
  130. * 添加活动配置JSON编辑器
  131. *
  132. * @param string $field 字段名
  133. * @param string $label 标签名
  134. * @return Field\Editor
  135. */
  136. public function jsonEditorActivityConfig(string $field = 'config', string $label = '活动配置'): Field\Editor
  137. {
  138. return $this->form->editor($field, $label)
  139. ->help('请输入有效的JSON格式配置');
  140. }
  141. /**
  142. * 添加活动条件JSON编辑器
  143. *
  144. * @param string $field 字段名
  145. * @param string $label 标签名
  146. * @return Field\Editor
  147. */
  148. public function jsonEditorActivityConditions(string $field = 'conditions', string $label = '活动条件'): Field\Editor
  149. {
  150. return $this->form->editor($field, $label)
  151. ->help('请输入有效的JSON格式条件配置');
  152. }
  153. /**
  154. * 添加活动奖励JSON编辑器
  155. *
  156. * @param string $field 字段名
  157. * @param string $label 标签名
  158. * @return Field\Editor
  159. */
  160. public function jsonEditorActivityRewards(string $field = 'rewards', string $label = '活动奖励'): Field\Editor
  161. {
  162. return $this->form->editor($field, $label)
  163. ->help('请输入有效的JSON格式奖励配置');
  164. }
  165. /**
  166. * 添加活动条件表格
  167. *
  168. * @param string $field 字段名
  169. * @param string $label 标签名
  170. * @return Field\Table
  171. */
  172. public function tableActivityConditions(string $field = 'conditions', string $label = '活动条件'): Field\Table
  173. {
  174. return $this->form->table($field, $label, function (Field\Table\InlineTable $table) {
  175. $table->select('type', '条件类型')
  176. ->options(CONDITION_TYPE::getAll())
  177. ->required();
  178. $table->text('target_id', '目标ID')
  179. ->required();
  180. $table->number('target_value', '目标值')
  181. ->min(0)
  182. ->required();
  183. $table->text('description', '描述');
  184. });
  185. }
  186. /**
  187. * 添加活动奖励表格
  188. *
  189. * @param string $field 字段名
  190. * @param string $label 标签名
  191. * @return Field\Table
  192. */
  193. public function tableActivityRewards(string $field = 'rewards', string $label = '活动奖励'): Field\Table
  194. {
  195. return $this->form->table($field, $label, function (Field\Table\InlineTable $table) {
  196. $table->select('type', '奖励类型')
  197. ->options(REWARD_TYPE::getAll())
  198. ->required();
  199. $table->text('reward_id', '奖励ID')
  200. ->required();
  201. $table->number('quantity', '数量')
  202. ->min(1)
  203. ->default(1)
  204. ->required();
  205. $table->text('description', '描述');
  206. });
  207. }
  208. /**
  209. * 添加奖励组选择
  210. *
  211. * @param string $field 字段名
  212. * @param string $label 标签名
  213. * @return Field\SelectTable
  214. */
  215. public function selectRewardGroup(string $field = 'reward_group_id', string $label = '奖励组'): Field\SelectTable
  216. {
  217. $table = RewardGroupLazyRenderable::make();
  218. return $this->form->selectTable($field, $label)
  219. ->from($table)
  220. ->model($table->getModel(), 'id', 'name');
  221. }
  222. /**
  223. * 添加活动名称输入
  224. *
  225. * @param string $field 字段名
  226. * @param string $label 标签名
  227. * @return Field\Text
  228. */
  229. public function textActivityName(string $field = 'name', string $label = '活动名称'): Field\Text
  230. {
  231. return $this->form->text($field, $label)
  232. ->required()
  233. ->rules('required|max:50');
  234. }
  235. /**
  236. * 添加活动描述输入
  237. *
  238. * @param string $field 字段名
  239. * @param string $label 标签名
  240. * @return Field\Textarea
  241. */
  242. public function textareaActivityDescription(string $field = 'description', string $label = '活动描述'): Field\Textarea
  243. {
  244. return $this->form->textarea($field, $label)
  245. ->rows(3)
  246. ->rules('max:200');
  247. }
  248. /**
  249. * 添加活动图标上传
  250. *
  251. * @param string $field 字段名
  252. * @param string $label 标签名
  253. * @return Field\Image
  254. */
  255. public function imageActivityIcon(string $field = 'icon', string $label = '活动图标'): Field\Image
  256. {
  257. return $this->form->image($field, $label)
  258. ->autoUpload()
  259. ->uniqueName()
  260. ->help('建议尺寸: 200x200px');
  261. }
  262. /**
  263. * 添加活动背景图上传
  264. *
  265. * @param string $field 字段名
  266. * @param string $label 标签名
  267. * @return Field\Image
  268. */
  269. public function imageActivityBackground(string $field = 'background', string $label = '活动背景图'): Field\Image
  270. {
  271. return $this->form->image($field, $label)
  272. ->autoUpload()
  273. ->uniqueName()
  274. ->help('建议尺寸: 1920x1080px');
  275. }
  276. /**
  277. * 添加活动排序输入
  278. *
  279. * @param string $field 字段名
  280. * @param string $label 标签名
  281. * @return Field\Number
  282. */
  283. public function numberActivitySort(string $field = 'sort', string $label = '排序'): Field\Number
  284. {
  285. return $this->form->number($field, $label)
  286. ->default(0)
  287. ->help('数字越小越靠前');
  288. }
  289. /**
  290. * 添加活动是否显示开关
  291. *
  292. * @param string $field 字段名
  293. * @param string $label 标签名
  294. * @return Field\Switch
  295. */
  296. public function switchActivityVisible(string $field = 'is_visible', string $label = '是否显示'): Field\Switch
  297. {
  298. return $this->form->switch($field, $label)
  299. ->default(true);
  300. }
  301. }