FormHelperTrait.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. namespace App\Module\Task\AdminControllers\Helper;
  3. use App\Module\Task\Enums\RESET_TYPE;
  4. use App\Module\Task\Enums\REWARD_TYPE;
  5. use App\Module\Task\Enums\TASK_STATUS;
  6. use App\Module\Task\Enums\TASK_TYPE;
  7. use App\Module\Task\Models\Task;
  8. use App\Module\Task\Models\TaskCategory;
  9. use App\Module\Task\Models\TaskCondition;
  10. use Dcat\Admin\Form;
  11. /**
  12. * 表单辅助特性
  13. *
  14. * 提供任务模块后台控制器的表单构建功能
  15. */
  16. trait FormHelperTrait
  17. {
  18. /**
  19. * 任务分类选择
  20. *
  21. * @param string $field 字段名
  22. * @param string $label 标签
  23. * @return Form\Field\Select
  24. */
  25. public function selectTaskCategory(string $field = 'category_id', string $label = '任务分类')
  26. {
  27. return $this->form->select($field, $label)->options(function () {
  28. return TaskCategory::pluck('name', 'id');
  29. })->required();
  30. }
  31. /**
  32. * 任务类型选择
  33. *
  34. * @param string $field 字段名
  35. * @param string $label 标签
  36. * @return Form\Field\Select
  37. */
  38. public function selectTaskType(string $field = 'type', string $label = '任务类型')
  39. {
  40. return $this->form->select($field, $label)->options(TASK_TYPE::getOptions())->required();
  41. }
  42. /**
  43. * 重置类型选择
  44. *
  45. * @param string $field 字段名
  46. * @param string $label 标签
  47. * @return Form\Field\Select
  48. */
  49. public function selectResetType(string $field = 'reset_type', string $label = '重置类型')
  50. {
  51. return $this->form->select($field, $label)->options(RESET_TYPE::getOptions())->default('none');
  52. }
  53. /**
  54. * 任务状态选择
  55. *
  56. * @param string $field 字段名
  57. * @param string $label 标签
  58. * @return Form\Field\Select
  59. */
  60. public function selectTaskStatus(string $field = 'status', string $label = '任务状态')
  61. {
  62. return $this->form->select($field, $label)->options(TASK_STATUS::getOptions())->default(TASK_STATUS::NOT_ACCEPTED->value);
  63. }
  64. /**
  65. * 奖励类型选择
  66. *
  67. * @param string $field 字段名
  68. * @param string $label 标签
  69. * @return Form\Field\Select
  70. */
  71. public function selectRewardType(string $field = 'reward_type', string $label = '奖励类型')
  72. {
  73. return $this->form->select($field, $label)->options(REWARD_TYPE::getOptions())->required();
  74. }
  75. /**
  76. * 条件类型选择
  77. *
  78. * @param string $field 字段名
  79. * @param string $label 标签
  80. * @return Form\Field\Select
  81. */
  82. public function selectConditionType(string $field = 'condition_type', string $label = '条件类型')
  83. {
  84. return $this->form->select($field, $label)->options([
  85. 'prerequisite' => '前置条件',
  86. 'progress' => '进度条件',
  87. ])->default('progress')->required();
  88. }
  89. /**
  90. * 任务选择
  91. *
  92. * @param string $field 字段名
  93. * @param string $label 标签
  94. * @return Form\Field\Select
  95. */
  96. public function selectTask(string $field = 'task_id', string $label = '任务')
  97. {
  98. return $this->form->select($field, $label)->options(function () {
  99. return Task::pluck('name', 'id');
  100. })->required();
  101. }
  102. /**
  103. * 条件选择
  104. *
  105. * @param string $field 字段名
  106. * @param string $label 标签
  107. * @return Form\Field\Select
  108. */
  109. public function selectCondition(string $field = 'condition_id', string $label = '条件')
  110. {
  111. return $this->form->select($field, $label)->options(function () {
  112. return TaskCondition::pluck('name', 'id');
  113. })->required();
  114. }
  115. /**
  116. * 前置任务选择
  117. *
  118. * @param string $field 字段名
  119. * @param string $label 标签
  120. * @return Form\Field\MultipleSelect
  121. */
  122. public function multipleSelectPrerequisiteTasks(string $field = 'prerequisite_tasks', string $label = '前置任务')
  123. {
  124. return $this->form->multipleSelect($field, $label)->options(function () {
  125. return Task::pluck('name', 'id');
  126. })->help('选择完成后才能接取此任务的前置任务');
  127. }
  128. /**
  129. * 是否激活开关
  130. *
  131. * @param string $field 字段名
  132. * @param string $label 标签
  133. * @return Form\Field\Switch
  134. */
  135. public function switchIsActive(string $field = 'is_active', string $label = '是否激活')
  136. {
  137. return $this->form->switch($field, $label)->default(1);
  138. }
  139. /**
  140. * 是否必要条件开关
  141. *
  142. * @param string $field 字段名
  143. * @param string $label 标签
  144. * @return Form\Field\Switch
  145. */
  146. public function switchIsRequired(string $field = 'is_required', string $label = '是否必要条件')
  147. {
  148. return $this->form->switch($field, $label)->default(1)->help('是否为必须完成的条件');
  149. }
  150. /**
  151. * 排序权重
  152. *
  153. * @param string $field 字段名
  154. * @param string $label 标签
  155. * @return Form\Field\Number
  156. */
  157. public function numberSortOrder(string $field = 'sort_order', string $label = '排序权重')
  158. {
  159. return $this->form->number($field, $label)->default(0)->help('数值越大越靠前');
  160. }
  161. /**
  162. * 所需等级
  163. *
  164. * @param string $field 字段名
  165. * @param string $label 标签
  166. * @return Form\Field\Number
  167. */
  168. public function numberLevelRequired(string $field = 'level_required', string $label = '所需等级')
  169. {
  170. return $this->form->number($field, $label)->default(1)->min(1)->help('接取任务所需的最低等级');
  171. }
  172. /**
  173. * 时间限制
  174. *
  175. * @param string $field 字段名
  176. * @param string $label 标签
  177. * @return Form\Field\Number
  178. */
  179. public function numberTimeLimit(string $field = 'time_limit', string $label = '时间限制(秒)')
  180. {
  181. return $this->form->number($field, $label)->help('任务完成的时间限制,为空表示无限制');
  182. }
  183. /**
  184. * 最大完成次数
  185. *
  186. * @param string $field 字段名
  187. * @param string $label 标签
  188. * @return Form\Field\Number
  189. */
  190. public function numberMaxCompletions(string $field = 'max_completions', string $label = '最大完成次数')
  191. {
  192. return $this->form->number($field, $label)->help('任务可完成的最大次数,为空表示无限制');
  193. }
  194. /**
  195. * 目标值
  196. *
  197. * @param string $field 字段名
  198. * @param string $label 标签
  199. * @return Form\Field\Number
  200. */
  201. public function numberTargetValue(string $field = 'target_value', string $label = '目标值')
  202. {
  203. return $this->form->number($field, $label)->default(1)->min(1)->required()->help('完成条件所需的目标值');
  204. }
  205. /**
  206. * 奖励数量
  207. *
  208. * @param string $field 字段名
  209. * @param string $label 标签
  210. * @return Form\Field\Number
  211. */
  212. public function numberQuantity(string $field = 'quantity', string $label = '奖励数量')
  213. {
  214. return $this->form->number($field, $label)->default(1)->min(1)->required();
  215. }
  216. /**
  217. * 进度值
  218. *
  219. * @param string $field 字段名
  220. * @param string $label 标签
  221. * @return Form\Field\Number
  222. */
  223. public function numberProgress(string $field = 'progress', string $label = '进度')
  224. {
  225. return $this->form->number($field, $label)->default(0)->min(0)->max(100)->help('任务完成进度(0-100)');
  226. }
  227. /**
  228. * JSON字段
  229. *
  230. * @param string $field 字段名
  231. * @param string $label 标签
  232. * @param string $help 帮助文本
  233. * @return \Dcat\Admin\Form\Field\Textarea
  234. */
  235. public function json(string $field, string $label, string $help = '')
  236. {
  237. return $this->form->textarea($field, $label)
  238. ->rows(3)
  239. ->help($help ?: "{$label},JSON格式,例如:{\"key\": \"value\"}")
  240. ->rules('nullable|json');
  241. }
  242. /**
  243. * 任务奖励嵌套表单
  244. *
  245. * @param string $field 字段名
  246. * @param string $label 标签
  247. * @return Form\NestedForm
  248. */
  249. public function hasRewards(string $field = 'rewards', string $label = '任务奖励')
  250. {
  251. return $this->form->hasMany($field, $label, function (Form\NestedForm $form) {
  252. $form->select('reward_type', '奖励类型')->options(REWARD_TYPE::getOptions())->required();
  253. $form->text('reward_param1', '奖励参数1')->required()->help('如物品类型、货币类型等');
  254. $form->text('reward_param2', '奖励参数2')->required()->help('如物品ID、货币ID等');
  255. $form->number('quantity', '奖励数量')->default(1)->min(1)->required();
  256. $form->textarea('extra_data', '额外数据')->rows(2)->help('奖励相关的额外数据,JSON格式');
  257. $form->number('sort_order', '排序权重')->default(0)->help('数值越大越靠前');
  258. });
  259. }
  260. /**
  261. * 任务接取消耗嵌套表单
  262. *
  263. * @param string $field 字段名
  264. * @param string $label 标签
  265. * @return Form\NestedForm
  266. */
  267. public function hasCosts(string $field = 'costs', string $label = '任务接取消耗')
  268. {
  269. return $this->form->hasMany($field, $label, function (Form\NestedForm $form) {
  270. $form->text('cost_type', '消耗类型')->required()->help('如物品、货币等');
  271. $form->text('cost_param1', '消耗参数1')->required()->help('如物品类型、货币类型等');
  272. $form->text('cost_param2', '消耗参数2')->required()->help('如物品ID、货币ID等');
  273. $form->number('quantity', '消耗数量')->default(1)->min(1)->required();
  274. $form->textarea('extra_data', '额外数据')->rows(2)->help('消耗相关的额外数据,JSON格式');
  275. });
  276. }
  277. /**
  278. * 任务达成条件嵌套表单
  279. *
  280. * @param string $field 字段名
  281. * @param string $label 标签
  282. * @return Form\NestedForm
  283. */
  284. public function hasAchievementConditions(string $field = 'achievementConditions', string $label = '任务达成条件')
  285. {
  286. return $this->form->hasMany($field, $label, function (Form\NestedForm $form) {
  287. $form->select('condition_id', '条件')->options(function () {
  288. return TaskCondition::pluck('name', 'id');
  289. })->required();
  290. $form->select('condition_type', '条件类型')->options([
  291. 'prerequisite' => '前置条件',
  292. 'progress' => '进度条件',
  293. ])->default('progress')->required();
  294. $form->textarea('params', '条件参数')->rows(2)->help('条件相关的参数,JSON格式');
  295. $form->number('target_value', '目标值')->default(1)->min(1)->required()->help('完成条件所需的目标值');
  296. $form->switch('is_required', '是否必要条件')->default(1)->help('是否为必须完成的条件');
  297. $form->number('sort_order', '排序权重')->default(0)->help('数值越大越靠前');
  298. });
  299. }
  300. /**
  301. * 自动接取任务开关
  302. *
  303. * @param string $field 字段名
  304. * @param string $label 标签
  305. * @return Form\Field\Switch
  306. */
  307. public function switchAutoAccept(string $field = 'auto_accept', string $label = '自动接取')
  308. {
  309. return $this->form->switch($field, $label)->default(0)->help('开启后,符合条件的用户将自动接取此任务');
  310. }
  311. /**
  312. * 自动完成任务开关
  313. *
  314. * @param string $field 字段名
  315. * @param string $label 标签
  316. * @return Form\Field\Switch
  317. */
  318. public function switchAutoComplete(string $field = 'auto_complete', string $label = '自动完成')
  319. {
  320. return $this->form->switch($field, $label)->default(0)->help('开启后,任务条件达成时将自动完成任务');
  321. }
  322. /**
  323. * 自动发放奖励开关
  324. *
  325. * @param string $field 字段名
  326. * @param string $label 标签
  327. * @return Form\Field\Switch
  328. */
  329. public function switchAutoReward(string $field = 'auto_reward', string $label = '自动发放奖励')
  330. {
  331. return $this->form->switch($field, $label)->default(0)->help('开启后,任务完成时将自动发放奖励');
  332. }
  333. /**
  334. * 自动重置任务开关
  335. *
  336. * @param string $field 字段名
  337. * @param string $label 标签
  338. * @return Form\Field\Switch
  339. */
  340. public function switchAutoReset(string $field = 'auto_reset', string $label = '自动重置')
  341. {
  342. return $this->form->switch($field, $label)->default(0)->help('开启后,任务完成后将根据重置类型自动重置');
  343. }
  344. }