FormHelper.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace App\Module\Mex\AdminControllers\Helper;
  3. use Dcat\Admin\Form;
  4. use App\Module\Mex\Enums\OrderStatus;
  5. use App\Module\Mex\Enums\OrderType;
  6. use App\Module\Mex\Enums\TransactionType;
  7. use App\Module\Mex\Enums\AdminOperationType;
  8. /**
  9. * Mex模块Form助手类
  10. *
  11. * 提供通用的表单字段配置方法
  12. */
  13. class FormHelper
  14. {
  15. protected Form $form;
  16. protected $controller;
  17. public function __construct(Form $form, $controller = null)
  18. {
  19. $this->form = $form;
  20. $this->controller = $controller;
  21. }
  22. /**
  23. * 用户ID字段
  24. */
  25. public function numberUserId(string $field = 'user_id', string $label = '用户ID', bool $required = true): Form\Field\Number
  26. {
  27. $field = $this->form->number($field, $label)
  28. ->min(1)
  29. ->help('请输入有效的用户ID');
  30. if ($required) {
  31. $field->required();
  32. }
  33. return $field;
  34. }
  35. /**
  36. * 商品ID字段
  37. */
  38. public function numberItemId(string $field = 'item_id', string $label = '商品ID', bool $required = true): Form\Field\Number
  39. {
  40. $field = $this->form->number($field, $label)
  41. ->min(1)
  42. ->help('请输入有效的商品ID');
  43. if ($required) {
  44. $field->required();
  45. }
  46. return $field;
  47. }
  48. /**
  49. * 价格字段
  50. */
  51. public function decimalPrice(string $field = 'price', string $label = '价格', bool $required = true): Form\Field\Decimal
  52. {
  53. $field = $this->form->decimal($field, $label)
  54. ->min(0)
  55. ->precision(5)
  56. ->help('支持5位小数的价格');
  57. if ($required) {
  58. $field->required();
  59. }
  60. return $field;
  61. }
  62. /**
  63. * 金额字段
  64. */
  65. public function decimalAmount(string $field, string $label, bool $required = true): Form\Field\Decimal
  66. {
  67. $field = $this->form->decimal($field, $label)
  68. ->min(0)
  69. ->precision(5)
  70. ->help('支持5位小数的金额');
  71. if ($required) {
  72. $field->required();
  73. }
  74. return $field;
  75. }
  76. /**
  77. * 数量字段
  78. */
  79. public function numberQuantity(string $field = 'quantity', string $label = '数量', bool $required = true): Form\Field\Number
  80. {
  81. $field = $this->form->number($field, $label)
  82. ->min(1)
  83. ->help('请输入正整数数量');
  84. if ($required) {
  85. $field->required();
  86. }
  87. return $field;
  88. }
  89. /**
  90. * 订单类型选择
  91. */
  92. public function selectOrderType(string $field = 'order_type', string $label = '订单类型', bool $required = true): Form\Field\Select
  93. {
  94. $field = $this->form->select($field, $label)
  95. ->options(OrderType::getAll())
  96. ->help('选择订单类型');
  97. if ($required) {
  98. $field->required();
  99. }
  100. return $field;
  101. }
  102. /**
  103. * 订单状态选择
  104. */
  105. public function selectOrderStatus(string $field = 'status', string $label = '状态', bool $required = true): Form\Field\Select
  106. {
  107. $field = $this->form->select($field, $label)
  108. ->options(OrderStatus::getAll())
  109. ->help('选择订单状态');
  110. if ($required) {
  111. $field->required();
  112. }
  113. return $field;
  114. }
  115. /**
  116. * 交易类型选择
  117. */
  118. public function selectTransactionType(string $field = 'transaction_type', string $label = '交易类型', bool $required = true): Form\Field\Select
  119. {
  120. $field = $this->form->select($field, $label)
  121. ->options(TransactionType::getAll())
  122. ->help('选择交易类型');
  123. if ($required) {
  124. $field->required();
  125. }
  126. return $field;
  127. }
  128. /**
  129. * 管理员操作类型选择
  130. */
  131. public function selectAdminOperationType(string $field = 'operation_type', string $label = '操作类型', bool $required = true): Form\Field\Select
  132. {
  133. $field = $this->form->select($field, $label)
  134. ->options(AdminOperationType::getAll())
  135. ->help('选择操作类型');
  136. if ($required) {
  137. $field->required();
  138. }
  139. return $field;
  140. }
  141. /**
  142. * 启用状态开关
  143. */
  144. public function switchEnabled(string $field = 'is_enabled', string $label = '启用状态', bool $default = true)
  145. {
  146. return $this->form->switch($field, $label)
  147. ->default($default ? 1 : 0)
  148. ->help('是否启用此配置');
  149. }
  150. /**
  151. * 原因文本域
  152. */
  153. public function textareaReason(string $field = 'reason', string $label = '原因', bool $required = false): Form\Field\Textarea
  154. {
  155. $field = $this->form->textarea($field, $label)
  156. ->rows(3)
  157. ->help('请输入详细原因');
  158. if ($required) {
  159. $field->required();
  160. }
  161. return $field;
  162. }
  163. /**
  164. * JSON数据字段
  165. */
  166. public function jsonData(string $field, string $label, bool $required = false): Form\Field\Textarea
  167. {
  168. $field = $this->form->textarea($field, $label)
  169. ->rows(5)
  170. ->help('请输入有效的JSON格式数据');
  171. if ($required) {
  172. $field->required();
  173. }
  174. return $field;
  175. }
  176. /**
  177. * 保护阈值字段
  178. */
  179. public function numberProtectionThreshold(string $field = 'protection_threshold', string $label = '保护阈值', bool $required = true): Form\Field\Number
  180. {
  181. $field = $this->form->number($field, $label)
  182. ->min(1)
  183. ->help('单次买入订单的最大数量限制');
  184. if ($required) {
  185. $field->required();
  186. }
  187. return $field;
  188. }
  189. /**
  190. * 最低价格字段
  191. */
  192. public function decimalMinPrice(string $field = 'min_price', string $label = '最低价格', bool $required = true): Form\Field\Decimal
  193. {
  194. $field = $this->form->decimal($field, $label)
  195. ->min(0)
  196. ->precision(5)
  197. ->help('商品的最低售价,用户卖出价格不能高于此价格');
  198. if ($required) {
  199. $field->required();
  200. }
  201. return $field;
  202. }
  203. /**
  204. * 最高价格字段
  205. */
  206. public function decimalMaxPrice(string $field = 'max_price', string $label = '最高价格', bool $required = true): Form\Field\Decimal
  207. {
  208. $field = $this->form->decimal($field, $label)
  209. ->min(0)
  210. ->precision(5)
  211. ->help('商品的最高售价,用户买入价格不能低于此价格');
  212. if ($required) {
  213. $field->required();
  214. }
  215. return $field;
  216. }
  217. /**
  218. * 管理员ID字段
  219. */
  220. public function numberAdminUserId(string $field = 'admin_user_id', string $label = '管理员ID', bool $required = false): Form\Field\Number
  221. {
  222. $field = $this->form->number($field, $label)
  223. ->min(1)
  224. ->help('执行操作的管理员ID');
  225. if ($required) {
  226. $field->required();
  227. }
  228. return $field;
  229. }
  230. /**
  231. * 显示字段
  232. */
  233. public function display(string $field, string $label): Form\Field\Display
  234. {
  235. return $this->form->display($field, $label);
  236. }
  237. /**
  238. * 隐藏字段
  239. */
  240. public function hidden(string $field): Form\Field\Hidden
  241. {
  242. return $this->form->hidden($field);
  243. }
  244. /**
  245. * 分割线
  246. */
  247. public function divider(string $title = ''): void
  248. {
  249. $this->form->divider($title);
  250. }
  251. }