增加扩展机制定位文件;将common模块实现扩展模式;发布新版本;

This commit is contained in:
2023-09-25 17:07:38 +08:00
parent 3dcf336bbc
commit 184dae8185
65 changed files with 3093 additions and 2751 deletions

View File

@@ -0,0 +1,57 @@
<?php
namespace base\common\provider;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Response;
use Throwable;
/**
* 应用异常处理类.
*/
class ExceptionHandleBase extends Handle
{
/**
* 不需要记录信息(日志)的异常类列表.
* @var array
*/
protected $ignoreReport = [
HttpException::class,
HttpResponseException::class,
ModelNotFoundException::class,
DataNotFoundException::class,
ValidateException::class,
];
/**
* 记录异常信息(包括日志或者其它方式记录).
*
* @param Throwable $exception
* @return void
*/
public function report(Throwable $exception): void
{
// 使用内置的方式记录异常日志
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
* @param \think\Request $request
* @param Throwable $e
* @return Response
*/
public function render($request, Throwable $e): Response
{
// 添加自定义异常处理机制
// 其他错误交给系统处理
return parent::render($request, $e);
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace base\common\provider;
// 应用请求对象类
class RequestBase extends \think\Request
{
protected $filter = [];
}

View File

@@ -0,0 +1,83 @@
<?php
namespace base\common\provider;
use think\View as ThinkView;
class ViewBase extends ThinkView
{
/**
* 设置布局
* ! 注意layout并非view类的标准方法仅仅是think-view和think-template的特性方法
* ! 但是如果在provider中定制则view的所有操作预期会发生错误
* ! 同时由于后台从设计支出就完全依赖think-view所以专门为此扩展和特性定义方法时没有问题的.
* @param bool|string $name 布局模板名称 false 则关闭布局
* @param string $replace 布局模板内容替换标识
* @return $this
*/
public function layout(bool|string $name, string $replace = ''): static
{
if (false === $name) {
// 关闭布局
$this->config(['layout_on' => false]);
} else {
// 开启布局
$this->config(['layout_on' => true]);
// 名称必须为字符串
if (is_string($name)) {
$this->config(['layout_name' => $name]);
}
if (!empty($replace)) {
$this->config(['layout_item' => $replace]);
}
}
return $this;
}
/**
* 配置模板引擎.
* @param array $config 参数
* @return $this
*/
public function config(array $config)
{
$this->driver()->config($config);
return $this;
}
/**
* 解析和获取模板内容 用于输出.
* @param string $template 模板文件名或者内容
* @param array $vars 模板变量
* @return string
* @throws \Exception
*/
public function fetch(string $template = '', array $vars = []): string
{
return $this->config([
'view_suffix' => 'html',
])->getContent(function () use ($vars, $template) {
$this->engine()->fetch($template, array_merge($this->data, $vars));
});
}
/**
* 解析和获取模板内容 用于输出.
* @param string $template 模板文件名或者内容
* @param array $vars 模板变量
* @return string
* @throws \Exception
*/
public function fetchJS(string $template = '', array $vars = []): string
{
return $this->config([
'view_suffix' => 'js',
])->getContent(function () use ($vars, $template) {
$this->engine()->fetch($template, array_merge($this->data, $vars));
});
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace base\common\provider\db;
use think\db\Query as DbQuery;
use think\facade\Env;
use think\facade\Log;
use think\helper\Str;
class QueryBase extends DbQuery
{
/**
* autoCache 自动生成缓存.
*
* @param null|string $key
* @param null|string|int $field_key_value
* @param null|string $tag
* @param null|string|int $field_tag_value
* @return void
*/
public function autoCache($key = null, $field_key_value = null, $tag = null, $field_tag_value = null)
{
$table_name = Str::snake($this->getName());
if (is_null($key)) {
$key = $this->getOptionsMd5();
}
if (!is_null($field_key_value)) {
$key = $key . '_' . $field_key_value;
}
$key = $table_name . '_' . $key;
if (!is_null($tag)) {
if (!is_null($field_tag_value)) {
$tag = $tag . '_' . $field_tag_value;
}
$tag = $table_name . '_' . $tag;
}
if (Env::get('app.auto_cache_log')) {
Log::debug('use auto cache:' . $key);
Log::debug('use auto cache tag:' . $tag);
}
$this->cache($key, null, $tag);
return $this;
}
public function getOptionsMd5()
{
$options = $this->getOptions();
// TODO:支持获取回调函数的设置
return md5(json_encode($options));
}
}