FormHelperTrait.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace App\Module\Ulogic\AdminControllers\Helper;
  3. use App\Module\AppMessage\Enums\APP_MESSAGE_STATUS;
  4. use App\Module\AppMessage\Enums\APP_MESSAGE_TYPE;
  5. use App\Module\AppMessage\Enums\APP_SENDER_TYPE;
  6. use App\Module\Ulogic\Enum\PUNISH_TYPE;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Form\Field;
  9. /**
  10. * 表单辅助特性
  11. *
  12. * 提供用户逻辑模块后台控制器的表单构建功能的具体实现
  13. */
  14. trait FormHelperTrait
  15. {
  16. /**
  17. * 添加用户ID输入
  18. *
  19. * @param string $field 字段名
  20. * @param string $label 标签名
  21. * @return Field\Number
  22. */
  23. public function numberUserId(string $field = 'user_id', string $label = '用户ID'): Field\Number
  24. {
  25. return $this->form->number($field, $label)
  26. ->required()
  27. ->min(1)
  28. ->help('用户ID,必须是有效的用户');
  29. }
  30. /**
  31. * 添加管理员ID输入
  32. *
  33. * @param string $field 字段名
  34. * @param string $label 标签名
  35. * @return Field\Number
  36. */
  37. public function numberAdminId(string $field = 'admin_id', string $label = '管理员ID'): Field\Number
  38. {
  39. return $this->form->number($field, $label)
  40. ->min(1)
  41. ->help('管理员ID,必须是有效的管理员');
  42. }
  43. /**
  44. * 添加惩罚类型选择
  45. *
  46. * @param string $field 字段名
  47. * @param string $label 标签名
  48. * @return Field\Select
  49. */
  50. public function selectPunishType(string $field = 'type', string $label = '惩罚类型'): Field\Select
  51. {
  52. return $this->form->select($field, $label)
  53. ->options(function () {
  54. $options = [];
  55. foreach (PUNISH_TYPE::cases() as $case) {
  56. $options[$case->name] = $case->name;
  57. }
  58. return $options;
  59. })
  60. ->required();
  61. }
  62. /**
  63. * 添加惩罚等级输入
  64. *
  65. * @param string $field 字段名
  66. * @param string $label 标签名
  67. * @return Field\Number
  68. */
  69. public function numberPunishLevel(string $field = 'level', string $label = '惩罚等级'): Field\Number
  70. {
  71. return $this->form->number($field, $label)
  72. ->min(1)
  73. ->max(10)
  74. ->default(1)
  75. ->required()
  76. ->help('惩罚等级,1-10级');
  77. }
  78. /**
  79. * 添加描述输入
  80. *
  81. * @param string $field 字段名
  82. * @param string $label 标签名
  83. * @return Field\Textarea
  84. */
  85. public function textareaDesc(string $field = 'desc', string $label = '描述'): Field\Textarea
  86. {
  87. return $this->form->textarea($field, $label)
  88. ->rows(3)
  89. ->required();
  90. }
  91. /**
  92. * 添加消息标题输入
  93. *
  94. * @param string $field 字段名
  95. * @param string $label 标签名
  96. * @return Field\Text
  97. */
  98. public function textMessageTitle(string $field = 'title', string $label = '消息标题'): Field\Text
  99. {
  100. return $this->form->text($field, $label)
  101. ->required()
  102. ->rules('required|max:100');
  103. }
  104. /**
  105. * 添加消息内容输入
  106. *
  107. * @param string $field 字段名
  108. * @param string $label 标签名
  109. * @return Field\Textarea
  110. */
  111. public function textareaMessageContent(string $field = 'content', string $label = '消息内容'): Field\Textarea
  112. {
  113. return $this->form->textarea($field, $label)
  114. ->rows(5)
  115. ->required()
  116. ->rules('required');
  117. }
  118. /**
  119. * 添加消息类型选择
  120. *
  121. * @param string $field 字段名
  122. * @param string $label 标签名
  123. * @return Field\Select
  124. */
  125. public function selectMessageType(string $field = 'type', string $label = '消息类型'): Field\Select
  126. {
  127. return $this->form->select($field, $label)
  128. ->options([
  129. APP_MESSAGE_TYPE::SYSTEM->value => '系统消息',
  130. APP_MESSAGE_TYPE::USER->value => '用户消息',
  131. APP_MESSAGE_TYPE::BUSINESS->value => '业务消息',
  132. APP_MESSAGE_TYPE::SECURITY->value => '安全消息',
  133. ])
  134. ->default(APP_MESSAGE_TYPE::SYSTEM->value)
  135. ->required();
  136. }
  137. /**
  138. * 添加消息状态选择
  139. *
  140. * @param string $field 字段名
  141. * @param string $label 标签名
  142. * @return Field\Select
  143. */
  144. public function selectMessageStatus(string $field = 'status', string $label = '消息状态'): Field\Select
  145. {
  146. return $this->form->select($field, $label)
  147. ->options([
  148. APP_MESSAGE_STATUS::DRAFT->value => '草稿',
  149. APP_MESSAGE_STATUS::PUBLISHED->value => '已发布',
  150. APP_MESSAGE_STATUS::ARCHIVED->value => '已归档',
  151. ])
  152. ->default(APP_MESSAGE_STATUS::DRAFT->value)
  153. ->required();
  154. }
  155. /**
  156. * 添加发送者类型选择
  157. *
  158. * @param string $field 字段名
  159. * @param string $label 标签名
  160. * @return Field\Select
  161. */
  162. public function selectSenderType(string $field = 'sender_type', string $label = '发送者类型'): Field\Select
  163. {
  164. return $this->form->select($field, $label)
  165. ->options([
  166. APP_SENDER_TYPE::SYSTEM->value => '系统',
  167. APP_SENDER_TYPE::USER->value => '用户',
  168. APP_SENDER_TYPE::ADMIN->value => '管理员',
  169. ])
  170. ->default(APP_SENDER_TYPE::SYSTEM->value)
  171. ->required();
  172. }
  173. /**
  174. * 添加发送者ID输入
  175. *
  176. * @param string $field 字段名
  177. * @param string $label 标签名
  178. * @return Field\Number
  179. */
  180. public function numberSenderId(string $field = 'sender_id', string $label = '发送者ID'): Field\Number
  181. {
  182. return $this->form->number($field, $label)
  183. ->min(0)
  184. ->default(0)
  185. ->help('发送者ID,系统消息可为0');
  186. }
  187. /**
  188. * 添加接收者ID输入
  189. *
  190. * @param string $field 字段名
  191. * @param string $label 标签名
  192. * @return Field\Number
  193. */
  194. public function numberReceiverId(string $field = 'receiver_id', string $label = '接收者ID'): Field\Number
  195. {
  196. return $this->form->number($field, $label)
  197. ->min(0)
  198. ->default(0)
  199. ->help('接收者ID,0表示全部用户');
  200. }
  201. /**
  202. * 添加是否允许回复选择
  203. *
  204. * @param string $field 字段名
  205. * @param string $label 标签名
  206. * @return Field\Switch
  207. */
  208. public function switchAllowReply(string $field = 'allow_reply', string $label = '允许回复'): Field\Switch
  209. {
  210. return $this->form->switch($field, $label)
  211. ->default(false);
  212. }
  213. }