diff --git a/src/ApiController.php b/src/ApiController.php index 03ac990..fb936bf 100644 --- a/src/ApiController.php +++ b/src/ApiController.php @@ -19,6 +19,7 @@ namespace DtApp\ThinkLibrary; +use DtApp\ThinkLibrary\helper\ValidateHelper; use stdClass; use think\App; use think\exception\HttpResponseException; @@ -29,7 +30,7 @@ use think\Request; * Class ApiController * @package DtApp\ThinkLibrary */ -class ApiController extends stdClass +abstract class ApiController extends stdClass { /** * 应用容器 @@ -162,23 +163,14 @@ class ApiController extends stdClass } /** - * 数据回调处理机制 - * @param string $name 回调方法名称 - * @param mixed $one 回调引用参数1 - * @param mixed $two 回调引用参数2 - * @return boolean + * @param array $rules + * @param string $type + * @return mixed */ - public function callback($name, &$one = [], &$two = []): bool + protected function _vali(array $rules, $type = '') { - if (is_callable($name)) { - return $name($this, $one, $two); - } - foreach ([$name, "_{$this->app->request->action()}{$name}"] as $method) { - if (method_exists($this, $method) && false === $this->$method($one, $two)) { - return false; - } - } - return true; + return ValidateHelper::instance() + ->init($rules, $type); } /** diff --git a/src/Controller.php b/src/Controller.php index ea18481..f57510a 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -29,7 +29,7 @@ use think\Request; * Class Controller * @package DtApp\ThinkLibrary */ -class Controller extends stdClass +abstract class Controller extends stdClass { /** * 应用容器 diff --git a/src/Helper.php b/src/Helper.php new file mode 100644 index 0000000..4647fee --- /dev/null +++ b/src/Helper.php @@ -0,0 +1,83 @@ +app = $app; + $this->class = $class; + } + + /** + * 获取数据库对象 + * @param $dbQuery + * @return Db + */ + protected function buildQuery($dbQuery) + { + return is_string($dbQuery) ? $this->app->db->name($dbQuery) : $dbQuery; + } + + /** + * 实例对象反射 + * @param mixed ...$args + * @return Helper + */ + public static function instance(...$args): Helper + { + return Container::getInstance()->invokeClass(static::class, $args); + } +} \ No newline at end of file diff --git a/src/helper/Files.php b/src/helper/Files.php index e33c2bd..8967ae4 100644 --- a/src/helper/Files.php +++ b/src/helper/Files.php @@ -43,12 +43,7 @@ class Files if (empty($name)) { throw new DtaException('请检查需要删除文件夹的名称'); } - if (file_exists($name)) { - if (unlink($name)) { - return true; - } - } - return false; + return file_exists($name) && unlink($name); } /** diff --git a/src/helper/Requests.php b/src/helper/Requests.php index c7ccb5e..35ad746 100644 --- a/src/helper/Requests.php +++ b/src/helper/Requests.php @@ -134,14 +134,9 @@ class Requests } } //协议法,因为有可能不准确,放到最后判断 - if (isset($_SERVER['HTTP_ACCEPT'])) { - // 如果只支持wml并且不支持html那一定是移动设备 - // 如果支持wml和html但是wml在html之前则是移动设备 - if ((strpos(request()->server('HTTP_ACCEPT'), 'vnd.wap.wml') !== false) && (strpos(request()->server('HTTP_ACCEPT'), 'text/html') === false || (strpos(request()->server('HTTP_ACCEPT'), 'vnd.wap.wml') < strpos(request()->server('HTTP_ACCEPT'), 'text/html')))) { - return true; - } - } - return false; + // 如果只支持wml并且不支持html那一定是移动设备 + // 如果支持wml和html但是wml在html之前则是移动设备 + return isset($_SERVER['HTTP_ACCEPT']) && (strpos(request()->server('HTTP_ACCEPT'), 'vnd.wap.wml') !== false) && (strpos(request()->server('HTTP_ACCEPT'), 'text/html') === false || (strpos(request()->server('HTTP_ACCEPT'), 'vnd.wap.wml') < strpos(request()->server('HTTP_ACCEPT'), 'text/html'))); } /** diff --git a/src/helper/ValidateHelper.php b/src/helper/ValidateHelper.php new file mode 100644 index 0000000..81a6108 --- /dev/null +++ b/src/helper/ValidateHelper.php @@ -0,0 +1,81 @@ + message // 最大值限定 + * age.between:1,120 => message // 范围限定 + * name.require => message // 必填内容 + * name.default => 100 // 获取并设置默认值 + * region.value => value // 固定字段数值内容 + * 更多规则参照 ThinkPHP 官方的验证类 + * + * @param array $rules 验证规则( 验证信息数组 ) + * @param string $input 输入内容 ( post. 或 get. ) + * @param callable|null $callable 异常处理操作 + * @return array + */ + public function init(array $rules, $input = '', ?callable $callable = null): array + { + if (is_string($input)) { + $type = trim($input, '.') ?: 'request'; + $input = $this->app->request->$type(); + } + [$data, $rule, $info] = [[], [], []]; + foreach ($rules as $name => $message) { + if (is_numeric($name)) { + [$name, $alias] = explode('#', $message . '#'); + $data[$name] = $input[($alias ?: $name)] ?? null; + } elseif (strpos($name, '.') === false) { + $data[$name] = $message; + } elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', $name . '#', $matches)) { + [, $_key, $_rule, $alias] = $matches; + if (in_array($_rule, ['value', 'default'])) { + if ($_rule === 'value') { + $data[$_key] = $message; + } elseif ($_rule === 'default') { + $data[$_key] = $input[($alias ?: $_key)] ?? $message; + } + } else { + $info[explode(':', $name)[0]] = $message; + $data[$_key] = $data[$_key] ?? ($input[($alias ?: $_key)] ?? null); + $rule[$_key] = isset($rule[$_key]) ? ($rule[$_key] . '|' . $_rule) : $_rule; + } + } + } + $validate = new Validate(); + if ($validate->rule($rule)->message($info)->check($data)) { + return $data; + } + + if (is_callable($callable)) { + return $callable($validate->getError()); + } + + $this->class->error($validate->getError()); + } +} \ No newline at end of file