FormHelperTrait.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace App\Module\System\AdminControllers\Helper;
  3. use App\Module\System\Enums\CONFIG_TYPE;
  4. use App\Module\System\Enums\VIEW_TYPE;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Form\Field;
  7. /**
  8. * 表单辅助特性
  9. *
  10. * 提供系统模块后台控制器的表单构建功能的具体实现
  11. */
  12. trait FormHelperTrait
  13. {
  14. /**
  15. * 添加配置键名输入
  16. *
  17. * @param string $field 字段名
  18. * @param string $label 标签名
  19. * @return Field\Text
  20. */
  21. public function textKeyname(string $field = 'keyname', string $label = '键名'): Field\Text
  22. {
  23. return $this->form->text($field, $label)
  24. ->required()
  25. ->rules('required|max:100|unique:app_configs,keyname,{{id}}')
  26. ->help('配置键名,唯一标识,不可重复');
  27. }
  28. /**
  29. * 添加配置标题输入
  30. *
  31. * @param string $field 字段名
  32. * @param string $label 标签名
  33. * @return Field\Text
  34. */
  35. public function textTitle(string $field = 'title', string $label = '标题'): Field\Text
  36. {
  37. return $this->form->text($field, $label)
  38. ->required()
  39. ->rules('required|max:100')
  40. ->help('配置标题,用于显示');
  41. }
  42. /**
  43. * 添加配置类型选择
  44. *
  45. * @param string $field 字段名
  46. * @param string $label 标签名
  47. * @return Field\Select
  48. */
  49. public function selectConfigType(string $field = 'type', string $label = '类型'): Field\Select
  50. {
  51. return $this->form->select($field, $label)
  52. ->options([
  53. CONFIG_TYPE::TYPE_INT->value => '整数',
  54. CONFIG_TYPE::TYPE_IMG->value => '图片',
  55. CONFIG_TYPE::TYPE_BOOL->value => '布尔值',
  56. CONFIG_TYPE::TYPE_STRING->value => '字符串',
  57. CONFIG_TYPE::TYPE_FLOAT->value => '浮点数',
  58. CONFIG_TYPE::TYPE_FILE->value => '文件',
  59. CONFIG_TYPE::TYPE_PERCENTAGE->value => '百分比',
  60. CONFIG_TYPE::TYPE_TIME->value => '时间',
  61. CONFIG_TYPE::TYPE_IS->value => '是否',
  62. CONFIG_TYPE::TYPE_JSON->value => 'JSON数组',
  63. CONFIG_TYPE::TYPE_EMBEDS->value => 'JSON键值对',
  64. ])
  65. ->required()
  66. ->help('配置值的数据类型');
  67. }
  68. /**
  69. * 添加配置值输入
  70. *
  71. * @param string $field 字段名
  72. * @param string $label 标签名
  73. * @return Field\Textarea
  74. */
  75. public function textareaValue(string $field = 'value', string $label = '值'): Field\Textarea
  76. {
  77. return $this->form->textarea($field, $label)
  78. ->rows(3)
  79. ->help('配置的值,根据类型不同有不同的格式要求');
  80. }
  81. /**
  82. * 添加配置分组输入
  83. *
  84. * @param string $field 字段名
  85. * @param string $label 标签名
  86. * @return Field\Text
  87. */
  88. public function textGroup(string $field = 'group', string $label = '分组'): Field\Text
  89. {
  90. return $this->form->text($field, $label)
  91. ->help('配置分组,用于分类管理');
  92. }
  93. /**
  94. * 添加配置子分组输入
  95. *
  96. * @param string $field 字段名
  97. * @param string $label 标签名
  98. * @return Field\Text
  99. */
  100. public function textGroup2(string $field = 'group2', string $label = '子分组'): Field\Text
  101. {
  102. return $this->form->text($field, $label)
  103. ->help('配置子分组,用于更细粒度的分类管理');
  104. }
  105. /**
  106. * 添加配置描述输入
  107. *
  108. * @param string $field 字段名
  109. * @param string $label 标签名
  110. * @return Field\Textarea
  111. */
  112. public function textareaDesc(string $field = 'desc', string $label = '描述'): Field\Textarea
  113. {
  114. return $this->form->textarea($field, $label)
  115. ->rows(3)
  116. ->help('配置的详细描述');
  117. }
  118. /**
  119. * 添加配置选项输入
  120. *
  121. * @param string $field 字段名
  122. * @param string $label 标签名
  123. * @return Field\Textarea
  124. */
  125. public function textareaOptions(string $field = 'options', string $label = '选项'): Field\Textarea
  126. {
  127. return $this->form->textarea($field, $label)
  128. ->rows(3)
  129. ->help('JSON格式的选项配置,如:{"option1":"选项1","option2":"选项2"}');
  130. }
  131. /**
  132. * 添加是否客户端可用选择
  133. *
  134. * @param string $field 字段名
  135. * @param string $label 标签名
  136. * @return Field\Switch
  137. */
  138. public function switchIsClient(string $field = 'is_client', string $label = '客户端可用'): Field\Switch
  139. {
  140. return $this->form->switch($field, $label)
  141. ->default(false)
  142. ->help('是否允许客户端访问此配置');
  143. }
  144. /**
  145. * 添加视图类型选择
  146. *
  147. * @param string $field 字段名
  148. * @param string $label 标签名
  149. * @return Field\Select
  150. */
  151. public function selectViewType(string $field = 'type1', string $label = '视图类型'): Field\Select
  152. {
  153. return $this->form->select($field, $label)
  154. ->options([
  155. VIEW_TYPE::PRIVATE->value => '私有',
  156. VIEW_TYPE::PUBLIC->value => '公共',
  157. ])
  158. ->default(VIEW_TYPE::PRIVATE->value)
  159. ->help('视图的访问权限类型');
  160. }
  161. /**
  162. * 添加路由名称输入
  163. *
  164. * @param string $field 字段名
  165. * @param string $label 标签名
  166. * @return Field\Text
  167. */
  168. public function textRouterName(string $field = 'router_name', string $label = '路由名称'): Field\Text
  169. {
  170. return $this->form->text($field, $label)
  171. ->help('视图对应的路由名称');
  172. }
  173. /**
  174. * 添加参数输入
  175. *
  176. * @param string $field 字段名
  177. * @param string $label 标签名
  178. * @return Field\Textarea
  179. */
  180. public function textareaParams(string $field = 'p1', string $label = '参数'): Field\Textarea
  181. {
  182. return $this->form->textarea($field, $label)
  183. ->rows(3)
  184. ->help('JSON格式的参数配置');
  185. }
  186. }