公共库

This commit is contained in:
wuyanwen 2019-12-12 09:14:08 +08:00
parent d154f3e1ac
commit ca4272d7a6
8 changed files with 229 additions and 54 deletions

View File

@ -6,12 +6,14 @@ namespace catcher;
* @package catcher * @package catcher
* *
* *
* @method CatchForm text($column, $label = '') * @method CatchForm text($column, $label = '', $required = false)
* @method CatchForm image($column, $label = '') * @method CatchForm image($column, $label = '', $required = false)
* @method CatchForm radio($column, $label = '') * @method CatchForm radio($column, $label = '', $required = false)
* @method CatchForm select($column, $label = '') * @method CatchForm select($column, $label = '', $required = false)
* @method CatchForm textarea($column, $label = '') * @method CatchForm textarea($column, $label = '', $required = false)
* @method CatchForm password($column, $label = '') * @method CatchForm password($column, $label = '', $required = false)
* @method CatchForm hidden($column, $label = '', $required = false)
* @method CatchForm dom($column, $label = '', $required = false)
* *
*/ */
class CatchForm class CatchForm
@ -30,35 +32,65 @@ class CatchForm
protected $btn; protected $btn;
public function action($acton) /**
*
* @time 2019年12月10日
* @param $acton
* @return CatchForm
*/
public function action($acton): CatchForm
{ {
$this->action = $acton; $this->action = $acton;
return $this; return $this;
} }
public function method($method) /**
*
* @time 2019年12月10日
* @param $method
* @return CatchForm
*/
public function method($method): CatchForm
{ {
$this->method = $method; $this->method = $method;
return $this; return $this;
} }
public function formId($formId) /**
*
* @time 2019年12月10日
* @param $formId
* @return CatchForm
*/
public function formId($formId): CatchForm
{ {
$this->formId = $formId; $this->formId = $formId;
return $this; return $this;
} }
public function enctype($enctype ="multipart/form-data") /**
*
* @time 2019年12月10日
* @param string $enctype
* @return CatchForm
*/
public function enctype($enctype ="multipart/form-data"): CatchForm
{ {
$this->enctype = $enctype; $this->enctype = $enctype;
return $this; return $this;
} }
public function id($id) /**
*
* @time 2019年12月10日
* @param $id
* @return CatchForm
*/
public function id($id): CatchForm
{ {
$this->fields[$this->name] = array_merge($this->fields[$this->name], [ $this->fields[$this->name] = array_merge($this->fields[$this->name], [
'id' => sprintf('id="%s"', $id), 'id' => sprintf('id="%s"', $id),
@ -66,8 +98,15 @@ class CatchForm
return $this; return $this;
} }
/**
public function class($class='', $labelClass = '', $inlineClass = '') *
* @time 2019年12月10日
* @param string $class
* @param string $labelClass
* @param string $inlineClass
* @return CatchForm
*/
public function class($class='', $labelClass = '', $inlineClass = ''): CatchForm
{ {
$this->fields[$this->name] = array_merge($this->fields[$this->name], [ $this->fields[$this->name] = array_merge($this->fields[$this->name], [
'class' => $class, 'class' => $class,
@ -78,7 +117,13 @@ class CatchForm
return $this; return $this;
} }
public function options(array $options) /**
*
* @time 2019年12月10日
* @param array $options
* @return CatchForm
*/
public function options(array $options): CatchForm
{ {
$this->fields[$this->name] = array_merge($this->fields[$this->name], [ $this->fields[$this->name] = array_merge($this->fields[$this->name], [
'options' => $options, 'options' => $options,
@ -87,7 +132,13 @@ class CatchForm
return $this; return $this;
} }
public function default($value) /**
*
* @time 2019年12月10日
* @param $value
* @return CatchForm
*/
public function default($value): CatchForm
{ {
$this->fields[$this->name] = array_merge($this->fields[$this->name], [ $this->fields[$this->name] = array_merge($this->fields[$this->name], [
'default' => $value, 'default' => $value,
@ -96,8 +147,12 @@ class CatchForm
return $this; return $this;
} }
/**
public function disabled() *
* @time 2019年12月10日
* @return CatchForm
*/
public function disabled(): CatchForm
{ {
$this->fields[$this->name] = array_merge($this->fields[$this->name], [ $this->fields[$this->name] = array_merge($this->fields[$this->name], [
'disabled' => '', 'disabled' => '',
@ -106,7 +161,14 @@ class CatchForm
return $this; return $this;
} }
public function placeholder($content)
/**
*
* @time 2019年12月10日
* @param $content
* @return CatchForm
*/
public function placeholder($content): CatchForm
{ {
$this->fields[$this->name] = array_merge($this->fields[$this->name], [ $this->fields[$this->name] = array_merge($this->fields[$this->name], [
'placeholder' => 'placeholder='.$content, 'placeholder' => 'placeholder='.$content,
@ -115,7 +177,12 @@ class CatchForm
return $this; return $this;
} }
public function readonly() /**
*
* @time 2019年12月10日
* @return CatchForm
*/
public function readonly(): CatchForm
{ {
$this->fields[$this->name] = array_merge($this->fields[$this->name], [ $this->fields[$this->name] = array_merge($this->fields[$this->name], [
'readonly' => 'readonly', 'readonly' => 'readonly',
@ -124,24 +191,35 @@ class CatchForm
return $this; return $this;
} }
/**
public function render() *
* @time 2019年12月10日
* @return string
*/
public function render(): string
{ {
$form = sprintf('<form id="%s" lay-filter="%s" class="layui-form model-form">', $this->formId, $this->formId); $form = sprintf('<form id="%s" lay-filter="%s" class="layui-form model-form">', $this->formId, $this->formId);
foreach ($this->fields as $field) { foreach ($this->fields as $field) {
$form .= sprintf($this->baseField(), $form .= in_array($field['type'], ['hidden']) ?
$this->{$field['type'].'Field'}($field)
: sprintf($this->baseField(),
$field['labelClass'] ?? '', $field['labelClass'] ?? '',
$field['label'], $field['label'],
$field['inlineClass'] ?? '', $field['inlineClass'] ?? '',
$this->{$field['type'].'Field'}($field)) ; $this->{$field['type'].'Field'}($field));
} }
return $form . $this->btn. '</form>'; return $form . $this->btn. '</form>';
} }
public function append($append) /**
*
* @time 2019年12月10日
* @param $append
* @return CatchForm
*/
public function append($append): CatchForm
{ {
$this->fields[$this->name] = array_merge($this->fields[$this->name], [ $this->fields[$this->name] = array_merge($this->fields[$this->name], [
'append' => $append, 'append' => $append,
@ -150,23 +228,36 @@ class CatchForm
return $this; return $this;
} }
/**
*
* @time 2019年12月10日
* @param $method
* @param $arguments
* @return $this
*/
public function __call($method, $arguments) public function __call($method, $arguments)
{ {
// TODO: Implement __call() method. // TODO: Implement __call() method.
$this->name = $arguments[0] ?? ''; $this->name = $arguments[0] ?? '';
$label = $arguments[1] ?? ''; $label = $arguments[1] ?? '';
$required = $arguments[2] ?? false;
$this->fields[$this->name] = [ $this->fields[$this->name] = [
'name' => $this->name, 'name' => $this->name,
'type' => $method, 'type' => $method,
'label' => $label, 'label' => $required ? '<i style="color:red">*</i> '.$label : $label,
'inline' => false, 'inline' => false,
]; ];
return $this; return $this;
} }
protected function inline() /**
*
* @time 2019年12月10日
* @return CatchForm
*/
protected function inline(): CatchForm
{ {
$this->fields[] = array_merge($this->fields, [ $this->fields[] = array_merge($this->fields, [
'inline' => true, 'inline' => true,
@ -175,7 +266,12 @@ class CatchForm
return $this; return $this;
} }
private function baseField() /**
*
* @time 2019年12月10日
* @return string
*/
private function baseField(): string
{ {
return return
'<div class="layui-form-item"> '<div class="layui-form-item">
@ -192,9 +288,9 @@ class CatchForm
* @time 2019年12月06日 * @time 2019年12月06日
* @param $filter * @param $filter
* @param string $position * @param string $position
* @return string * @return CatchForm
*/ */
public function formBtn($filter, $position = 'text-right') public function formBtn($filter, $position = 'text-right'): CatchForm
{ {
$this->btn = sprintf('<div class="layui-form-item %s"> $this->btn = sprintf('<div class="layui-form-item %s">
<button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button> <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
@ -204,7 +300,14 @@ class CatchForm
return $this; return $this;
} }
public function verify($rule, $equalTo = []) /**
*
* @time 2019年12月10日
* @param $rule
* @param array $equalTo
* @return CatchForm
*/
public function verify($rule, $equalTo = []): CatchForm
{ {
if (empty($equalTo)) { if (empty($equalTo)) {
$this->fields[$this->name] = array_merge($this->fields[$this->name], [ $this->fields[$this->name] = array_merge($this->fields[$this->name], [
@ -222,6 +325,12 @@ class CatchForm
return $this; return $this;
} }
/**
*
* @time 2019年12月10日
* @param $field
* @return string
*/
private function textField($field) private function textField($field)
{ {
return return
@ -238,6 +347,12 @@ class CatchForm
} }
/**
*
* @time 2019年12月10日
* @param $field
* @return string
*/
private function selectField($field) private function selectField($field)
{ {
$select = sprintf('<select name="%s">', $field['name']); $select = sprintf('<select name="%s">', $field['name']);
@ -251,6 +366,12 @@ class CatchForm
return $select . '</select>'; return $select . '</select>';
} }
/**
*
* @time 2019年12月10日
* @param $field
* @return string
*/
private function passwordField($field) private function passwordField($field)
{ {
return sprintf('<input name="%s" class="layui-input" %s type="password" %s %s>', return sprintf('<input name="%s" class="layui-input" %s type="password" %s %s>',
@ -261,11 +382,50 @@ class CatchForm
); );
} }
private function radioField() private function radioField($field)
{} {
$radio = '';
foreach ($field['options'] as $option) {
$radio .= sprintf('<input name="%s" type="radio" value="%s" title="%s" %s/>',
$field['name'], $option['value'], $option['title'], $option['value'] == $field['default'] ? 'checked' : ''
);
}
private function textareaField() return $radio;
{} }
/**
*
* @time 2019年12月09日
* @param $field
* @return string
*/
private function textareaField($field): string
{
return sprintf('<textarea name="%s" %s class="layui-textarea">%s</textarea>',
$field['name'],
$field['placeholder'] ?? '',
$field['default'] ?? ''
);
}
private function domField($field)
{
return $field['name'];
}
/**
*
* @time 2019年12月10日
* @param $field
* @return string
*/
private function hiddenField($field): string
{
return sprintf('<input name="%s" value="%s" type="hidden">',
$field['name'], $field['default']
);
}
private function imageField() private function imageField()
{} {}

View File

@ -1 +1,22 @@
<?php <?php
namespace catcher;
class Tree
{
public static function done(array $items, $pid = 0, $pidField = 'parent_id', $children = 'children')
{
$tree = [];
foreach ($items as $key => $item) {
if ($item[$pidField] == $pid) {
$child = self::done($items, $item['id'], $pidField);
$item[$children] = count($child) ? $child : [];
$tree[] = $item;
}
}
return $tree;
}
}

View File

@ -4,7 +4,7 @@ namespace catcher\base;
use catcher\CatchAdmin; use catcher\CatchAdmin;
use think\facade\View; use think\facade\View;
abstract class BaseController abstract class CatchController
{ {
/** /**
* *

View File

@ -5,7 +5,7 @@ use catcher\traits\db\BaseOptionsTrait;
use catcher\traits\db\TransTrait; use catcher\traits\db\TransTrait;
use think\model\concern\SoftDelete; use think\model\concern\SoftDelete;
abstract class BaseModel extends \think\Model abstract class CatchModel extends \think\Model
{ {
use SoftDelete; use SoftDelete;
use TransTrait; use TransTrait;

View File

@ -5,7 +5,7 @@ use app\Request;
use catcher\exceptions\ValidateFailedException; use catcher\exceptions\ValidateFailedException;
use think\Validate; use think\Validate;
abstract class BaseRequest extends Request abstract class CatchRequest extends Request
{ {
/** /**
* Request constructor. * Request constructor.

View File

@ -4,7 +4,7 @@ namespace catcher\base;
use catcher\validates\Sometimes; use catcher\validates\Sometimes;
use think\Validate; use think\Validate;
abstract class BaseValidate extends Validate abstract class CatchValidate extends Validate
{ {
public function __construct() public function __construct()
{ {

View File

@ -1 +1,7 @@
<?php <?php
namespace catcher\exceptions;
class FailedException extends \Exception
{
protected $code = 10010;
}

View File

@ -12,13 +12,7 @@ trait BaseOptionsTrait
*/ */
public function storeBy(array $data) public function storeBy(array $data)
{ {
foreach ($data as $field => $value) { if ($this->allowField($this->field)->save($data)) {
if (in_array($field, $this->field)) {
$this->{$field} = $value;
}
}
if ($this->save()) {
return $this->{$this->getPk()}; return $this->{$this->getPk()};
} }
@ -34,15 +28,9 @@ trait BaseOptionsTrait
*/ */
public function updateBy($id, $data) public function updateBy($id, $data)
{ {
$model = $this->findBy($id);
foreach ($data as $field => $value) {
if (in_array($field, $this->field)) {
$model->{$field} = $value;
}
}
if ($model->save()) { if (static::update($data, [$this->getPk() => $id], $this->field)) {
return $model->id; return true;
} }
return false; return false;