diff --git a/composer.json b/composer.json index 18871b4..6706fc2 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "topthink/framework": "6.0.6", "topthink/think-orm": "2.0.36", "topthink/think-migration": "^3.0", - "thans/tp-jwt-auth": "1.1", + "thans/tp-jwt-auth": "^1.1", "overtrue/wechat": "^4.2", "phpoffice/phpspreadsheet": "^1.12", "dragonmantank/cron-expression": "3.1", @@ -30,7 +30,9 @@ "overtrue/easy-sms": "^1.1", "jaguarjack/migration-generator": "dev-master", "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": { "topthink/think-trace": "^1.0", diff --git a/extend/catcher/CatchExceptionHandle.php b/extend/catcher/CatchExceptionHandle.php index e86ea2f..7c94f27 100644 --- a/extend/catcher/CatchExceptionHandle.php +++ b/extend/catcher/CatchExceptionHandle.php @@ -3,7 +3,6 @@ declare(strict_types=1); namespace catcher; -use app\ExceptionHandle; use catcher\exceptions\CatchException; use catcher\exceptions\FailedException; use think\db\exception\DataNotFoundException; diff --git a/extend/catcher/library/form/Form.php b/extend/catcher/library/form/Form.php new file mode 100644 index 0000000..10cb7bd --- /dev/null +++ b/extend/catcher/library/form/Form.php @@ -0,0 +1,254 @@ +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()); + } +} diff --git a/extend/catcher/library/form/FormFactory.php b/extend/catcher/library/form/FormFactory.php new file mode 100644 index 0000000..cf09ed0 --- /dev/null +++ b/extend/catcher/library/form/FormFactory.php @@ -0,0 +1,34 @@ +create(); + } +} \ No newline at end of file diff --git a/extend/catcher/library/form/FormOptions.php b/extend/catcher/library/form/FormOptions.php new file mode 100644 index 0000000..acc74e0 --- /dev/null +++ b/extend/catcher/library/form/FormOptions.php @@ -0,0 +1,42 @@ + $value[0], 'label' => $label[0]], + ['value' => $value[1], 'label' => $label[1]], + ]; + } +} diff --git a/extend/catcher/library/form/FormValidates.php b/extend/catcher/library/form/FormValidates.php new file mode 100644 index 0000000..9f086f9 --- /dev/null +++ b/extend/catcher/library/form/FormValidates.php @@ -0,0 +1,126 @@ +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('必须为纯汉字'); + } +} \ No newline at end of file