- 优化门面

- 增加验证器
v6
李光春 4 years ago
parent 515a769293
commit c4922df3de

@ -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);
}
/**

@ -29,7 +29,7 @@ use think\Request;
* Class Controller
* @package DtApp\ThinkLibrary
*/
class Controller extends stdClass
abstract class Controller extends stdClass
{
/**
* 应用容器

@ -0,0 +1,83 @@
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 https://github.com/GC0202/ThinkLibrary
// | gitlab 仓库地址 https://gitlab.com/liguangchun/thinklibrary
// | weixin 仓库地址 https://git.weixin.qq.com/liguangchun/ThinkLibrary
// | huaweicloud 仓库地址 https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace DtApp\ThinkLibrary;
use think\App;
use think\Container;
use think\Db;
/**
* 控制器挂件
* Class Helper
* @package DtApp\ThinkLibrary
*/
abstract class Helper
{
/**
* 应用容器
* @var App
*/
public $app;
/**
* 数据库实例
* @var
*/
public $query;
/**
* 控制器实例
* @var Controller
*/
public $class;
/**
* Helper constructor.
* @param Controller $class
* @param App $app
*/
public function __construct(Controller $class, App $app)
{
$this->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);
}
}

@ -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);
}
/**

@ -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')));
}
/**

@ -0,0 +1,81 @@
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 https://github.com/GC0202/ThinkLibrary
// | gitlab 仓库地址 https://gitlab.com/liguangchun/thinklibrary
// | weixin 仓库地址 https://git.weixin.qq.com/liguangchun/ThinkLibrary
// | huaweicloud 仓库地址 https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\helper;
use DtApp\ThinkLibrary\Helper;
use think\Validate;
class ValidateHelper extends Helper
{
/**
* 快捷输入并验证( 支持 规则 # 别名
*
* age.require => 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());
}
}
Loading…
Cancel
Save