| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- namespace App\Module\Ulogic\AdminControllers\Helper;
- use App\Module\AppMessage\Enums\APP_MESSAGE_STATUS;
- use App\Module\AppMessage\Enums\APP_MESSAGE_TYPE;
- use App\Module\AppMessage\Enums\APP_SENDER_TYPE;
- use App\Module\Ulogic\Enum\PUNISH_TYPE;
- use Dcat\Admin\Form;
- use Dcat\Admin\Form\Field;
- /**
- * 表单辅助特性
- *
- * 提供用户逻辑模块后台控制器的表单构建功能的具体实现
- */
- trait FormHelperTrait
- {
- /**
- * 添加用户ID输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Number
- */
- public function numberUserId(string $field = 'user_id', string $label = '用户ID'): Field\Number
- {
- return $this->form->number($field, $label)
- ->required()
- ->min(1)
- ->help('用户ID,必须是有效的用户');
- }
- /**
- * 添加管理员ID输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Number
- */
- public function numberAdminId(string $field = 'admin_id', string $label = '管理员ID'): Field\Number
- {
- return $this->form->number($field, $label)
- ->min(1)
- ->help('管理员ID,必须是有效的管理员');
- }
- /**
- * 添加惩罚类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectPunishType(string $field = 'type', string $label = '惩罚类型'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options(function () {
- $options = [];
- foreach (PUNISH_TYPE::cases() as $case) {
- $options[$case->name] = $case->name;
- }
- return $options;
- })
- ->required();
- }
- /**
- * 添加惩罚等级输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Number
- */
- public function numberPunishLevel(string $field = 'level', string $label = '惩罚等级'): Field\Number
- {
- return $this->form->number($field, $label)
- ->min(1)
- ->max(10)
- ->default(1)
- ->required()
- ->help('惩罚等级,1-10级');
- }
- /**
- * 添加描述输入
- *
- * @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)
- ->required();
- }
- /**
- * 添加消息标题输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Text
- */
- public function textMessageTitle(string $field = 'title', string $label = '消息标题'): Field\Text
- {
- return $this->form->text($field, $label)
- ->required()
- ->rules('required|max:100');
- }
- /**
- * 添加消息内容输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Textarea
- */
- public function textareaMessageContent(string $field = 'content', string $label = '消息内容'): Field\Textarea
- {
- return $this->form->textarea($field, $label)
- ->rows(5)
- ->required()
- ->rules('required');
- }
- /**
- * 添加消息类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectMessageType(string $field = 'type', string $label = '消息类型'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options([
- APP_MESSAGE_TYPE::SYSTEM->value => '系统消息',
- APP_MESSAGE_TYPE::USER->value => '用户消息',
- APP_MESSAGE_TYPE::BUSINESS->value => '业务消息',
- APP_MESSAGE_TYPE::SECURITY->value => '安全消息',
- ])
- ->default(APP_MESSAGE_TYPE::SYSTEM->value)
- ->required();
- }
- /**
- * 添加消息状态选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectMessageStatus(string $field = 'status', string $label = '消息状态'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options([
- APP_MESSAGE_STATUS::DRAFT->value => '草稿',
- APP_MESSAGE_STATUS::PUBLISHED->value => '已发布',
- APP_MESSAGE_STATUS::ARCHIVED->value => '已归档',
- ])
- ->default(APP_MESSAGE_STATUS::DRAFT->value)
- ->required();
- }
- /**
- * 添加发送者类型选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Select
- */
- public function selectSenderType(string $field = 'sender_type', string $label = '发送者类型'): Field\Select
- {
- return $this->form->select($field, $label)
- ->options([
- APP_SENDER_TYPE::SYSTEM->value => '系统',
- APP_SENDER_TYPE::USER->value => '用户',
- APP_SENDER_TYPE::ADMIN->value => '管理员',
- ])
- ->default(APP_SENDER_TYPE::SYSTEM->value)
- ->required();
- }
- /**
- * 添加发送者ID输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Number
- */
- public function numberSenderId(string $field = 'sender_id', string $label = '发送者ID'): Field\Number
- {
- return $this->form->number($field, $label)
- ->min(0)
- ->default(0)
- ->help('发送者ID,系统消息可为0');
- }
- /**
- * 添加接收者ID输入
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Number
- */
- public function numberReceiverId(string $field = 'receiver_id', string $label = '接收者ID'): Field\Number
- {
- return $this->form->number($field, $label)
- ->min(0)
- ->default(0)
- ->help('接收者ID,0表示全部用户');
- }
- /**
- * 添加是否允许回复选择
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Field\Switch
- */
- public function switchAllowReply(string $field = 'allow_reply', string $label = '允许回复'): Field\Switch
- {
- return $this->form->switch($field, $label)
- ->default(false);
- }
- }
|