FormHelperTrait.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace App\Module\Point\AdminControllers\Helper;
  3. use App\Module\Point\Enums\LOG_TYPE;
  4. use App\Module\Point\Enums\POINT_TYPE;
  5. /**
  6. * 表单辅助特性
  7. *
  8. * 提供积分模块后台控制器的表单构建功能的具体实现
  9. */
  10. trait FormHelperTrait
  11. {
  12. /**
  13. * 添加积分类型选择字段
  14. *
  15. * @param string $field 字段名
  16. * @param string $label 标签名
  17. * @param bool $required 是否必填
  18. * @return void
  19. */
  20. public function selectPointId(string $field = 'point_id', string $label = '积分类型', bool $required = true): void
  21. {
  22. $select = $this->form->select($field, $label)->options([
  23. 1 => '经验积分',
  24. 2 => '成就积分',
  25. 3 => '活动积分',
  26. 4 => '签到积分',
  27. 5 => '推荐积分',
  28. ]);
  29. if ($required) {
  30. $select->required();
  31. }
  32. }
  33. /**
  34. * 添加操作类型选择字段
  35. *
  36. * @param string $field 字段名
  37. * @param string $label 标签名
  38. * @param bool $required 是否必填
  39. * @return void
  40. */
  41. public function selectOperateType(string $field = 'operate_type', string $label = '操作类型', bool $required = true): void
  42. {
  43. $select = $this->form->select($field, $label)->options(LOG_TYPE::getAllTypes());
  44. if ($required) {
  45. $select->required();
  46. }
  47. }
  48. /**
  49. * 添加积分数量输入字段
  50. *
  51. * @param string $field 字段名
  52. * @param string $label 标签名
  53. * @param bool $required 是否必填
  54. * @param int $min 最小值
  55. * @return void
  56. */
  57. public function numberAmount(string $field = 'amount', string $label = '积分数量', bool $required = true, int $min = 0): void
  58. {
  59. $number = $this->form->number($field, $label)->min($min);
  60. if ($required) {
  61. $number->required();
  62. }
  63. }
  64. /**
  65. * 添加积分余额输入字段
  66. *
  67. * @param string $field 字段名
  68. * @param string $label 标签名
  69. * @param bool $required 是否必填
  70. * @param int $min 最小值
  71. * @return void
  72. */
  73. public function numberBalance(string $field = 'balance', string $label = '积分余额', bool $required = true, int $min = 0): void
  74. {
  75. $number = $this->form->number($field, $label)->min($min);
  76. if ($required) {
  77. $number->required();
  78. }
  79. }
  80. /**
  81. * 添加用户ID输入字段
  82. *
  83. * @param string $field 字段名
  84. * @param string $label 标签名
  85. * @param bool $required 是否必填
  86. * @return void
  87. */
  88. public function numberUserId(string $field = 'user_id', string $label = '用户ID', bool $required = true): void
  89. {
  90. $number = $this->form->number($field, $label)->min(1);
  91. if ($required) {
  92. $number->required();
  93. }
  94. }
  95. /**
  96. * 添加状态选择字段
  97. *
  98. * @param string $field 字段名
  99. * @param string $label 标签名
  100. * @param bool $required 是否必填
  101. * @return void
  102. */
  103. public function selectStatus(string $field = 'status', string $label = '状态', bool $required = true): void
  104. {
  105. $select = $this->form->select($field, $label)->options([
  106. 0 => '待处理',
  107. 1 => '已完成',
  108. 2 => '已失败',
  109. ]);
  110. if ($required) {
  111. $select->required();
  112. }
  113. }
  114. /**
  115. * 添加订单类型选择字段
  116. *
  117. * @param string $field 字段名
  118. * @param string $label 标签名
  119. * @param bool $required 是否必填
  120. * @return void
  121. */
  122. public function selectOrderType(string $field = 'order_type', string $label = '订单类型', bool $required = true): void
  123. {
  124. $select = $this->form->select($field, $label)->options([
  125. 'exchange' => '积分兑换',
  126. 'consume' => '积分消费',
  127. 'reward' => '积分奖励',
  128. 'refund' => '积分退款',
  129. ]);
  130. if ($required) {
  131. $select->required();
  132. }
  133. }
  134. /**
  135. * 添加备注文本域字段
  136. *
  137. * @param string $field 字段名
  138. * @param string $label 标签名
  139. * @param bool $required 是否必填
  140. * @param int $rows 行数
  141. * @return void
  142. */
  143. public function textareaRemark(string $field = 'remark', string $label = '备注', bool $required = false, int $rows = 3): void
  144. {
  145. $textarea = $this->form->textarea($field, $label)->rows($rows);
  146. if ($required) {
  147. $textarea->required();
  148. }
  149. }
  150. /**
  151. * 添加操作ID输入字段
  152. *
  153. * @param string $field 字段名
  154. * @param string $label 标签名
  155. * @param bool $required 是否必填
  156. * @return void
  157. */
  158. public function textOperateId(string $field = 'operate_id', string $label = '操作ID', bool $required = true): void
  159. {
  160. $text = $this->form->text($field, $label);
  161. if ($required) {
  162. $text->required();
  163. }
  164. }
  165. /**
  166. * 添加订单号输入字段
  167. *
  168. * @param string $field 字段名
  169. * @param string $label 标签名
  170. * @param bool $required 是否必填
  171. * @return void
  172. */
  173. public function textOrderNo(string $field = 'order_no', string $label = '订单号', bool $required = true): void
  174. {
  175. $text = $this->form->text($field, $label);
  176. if ($required) {
  177. $text->required();
  178. }
  179. }
  180. /**
  181. * 添加标题输入字段
  182. *
  183. * @param string $field 字段名
  184. * @param string $label 标签名
  185. * @param bool $required 是否必填
  186. * @return void
  187. */
  188. public function textTitle(string $field = 'title', string $label = '标题', bool $required = true): void
  189. {
  190. $text = $this->form->text($field, $label);
  191. if ($required) {
  192. $text->required();
  193. }
  194. }
  195. /**
  196. * 添加描述文本域字段
  197. *
  198. * @param string $field 字段名
  199. * @param string $label 标签名
  200. * @param bool $required 是否必填
  201. * @param int $rows 行数
  202. * @return void
  203. */
  204. public function textareaDescription(string $field = 'description', string $label = '描述', bool $required = false, int $rows = 4): void
  205. {
  206. $textarea = $this->form->textarea($field, $label)->rows($rows);
  207. if ($required) {
  208. $textarea->required();
  209. }
  210. }
  211. /**
  212. * 添加JSON数据文本域字段
  213. *
  214. * @param string $field 字段名
  215. * @param string $label 标签名
  216. * @param bool $required 是否必填
  217. * @param int $rows 行数
  218. * @return void
  219. */
  220. public function textareaJson(string $field, string $label, bool $required = false, int $rows = 5): void
  221. {
  222. $textarea = $this->form->textarea($field, $label)->rows($rows);
  223. if ($required) {
  224. $textarea->required();
  225. }
  226. }
  227. /**
  228. * 添加时间戳隐藏字段处理
  229. *
  230. * @return void
  231. */
  232. public function hiddenTimestamps(): void
  233. {
  234. $this->form->hidden('create_time');
  235. $this->form->hidden('update_time');
  236. }
  237. }