add:新增表单生成
This commit is contained in:
parent
822ff3874a
commit
b4a1e9bc4e
@ -21,7 +21,7 @@
|
|||||||
"topthink/framework": "6.0.6",
|
"topthink/framework": "6.0.6",
|
||||||
"topthink/think-orm": "2.0.36",
|
"topthink/think-orm": "2.0.36",
|
||||||
"topthink/think-migration": "^3.0",
|
"topthink/think-migration": "^3.0",
|
||||||
"thans/tp-jwt-auth": "1.1",
|
"thans/tp-jwt-auth": "^1.1",
|
||||||
"overtrue/wechat": "^4.2",
|
"overtrue/wechat": "^4.2",
|
||||||
"phpoffice/phpspreadsheet": "^1.12",
|
"phpoffice/phpspreadsheet": "^1.12",
|
||||||
"dragonmantank/cron-expression": "3.1",
|
"dragonmantank/cron-expression": "3.1",
|
||||||
@ -30,7 +30,9 @@
|
|||||||
"overtrue/easy-sms": "^1.1",
|
"overtrue/easy-sms": "^1.1",
|
||||||
"jaguarjack/migration-generator": "dev-master",
|
"jaguarjack/migration-generator": "dev-master",
|
||||||
"lcobucci/jwt": "3.3",
|
"lcobucci/jwt": "3.3",
|
||||||
"jaguarjack/think-filesystem-cloud": "1.0"
|
"jaguarjack/think-filesystem-cloud": "1.0",
|
||||||
|
"topthink/think-view": "^1.0",
|
||||||
|
"xaboy/form-builder": "~2.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"topthink/think-trace": "^1.0",
|
"topthink/think-trace": "^1.0",
|
||||||
|
@ -3,7 +3,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace catcher;
|
namespace catcher;
|
||||||
|
|
||||||
use app\ExceptionHandle;
|
|
||||||
use catcher\exceptions\CatchException;
|
use catcher\exceptions\CatchException;
|
||||||
use catcher\exceptions\FailedException;
|
use catcher\exceptions\FailedException;
|
||||||
use think\db\exception\DataNotFoundException;
|
use think\db\exception\DataNotFoundException;
|
||||||
|
254
extend/catcher/library/form/Form.php
Normal file
254
extend/catcher/library/form/Form.php
Normal file
@ -0,0 +1,254 @@
|
|||||||
|
<?php
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Catch-CMS Design On 2020
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | CatchAdmin [Just Like ~ ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace catcher\library\form;
|
||||||
|
|
||||||
|
use catcher\exceptions\FailedException;
|
||||||
|
use FormBuilder\Exception\FormBuilderException;
|
||||||
|
use FormBuilder\Factory\Elm;
|
||||||
|
use FormBuilder\UI\Elm\Components\Upload;
|
||||||
|
use FormBuilder\UI\Elm\Traits\CascaderFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\CheckBoxFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\ColorPickerFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\DatePickerFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\FormStyleFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\FrameFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\GroupFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\HiddenFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\InputFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\InputNumberFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\RadioFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\RateFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\SelectFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\SliderFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\SwitchesFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\TimePickerFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\TreeFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\UploadFactoryTrait;
|
||||||
|
use FormBuilder\UI\Elm\Traits\ValidateFactoryTrait;
|
||||||
|
|
||||||
|
abstract class Form
|
||||||
|
{
|
||||||
|
use CascaderFactoryTrait;
|
||||||
|
use CheckBoxFactoryTrait;
|
||||||
|
use ColorPickerFactoryTrait;
|
||||||
|
use DatePickerFactoryTrait;
|
||||||
|
use FrameFactoryTrait;
|
||||||
|
use HiddenFactoryTrait;
|
||||||
|
use InputNumberFactoryTrait;
|
||||||
|
use InputFactoryTrait;
|
||||||
|
use RadioFactoryTrait;
|
||||||
|
use RateFactoryTrait;
|
||||||
|
use SliderFactoryTrait;
|
||||||
|
use SelectFactoryTrait;
|
||||||
|
use FormStyleFactoryTrait;
|
||||||
|
use SwitchesFactoryTrait;
|
||||||
|
use TimePickerFactoryTrait;
|
||||||
|
use TreeFactoryTrait;
|
||||||
|
use UploadFactoryTrait;
|
||||||
|
use ValidateFactoryTrait;
|
||||||
|
use GroupFactoryTrait;
|
||||||
|
use FormOptions;
|
||||||
|
use FormValidates;
|
||||||
|
|
||||||
|
protected $primaryKeyField = 'id';
|
||||||
|
|
||||||
|
protected $fieldsValue = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 必须实现的
|
||||||
|
*
|
||||||
|
* @time 2021年03月06日
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
abstract public function fields(): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 Form
|
||||||
|
*
|
||||||
|
* @time 2021年03月06日
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function create(): array
|
||||||
|
{
|
||||||
|
$fields = $this->getFields();
|
||||||
|
|
||||||
|
// 获取表单字段填充的值
|
||||||
|
if (method_exists($this, 'getFieldsValue')) {
|
||||||
|
$this->fieldsValue = $this->getFieldsValue();
|
||||||
|
|
||||||
|
$fields = $this->setFieldsValue($fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->rule($fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置 fields values
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @param $fields
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
protected function setFieldsValue($fields)
|
||||||
|
{
|
||||||
|
foreach ($fields as $field) {
|
||||||
|
$name = $field->getName();
|
||||||
|
|
||||||
|
if (isset($this->fieldsValue[$name])) {
|
||||||
|
$field->value($this->fieldsValue[$name]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 form fields
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getFields(): array
|
||||||
|
{
|
||||||
|
return array_merge($this->fields(), [
|
||||||
|
self::hidden($this->primaryKeyField, 0)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* form rule
|
||||||
|
*
|
||||||
|
* @time 2021年03月06日
|
||||||
|
* @param array $rules
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function rule(array $rules): array
|
||||||
|
{
|
||||||
|
if ($this->primaryKeyField) {
|
||||||
|
array_push($rules, self::hidden($this->primaryKeyField, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
try{
|
||||||
|
return Elm::createForm('', $rules)->formRule();
|
||||||
|
} catch (FormBuilderException $e) {
|
||||||
|
throw new FailedException('Form Created Failed: ' .$e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传图片地址
|
||||||
|
*
|
||||||
|
* @time 2021年03月03日
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function uploadImageUrl(): string
|
||||||
|
{
|
||||||
|
return env('app.domain') . '/cms/upload/image';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件地址
|
||||||
|
*
|
||||||
|
* @time 2021年03月10日
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function uploadImageFile(): string
|
||||||
|
{
|
||||||
|
return env('app.domain') . '/cms/upload/file';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传图片地址
|
||||||
|
*
|
||||||
|
* @time 2021年03月03日
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function uploadFileUrl(): string
|
||||||
|
{
|
||||||
|
return env('app.domain') . '/cms/upload/file';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected static function authorization(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'authorization' => 'Bearer ' . request()->user()->remember_token,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传图片
|
||||||
|
*
|
||||||
|
* @time 2021年03月03日
|
||||||
|
* @param $title
|
||||||
|
* @param string $value
|
||||||
|
* @return \FormBuilder\UI\Elm\Components\Upload
|
||||||
|
*/
|
||||||
|
public static function image(string $title, string $value = ''): Upload
|
||||||
|
{
|
||||||
|
return self::uploadImage('image', $title, self::uploadImageUrl(), $value)
|
||||||
|
->uploadName('image')
|
||||||
|
->data(['none' => ''])
|
||||||
|
->headers(self::authorization());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 多图
|
||||||
|
*
|
||||||
|
* @time 2021年03月03日
|
||||||
|
* @param $title
|
||||||
|
* @param array $value
|
||||||
|
* @return \FormBuilder\UI\Elm\Components\Upload
|
||||||
|
*/
|
||||||
|
public static function images(string $title, array $value = []): Upload
|
||||||
|
{
|
||||||
|
return self::uploadImages('image', $title, self::uploadImageUrl(), $value)
|
||||||
|
->uploadName('image')
|
||||||
|
->data(['none' => ''])
|
||||||
|
->headers(self::authorization());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
*
|
||||||
|
* @time 2021年03月10日
|
||||||
|
* @param string $title
|
||||||
|
* @param string $value
|
||||||
|
* @return Upload
|
||||||
|
*/
|
||||||
|
public static function file(string $title, string $value = ''): Upload
|
||||||
|
{
|
||||||
|
return self::uploadFile('file', $title, self::uploadFileUrl(), $value)
|
||||||
|
->uploadName('image')
|
||||||
|
->data(['none' => ''])
|
||||||
|
->headers(self::authorization());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传多文件
|
||||||
|
*
|
||||||
|
* @time 2021年03月10日
|
||||||
|
* @param string $title
|
||||||
|
* @param array $value
|
||||||
|
* @return Upload
|
||||||
|
*/
|
||||||
|
public static function files(string $title, array $value = []): Upload
|
||||||
|
{
|
||||||
|
return self::uploadFiles('file', $title, self::uploadFileUrl(), $value)
|
||||||
|
->uploadName('file')
|
||||||
|
->data(['none' => ''])
|
||||||
|
->headers(self::authorization());
|
||||||
|
}
|
||||||
|
}
|
34
extend/catcher/library/form/FormFactory.php
Normal file
34
extend/catcher/library/form/FormFactory.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
namespace catcher\library\form;
|
||||||
|
|
||||||
|
use catcher\exceptions\FailedException;
|
||||||
|
|
||||||
|
abstract class FormFactory
|
||||||
|
{
|
||||||
|
|
||||||
|
abstract public static function from();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建 Form
|
||||||
|
*
|
||||||
|
* @time 2021年03月02日
|
||||||
|
* @param $name
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function create($name)
|
||||||
|
{
|
||||||
|
$form = static::from() . '\\'. ucfirst($name);
|
||||||
|
|
||||||
|
if (!class_exists($form)) {
|
||||||
|
throw new FailedException(sprintf('Form [%s] not found! Please create it first', ucfirst($name)));
|
||||||
|
}
|
||||||
|
|
||||||
|
$form = app($form);
|
||||||
|
|
||||||
|
if (!$form instanceof Form) {
|
||||||
|
throw new FailedException(sprintf('Form [%s] must implements interface [FormCreate]', ucfirst($name)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $form->create();
|
||||||
|
}
|
||||||
|
}
|
42
extend/catcher/library/form/FormOptions.php
Normal file
42
extend/catcher/library/form/FormOptions.php
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Catch-CMS Design On 2020
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | CatchAdmin [Just Like ~ ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
// | Author: JaguarJack [ njphper@gmail.com ]
|
||||||
|
// +----------------------------------------------------------------------
|
||||||
|
|
||||||
|
namespace catcher\library\form;
|
||||||
|
|
||||||
|
|
||||||
|
trait FormOptions
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 是/否的选项
|
||||||
|
*
|
||||||
|
* @time 2021年03月06日
|
||||||
|
* @param array $label
|
||||||
|
* @param array $value
|
||||||
|
* @return array[]
|
||||||
|
*/
|
||||||
|
protected function yesOrNo(array $label = [], array $value = []): array
|
||||||
|
{
|
||||||
|
if (!count($label)) {
|
||||||
|
$label = ['是', '否'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!count($value)) {
|
||||||
|
$value = [1, 2];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
['value' => $value[0], 'label' => $label[0]],
|
||||||
|
['value' => $value[1], 'label' => $label[1]],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
126
extend/catcher/library/form/FormValidates.php
Normal file
126
extend/catcher/library/form/FormValidates.php
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
namespace catcher\library\form;
|
||||||
|
|
||||||
|
trait FormValidates
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 纯数字验证
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validateAlpha(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('^[A-Za-z]+$')->message('必须为纯字母');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字母和数字
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validateAlphaNum(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('^[A-Za-z0-9]+$')->message('必须为字母和数字');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validateAlphaDash(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('^[A-Za-z0-9\-\_]+$')->message('必须为字母和数字,下划线_及破折号-');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手机号
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validateMobile(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('^1[3-9]\d{9}$')->message('请输入正确的手机号格式');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 身份证
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validateIdCard(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)')->message('身份证输入格式不正确');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮政编码
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validateZip(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('\d{6}')->message('请输入有效的邮政编码');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IP 地址
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validateIp(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))')->message('请输入正确的 IP 地址');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 座机
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validateLandLine(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('\d{3}-\d{8}|\d{4}-\d{7}')->message('请输入正确的座机格式');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validatePassword(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('^[a-zA-Z]\w{5,18}$')->message('以字母开头,长度在6~18之间,只能包含字母、数字和下划线');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 强密码
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validateStrongPassword(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$')->message('必须包含大小写字母和数字的组合,不能使用特殊字符,长度在8-20之间');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 纯汉字
|
||||||
|
*
|
||||||
|
* @time 2021年03月12日
|
||||||
|
* @return \FormBuilder\UI\Elm\Validate
|
||||||
|
*/
|
||||||
|
public static function validateChineseCharacter(): \FormBuilder\UI\Elm\Validate
|
||||||
|
{
|
||||||
|
return Form::validateStr()->pattern('^[\u4e00-\u9fa5]{0,}$')->message('必须为纯汉字');
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user