| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace App\Module\System\AdminControllers\Helper;
- use App\Module\System\Enums\CONFIG_TYPE;
- use App\Module\System\Enums\VIEW_TYPE;
- use Dcat\Admin\Form;
- use Dcat\Admin\Form\Field;
- /**
- * 表单辅助特性
- *
- * 提供系统模块后台控制器的表单构建功能的具体实现
- */
- trait FormHelperTrait
- {
- /**
- * 添加配置键名输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Text
- */
- public function textKeyname(string $field = 'keyname', string $label = '键名'): Field\Text
- {
- return $this->form->text($field, $label)
- ->required()
- ->rules('required|max:100|unique:app_configs,keyname,{{id}}')
- ->help('配置键名,唯一标识,不可重复');
- }
- /**
- * 添加配置标题输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Text
- */
- public function textTitle(string $field = 'title', string $label = '标题'): Field\Text
- {
- return $this->form->text($field, $label)
- ->required()
- ->rules('required|max:100')
- ->help('配置标题,用于显示');
- }
- /**
- * 添加配置类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectConfigType(string $field = 'type', string $label = '类型'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options([
- CONFIG_TYPE::TYPE_INT->value => '整数',
- CONFIG_TYPE::TYPE_IMG->value => '图片',
- CONFIG_TYPE::TYPE_BOOL->value => '布尔值',
- CONFIG_TYPE::TYPE_STRING->value => '字符串',
- CONFIG_TYPE::TYPE_FLOAT->value => '浮点数',
- CONFIG_TYPE::TYPE_FILE->value => '文件',
- CONFIG_TYPE::TYPE_PERCENTAGE->value => '百分比',
- CONFIG_TYPE::TYPE_TIME->value => '时间',
- CONFIG_TYPE::TYPE_IS->value => '是否',
- CONFIG_TYPE::TYPE_JSON->value => 'JSON数组',
- CONFIG_TYPE::TYPE_EMBEDS->value => 'JSON键值对',
- ])
- ->required()
- ->help('配置值的数据类型');
- }
- /**
- * 添加配置值输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Textarea
- */
- public function textareaValue(string $field = 'value', string $label = '值'): Field\Textarea
- {
- return $this->form->textarea($field, $label)
- ->rows(3)
- ->help('配置的值,根据类型不同有不同的格式要求');
- }
- /**
- * 添加配置分组输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Text
- */
- public function textGroup(string $field = 'group', string $label = '分组'): Field\Text
- {
- return $this->form->text($field, $label)
- ->help('配置分组,用于分类管理');
- }
- /**
- * 添加配置子分组输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Text
- */
- public function textGroup2(string $field = 'group2', string $label = '子分组'): Field\Text
- {
- return $this->form->text($field, $label)
- ->help('配置子分组,用于更细粒度的分类管理');
- }
- /**
- * 添加配置描述输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Textarea
- */
- public function textareaDesc(string $field = 'desc', string $label = '描述'): Field\Textarea
- {
- return $this->form->textarea($field, $label)
- ->rows(3)
- ->help('配置的详细描述');
- }
- /**
- * 添加配置选项输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Textarea
- */
- public function textareaOptions(string $field = 'options', string $label = '选项'): Field\Textarea
- {
- return $this->form->textarea($field, $label)
- ->rows(3)
- ->help('JSON格式的选项配置,如:{"option1":"选项1","option2":"选项2"}');
- }
- /**
- * 添加是否客户端可用选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Switch
- */
- public function switchIsClient(string $field = 'is_client', string $label = '客户端可用'): Field\Switch
- {
- return $this->form->switch($field, $label)
- ->default(false)
- ->help('是否允许客户端访问此配置');
- }
- /**
- * 添加视图类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectViewType(string $field = 'type1', string $label = '视图类型'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options([
- VIEW_TYPE::PRIVATE->value => '私有',
- VIEW_TYPE::PUBLIC->value => '公共',
- ])
- ->default(VIEW_TYPE::PRIVATE->value)
- ->help('视图的访问权限类型');
- }
- /**
- * 添加路由名称输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Text
- */
- public function textRouterName(string $field = 'router_name', string $label = '路由名称'): Field\Text
- {
- return $this->form->text($field, $label)
- ->help('视图对应的路由名称');
- }
- /**
- * 添加参数输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Textarea
- */
- public function textareaParams(string $field = 'p1', string $label = '参数'): Field\Textarea
- {
- return $this->form->textarea($field, $label)
- ->rows(3)
- ->help('JSON格式的参数配置');
- }
- }
|