| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- namespace App\Module\Mex\AdminControllers\Helper;
- use Dcat\Admin\Form;
- use App\Module\Mex\Enums\OrderStatus;
- use App\Module\Mex\Enums\OrderType;
- use App\Module\Mex\Enums\TransactionType;
- use App\Module\Mex\Enums\AdminOperationType;
- /**
- * Mex模块Form助手类
- *
- * 提供通用的表单字段配置方法
- */
- class FormHelper
- {
- protected Form $form;
- protected $controller;
- public function __construct(Form $form, $controller = null)
- {
- $this->form = $form;
- $this->controller = $controller;
- }
- /**
- * 用户ID字段
- */
- public function numberUserId(string $field = 'user_id', string $label = '用户ID', bool $required = true): Form\Field\Number
- {
- $field = $this->form->number($field, $label)
- ->min(1)
- ->help('请输入有效的用户ID');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 商品ID字段
- */
- public function numberItemId(string $field = 'item_id', string $label = '商品ID', bool $required = true): Form\Field\Number
- {
- $field = $this->form->number($field, $label)
- ->min(1)
- ->help('请输入有效的商品ID');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 价格字段
- */
- public function decimalPrice(string $field = 'price', string $label = '价格', bool $required = true): Form\Field\Decimal
- {
- $field = $this->form->decimal($field, $label)
- ->min(0)
- ->precision(5)
- ->help('支持5位小数的价格');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 金额字段
- */
- public function decimalAmount(string $field, string $label, bool $required = true): Form\Field\Decimal
- {
- $field = $this->form->decimal($field, $label)
- ->min(0)
- ->precision(5)
- ->help('支持5位小数的金额');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 数量字段
- */
- public function numberQuantity(string $field = 'quantity', string $label = '数量', bool $required = true): Form\Field\Number
- {
- $field = $this->form->number($field, $label)
- ->min(1)
- ->help('请输入正整数数量');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 订单类型选择
- */
- public function selectOrderType(string $field = 'order_type', string $label = '订单类型', bool $required = true): Form\Field\Select
- {
- $field = $this->form->select($field, $label)
- ->options(OrderType::getAll())
- ->help('选择订单类型');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 订单状态选择
- */
- public function selectOrderStatus(string $field = 'status', string $label = '状态', bool $required = true): Form\Field\Select
- {
- $field = $this->form->select($field, $label)
- ->options(OrderStatus::getAll())
- ->help('选择订单状态');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 交易类型选择
- */
- public function selectTransactionType(string $field = 'transaction_type', string $label = '交易类型', bool $required = true): Form\Field\Select
- {
- $field = $this->form->select($field, $label)
- ->options(TransactionType::getAll())
- ->help('选择交易类型');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 管理员操作类型选择
- */
- public function selectAdminOperationType(string $field = 'operation_type', string $label = '操作类型', bool $required = true): Form\Field\Select
- {
- $field = $this->form->select($field, $label)
- ->options(AdminOperationType::getAll())
- ->help('选择操作类型');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 启用状态开关
- */
- public function switchEnabled(string $field = 'is_enabled', string $label = '启用状态', bool $default = true)
- {
- return $this->form->switch($field, $label)
- ->default($default ? 1 : 0)
- ->help('是否启用此配置');
- }
- /**
- * 原因文本域
- */
- public function textareaReason(string $field = 'reason', string $label = '原因', bool $required = false): Form\Field\Textarea
- {
- $field = $this->form->textarea($field, $label)
- ->rows(3)
- ->help('请输入详细原因');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * JSON数据字段
- */
- public function jsonData(string $field, string $label, bool $required = false): Form\Field\Textarea
- {
- $field = $this->form->textarea($field, $label)
- ->rows(5)
- ->help('请输入有效的JSON格式数据');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 保护阈值字段
- */
- public function numberProtectionThreshold(string $field = 'protection_threshold', string $label = '保护阈值', bool $required = true): Form\Field\Number
- {
- $field = $this->form->number($field, $label)
- ->min(1)
- ->help('单次买入订单的最大数量限制');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 最低价格字段
- */
- public function decimalMinPrice(string $field = 'min_price', string $label = '最低价格', bool $required = true): Form\Field\Decimal
- {
- $field = $this->form->decimal($field, $label)
- ->min(0)
- ->precision(5)
- ->help('商品的最低售价,用户卖出价格不能高于此价格');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 最高价格字段
- */
- public function decimalMaxPrice(string $field = 'max_price', string $label = '最高价格', bool $required = true): Form\Field\Decimal
- {
- $field = $this->form->decimal($field, $label)
- ->min(0)
- ->precision(5)
- ->help('商品的最高售价,用户买入价格不能低于此价格');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 管理员ID字段
- */
- public function numberAdminUserId(string $field = 'admin_user_id', string $label = '管理员ID', bool $required = false): Form\Field\Number
- {
- $field = $this->form->number($field, $label)
- ->min(1)
- ->help('执行操作的管理员ID');
-
- if ($required) {
- $field->required();
- }
-
- return $field;
- }
- /**
- * 显示字段
- */
- public function display(string $field, string $label): Form\Field\Display
- {
- return $this->form->display($field, $label);
- }
- /**
- * 隐藏字段
- */
- public function hidden(string $field): Form\Field\Hidden
- {
- return $this->form->hidden($field);
- }
- /**
- * 分割线
- */
- public function divider(string $title = ''): void
- {
- $this->form->divider($title);
- }
- }
|